Interview Kickstart has enabled over 21000 engineers to uplevel.
System.out.println() in Java is one of the most used statements. We use it to print the argument passed to it and display it on the screen, but you might not know a lot more than that about it.
In this article, we’ll understand Java System.out.println() in detail, so as a programmer, you can know even more about something you need to use so often.
To define System.out.println() in Java, it is a simple statement that prints any argument you pass and adds a new line after it.
To understand more about it, let us take a deeper look at the structure of Java System.out.println.
The statement System.out.println contains three major parts: System, out, and println.
The structure of the statement System.out.println(), being in line with the standards, reflects all these relationships.
Here’s the syntax of Java System.out.println; please note that the argument/parameter can be anything you want to print:
System.out.println(argument)
We’ll now delve into various types of examples to see how this works.
In this example, we’ll look at some of the ways we can use Java System.out.println() to print different types of parameters in different ways.
import java.io.*;
class example {
public static void main(String[] args)
{
// Printing different types of data
System.out.println("I am a string");
System.out.println(1);
System.out.println(false);
System.out.println(1.2);
System.out.println('z');
}
}
I am a string
1
false
1.2
z
System.out refers to a PrintStream to which you can print characters. It is usually used to display results or outputs of execution in the screen console or terminal. Although not necessarily the best way, we also use it to print some debug statements.
“out” also has methods other than println. For example, print, printf, format, and flush. Let us take a quick look at some of them.
import java.io.*;
class example {
public static void main(String[] args)
{
System.out.printf("I am a string");
System.out.print(1);
System.out.println(false);
System.out.format("We'll format %f using this", 1.2);
System.out.print("\n");
System.out.format("We'll format %s using this", 1.2);
System.out.flush();
}
}
I am a string1false
We'll format 1.200000 using this
We'll format 1.2 using this
System.in refers to an InputStream connected to a standard console to get input, typically a keyboard. We don’t use it as frequently as System.out since we often use command line arguments, files, or applications having GUI to give Java the input, which is a different way to get the input compared to System.in.
Let us understand its usage better with an example.
import java.io.*;
import java.util.Scanner;
class example {
public static void main(String[] args)
{
Scanner scanVariable = new Scanner(System.in);
String variableStoringInput = "";
System.out.print("Enter something: \n");
// Reading input
variableStoringInput = scanVariable.nextLine();
// Printing read input
System.out.println("Your input was: " + variableStoringInput);
}
}
Enter something:
Your input was: 1
System.err refers to a PrintStream that prints and displays the message in the parameter to the standard error output stream. It is similar to System.out, the main difference being that .err is used almost exclusively to print error messages. Some IDEs like Eclipse show .err messages in a different color like red to indicate an error message, separating it from texts printed using .out.
Let us look at an example to understand this better:
import java.io.*;
class example {
public static void main(String[] args)
{
try {
InputStream input = new FileInputStream("c:\\abc");
System.out.println("Opening successful");
}
catch (IOException ex) {
System.err.println("Opening failed");
ex.printStackTrace();
}
}
}
stdout
<empty>
stdin
stderr
File opening failed:
java.io.FileNotFoundException: c:\ (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:211)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:153)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:108)
at example.main(Main.java:7)
We’ll now look at an example to understand the various ways in which println() has been overloaded in Java to accept many different types of data types.
import java.io.*;
class example {
public static void main(String[] args)
{
// Printing a string and a number separately
System.out.println("I am a string");
System.out.println(1);
// Printing a string and a number together
System.out.println(10+" I am a string that was printed along with a number");
// Printing a string and a number together and printing the result of a calculation between two numbers
int num1 = 100, num2=200;
System.out.println("The product of "+num1+" and "+ num2 + " is: ");
System.out.println(num1*num2);
// Taking different types of variables and printing them all separately
int integerVariable=12;
double doubleVariable = 12.3;
float floatVariable = 12.3f;
boolean booleanVariable = false;
char characterVariable = 'A';
String stringVariable = "I am the content inside stringVariable";
System.out.println(integerVariable);
System.out.println(doubleVariable);
System.out.println(floatVariable);
System.out.println(booleanVariable);
System.out.println(characterVariable);
System.out.println(stringVariable);
// Taking different types of variables and printing them all together, separated by space
System.out.println(integerVariable+" "+doubleVariable+" "+floatVariable+" "+booleanVariable+" "+characterVariable+" "+stringVariable);
}
}
I am a string
1
10 I am a string that was printed along with a number
The product of 100 and 200 is:
20000
12
12.3
12.3
false
A
I am the content inside stringVariable
12 12.3 12.3 false A I am the content inside stringVariable
While both the methods print the arguments passed to them and are available under System.out, there’s a difference between the two:
import java.io.*;
class example {
public static void main(String[] args)
{
int integerVariable=12;
double doubleVariable = 12.3;
float floatVariable = 12.3f;
boolean booleanVariable = false;
char characterVariable = 'A';
String stringVariable = "I am the content inside stringVariable";
// Printing different types of data using print
System.out.print(integerVariable);
System.out.print(doubleVariable);
System.out.print(floatVariable);
System.out.print(booleanVariable);
System.out.print(characterVariable);
System.out.print(stringVariable);
// Printing different types of data using println
System.out.println(integerVariable);
System.out.println(doubleVariable);
System.out.println(floatVariable);
System.out.println(booleanVariable);
System.out.println(characterVariable);
System.out.println(stringVariable);
}
}
1212.312.3falseAI am the content inside stringVariable12
12.3
12.3
false
A
I am the content inside stringVariable
Compared to many IO operations, Java System.out.println() is a slow operation as it causes a heavier overhead on the machine than other IO operations.
When multiple threads are passed, println() can have a reduced performance as it’s a synchronized method. Some alternatives to using println() for output involve using the PrintWriter class or BufferedWriter class, both of which are faster than PrintStream’s println().
Here are some tech interview questions related to Java System.out.println that you can expect at FAANG:
Whether you’re a Coding Engineer gunning for Software Developer or Software Engineer roles, a Tech Lead, or you’re targeting management positions at top companies, IK offers courses specifically designed for your needs to help you with your technical interview preparation!
If you’re looking for guidance and help with getting started, enrol in our program. As pioneers in the field of technical interview preparation, we have trained thousands of software engineers to crack the most challenging coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more!
Q1. What is the Difference Between println() and print()?
The main difference between the two is that print() retains the cursor in the same line after printing the argument, while println() moves the cursor to the next line.
Q2. What is Method Overloading in the Context of println()?
Method overloading means that a class has multiple methods of the same name but different parameters, so the method can be run for different types or numbers of parameters. For example, in Java, println() is a method that’s overloaded to accept all different data types as parameters for printing.
Q3. Is There a Shortcut for System.out.println in Java?
Yes, static import of java.lang.System.out will remove the need for the “System.” part of the statement. That said, while System.out.println can seem like a long statement to write each time you want to print something, the static import isn’t recommended as it decreases code readability. In IDEs like Eclipse, shortcuts like Ctrl+Spacebar can help you be quick while using the statement.
Q4. What is a Final Class?
A final class is a class that cannot be extended. For example, System is a final class as it cannot be extended. We make a class final when we need to avoid alteration of base behavior due to the class being extended.
Q5. What is the Main Difference Between System.out and System.err?
While System.in, System.out, and System.err are all initialized when a Java VM starts by the Java runtime. Also, .out and .err are both a type of PrintSteam. Here, .out prints anything that needs to be printed on the output section. On the other hand, .err almost exclusively is used to print error messages, either separately in the stderr output stream or a different color in IDEs like Eclipse.
Related reads:
Attend our webinar on
"How to nail your next tech interview" and learn