Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

15/1/2015

How Does System.out.println work in Java

Translate:

Slectionner une langue

Search

Home
Java
C/C++
Databases/SQL
PHP
Javascript
Data Structures
Design Pattern Questions
Operating Systems
Recursion
Networking
Excel Interview Questions
HTML5
General/Miscellaneous
Apache Interview Questions
Non-Technical Questions
Interviewing in India
Working As a Software Engineer
Job Advice For Programmers
Financial Analyst Questions
Puzzles
Assortment of Knowledge
American Vocabulary
Technical Vocabulary
Science Questions
popup
About
searchresults
newsletter-signup
Java
Java Interview Questions
How System.out.println() works
JVM platform dependent?
Method overriding vs overloading
Why have a private constructor
Difference between object and class
How copy constructors work
Final modifier
Finally block
Java: Will Finally run after return?
Java Protected Example
Java serializable class example
Multiple Inheritance
How to Create a Thread in Java
Interface vs. Abstract Class
Thread States in Java
Arithmetic Exception
Overridden Method
Dynamic Binding
Can constructors be synchronized in Java?
Does Java pass by reference or by value?
Difference between a primitive type and a class
type?
Does Java have pointers?
Downcasting in Java
Java: Diamond Problem
Java: Can an interface extend another interface?
Java: Are objects of the same type as the interface
implemented?
Java: Can an interface be instantiated?
Find First Nonrepeated Character
Java: Whats the difference between equals() and
==?
Find trailing zeros in factorial
Java Reflection Example
Bit Manipulation Interview Questions and
Answers
XOR in Java
When to use inner classes in Java
Java Inner Class Example
Inner vs nested class

Pluralsight Training
Advanced & Beginner Classes. Free 10 Day Trial. Sign Up
Now!

In Java, how does System.out.println()


work?
ads

Resume Writing Tips


Java Application Development
Java Tests
Online Mock Interviews
Most Likely Interview Questions
Java Certification

This question is an excellent example


of how just some very basic
knowledge of Java can lead you to the
correct answer. Most interviewers
would not expect you to know the
answer to do this right away but
would like to see how you think and
arrive at an answer.
Marcus Aurelius (a Roman emperor)
once said: "Of each particular thing
ask: what is it in itself? What is its
nature?". This problem is an excellent
example of how that sort of thinking
can help one arrive at an answer with
only some basic Java knowledge.

With that in mind, lets break this down, starting with the dot operator. In Java, the dot
operator can only be used to call methods and variables so we know that out must be
either a method or a variable. Now, how do we categorize out? Well, out could not
possibly be a method because of the fact that there are no parentheses the ( ) after
out, which means that out is clearly not a method that is being invoked. And, out
does not accept any arguments because only methods accept arguments you will never
see something like System.out(2,3).println. This means out must be a variable.

What is out in System.out.println()?


We now know that out is a variable, so we must now ask ourselves what kind of
variable is it? There are two possibilities it could be a static or an instance variable.
Because out is being called with the System class name itself, and not an instance of
a class (an object), then we know that out must be a static variable, since only static
variables can be called with just the class name itself. So now we know that out is a
static member variable belonging to the System class.

Is out in System.out.println() an instance variable?


Resume Writing Tips
Java Application Development
Online Mock Interviews
Most Likely Interview Questions

Noticing the fact that println()


is clearly a method, we can
further classify the out in
System.out.println(). We have
already reasoned that out is a
static variable belonging to the
class System. But now we can
see that out must be an
instance of a class, because it is
invoking the method println().

Java Certification

The thought process that one


should use to arrive at an answer
is purposely illustrated above.
Examples of Cloud Computing
Without knowing the exact
answer beforehand, you can
ads by Yahoo!
arrive at an approximate one by
applying some basic knowledge of Java. Most interviewers wouldnt expect you to
know how System.out.println() works off the top of your head, but would rather see you
use your basic Java knowledge to arrive at an answer thats close to exact.

When and where is the out instantiated in

http://www.programmerinterview.com/index.php/java-questions/how-system-out-println-works/

1/4

15/1/2015

How Does System.out.println work in Java

Java Anonymous Class Example


Anonymous Class Interface
Argument Defined Anonymous Inner Class

System.out.println?
When the JVM is initialized, the method initializeSystemClass() is called that does
exactly what its name says it initializes the System class and sets the out variable.
The initializeSystemClass() method actually calls another method to set the out
variable this method is called setOut().

Email
Country

Inventory Software

United States

Quickbooks Inventory
Management Free - 14 Day
Trial.
Programmer Interview
Jaime 23 468

The final answer to how system.out.println() works


The more exact answer to the original question is this: inside the System class is the
declaration of out that looks like: public static final PrintStream out, and inside the
Prinstream class is a declaration of println() that has a method signature that looks
like: public void println().
Here is what the different pieces of System.out.println() actually look like:
//the System class belongs to java.lang package
class System {
public static final PrintStream out;
//...
}
//the Prinstream class belongs to java.io package
class PrintStream{
public void println();
//...
}
ads

Resume Writing Tips


Java Application Development
Java Tests
Online Mock Interviews
Most Likely Interview Questions
Java Certification
Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

Subscribe to our newsletter for more free interview questions.


Follow @programmerintvw
+8 Recommend this on Google

FOLLOW Varoon Sahgal, Author of ProgrammerInterview on

Previous
Next
8 Comments

Programmer Interview

Sort by Best

Login

Share Favorite

Join the discussion


http://www.programmerinterview.com/index.php/java-questions/how-system-out-println-works/

2/4

15/1/2015

How Does System.out.println work in Java


visitor

8 months ago

I was wondering how the hell can we call a non static println() method without
any instance, thanks for the info about initializeSystemClass().
12

Reply Share

Haveesh > visitor

16 days ago

Out is a object of PrintStream class that resides in System class. So


System.out.println() is correct...

Reply Share

a year ago

System is public final and PrintStream is public.


And PrintStream has println method definition, not just the declaration. Actually
there are multiple println overloaded method definitions there.
println() calls print() which in turn calls write() and then the calling chain goes
like that to print the parameter passed.
5

Reply Share

hamreen ahmad

5 months ago

'out' is an object of the reference type PrintStream


3

Reply Share

MichaelM

2 months ago

The questions from others is a reason why these types of questions seem
irrelevant to me. Maybe when would you use the command or what is it used
for? I wonder how many actual programmers could articulate something like
this versus use it adequately? If you're basing hiring someone on these types
of questions I'd ask if you're hiring a programmer or a professor? lol
1

Reply Share

Youngsam

a month ago

http://www.javainterview.net


Guest

Reply Share

6 months ago

The question is how is System.out connected to println(). Are you saying it's a
variable that calls a method. I think that's the puzzling part.

Reply Share

MANTU KUMAR > Guest

4 months ago

System class uses static final reference variable 'out' ( public static
final PrintStream out; ) of type PrintStream to invoke the method
'println()' defined in PrintStream class. Actually System class imports
'java.io' package to so that it can invoke the method 'println()'
You can view : System.out.println() explained for better understanding.

Reply Share

WHAT'S THIS?

ALSO ON PROGRAMMER INTERVIEW

How copy constructors work

When to use inner classes in Java

3 comments 8 months ago

2 comments 8 months ago

Ani Couldn't understand well.

gevix Master-details models use

Where should we use copy


constructor? Need more

inner classes extensively.

Overridden Method

Does Java have pointers?

2 comments 8 months ago

2 comments 8 months ago

hriday dubey Override Class

Swoop Diggity "You can not

inheriting the method from its super


class has the option to

perform arithmetic operations on


references. So, adding 1 to a

Subscribe

Add Disqus to your site

http://www.programmerinterview.com/index.php/java-questions/how-system-out-println-works/

Privacy

3/4

15/1/2015

How Does System.out.println work in Java

Would you like to thank ProgrammerInterview.com for being a helpful free resource? Then why not tell a friend about us, or
simply add a link to this page from your webpage using the HTML below.

Link to
<a href="http://www.programmerinterview.com/index.php/java-questions/how-system-out-println-works/">Programmer and Software Interview Questions and AnswersHow Does Syst
this
page:
Please bookmark with social media, your votes are noticed and appreciated:

Jaime 23 468 personnes aiment a. Soyez le premier


de vos amis.

Copyright 2013 | Programmer Job Board | India Job Board for Programmers | About |

http://www.programmerinterview.com/index.php/java-questions/how-system-out-println-works/

4/4

You might also like