What's the meaning of System.out.println in Java? - java

Is this static println function in out class from System namespace?
namespace System {
class out {
static println ...
}
How can I interpret this name? And where in JRE this function is defined? In java.lang.System/java.lang.Object?

No. Actually out is a static member in the System class (not as in .NET), being an instance of PrintStream. And println is a normal (overloaded) method of the PrintStream class.
See http://download.oracle.com/javase/6/docs/api/java/lang/System.html#out.
Actually, if out/err/in were classes, they would be named with capital character (Out/Err/In) due to the naming convention (ignoring grammar).

System is a class, that has a public static field out. So it's more like
class System
{
public static PrintStream out;
}
class PrintStream
{
public void println ...
}
This is a slight oversimplification, as the PrintStream class is actually in the java.io package, but it's good enough to show the relationship of stuff.

System.out.println()
High level Understanding
For understanding this we need to recall few basics of java:
dot (.) operator in java: In java . (dot operator) is used only to call methods or variables.
So we can say out is either method or variable.
Methods in java : we know methods always have parenthesis ‘( )’ after method name, So out cannot be a method in java. So out its a variable and println() is a method.
Class name in java: Class name should start with Capital letter ideally in java, So System is a class.
Now with basic knowledge of java we know :
System is a Class
out is a Variable
println() is a method
Lets get more in details:
out variable: static or instance?
called using class name, so we know its static variable of System class.
but its calling a method println() method so ‘out’ is an object of the reference type PrintStream.
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();
//...
}
Explained answer on my youtube what is System.out.println

Check following link:
http://download.oracle.com/javase/1.5.0/docs/api/java/lang/System.html
You will clearly see that:
System is a class in the java.lang package.
out is a static member of the System class, and is an instance of java.io.PrintStream.
println is a method of java.io.PrintStream. This method is overloaded to print message to output destination, which is typically a console or file.

println and print are the two overloaded methods which belong to the PrintStream class.
To access them we need an instance of this class.
A static property called out of type PrintStream is created on the System class.
Hence to access the above methods we use the following statements:
System.out.println("foo");
System.out.print("foo");

System.out.println("Hello World");
System: It is the name of standard class that contains objects
that encapsulates the standard I/O devices of your system.
It is contained in the package java.lang. Since java.lang package is imported in every java program by default,therefore java.lang package is the only package in Java API which does not require an import declaration.
out:The object out represents output stream(i.e Command
window)and is the static data member of the class
System.
So note here System.out (System -Class & out- static object i.e why its simply referred to by classname and we need not create any object).
println:The println() is method of out object that
takes the text string as an argument and displays it to the standard
output i.e on monitor screen.
Note
System -Class
out -static Object
println() -method
Remember a function (in java function is called method) always has the format function()

• System is a class in java.lang package
• out is a static object of PrintStream class in java.io package
• println() is a method in the PrintStream class

System is a class of java.lang package, out is an object of PrintStream class and also static data member of System class, print() and println() is an instance method of PrintStream class.
it is provide soft output on console.

It is quite simple to understand the question, but to answer it we need to dig deeper in to Java native code.
System is static class and cannot be instantiated
out is a reference variable defined in System
println() is the method used to print on standard output.
A brief and nice explanation is always welcome on this as we can learn much from this single line of statement itself!

Because out is being called with the System class name itself, not an instance of a class (an object), So out must be a static variable belonging to the class System. out must be instance of a class, because it is invoking the method println().
// the System class belongs to java.lang package
class System {
public static final PrintStream out;
}
class PrintStream {
public void println();
}

System is a class in java.lang package. And out is a PrintStream object. Nice explanation # http://lazy-geeks.blogspot.in/2015/01/what-is-systemoutprintln.html

System.out.println();
System is the class
out is a variable in the System class and it is a static and variable type is PrintStream.
Here is the out variable in System class:
public final static PrintStream out = null;
You can see implementation of System here.
println() is a overloaded method in PrintStream class.
PrintStream includes three overloaded printing methods, those are:
print()
println()
printf()
You can see implementation of PrintStream here.
You cannot instantiate System class and it is child class of Object and the Object is the father(superclass) of every classes including classes that you defined.
Here is what the oracle docs says:
public final class System extends Object
The System class contains several useful class fields and methods. It
cannot be instantiated.
Among the facilities provided by the System class are standard input,
standard output, and error output streams; access to externally
defined properties and environment variables; a means of loading files
and libraries; and a utility method for quickly copying a portion of
an array.
Since:
JDK1.0
If you donot know what is meant by instantiate, read this questioh. It is C# question but the concept is same.
Also, What is the difference between an Instance and an Object?
If you donot know what is meant by overload read this quesiotn.

System is a class in java.lang package.
out is the static data member in System class and reference variable of PrintStream class.
Println() is a normal (overloaded) method of PrintStream class.

From the javadoc about System, here's what the doc says:
public final class System
extends Object
The System class contains several useful class fields and methods. It cannot be instantiated.
Among the facilities provided by the System class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array.
Since:
JDK1.0
Regarding System.out
public static final PrintStream out
The "standard" output stream class Prinstream belongs to java.io package. This stream is already open and ready to accept output data.
When the JVM is initialized, the method initializeSystemClass() is called that does exactly what it’s 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().
Typically this stream corresponds to display output or another output destination specified by the host environment or user.
Regarding println();
class PrintStream{
public void println();
}
For simple stand-alone Java applications, a typical way to write a line of output data is:
System.out.println(data);

System is the java class.
out is the instance and also static member of PrintStream.
println is the method of PrintStream.

System.out.println("...") in Java code is translated into JVM. Looking into the JVM gave me better understanding what is going on behind the hood.
From the book Programming form the Java Virtual Machine.
This code is copied from https://github.com/ymasory/programming-for-the-jvm/blob/master/examples/HelloWorld.j.
This is the JVM source code.
.class public HelloWorld
.super java/lang/Object
.method public static main([Ljava/lang/String;)V
.limit stack 2
.limit locals 1
getstatic java/lang/System/out Ljava/io/PrintStream;
ldc "Hello, world"
invokevirtual java/io/PrintStream/println
(Ljava/lang/String;)V
return
.end method
.end class
As "The JVM doesn't permit byte-level access to memory" the out object in type Ljava/io/PrintSteram; is stored in a stack with getstatic JVM command.
Then the argument is pushed on the stack before called a method println of the java/io/PrintStream class from an instance named out. The method's parameter is (Ljava/lang/String;) and output type is void (V).

System: is predefined class of java.lang package.
out: is a static member of printStream class and its connect with console.
Println: is a method of printstream class and its not a static.

System.out.println
System is a class in the java.lang package.
out is a static data member of the System class and references a variable of the PrintStream class.

System - class which is final in nature. public final class System{}. Belongs to java.lang package
out - static reference variable of type PrintStream
println() - non static method in PrintStream class.
PrintStream belongs to java.io package.
To understand it better you can visit : How System.out.println() Works In Java

Related

Is System.out.println() a classwide method or instance method?

To my belief, it would be a classwide method, since it can exist throughout the class, rather than confined to a single method. I'm not 100% sure about this however, so if anyone could provide some guidance on this, it would be greatly appreciated.
in System class out is a static PrintStream instance. PrintStream class has println() method which is at instance level.
public final class System extends Object
The System class contains several useful class fields and methods. It cannot be instantiated.
Among the facilities provided by the System class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array.
For more info check out the Java Documentation
It is a instance method of PrintStream class defined in System class as
public final static PrintStream out = null;

how does jvm enter in public static void main?

How can jvm enter in default class:
class try1
{
public static void main(String args[])
{
...
}
}
In it how does JVM access this method?
In packages, if a class is 'default' its public methods cant be accessed from outside the package, so how does jvm enter this class?
It is not JVM itself who invokes main method. This is rather a job of Java launcher, i.e. java.exe.
Java launcher is a small program written in C that uses regular JNI functions:
JNI_CreateJavaVM to create a new instance of JVM and to obtain an instance of JNIEnv;
JNIEnv::FindClass to locate the main class specified in the command line;
JNIEnv::GetStaticMethodID to find public static void main(String[]) method in class #2.
JNIEnv::CallStaticVoidMethod to invoke the method found in #3.
In fact, JNI allows you to work with all classes, methods and fields, even with private modifier.
First of all the JVM does not enter the method, it invokes (calls) it (yes, it matters). The keyword public declares that the method can be accessed from anywhere (different packages); the static keyword declares that you can call the method without instatiating the class (among other things) and as far as I know the class that contains the main method is always public.
You explicitly told java what class to load on the command line or in a .jar's Manifest if you're running an executable jar.
The Java Specification Chapter 12 goes briefly into what happens when the JVM starts up. (The JVM Specification Chapter 5 covers it in more detail.)
In short:
java try1 will load the try1 class, then link, verify, resolve, and initialize it.
Once that's done, it will look for a main method that is public, static, and void that accepts an array of Strings, then it will execute that method.
The JVM doesn't care that your class wasn't public. As the first class loaded, it is the current compilation unit and initial access control is calculated from it.

java Why we write System with out.print() [duplicate]

This question already has answers here:
How does System.out.print() work?
(9 answers)
Closed 8 years ago.
I have read that System is a class, out is a object and print is a method but to call a method, we simply need to use object then why should we use System.
Can anyone please tell me.
Because that's the way we access to static members of a class. In this case, the class is System and its static member out is declared as
public final static PrintStream out = null;
Since it's public we can access to it directly, but using the notation SomeClass.someStaticMember
System.out
and if we want to call a method of that object (because that member is an object), we have to call it as
System.out.println()
System – is a final class and cannot be inherited. As per javadoc, “…Among the facilities provided by the System class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array…”
out – is a static member field of System class and is of type PrintStream. Its access specifiers are public final. This gets instantiated during startup and gets mapped with standard output console of the host. This stream is open by itself immediately after its instantiation and ready to accept data.
println – println prints the argument passed to the standard console and a newline. There are multiple println methods with different arguments (overloading). Every println makes a call to print method and adds a newline. print calls write() and the story goes on like that.
An Example to explain
Let me try explaining this way.
How do you use a convert an String to an int?
We use Integer.parseInt();
Here parseInt() is a static method in the Integer class.
Similarlly, out is a static Member of type PrintStream in the System class.
public final static PrintStream out = nullPrintStream();//line 82
Hence we use System.out
println() is a method os PrintStream hence we use System.out.println()
If you don't like to write System everytime, you could import it:
import static java.lang.System.*;
public class Main {
public static void main(String[] args) {
out.print("Hello, world!");
}
}
But usage of System.out.print() seems to be a common habit already.
System is a class and out is a static member field that System contains
out is a variable(ref variable) that is declared in System class
public final static PrintStream out = null;
as it is static variable you need to call it through class name.As System class's constructor is private you can not create the object of System class & you can't call it through reference .There is a way you can directly call out.println by using static import..refer bellow example ..
import static java.lang.System.out;
public class MainTest {
public static void main(String[] args) {
out.print("Hello");
}
}
Hope it may help .

How Class/Static Methods get invoked

While reading The Ruby Programming Language I came to know that, class methods are invoked on an object which got the same name as class.
I found objective-c also does a similar thing from a blog. Here Classes happens to be instances of meta-classes.
As another major Object Oriented language i would love to know how Java implements/achieve this.
Edit
I appreciate the answers from everyone. But my question wasn't about how to invoke a class method. As many answers were answering that. Apologies if my question was not well framed or it gave you a wrong idea.
In Java We can call static methods with class name.like
ClassName.staticMethod(args);
Keep in mind that they are class level methods and variables and beyond to any object , So thats why they are called by class name.
You can see its live demo by JVM when compiling any java program with error because main is also a static method
public class TradingSystem {
String description = "electronic trading system";
public static void main(String[] args) {
description = "commodity trading system";
}
}
Cannot make a static reference to the non-static field description
at TradingSystem.main(TradingSystem.java:8)
Also see 10 points about static keyword in Java AND Here is the doc
In the Java language, static methods are invoked on the class instead of an object e.g. System.currentTimeMillis. So conceptually this is very similar to Ruby, ObjC, Smalltalk, etc.
Unlike Ruby and Objective C, there is no object instance that those methods get invoked upon: The Java bytecode has a special bytecode instruction that invokes the static method; this bytecode instruction does not use an object pointer from the stack:
INVOKESTATIC "java/lang/System" "currentTimeMillis" "()J"
When using reflection, this special handling of static methods is represented by the fact that you don't need to specify an object that the method is called upon. Instead, you can supply null as the target of the call.
Refer the JLS:
A class method is always invoked without reference to a particular object.
When the Java virtual machine invokes a class method, it selects the method to invoke based on the type of the object reference, which is always known at compile-time, static (early) binding.
Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class, as
ClassName.methodName(args)
moreover a static method is called by prefixing it with a class name, eg, Math.max(i,j);. Curiously, it can also be qualified with an object, which will be ignored, but the class of the object will be used.
Official Docs
In java static methods belongs to the class rather than objects of that class. So, static methods are called by className as:
ClassName.staticMethod();
static method or variables are invoked using the class itself.
e.g.
if you have a static member as public static Integer age in class Employee, then you invoke it using Employee.age
same goes with methods also. e.g. Employee.paySalary();
Let us consider an example.
public class StaticDemo {
public static void methodToPrintSomething(){
System.out.println("printing any crap");
}
public static void main(String[] args) {
StaticDemo.methodToPrintSomething();
StaticDemo obj = new StaticDemo();
obj .methodToPrintSomething();
}
}
Here method is called using obj.methodToPrintSomething(). Another interesting thing you can find is the statement obj.methodToPrintSomething(). We are creating an object to call a static method, but that does not mean we are calling methodToPrintSomething() on the object. As it is static, internally the statement obj.methodToPrintSomething() will be treated as StaticDemo.methodToPrintSomething()

Just wondering System.out.println()

Just asking if I have the right understanding
System.out.println();
System is the package
out is the class
println() is the method
If this is wrong, then please tell me what the correct answer is.
No,
System is the class, which resides in java.lang package (that's why you don't need to import it).
out is a static variable of System class. It is public, so that you can access it from outside and it is static so that you it's associated to the class declaration and not to any instance of it.
println() is a method, indeed. And it is a method of out variable, which is a PrintStream instance.
out is a static object of printstream class
System -class,
PrintStream -class,
out - static object of PrintStream class ,
println - public method in PrintStream Class
System = class
out = static object of the PrintStream class
println() = method
read this http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/System.html
System is a class from package java.lang.
out is a public, static member of System class, and println is a method, yes.
System is not a package. It is a class which is contained inside java.lang package
Take a look here http://docs.oracle.com/javase/7/docs/api/java/lang/System.html
out is a PrintStream object (static in case of System class) in which println() is one of the methods
No, your understanding is wrong.
"Then What is right" -
System - a class,
out - a static public member of type PrintStream ,
and oh yes println() is a method.
You were 33% right ;) read java documentation for this here

Categories

Resources