Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What Is System Out Println in Java?
3.
System.out.println() Syntax in Java
3.1.
Example 1
3.2.
java
3.3.
Output
3.4.
Example 2
3.5.
java
3.6.
Output
3.7.
java
4.
Overloads of println() method
4.1.
Example
4.2.
Example
4.3.
java
4.4.
Output
5.
Difference between print() and println() methods
5.1.
Example
5.2.
java
5.3.
Output
6.
Performance Analysis of System.out.println
7.
Frequently Asked Questions
7.1.
What is the purpose of system out Println() in Java?
7.2.
How to write out println in Java?
7.3.
What is the relationship between system out and Println?
7.4.
Is system out Println() a static method?
8.
Conclusion
Last Updated: Jun 7, 2024
Medium

System.out.println in Java

Author Tisha
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

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. 

java system out println

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.

  1. System: This is the final class that is defined in java.lang package. 
  2. out: This is an instance of PrintStream type, and the access specifiers are final and public. 
  3. println(): This is a method in the PrintStream class.
Working of System.out.println()

We can see the working of System.out.println() method by dint of above explained parts of it with the image.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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");
   }
}

Output

You are
Welcome to 
Coding Ninjas


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);
}
}

Output

The sum of 5 and 5 is: 55


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!");
}
}

Output

Error: Invalid input!


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);
   }
}

Output

T
5
CN
Coding Ninjas
false
10.2
5.1

 

Hence, we can see that this method prints different data types. You can pass different data types in this method and it will work accordingly.

Difference between print() and println() methods

Now, let us see the difference between print() and println() methods in Java in tabular form:

FeatureSystem.out.print()System.out.println()
Adds new line character at the end?NoYes
Can be used without parameters?NoYes
Effect on cursor positionNext print starts in the same lineNext print starts on the next line
ExamplesSystem.out.print("Hello, world!");System.out.println("Hello, world!");

Example

  • java

java

class Solution{
   public static void main(String[] args) {
       System.out.println("This is the print() statement:");
       System.out.print("Learn ");
       System.out.print("code ");
       System.out.print("with ");
       System.out.print("Coding ");
       System.out.print("Ninjas ");

       System.out.println();
       System.out.println("This is the println() statements:");
       System.out.println("Learn ");
       System.out.println("code");
       System.out.println("with ");
       System.out.println("coding ");
       System.out.println("Ninjas");
   }
}

Output

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 also refer to the below articles:


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.

Happy Learning!

Previous article
Methods to Print New Line in Java
Next article
Java Tokens
Live masterclass