• Log in or sign up for an account here: https://www.sythe.org/login

Understanding basics of Java - print and println

diabl0

Apprentice

Banned
Joined
Jul 25, 2008
Sythe Gold
0
Messages
902
Hello everybody, I know there are some new users of Java here reading this, and I am sure they have take a look at a line of code and seen:

System.out.print("Whatever");

and

System.out.println("Whatever");

and were probably like wtf? Am I right? Well, today I am going to clear up the difference of print, and println for the new users. Okay, lets say you have a code such as:

System.out.print("Hello how are you");
System.out.print("I am doing fine");

but when you go to compile and run it, it will all be on the same line. and your like wtf? How do I get it to the next line? Well that is where println comes in.

Println stands for Print line, So if you were to write that same line of code above with println instead of print, it would come out such as:

System.out.println("Hello how are you");
System.out.print("I am doing fine");

It would appear as this with println:
print3v.jpg


"Hello how are you
I am doing fine"

Whereas print would show:
print2l.jpg


"Hello how are youI am doing fine"

You do not need to add println on the last sentence though, notice how I added it in the Hello how are you line, but not in the I am doing fine line? That is because when you use println, it only makes the next print out drop to the next line.

So if we had:

System.out.print("Hello how are you");
System.out.println("I am doing fine");

It would read:
Hello how are youI am doing fine.

Because println was on the second line, instead of the first. It always needs to be above the line you want to drop down.

Well, I hope this cleared some stuff up for new users to Java. Thank you guys.



~~ Diabl0
 
Quite a long post to describe a simple concept...

println() goes to the next line...

You could state that it's the condensed form of print("\n") or the c++ equivelant of cout << endl
 
Runescape Gamblingrunescape gambling
Back
Top