• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Return Values

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Vasiq Molvizadah
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for the explanation buddy.....and btw that code was from SCJP Study Guide for Java 6...

 
Vasiq Molvizadah
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ???
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Vasiq Molvizadah
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

.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
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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

 
Marshal
Posts: 79261
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to go through it with a pencil and paper, then you can understand the different steps.
 
Vasiq Molvizadah
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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....


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic