Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

print vs println using split method

 
Oceana Wickramasinghe
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When using this code



i get the output :sddsfsdd

So its not returning empty strings.

But if i replace print with println

i get the output:

sd

dsf



sdd

Can someone tell me why println prints empty strings but why print doesnt?
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
println adds a newline after each token. print does not.

So what you are seeing with print is a concatenation of the tokens (including the empty ones "").

try:


WP
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oceana Wickramasinghe wrote:
So its not returning empty strings.


But it is.

Try this:


Then try it again with println() instead of print().

Then try both again without any of the "empty" lines.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The println() method prints a String value followed by a line break character. What you're seeing are the line break characters, not the empty String values.

Edit: Dang, way too slow...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic