Home Java Java-get-example Java example program to get the object's ID



Java example program to get the object's ID
Posted on: October 24, 2008 at 12:00 AM
In java there is no any specific method that provides us the object's ID. But each object has its own unique hash value which can be treated as unique Id for that object.

Java example program to get the object's ID

     

java get Object id

In java there is no any specific method that provides us the object's ID. But each object has its own unique hash value which can be treated as unique Id for that object.

To explain this concept here we are getting the hash code for the two object and this can be done by using the hashCode() method on both of the created objects. 

String ind = new String("India");
System.out.println("Hash code for String object: "  + ind.hashCode());   

Above line of code creates a new String object "ind" and its hash code can be get by the method hashCode().  

GetObjectId getObj = new GetObjectId(); 
System.out.println("Hash code for Class object: "  + getObj.hashCode()); 

In above lines we have created a new object of GetObjectId and for its hash code we will be using hashCode() method. Here is the full example code of GetObjectId.java as follows:

GetObjectId.java

public class GetObjectId
{
  public GetObjectId(){}
  public static void main(String args[]){
  String ind = new String("India");
  System.out.println("Hash code for String object: " 
 
+ ind.hashCode());
  GetObjectId getObj = new GetObjectId();
  System.out.println("Hash code for Class object: " 
  + getObj.hashCode
());
 }
}

Output:

C:\javaexamples>javac GetObjectId.java

C:\javaexamples>java GetObjectId
Hash code for String object: 70793495
Hash code for Class object: 4072869

Download Source Code

Related Tags for Java example program to get the object's ID:
javacidehashobjectmethodviuniquevalueidifforeachcisheitdescanpeinnoasmtrcajesspecmeobjprowhichuniqsspatanyishaeaspecificvassthavaluhatjepro


More Tutorials from this section

Ask Questions?    Discuss: Java example program to get the object's ID   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.