1

Can any one explain exactly what the below means::

System.out.println()

I know that :

System : Is a class

I don't know about "out"

println : Static Method.

Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176
mayur rahatekar
  • 4,410
  • 12
  • 37
  • 51

7 Answers7

11

out is a static field that holds a reference to PrintStream object.

println is NOT a static method.

Here is the declaration of the out variable in System.java

/**
 * The "standard" output stream. This stream is already
 * open and ready to accept output data. Typically this stream
 * corresponds to display output or another output destination
 * specified by the host environment or user.
 * <p>
 * For simple stand-alone Java applications, a typical way to write
 * a line of output data is:
 * <blockquote><pre>
 *     System.out.println(data)
 * </pre></blockquote>
 * <p>
 * See the <code>println</code> methods in class <code>PrintStream</code>.
 *
 * @see     java.io.PrintStream#println()
 * @see     java.io.PrintStream#println(boolean)
 * @see     java.io.PrintStream#println(char)
 * @see     java.io.PrintStream#println(char[])
 * @see     java.io.PrintStream#println(double)
 * @see     java.io.PrintStream#println(float)
 * @see     java.io.PrintStream#println(int)
 * @see     java.io.PrintStream#println(long)
 * @see     java.io.PrintStream#println(java.lang.Object)
 * @see     java.io.PrintStream#println(java.lang.String)
 */
public final static PrintStream out = nullPrintStream();

And this is how println method looks like:

/**
 * Terminates the current line by writing the line separator string.  The
 * line separator string is defined by the system property
 * <code>line.separator</code>, and is not necessarily a single newline
 * character (<code>'\n'</code>).
 */
public void println() {
newLine();
}
adarshr
  • 61,315
  • 23
  • 138
  • 167
2

Source Page

System.out.println()

System is a built-in class present in the java.lang package. This class has a final modifier, which means that, it cannot be inherited by other classes. It contains predefined methods and fields, which provides facilities like standard input, output, etc.

out is a static final field (ie, variable) in the System class, which is of the type PrintStream (a built-in class, contains methods to print the different data values). static fields and methods must be accessed by using the class name, so ( System.out ).

out here denotes the reference variable of the type PrintStream class.

println() is a public method in the PrintStream class used to print the data values. Hence to access a method in the PrintStream class, we use out.println() (as non static methods and fields can only be accessed by using the reference variable)

eg:

int i = 3;
System.out.println(i);

The above code prints the value of 3 in the screen and brings the control to the next line.

Leigh
  • 28,765
  • 10
  • 55
  • 103
Sarin Jacob Sunny
  • 2,138
  • 3
  • 29
  • 61
  • 1
    [Similar question1](http://stackoverflow.com/questions/3406703/whats-the-meaning-of-system-out-println-in-java/11202369#11202369), [Similar question2](http://stackoverflow.com/questions/10004856/just-wondering-system-out-println/11202355#11202355) – Sarin Jacob Sunny Jun 26 '12 at 09:28
2

out is a static field of System, of class PrintStream:

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/System.html

Paul Medcraft
  • 1,386
  • 11
  • 23
2

"out" is a static public field with Stream value.

public final class System {
    public final static PrintStream out = nullPrintStream();
...
}
e-zinc
  • 4,491
  • 2
  • 20
  • 17
2

out is a class static field of type PrintStream. Read here

adarshr
  • 61,315
  • 23
  • 138
  • 167
Michel Foucault
  • 1,724
  • 3
  • 25
  • 48
2

System is a class. out is a static field of the System class, and its type is PrintStream. println is an instance method of the PrintStream class.

Just look at the javadoc, and you'll have all the info you're looking for.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

Q. To find the length of S in given code, what you have to write in place of Ans??

 class Test{

       static String S="java";

       public static void main(String[] args) {

       System.out.println(Ans);

       }

 }

 Ans: Test.S.length()
  1. Here, S is static variable of type String present in Test class

  2. So, static variable is access using class_name.static_variable_name as Test.S

  3. To find length of static variable S, length() method is used of class String where S is an object and we know object can access method as S.length()

Same concept is used in System.out.println() as:

 class System{

      static PrintStream out;

 }
  1. System is class_name

  2. out is static variable of type PrintStream present in System class. It is also an object of class PrintStream and access method println() of same class.