QUESTIONS & ANSWERS

What is the difference between print() and println() in java?

In Java, print() and println() are two methods used to output data to the console or other output streams. The main difference between them is that println() method adds a newline character \n at the end of the output, whereas print() method doesn't.

Here's an example to illustrate the difference:

System.out.print("Hello, ");
System.out.print("World!");

Output:

Hello, World!
System.out.println("Hello, ");
System.out.println("World!");

Output:

Hello, 
World!

As you can see, the first example outputs both strings on the same line without any separation. The second example outputs each string on a separate line with a newline character added at the end of each string.

In summary, if you want to output text on the same line without any separation, use the print() method. If you want to output text on a new line, use the println() method.

03/03/2023, 10:21 am Read : 324 times