This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Java in General and the fly likes Return Values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Return Values" Watch "Return Values" New topic
Author

Return Values

Vasiq Molvizadah
Ranch Hand

Joined: Dec 24, 2009
Posts: 66
Hey guys....recently I compiled a program which was written in a book and it was about Return Types....
The program was about...returning a new class object in which the return type which was declared was a class itself....





The output which i'm getting is .....

You are in Class 1 constructor
returnpkg.Class1@19821f

I know why i got the first line....but from where did this second line came from....any help guys....


Human Knowledge Belongs to the World.
- Vasiq Mz
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

Vasiq Molvizadah wrote:The output which i'm getting is .....

You are in Class 1 constructor
returnpkg.Class1@19821f

I know why i got the first line....but from where did this second line came from....any help guys....


The println() method calls the toString() method to convert the object to a string so that it may be printed. Your class, which doesn't declare a toString() method, will inherit it from the Object class, which returns the name of the object, and the identity hash.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Vasiq Molvizadah
Ranch Hand

Joined: Dec 24, 2009
Posts: 66
Thanks a lot for the explanation buddy.....and btw that code was from SCJP Study Guide for Java 6...

Vasiq Molvizadah
Ranch Hand

Joined: Dec 24, 2009
Posts: 66
I've noticed one thing which is that....if you pass a new object to the println() method...then only it'll print the classname and the hashcode....it won't print the hashcode and the classname when you pass a method using a reference inside the println() method....
Why is it like that ???
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

The output of the classname and the hashcode is the default implementation of the toString of the Object class.
I don't know that you mean with "pass a new object to the println() method" because you'll always give a copy of the reference to the object to a method. Not an object itself. Could you show the code that is causing that output?


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Vasiq Molvizadah
Ranch Hand

Joined: Dec 24, 2009
Posts: 66


OUTPUT :-

I'm the constructor...
covariant.Test1@3e25a5
I'm the constructor...
I'm the method
null

In the above code....in the main ..the println() method will call the constructor first which prints the class name and hash code but in second line ..the println() method is calling the method Test1...in that case it is just printing the the method and the constructor without the classname and the hashcode....Why is it like that ???
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

.in that case it is just printing the the method and the constructor without the classname and the hashcode....Why is it like that ???



It is not "printing the method". It is instantiating an instance, calling a method of that instance, getting the value returned from the method, and passing it to the println() method. In other words, it is merely printing the value that is returned from the method... and if you look at your code, your method is returning a null reference.

Henry
Vasiq Molvizadah
Ranch Hand

Joined: Dec 24, 2009
Posts: 66
Thanks for the explanation Henry...
what I'm getting after reading is that....you will get the classcode and hashcode printed only if you call a constructor inside a println() method and the reason for that is becasue...Constructor has no return type...so that's why it goes to toString() method of the Superclass Object...
Correct me if i'm wrong...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

It uses Object's toString() implementation because you haven't overridden it in your own class. If you add it then that implementation will be used. It has nothing to do with the constructor. PrintStream.println(Object) always calls toString() on the argument (unless it's null).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Cris Ter
Greenhorn

Joined: Aug 01, 2009
Posts: 4
Step by step ,

System.out.println(c.doStuff()) , here the c.doStuff() returns a new Object of Class1 i.e. returns new Class1()

So System.out.println(c.doStuff()) transforms to System.out.println(new Class1())

While invoking new Class1(), the constructor runs and hence you get You are in Class 1 constructor

After that you are trying to print the object ( the System.out.println statement ). Since you dont have toString() override in Class1, the default identity hash is printed.
Vasiq Molvizadah
Ranch Hand

Joined: Dec 24, 2009
Posts: 66


Can someone explain me this program step by step.....it will clear all my doubts....

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38513
    
  23
You need to go through it with a pencil and paper, then you can understand the different steps.
Vasiq Molvizadah
Ranch Hand

Joined: Dec 24, 2009
Posts: 66
Campbell Ritchie wrote:You need to go through it with a pencil and paper, then you can understand the different steps.


The first statement of println() method will invoke the constructor....and it'll execute the constructor body, but to print the whole object ..it has to be converted into String which is possible with the help of toString() method which i had overridden in the class..so it'll directly call that method and it'll execute that method.

Then the second println() method will invoke the go() method and it'll execute the body.. since the method has a return type of String..there's no need to convert the method to String...so it'll directly print the return value of the method...

I don't know if I'm wrong or right.....if I'm wrong please correct me

Thanks for reading it....


 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Return Values