Just wondering System.out.println() - java

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

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;

Something about System.out.println

The text book has it mentioned as:
System.out -> Class
Println -> Method
But, I disagree.
I think it's:
System -> Class
Out -> Method
Now, according to my logic, should Println be a method inside the method (Out)?
What is the correct concept here?
System.out isn't a class or a method, it's a static field on the System class that is a reference to an instance of a class (PrintStream). println is an instance method of PrintStream (or more completely, println is the common name shared by a group of instance methods that are overloaded with different arguments).
System is a class. Out is an object (public static final PrintStream out). println and print are methods. Trust the textbook.
Proof
Methods cannot contain other methods, plus "out" never has any parentheses or takes any arguments, so "out" cannot be a method.

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 .

Working with Math class without instantiating it

I am new to programming and was studying "Head First Java", I just saw a problem where there was used Math class like this
int x= Math.round(float value);
and it was mentioned we don't need to instantiate Math class because its constructor is set private. What does that mean? Until now I read we need to instantiate that class and reference variable to play around with methods and instance variables of the class how does Math class work like this?
we don't need to instantiate Math class because its Constructor is set Private
Because all the methods in Math class are static you can use the class name to invoke them. So there is no use instantiating the class , hence the constructor was declared private. it will also prevent sub classing the Math class, since it is the only constructor.
Look at the open source code :
Don't let anyone instantiate this class.
private Math() {} // only constructor defined in Math class
The methods of Math class doesn't depend on the internal state of the class , they are just like utility functions . So it was wise to declare them as static. static methods can be invoked by directly using the classname , hence no use of instantiating the class. They belong to the class, not specific objects of that class.
You can refer the JLS 8.4.3.2 :
A class method is always invoked without reference to a particular object.
The Math class have all methods as a static, and you need to get the method from the class itself. No need to create instance variable to access Static variable and methods
Refer Math Class java doc. you find all method here static i,e. Math.round
Private constructors means that they can only be called upon from within the class to which they belong, a good example of the use of private constructors can be found here Can a constructor in Java be private?
Private constructors do however have nothing to do with the fact that you can use methods from the MATH class without instantiating them. This is because the methods of MATH class are static i.e. a static method can be called upon without instantiating an object of the class to which the methods belong.
As said in the comments above, you have no use of instantiating a MATH object, therefore the constructor is private, but you could use the MATH methods anyway had the constructor been public.

What's the meaning of System.out.println in 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

Categories

Resources