//print and create new line afterSystem.out.println("text");System.out.println(String);//You can use any variable type, not just strings, although//they are the most common//Print without creating a new lineSystem.out.print("text");System.out.print(String);
System.out.print(<string>);//prints in the same line as the previous printSystem.out.println(<string>);//prints in a new line// ExampleSystem.out.print("This ");System.out.print("will ");System.out.print("be ");System.out.print("all ");System.out.print("in ");System.out.print("one ");System.out.print("line.");System.out.println("Hello World!");System.out.println("second line");
/* The println() function adds a new line after printing
the value/data inside it. Here, the suffix ln works as the
newline character,
. If you consider the following example:
*/publicclassMain{publicstaticvoidmain(String[] args){System.out.println("Hello World!");}}/* You might not figure out exactly what is happening under
the hood as you are printing only one line,
and you get the following output:
*/// Hello World!/* But if you try to print several different expressions
using the println() then you'll see the difference clearly!
*/publicclassMain{publicstaticvoidmain(String[] args){System.out.println("Hello World!");System.out.println("Welcome to freeCodeCamp");}}/* Here, you can see that after executing the first print
statement, it is adding one new line character (
).
So you are getting the second print statement,
Welcome to freeCodeCamp, in the next line.
The whole output will be like below:
*/// Hello World!// Welcome to freeCodeCamp
#Here are few methods toprint data.1)System.out.println();--> move the cursor on new line
2)System.out.print();--> on same line
3)System.out.printf();--> on same line
java providing build-in class called System, and Systemclass is having
build-in object called out,this object is attached tothe monitor or console,
means a static object.
and the out object having the methods called println, print, and printf, etc.
print method takes parameters or values, means parameter in different type.
print method support all data type like -->int,float,char,String,double,long, etc.NOTE:if method can have same name but different in parameters called
overloaded method, so print method is also a overloaded method but passing
different in parameters.
###############################################
IMPORTANTNOTE:
suppose x and y are two variables and it contains int type value as 5 and 6.Then how the print method will works ?.........................................System.out.println("Hello "+ x + y);First string is concatenate withx and then concatenate withy
the output we get -->Hello56........................................InOther way ----------.......................................System.out.println(x + y +" Hello");First x and y will add and then concatenate withstring.
the output we get -->11Hello......................................
#########################################
iftoachieve the first way toadd x and y use parentheses () arount x and y
like (x + y), now it add and then concatenate
System.out.println("Hello "+(x + y));
output -->Hello11
/* For this function, let me use the example I have used
just now. You should be able to see the difference right away:
*/publicclassMain{publicstaticvoidmain(String[] args){System.out.print("Hello World!");System.out.print("Welcome to freeCodeCamp");}}/* Here, you see that I used print instead of using println
like I did earlier. The print doesn't add the additional
(new line character) after executing the task in it.
This means that you will not get any new line after executing
any print statement like above.
*/// The output will be like this:// Hello World!Welcome to freeCodeCamp/* If you want, then you can also solve this issue using
like below:
*/publicclassMain{publicstaticvoidmain(String[] args){System.out.print("HelloWorld!
");System.out.print("Welcome to freeCodeCamp");}}/* This time, the
will work as the new line character and
you will get the second string in a new line.
The output is like below:
*/// Hello World!// Welcome to freeCodeCamp/* You can also print the two strings using only one print
statement like below:
*/publicclassMain{publicstaticvoidmain(String[] args){System.out.print("HelloWorld!WelcometofreeCodeCamp");}}// The output will be the same this time:// Hello World!// Welcome to freeCodeCamp