• Post Reply Bookmark Topic Watch Topic
  • New Topic

How instance controlled classes avoid duplicacy?  RSS feed

 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to figure out how instance controlled classes like Integer or String class ensures that duplicates are not there.
Suppose I have created thousands of string literals already in my program and then I try to create another literal.
Would java check with already existing huge number of literals and create new one if one is already not there?
Surely this is not a good approach.
Or there is another approach for it?

Manish
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know for sure, but my guess is this...

String literals are figured out at compile time. I would bet that there is some kind of table or hashmap, so each time the compiler encounters a new literal, it does a quick lookup to see if it exists. I would imagine even if you do have thousands of literals (and ugghh, by the way), it wouldn't take but a moment to do all the look-ups - minimal in comparison to the total compile time.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manish ghildiyal wrote:I am trying to figure out how instance controlled classes like Integer or String class ensures that duplicates are not there.

I'm not quite sure where you got the idea that Strings and Integers are instance-controlled, because that normally means that you can't create "duplicate" instances. With both the above classes you most certainly can.

I suppose you could say that String literals are instance-controlled, but to be honest it's not wildly important. In these days of memory measured in gigabytes, even thousands of duplicates won't affect your program that much...millions, perhaps...

You might want to have a look at the CachedObjects page for more info.

Winston
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Winston,

Thanks for inputs.

I came to conclusion that Strings and Wrapper classes are instance controlled because we can use '==' instead of equals to check the
equality for them.But now I realise that's for only those instances which are not made using constructor.

BTW, how is only a portion of a post is placed in quote as you did while replying in your second reply.

Manish
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manish ghildiyal wrote:I came to conclusion that Strings and Wrapper classes are instance controlled because we can use '==' instead of equals to check the equality for them.But now I realise that's for only those instances which are not made using constructor.

Then I'm afraid you were wrong. you should AvoidTheEqualityOperator (←click) at all costs.

BTW, how is only a portion of a post is placed in quote as you did while replying in your second reply.

It's controlled by the scope of "[quote] | [/quote]" tags. Have a play around with them
and use the 'Preview' button before you post.

Winston
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!