Java println vs print: Key Differences
In Java, both println and print are methods used for printing output, but they have some differences between them.
- The println method automatically adds a new line after outputting text, while the print method does not. This means that each time the println method is called to output text, the next output will start on a new line.
- The println method in Java appends a newline character “\n” after printing the text, whereas the print method does not.
For example:
System.out.print("Hello ");
System.out.print("World");
The output result is:
Hello World
If the println method is used:
System.out.println("Hello");
System.out.println("World");
The output is:
Hello
World
Therefore, if you want to start a new line after outputting text, you can use the println method; if you want to output multiple texts on the same line, you can use the print method.