Results 1 to 5 of 5

Thread: Object ID

  1. #1
    javaplus is offline Member
    Join Date
    Nov 2007
    Posts
    97
    Rep Power
    0

    Default Object ID

    I believe that every object I create, has its unique ID in JVM. I want to get that ... lets say print on the console.

    Can anyone help me with this...
    Ill be thankful

  2. #2
    javagk is offline Member
    Join Date
    Nov 2007
    Posts
    12
    Rep Power
    0

    Default

    Each object has a unique hash value which you can treat as unique ID for that object.
    Java Code:
    String abc = new String("Australia");
    System.out.println("Hash code for String object: " + abc.hashCode());
    
    Student obj = new Student();
    System.out.println("Hash code for Student object: " + obj.hashCode());
    Output:
    Java Code:
    Hash code for String object: -1357076128
    Hash code for Student object: 26022015

  3. #3
    javaplus is offline Member
    Join Date
    Nov 2007
    Posts
    97
    Rep Power
    0

    Default

    Thanks mate. Works fine.

  4. #4
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default

    It is an interesting info for me.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    It can be important in many cases. Important thing to remember is that hash codes are signed numbers(long). I miss that point in many cases when I'm learning it. Since that hashCode() can be overridden by subclasses you may associate with your own hash code with object.

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2010, 03:12 AM
  2. JSP - Application object example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-17-2008, 08:40 AM
  3. Object
    By sireesha in forum New To Java
    Replies: 1
    Last Post: 01-10-2008, 02:31 AM
  4. Object size
    By kavithas in forum New To Java
    Replies: 0
    Last Post: 11-30-2007, 01:00 PM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 04:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •