Java console-based output
In Java, System.out
is used to output data to the console. It is a static reference field inside the java.lang.System
class that holds an instance of PrintStream
type.
This PrintStream
class is present inside the java.io
package, and it provides different method to print data, like println()
. In the case of the System.out
object, these data is printed to the console in the standard output stream.
System.out.println("Welcome to JCoder!!!");
Output:
Welcome to JCoder!!!
We have already used System.out
to print our output to console in previous programs.
print()
vs println()
method
-
print()
is used to print the parameters provided but it doesn’t set the cursor to a new line. -
println()
prints the parameters provided and then set the cursor to a new line.
println() can be used without parameters which gives the output as a blank line. But, if we use print() without parameters, we get a compilation error.
|
String firstString = "Welcome";
String secondString = "to";
String thirdString = "JCoder";
//Print using print()
System.out.print(firstString + " ");
System.out.print(secondString + " " +thirdString);
System.out.print("!!!");
//Set cursor to new line using println()
System.out.println();
System.out.println("Have a good day!!!");
Output:
Welcome to JCoder!!!
Have a good day!!!
Even though you can use System.out to print debug statements for basic Java programs and in personal projects, for professional projects the industry standard is to use a logging library like slf4j with log4j for example, but this will be covered in a later course.
|