GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many objects are created? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How many objects are created?" Watch "How many objects are created?" New topic
Author

How many objects are created?

Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 187
Input

Output

As shown in output, the hashcode of the two different objects are same. So, it means that there is only one object is created by those two statements of Input at line no. 01 and 02???
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 187
Does hashCode() represents uniqueness of the Object?
Does hashCode() represents address of the storage of the Object?
HashCode must be unique for each Object, but not the same for subsequent execution of the same...
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

Gaurangkumar Khalasi wrote:So, it means that there is only one object is created by those two statements of Input at line no. 01 and 02???

If that was true, then (s2==s1) would return true. That's how you check if they're the same object.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

Gaurangkumar Khalasi wrote:Does hashCode() represents uniqueness of the Object?
Does hashCode() represents address of the storage of the Object?
HashCode must be unique for each Object, but not the same for subsequent execution of the same...


Briefly: No, no, and no, it doesn't. The only requirement on hashCode (look at the contract in the Object class) is that if two objects are equal (according to the equals() method), then the hash codes should be the same. The other way round isn't true.
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 187
Thank You
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Gaurangkumar Khalasi wrote:Input

Output

As shown in output, the hashcode of the two different objects are same. So, it means that there is only one object is created by those two statements of Input at line no. 01 and 02???


to add to the above discussion remember that hashcode method cannot be used to test for an object's equality ( for that you have your good old equals method). however it can be used to test two objects inequality.
also the operator == is used to test "referential equality". it test whether the references points to same object or not. in this case you are creating 2 string objects using new operator, so two different objects will be created , which will be referenced by two different references. hence it will return false. had you created strings "literally" then you would have got true in that case.
Guilherme Bazilio
Greenhorn

Joined: Mar 02, 2009
Posts: 23
Hey there guys,

I'm also studying for the exam and I remember to have read something about Strings.

gurpeet singh wrote:

had you created strings "literally" then you would have got true in that case.


Seems like String objects uses something like a "pool of Strings".

As you explicitly (using new keyword) created two String objects, then the pool was discarded.

Can anyone confirm if it's right?

Thanks!
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 187
Guilherme Bazílio wrote:As you explicitly (using new keyword) created two String objects, then the pool was discarded.

Can anyone confirm if it's right?


Yes.
For more information:
http://www.coderanch.com/t/547252/java/java/String-Constant-Pool
http://www.coderanch.com/t/550614/java/java/many-objects-will-created-string
and you can search more on javaranch...
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Guilherme Bazílio wrote:Hey there guys,

I'm also studying for the exam and I remember to have read something about Strings.

gurpeet singh wrote:

had you created strings "literally" then you would have got true in that case.


Seems like String objects uses something like a "pool of Strings".

As you explicitly (using new keyword) created two String objects, then the pool was discarded.

Can anyone confirm if it's right?

Thanks!


i wouldn't say it is discarded. pool is always there. when you use 'new' keyword ,we can say that pool is not involved.
 
GeeCON Prague 2014
 
subject: How many objects are created?