• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Ques on intern

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have read about String being interned in java i.e. the String objects with the same contents are maintained as a single object in the pool.

Now my ques is why doesn't java apply the same concept to other objects eg. Wrapper classes.

eg: Integer i1 = new Integer(10);
Integer i2 = new Integer(10);

Will i1 and i2 be interned.

Also what are the advantages of interning.

Regards

Nikhil Bansal
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only Strings get into the pool. When Java was first created it was aimed at Set top boxes and similar devices with little memory. The pooling helped conserve the memory.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a matter of fact, since JDK 5 there are other "pools" for various wrapper classes (integers in the range -128 to 127). Some more info can be found here. Typically, wrapper classes uses less memory than Strings, and aren't usually used as much as Strings are. Which is probably why they didn't create pools for wrapper instances before JDK 5. However with JDK 5 and autoboxing, wrapper instances occur more frequently than they used to. I'm guessing this is why Sun decided to start some limited pooling of wrappers.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its interesting that only strings get into pool and not for wrapper classes. Can any one explain how to know whether two string objects are interned or not.

If possible with an example.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From JDK 5 Wrapper Objects still go to the pool under certain conditions as
when the value is boolean(true or false),char('\u0000' to '\u007f'),
byte, and also short,int,long in range -128 to 127.
But float and Double doesn't go for pool.

Then all the string objects will be in the pool, but those created with new will have their refrence pointing to the different object with same value on the heap;

Correct me if wrong
[ March 22, 2006: Message edited by: Shaliey Gowtham ]
 
rajeshwar Rao
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)Well as per my knowledge two strings with same content which are created using new operator are not interned.

2)The two strings which return same constant expression during compile time they are said to be interned and if it is different they are not interned.


For example:

String str1 = new String("rajesh");
String str2 = new String("war");
String str3 = new String("rajeshwar");
String str4 = "rajesh"+"war";
String str5 = "rajesh"+str2;

In the above example str4 denotes same constant expression but str5 denotes same constant expression but at run time not at compile time.

So here str3 and str4 are interned.
and str3 and str5 are not interned.


I hope you are clear when two strings are said to be interned.


my question is even wrapper classes are treated as objects like strings then why they are not interned like strings.
 
Nikhil Bansal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot rajeswar.

Regards

Nikhil
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah - there are several problems here. I think the first thing you need to know is that the String pool, the intern() method, and similar functionality for wrapper classes, are not on the exam. You don't need to worry about this. You do need to know that any time you construct any object using new, you create a new object which is not part of any pool. This is true whether we're talking about a String or a wrapper.

Additional details may be of interest, but since this is in the SCJP topic for some reason, let's remind people: you don't need to worry about this stuff.

But, for those who are nonetheless curious...

[rajeshwar]: 2)The two strings which return same constant expression during compile time they are said to be interned and if it is different they are not interned.

If the strings are represented by compile-time constant expressions, the values of those strings are interned, period. If they're the same, then both strings are represented by the same interned String instance. If they're different, then they are represented by two different interned String instances.

[rajeshwar]: So here str3 and str4 are interned.
and str3 and str5 are not interned.


Hunh? You've contradicted yourself about str3. In your example, only str4 is interned; all the others (str1, str2, str3, str5) are not. You may want to study the definition of compile-time constant expression to understand why

[rajeshwar]: my question is even wrapper classes are treated as objects like strings then why they are not interned like strings.

I addressed this earlier in my previous post - did you read it?
[ March 22, 2006: Message edited by: Jim Yingst ]
 
You showed up just in time for the waffles! And this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic