Quantcast
Channel: Java Programming
Viewing all articles
Browse latest Browse all 6

How System.out.println() works

$
0
0

Meaning of System.out.println()


System : System is a class which is in java.lang package

java.lang.Object
java.lang.System

  • The above code means that System class which is in java.lang package extends Object class which is also in java.lang package or System is a subclass of Object class. 
  • The Object is the root of the class hierarchy. Every class has Object as its super class. All objects, including array implements the methods of this class.
  • The System class is not imported because it is in the java.lang package. All the classes in the java.lang package are implicitly imported in every Java program.

What Is out In System.out.println()

 out : static reference variable defined in System class of reference type PrintStream


public final class System
{
public static final PrintStream out = null;
//...
}


println() : non static method defined in PrintStream class.


public class PrintStream
{
public void println()
{
//...
}
}


PrintStream class belongs to java.io package.


java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
java.io.PrintStream

  • The above code shows that PrintStream extends FilterOutputStream. FilterOutputStream extends OutputStream which extends Object Class.

What System.out.println() Does ?


System.out.println() terminates the current line by writing the line separator string. The line separator string is defined by the system property line.separator and is not necessarily a single newline character ('\n'). There are various other methods which are defined in PrintStream class. Some of them are shown here :


public class PrintStream{
public void println()
{
// Terminates the current line by writing line separator string
}

public void println(long x)
{
//Prints a long and then terminate the line.
// This method behaves as though it invokes print(long) and then println()
}

public void println(Object x)
{
// Prints an Object and then terminate the line.
// It calls at first String.valueOf(x) to get the printed object's string value
// Then behaves as though it invokes print(String) and then println()
}
// And other methods
}


How 'out' in System.out.println() instantiated

Now the question arises how, when and where 'out' reference variable is instantiated. 

  • JVM ( Java Virtual Machine ) invokes initializeSystemClass() method to complete the initialization of the System class.
  • initializeSystemClass()  method is in System class.
  • initializeSystemClass() then calls setOut0() that actually sets the 'out' reference variable. 
  • The function setOut0() is a native function. We can find its implementation in System.c

public final class System
{
public static final PrintStream out=null;
private static void initializeSystemClass()
{
setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));
}
}

System class also contains setOut() method which can be used to reassign the "standard" output stream i.e the 'out' reference variable.

public final class System
{
public static final PrintStream out=null;
public static void setOut(PrintStream out)
{
setOut0(out);
}
private static void initializeSystemClass()
{
setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));
}
}


How we invoke println() method with the help of System.out

  • We know that println() is a method in PrintStream class and 'out' reference variable of type PrintStream in System class.
  • System is in java.lang package and PrintStream in java.io package. 
  • java.lang is implicitly imported by every java program. But not java.io package. 

So the question arises how System class invokes the println() method in PrintStream class. The answer is pretty simple : System.java imports java.io package.

package java.lang;
import java.io.*;
// imports other packages as well
public final class System
{
public static final PrintStream out=null;
//...
}


How System.out.println() Actually Works ?

So on the basis of the above we come to the following conclusion :


package java.lang;
import java.io.*;
// imports other packages as well
public final class System
{
public static final PrintStream out=null;
public static void setOut(PrintStream out)
{
setOut0(out);
}
private static void initializeSystemClass()
{
setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));
}
}


package java.io;
public class PrintStream{
public void println()
{
// Terminates the current line by writing line separator string
}

public void (long x)
{
//Prints a long and then terminate the line.
// This method behaves as though it invokes print(long) and then println()
}

public void println(Object x)
{
// Prints an Object and then terminate the line.
// It calls at first String.valueOf(x) to get the printed object's string value
// Then behaves as though it invokes print(String) and then println()
}
// And other methods
}

System.out.println() Example


// A java program that demonstrates the use of System.out.println()
class SystemDemo
{
public static void main(String args[])
{
int number=10;
System.out.print("Hello "); // prints 'Hello' but does not go to next line
System.out.println("Reader"); // prints 'Reader' and goes to next line
System.out.println("How are you : ");
System.out.println("Number assigned value : " + number);
}
}

Output : 

Hello Reader
How are you :
Number assigned value : 10

Viewing all articles
Browse latest Browse all 6

Trending Articles