• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is the difference between the print() and println() methods?  RSS feed

 
Natalie Oliver
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am curious about this!! what is the difference between the print() and println() methods in Java???
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's pretty easy to see what the difference is if you have two of these statements one after another.

For questions like this, a little experimentation is the best way to find the answer. Go ahead and try it. Then tell us what you learned.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu, using the first two (print) statements,  I believe it is possible that nothing will show at all, which might confuse the OP.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred, can you give more details as to when that might happen? The only reason I can think of that might cause that is some kind of buffering that doesn't get flushed if you're using an IDE.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu, that's exactly one of the times. Also, stdout may not get flushed until a newline is encountered, especially if you are running in a debugger (and especially on some older Unix platforms such as Sun, Apollo, or SGI..
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also the OP should read the API doc to see what it says each method does: http://docs.oracle.com/javase/8/docs/api/index.html
 
Dwayne Barsotta
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, guys I readily admit that you all are extremely more knowledgeable than me.  But I believe the OP is probably running into the same situations I am running into.  most of the answers found through the DOC's are bloated with words and phrases that make understanding the concept extremely confusing.  It seems that to understand the answer, you already need to have a great knowledge of the subject.  matter -->  thus if you already understand the subject than questions like this are really null and void.  as you should already understand.  Obviously, the OP does not understand the base difference.  Too many posts I read extremely over-complicate the answer leaving the OP questioning .....  should I just drop this place or not.  When the underlying issue is the teacher does not understand they need to "dummy" themselves to the OP's level without being disrespectful.

In most cases, simple explanations and examples tend to get more productivity.  I have learned this lesson the hard way.  Being a truck technician by trade for 20+ years I constantly have to remind myself of this every time I teach a rookie a new skill or answer trivial questions.

In the OP's post, I believe they want to know the visible difference between System.out.print();  and System.out.print():

 
Dwayne Barsotta
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I couldn't edit my above post.  Here is the console output of the above program"

print() examples
First group of text, Second group of text.

println() example
First Line of Text
Second Line of Text

print() to force a new line
First example to use print() and to force a new line
Second example to use print() and to force a new line
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dwayne, it was a nice gesture from you but in this case, we really need to let OP do her own homework
 
Dwayne Barsotta
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess your right, I just figured it was the same as my first java course were there was little explanation on the practical differences.  Will keep this in mind for the future.   I've been seriously thinking of taking a hands on class for intermediate java.  Or paying someone to Skype with who can teach me aspects of java that I am having issues with.  Books are great but there is no one on one explanations based on the students pretension.
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dwayne Barsotta wrote:I guess your right, I just figured it was the same as my first java course were there was little explanation on the practical differences.  Will keep this in mind for the future.   I've been seriously thinking of taking a hands on class for intermediate java.  Or paying someone to Skype with who can teach me aspects of java that I am having issues with.  Books are great but there is no one on one explanations based on the students pretension.

I agree with Junilu.
If you refer to Junilu's first post in this thread, I can say that...
If OP is told that println() puts a newline character and print() does not, there is chance that OP forgets this after sometime.
But if asked to execute the mentioned code, OP will learn things and there is chance that OP remembers this for lifetime.

This is a very small thing in this case, but it applied to every problem.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Natalie: while we are happy to help anyone who asks any kind of question related to Java, we do ask that students stay honest and do their own homework.

Based on what we've seen, the questions you have posted so far do appear to be homework: http://www.mooreschools.com/cms/lib/OK01000367/Centricity/Domain/1996/Chapter%201%20Critical%20thinking.pdf
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dwayne Barsotta wrote:I've been seriously thinking of taking a hands on class for intermediate java.  Or paying someone to Skype with who can teach me aspects of java that I am having issues with.  Books are great but there is no one on one explanations based on the students pretension.

Have you considered participating in the Ranch's Cattle Drive? https://coderanch.com/f/19/Cattle-Drive
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:. . . , the questions you have posted so far do appear to be homework: . . .
In which case it is badly written homework because there is no such thing as the print() method or the println() method. There are methods with those names in the PrintStream class, but that is a different question. And the answer to, “What is the difference between the PrintStream.print() method and the PrintStream.println() method?” will follow in my next post.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer: the PrintStream.println() method exists and the PrintStream.print() method doesn't exist, as you can see here. The first link will tell you what PrintStream.println() does. The second link will allow you to find PrintStream.println(String), which actually tells you what the difference is. I think those documentation links are easy to read, but some other methods are less easy to read.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!