System.out.println in Java
Java System.out.println() is used to print an argument that is passed to it. The statement can be broken into 3 parts which can be understood separately as:
- System: It is a final class defined in the java.lang package.
- out: This is an instance of PrintStream type, which is a public and static member field of the System class.
- println(): As all instances of PrintStream class have a public method println(), hence we can invoke the same on out as well. This is an upgraded version of print(). It prints any argument passed to it and adds a new line to the output. We can assume that System.out represents the Standard Output Stream.
Parameters: The parameter might be anything that the user wishes to print on the output screen.
Welcome To GeeksforGeeks
The addition of 10 and 20 is: 30
Just like System.out, Java provides us with two other standard or default input-output streams:
- System.in: This is the standard input stream that is used to read characters from the keyboard or any other standard input device.
InputStreamReader inp = new InputStreamReader(System.in);
- System.err: This is the standard error stream that is used to output all the error data that a program might throw, on a computer screen or any standard output device.
Overloads of println() method
As we know, Method Overloading in Java allows different methods to have the same name, but different signatures or parameters where each signature can differ by the number of input parameters or type of input parameters or both. From the use of println() we observed that it is a single method of PrintStream class that allows the users to print various types of elements by accepting different type and number of parameters.
System.out.println(), System.out.println(int), System.out.println(double), System.out.println(string), System.out.println(character), etc.
PrintStream has around 10 different overloads of println() method that are invoked based on the type of parameters passed by the user.
10 G GeeksforGeeks 10.2 13.5 true Hello
System.out.print(): This method prints the text on the console and the cursor remains at the end of the text at the console. The next printing takes place from just here. This method must take atleast one parameter else it will throw an error.
System.out.println(): This method prints the text on the console and the cursor remains at the start of the next line at the console. The next printing takes place from the next line. This method may or may not take any parameter.
Using print() GfG! GfG! GfG! Using println() GfG! GfG! GfG!
Performance Analysis of System.out.println()
println() is a method that helps display output on a console. This might be dependent on various factors that drives the performance of this method. The message passed using println() is passed to the server’s console where kernel time is required to execute the task. Kernel time refers to the CPU time. Since println() is a synchronized method, so when multiple threads are passed could lead to the low-performance issue. System.out.println() is a slow operation as it incurs heavy overhead on the machine compared to most IO operations.
They are fast as compared to the println() of the PrintStream class.
- Difference between print() and println()
- Input-Output in Java
- PrintStream println() method in Java with Examples
- Redirecting System.out.println() output to a file in Java
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.