Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In this article, we will learn the java system out println method and will also see the difference between the methods system.out.print() and system.out.println() in java. We will also see the syntax, implementation, and some examples of it.
What Is System Out Println in Java?
System.out.println() is a method in Java or a statement that is used to display a message, data, or string on the screen as the output. It displays the arguments that are passed to it.
Below we can understand the parts of the java system out println.
System: This is the final class that is defined in java.lang package.
out: This is an instance of PrintStream type, and the access specifiers are final and public.
println(): This is a method in the PrintStream class.
We can see the working of System.out.println() method by dint of above explained parts of it with the image.
System.out.println() Syntax in Java
Below is the syntax of Java System out println.
System.out.println(argument)
Example 1
java
java
class Solution { public static void main(String[] args) { System.out.println("You are"); System.out.println("Welcome to"); System.out.println("Coding Ninjas"); } }
You can also try this code with Online Java Compiler
Here, we simply printed the text written inside System.out.println
Example 2
java
java
class solution{ public static void main(String[] args){ int a=5; int b=5; System.out.print("The sum of "); System.out.println(a + " and "+ b + " is: "+ a+b); } }
You can also try this code with Online Java Compiler
Here, we printed the sum of two different variables.
Just like System.out, Java provides us with two other standard or default input-output streams:
1. System.in
In Java, System.in is a static variable of the System class that represents the standard input stream. It is a pre-connected object of the InputStream class that allows us to read input from the console or another input source using various methods, including read(), readLine(), and readAllBytes().
2. System.err
In Java, System.err is a static variable of the System class that represents the standard error stream. It is a pre-connected object of the PrintStream class that allows us to print error messages and diagnostics to the console or another output stream using various methods, including println(), print(), and printf().
Example
java
java
class Main { public static void main(String[] args) { System.err.println("Error: Invalid input!"); } }
You can also try this code with Online Java Compiler
In this example, we use System.err.println() method to print an error message "Error: Invalid input!" to the console, and since it is a standard error stream, it will be printed to the default console error output. We can also assign System.err to a new PrintStream object to redirect the standard error output to a file or another output stream.
Overloads of println() method
We already know that Method Overloading in Java allows us to make different methods to have the same name but with different parameters or signatures where each signature is different by the number of input parameters. With the use of println(), we know that this is a single method of PrintStream class with which users can print the various type of elements by accepting different numbers and types of parameters.
Example
System.out.println()
System.out.println(character)
System.out.println(double)
System.out.println(int)
System.out.println(string)
Note - PrintStream has ten different overloads of println() method that is implemented based on the type of parameters passed.
Example
java
java
class Solution { public static void main(String[] args) {
/*Here, we Declare different data types*/ int num = 5; char ch = 'T'; String str = "Coding Ninjas"; double d = 5.1; float f = 10.2f; boolean bool = false;
/*Different overloads of the println() method*/ System.out.println(); System.out.println(ch); System.out.println(num); System.out.println("CN"); System.out.println(str); System.out.println(bool); System.out.println(f); System.out.println(d); } }
You can also try this code with Online Java Compiler
This is the print statement:
Learn code with Coding Ninjas
This is the println() statements:
Learn
code
with
coding
Ninjas
As we can see in the above output when we use system.out.print() method, the cursor does not move to the next line whereas, when we use system.out.println() method, the cursor moves to the following line after executing it.
Performance Analysis of System.out.println
In Java, System.out.println() is a method used to print a message to the standard output stream (by default, the console). It belongs to the PrintStream class, and its syntax is as follows:
public void println()
public void println(boolean x)
public void println(char x)
public void println(char[] x)
public void println(double x)
public void println(float x)
public void println(int x)
public void println(long x)
public void println(Object x)
public void println(String x)
The println() method adds a new line character at the end of the message, which means that the cursor will be moved to a new line after printing the message.
For example, the following code snippet prints "Hello, World!" to the console:
System.out.println("Hello, World!");
The output will be:
Hello, World!
In addition to the println() method, the PrintStream class also provides other methods, such as print(), printf(), and format(), which allow us to print messages without adding a newline character or format the output using placeholders.
Overall, System.out.println() is a commonly used method in Java for printing messages to the console during program execution.
Frequently Asked Questions
What is the purpose of system out Println() in Java?
Java System out println serves the purpose of printing the output on the screen based on the arguments that are passed to it. It prints the output and adds a new line character at the end.
How to write out println in Java?
To write out println in Java, use the following syntax, System.out.println(/* data */);. It will print the data to the console, followed by a new line character.
What is the relationship between system out and Println?
Here, ‘system’ is a class, and ‘out’ is the object of the PrintStream class. The Println() method is a member of this class to print the result and move the cursor to the next line. It prints the data to the standard output stream.
Is system out Println() a static method?
The system out Println() is indeed a static method. We are able to call this method without the need to create an instance in the class. It is itself associated with the class.
Conclusion
In this article, we have learned about the java system out println method and the difference between system.out.print() and system.out.println() method. We have also seen the programming and some examples of it.
You can learn the basics of Java and data structures and algorithms in Java on Coding Ninjas. Refer to our guided path on code studio to learn more about DSA Competitive Programming, Javascript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Also, look at the interview experiences for placement preparations.