Reputation: 4460
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.
Upvotes: 1
Views: 6554
Reputation: 140
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()
Here, S is static variable of type String present in Test class
So, static variable is access using class_name.static_variable_name as Test.S
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;
}
System is class_name
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.
Upvotes: 0
Reputation: 2138
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.
Upvotes: 2
Reputation: 1732
out
is a class static field of type PrintStream
. Read here
Upvotes: 2
Reputation: 4581
"out" is a static public field with Stream value.
public final class System {
public final static PrintStream out = nullPrintStream();
...
}
Upvotes: 2
Reputation: 691645
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.
Upvotes: 2
Reputation: 1406
out is a static field of System, of class PrintStream:
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/System.html
Upvotes: 2
Reputation: 62573
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();
}
Upvotes: 11