Register for our webinar

How to Nail your next Technical Interview

1 hour
Loading...
1
Enter details
2
Select webinar slot
*Invalid Name
*Invalid Name
By sharing your contact details, you agree to our privacy policy.
Step 1
Step 2
Congratulations!
You have registered for our webinar
check-mark
Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
*All webinar slots are in the Asia/Kolkata timezone
Step 1
Step 2
check-mark
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.
close-icon
Iks white logo

You may be missing out on a 66.5% salary hike*

Nick Camilleri

Head of Career Skills Development & Coaching
*Based on past data of successful IK students
Iks white logo
Help us know you better!

How many years of coding experience do you have?

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Iks white logo

FREE course on 'Sorting Algorithms' by Omkar Deshpande (Stanford PhD, Head of Curriculum, IK)

Thank you! Please check your inbox for the course details.
Oops! Something went wrong while submitting the form.
Our June 2021 cohorts are filling up quickly. Join our free webinar to Uplevel your career
close
closeAbout usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar

Know All About System.out.println() in Java

Last updated by Vartika Rai on Apr 01, 2024 at 01:04 PM | Reading time: 10 minutes

The fast well prepared banner

Attend our Free Webinar on How to Nail Your Next Technical Interview

WEBINAR +LIVE Q&A

How To Nail Your Next Tech Interview

Know All About System.out.println() in Java
Hosted By
Ryan Valles
Founder, Interview Kickstart
strategy
Our tried & tested strategy for cracking interviews
prepare list
How FAANG hiring process works
hiring process
The 4 areas you must prepare for
hiring managers
How you can accelerate your learnings

In Java, System.out.println() 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.

If you are preparing for a tech interview, check out our technical interview checklist, interview questions page, and salary negotiation e-book to get interview-ready! Also, read Overriding in Java, Length vs. Length Method in Java, and Split String Method in Java for more specific insights and guidance on Java concepts and coding interview preparation.

Having trained over 11,000 software engineers, we know what it takes to crack the toughest tech interviews. Our alums consistently land offers from FAANG+ companies. The highest ever offer received by an IK alum is a whopping $1.267 Million!

At IK, you get the unique opportunity to learn from expert instructors who are hiring managers and tech leads at Google, Facebook, Apple, and other top Silicon Valley tech companies.

Want to nail your next tech interview? Sign up for our FREE Webinar.

In this article, we’ll learn:

  • What Is System Out Println in Java?
  • Java System.out.println() Syntax with Example
  • Overloads of the println() Method
  • Difference Between print() and println()
  • Analysis of System.out.println
  • FAANG Interview Questions on Java System.out.println
  • FAQs on Java System.out.println

What Is System Out Println in Java?

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. 

  • println() is responsible for printing the argument and printing a new line
  • System.out refers to the standard output stream

To understand more about it, let us take a deeper look at the structure of Java System.out.println.

Structure of System Out Println in Java

The statement System.out.println contains three major parts: System, out, and println. 

  • System refers to a final class that we can find in the java.lang.package and this System class has a public and static member field called PrintStream. 
  • All instances of the class PrintStream have a public method called println()
  • out is an instance of the PrintStream class 

The structure of the statement System.out.println(), being in line with the standards, reflects all these relationships.

System.out.println() Syntax in Java

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.

Example of Java System.out.println()

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.

Code

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

    }

}

Output

I am a string

1

false

1.2

z

System.out

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.

Example

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

 

    }

}

Output

I am a string1false

We'll format 1.200000 using this

We'll format 1.2 using this

System.in

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. 

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

      

 

    }

 

}

Output

Enter something: 

Your input was: 1

System.err

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:

Example

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

}

 

    }

}

Output

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)

Overloads of the println() Method

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.

Example

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

    }

}

Output

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

Difference Between print() and println(): System.out.print() vs. System.out.println()

While both the methods print the arguments passed to them and are available under System.out, there’s a difference between the two:

Examples

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

 

    }

}

Output

 

1212.312.3falseAI am the content inside stringVariable12

12.3

12.3

false

A

I am the content inside stringVariable

Analysis of System.out.println

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().

FAANG Interview Questions on Java System.out.println

Here are some tech interview questions related to Java System.out.println that you can expect at FAANG:

  1. Can you use Java system.out.println() in a method?
  2. What is the use of out in system.out.println()?
  3. Where does Java system.out.println go?
  4. What is system in System.out.println()?
  5. What is println() in System.out.println()?

FAQs on Java System.out.println

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.

Ready to Nail Your Next Coding Interview?

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, sign up for our FREE webinar. 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!

Sign up now!

Last updated on: 
April 1, 2024
Author

Vartika Rai

Product Manager at Interview Kickstart | Ex-Microsoft | IIIT Hyderabad | ML/Data Science Enthusiast. Working with industry experts to help working professionals successfully prepare and ace interviews at FAANG+ and top tech companies

Attend our Free Webinar on How to Nail Your Next Technical Interview

Register for our webinar

How to Nail your next Technical Interview

1
Enter details
2
Select webinar slot
By sharing your contact details, you agree to our privacy policy.
Step 1
Step 2
Congratulations!
You have registered for our webinar
check-mark
Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
Step 1
Step 2
check-mark
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.

Know All About System.out.println() in Java

Worried About Failing Tech Interviews?

Attend our webinar on
"How to nail your next tech interview" and learn

Ryan-image
Hosted By
Ryan Valles
Founder, Interview Kickstart
blue tick
Our tried & tested strategy for cracking interviews
blue tick
How FAANG hiring process works
blue tick
The 4 areas you must prepare for
blue tick
How you can accelerate your learnings
Register for Webinar
entroll-image