01 January, 2015

What is System.out.println

Hi there,

In this post, lets understand what is System.out.println, and few details about it.

Before we start with our understanding of System.out.println, let us try and understand the below java class.

public class Test{

public static String foo;


public static void setFoo(String foo){
this.foo = foo;
}
}

now let us assume, if i have to print the length of a string foo. How do i do that.

int length = Test.foo.length();

in the above statement, i have used the length function of String class. Since my foo is static in Test class, i have called foo as Test.foo.

now that we have understood, that length being a method of a static variable we have used the above convention.

Applying the same logic to System.out.println


System is a class in java.lang package. In the system class out is a static class variable of type java.io.PrintStream. And println is a method of PrintStream class.

As out is a static variable in System class. it can be referred as System.out
and since println is a method of PrintStream class for which out is a instance variable in system class.
we can now refer it as System.out.println


we can confirm this by the following.

e:/>javap java.lang.System

public final class java.lang.System {
  public static final java.io.InputStream in;
  public static final java.io.PrintStream out;
  public static final java.io.PrintStream err;
.............
e.t.c

}

javap java.io.PrintStream

public class java.io.PrintStream extends java.io.FilterOutputStream implements java.lang.Appendable,java.io.Closeable {
.................
  public void println();
  public void println(boolean);
  public void println(char);
  public void println(int);
  public void println(long);
  public void println(float);
  public void println(double);
  public void println(char[]);
  public void println(java.lang.String);
  public void println(java.lang.Object);
.................

}