Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between printf and println in java? [duplicate]

Just came to know that java does has a method named printf, then what is the difference between printf & println?

like image 207
Bhargav Modi Avatar asked Feb 27 '15 06:02

Bhargav Modi


People also ask

What is the difference between system out Println () and system out in printf ()?

println is used when you want to create a new line with just simple text or even text containing concatenation. ("Hello " + "World" = "Hello World.") printf is used when you want to format your string. This will clean up any concatenation.

What is the difference between print () and println () functions?

The main difference between print and println is that print method prints the string but does not move the cursor to a new line while println method prints the string and moves the cursor to a new line.

Can you print doubles in Java?

Just use %. 2f as the format specifier. This will make the Java printf format a double to two decimal places. /* Code example to print a double to two decimal places with Java printf */ System.

What is the difference between printf and format in Java?

printf: prints the formatted String into console much like System. out. println() but. format: method return a formatted String, which you can store or use the way you want.


1 Answers

System.out.println(); is efficient for simply printing a line of text. If the line of text needs to be formatted (ex: alignment (left-justified, etc.), etc.), then System.out.printf(); would be used.

Check out this link for more information.

like image 59
Ryan Shukis Avatar answered Oct 20 '22 12:10

Ryan Shukis