2011-10-10

What is the difference between print and println in java

Both methods are used to print data on console, the only difference between above two methods is 'System.out.println' prints data and place the cursor in next line. So the next coming output prints in the next line whereas 'System.out.print' prints data and place the cursor in the same line. So the next coming output prints in the same line.


Let us take an example


for(int i = 0; i < 5; i++)
System.out.print(" " + i);
The above code generates the following output

0 1 2 3 4

for(int i = 0; i < 5; i++)
System.out.println(" " + i);
The above code generates the following output


0
1
2
3
4


Comment for any doubts.....

Loading

Enter your Email here: