• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding the pool of strings in Mala Gupta's book

 
Mohammad Ali Asgar
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,

I read in Ch4 of Mala Gupta's book that if a string is created by directly placing a sequence of characters within a pair of " " AND without using the new operator, then the object created is placed in a pool of String objects.

I wonder whether the following piece of code also puts objects (with values Java and Oracle) in that pool since the new operator is not present.


I would appreciate a reply from anyone with decent knowledge of Java.

__ softwarelover
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammad Ali Asgar wrote:I would appreciate a reply from anyone with decent knowledge of Java.

That's something you can verify yourself if you have a good understanding of the working of the string constant pool.

If they are in the String constant pool, the output will print true, false otherwise.
 
Mohammad Ali Asgar
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Yes, i get it now. The == operator will compare references and that can help verify whether strings were created or reused from the pool.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammad Ali Asgar wrote:The == operator will compare references and that can help verify whether strings were created or reused from the pool.

 
Mohammad Ali Asgar
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Interesting outputs:

true
true
false

Because of that new operator, two copies of "OCA SE7" exist.

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammad Ali Asgar wrote:Because of that new operator, two copies of "OCA SE7" exist.

That's well explained in this excellent article.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic