File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can someone please explain System.out.println

 
Kay Kingston
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that System is a final class and it cannot be instantiated, out is a static final variable of type PrintStream in System class and println is a method in PrintStream class.Still I don't understand why we use System.out to call println() method.
To my knowledge a method can be called using an object reference, in case of static behaviors we use classname. Then why here we are using System.out.println? Can't we just create an object of PrintStream class and call the println() method as PrintStream class can be instantiated.
Are there any ways of calling a method apart from those I know(I have mentioned above what I know)?
Please help me....
 
Rakesh Keerthi
Ranch Hand
Posts: 152
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kay,

This is a nice question.
System is static class from java.lang package(default package in java) and cannot be instantiated ,out is a reference variable defined in System also it is a final variable, println()/print() are the methods used to print on standard output. and these are oveloaded methods from PrintStream class.

Hope this helps


 
Kay Kingston
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rakesh Keerthi wrote:Hi Kay,

This is a nice question.
System is static class from java.lang package(default package in java) and cannot be instantiated ,out is a reference variable defined in System also it is a final variable, println()/print() are the methods used to print on standard output. and these are oveloaded methods from PrintStream class.

Hope this helps




Hey thanks for your reply but what all you said.... I do know and almost all I have said that in my query.
What I don't understand is the way it is being called System.out.println()(ie classname.variable.method).
Lets say I want to use hashcode() method of String class, what I do in my program is
Declare and Initialize a variable of String type like
String s="Hi";
and I say
s.hashcode();
right?
I didn't use and I suppose I can't use some classname.variable.hashcode();
And of course referring the JSE doc didn't help me
Will be very thankful if someone clears my doubt

 
Rakesh Keerthi
Ranch Hand
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I tried an example corresponding to what you have written by creating 2 classes and using obj.hashcode() method, we have to use System.out.print() to print the data on console. the code i tried is as below.




and the output would be.

Hash code value is:611019154


please let me know if this is clear or you need anything else.
Apologies if anything is unclear as i'm also learning java newly.

 
Punit Jain
Ranch Hand
Posts: 1012
2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kay Kingston wrote:
To my knowledge a method can be called using an object reference, in case of static behaviors we use classname. Then why here we are using System.out.println? Can't we just create an object of PrintStream class and call the println() method as PrintStream class can be instantiated.

you mean by this:
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create your own PrintStream and write to it. It's actually quite a common occurrence. You might do it to write to a file, write to an area of memory, write across the network etc.

But you want more than that here. You want a PrintStream that specifically writes to the console. That means it needs to write to standard out, and how that works is going to depend on the environment you're in (Unix, Windows etc). Helpfully, the JVM creates a PrintStream for you that's set up to write to standard out for your particular environment. And the JVM makes this available through a public static variable of the System class: System.out.



 
Kay Kingston
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punit Jain wrote:
Kay Kingston wrote:
To my knowledge a method can be called using an object reference, in case of static behaviors we use classname. Then why here we are using System.out.println? Can't we just create an object of PrintStream class and call the println() method as PrintStream class can be instantiated.

you mean by this:


Yes that is what I meant but can you please explain System.out that you passed as an argument. Of course I know PrintStream doesn't have a no argument constructor, it has a constructor with OutputStream as an argument.And does the above code actually works?
 
Punit Jain
Ranch Hand
Posts: 1012
2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kay Kingston wrote:
Punit Jain wrote:
Kay Kingston wrote:
To my knowledge a method can be called using an object reference, in case of static behaviors we use classname. Then why here we are using System.out.println? Can't we just create an object of PrintStream class and call the println() method as PrintStream class can be instantiated.

you mean by this:


Yes that is what I meant but can you please explain System.out that you passed as an argument. Of course I know PrintStream doesn't have a no argument constructor, it has a constructor with OutputStream as an argument


Matthew Brown wrote:
But you want more than that here. You want a PrintStream that specifically writes to the console. That means it needs to write to standard out, and how that works is going to depend on the environment you're in (Unix, Windows etc). Helpfully, the JVM creates a PrintStream for you that's set up to write to standard out for your particular environment. And the JVM makes this available through a public static variable of the System class: System.out.

Kay Kingston wrote:
And does the above code actually works?

try this.
 
Rajdeep Biswas
Ranch Hand
Posts: 187
Eclipse IDE Java Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The default implementation when you write System.out.println("..."); is to write to the system console. If you want your own implementation as to how and where the output should go, you have to mention your own OutputStream and call the above-mentioned or other PrintStream constructor. A simple example at this link.
Can't we just create an object of PrintStream class and call the println() method as PrintStream class can be instantiated.

In that case the default implementation is gone. You have to, say for example, create a file, then pass it...You can try some code to check what all you can do!
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kay Kingston wrote:


Yes that is what I meant but can you please explain System.out that you passed as an argument. Of course I know PrintStream doesn't have a no argument constructor, it has a constructor with OutputStream as an argument.And does the above code actually works?

It works (try it yourself), but it doesn't do anything useful.

The PrintStream constructor can take any Stream. It "wraps" the Stream you pass to it - the idea is that you can write to the same location as that Stream, but using the interface that PrintStream provides (which is very convenient for writing text).

Since a PrintStream is-a Stream, you can pass it as the constructor argument to a new PrintStream. But all that gives you is a PrintStream that writes to the same location as the original PrintStream. In other words, it does exactly the same as the object you started with. So you haven't gained anything.
 
Pallavi Sadit
Ranch Hand
Posts: 44
3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kay,
The variable out in the System class is initialized with the default output stream object at the time of JVM startup. So, System.out is actually returning a PrintStream object on which you can call any method of PrintStream including println(). Wherever you need to use the default output stream (console) to print your data, you can directly call the System.out.println() , wherever you need to use an output stream of your choice like a file, you can instantiate a PrintStream object with that output stream and use it .
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic