• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

What dose this output mean????

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Check
{
public static void main(Stirngs agrs[])
{
Check c = new Check();
System.out.println(c);
}
}

Now wat is the value that is printed in the output???
Is it any grabage value from the memory?
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it is the output of the toString method defined in Object.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Srikanth,

Nothing like garbage value as you see in C, C++;


It if Nameoftheclass@hashCode() of your object.
hashCode() is a public method declared in the Object class. Every class inherits that method.

You add the following to your class and see the output:



Now you try to print the object using println();
you will get output like:

YourClassName@32af

Got it?
Regards,
cmbhatt
[ April 11, 2007: Message edited by: Chandra Bhatt ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Srikanth CS,"

Welcome to JavaRanch!

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and last name.

Initials are okay for a first name, but not a last name.

You can edit your display name here. Thank you for your prompt attention!

-Marc
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Have a look at following link.

What is Object hash code?
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

This is also good.

http://www-128.ibm.com/developerworks/java/library/j-jtp05273.html
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chandra:

Hi Srikanth,

Nothing like garbage value as you see in C, C++;


It if Nameoftheclass@hashCode() of your object.
hashCode() is a public method declared in the Object class. Every class inherits that method.

You add the following to your class and see the output:

code:


public int hashCode() {
return 32af;

}



Now you try to print the object using println();
you will get output like:

YourClassName@32af

Got it?
Regards,
cmbhatt

^
I tried putting the above hashCode() method as
-----------------------------------------------

public class Tempo
{

public int hashCode()
{
return 32af;

}
public static void main(String[] args)
{
Tempo temp = new Tempo();
System.out.println(temp);

}
}
-----------------------------------------------

While compiling, it give an error like this:

_------------------------------------------------
C:\Practice\RA\java\2007\Tempo.java:10: ';' expected
return 32af;
^
1 error

Tool completed with exit code 1
-------------------------------------------------

When I don't override hashCode(), println prints probably memory address.

Why do you think it might be giving the error?


Thanks,

Raghav
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Raghav,

I made a typo,
Try this!




cmbhatt
 
Keith Lynn
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
32af is not an int.

If you want to use it as a hex number, then precede the number with 0x.

This will compile.

 
Raghav Aggarwala
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great! it works. Thanks guys.
 
Raghav Aggarwala
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Srinivas:

.
.Hello,
.
.Have a look at following link.
.
.What is Object hash code?
.

http://www.devx.com/tips/Tip/5839

The link you gave above states that:

-----------------------

Just because an object's reference equals another object's reference (remember that the equals() method compares object references by default), it does not necessarily mean that the hash codes also match.

-----------------------

Isn't that breach of contract of hashCode() method implementaion?

" If two objects are equal according to equals method, they must return same hashCode"


Raghav
 
Raghav Aggarwala
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Srinivas Kalvala:
Hello,

This is also good.

http://www-128.ibm.com/developerworks/java/library/j-jtp05273.html




Srinivas:

.
.Hello,
.
.Have a look at following link.
.
.What is Object hash code?
.

http://www.devx.com/tips/Tip/5839

The link you gave above states that:

-----------------------

Just because an object's reference equals another object's reference (remember that the equals() method compares object references by default), it does not necessarily mean that the hash codes also match.

-----------------------

Isn't that breach of contract of hashCode() method implementaion?

" If two objects are equal according to equals method, they must return same hashCode"


Raghav
 
Srikanth CS
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys the information was very useful and the link as well.
 
Because those who mind don't matter and those who matter don't mind - Seuss. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic