• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

How many Objects ?

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

Have doubt ...need clarification

String one = new String("someString");
String two = new String("someString");
two.intern();

How many objects will be created for this..

What is the use of intern() method ? Can any one expalin in detail

Regards,
Venkat
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Whenever you declear these,

String one = new String("someString");
String two = new String("someString");
Two Object will be created on the heap.
But if you declear
two.intern(); then one object will be garbase collected & two will refer the String Object that is created in 1st line.

intern() will search the String Pool whether there is any String Object available that has the same content,if found the it will return that reference.

I think the statemnt will be,
two=two.intern();
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi...


In the case of string literals , intern is called implicitly whereas in case of String objects we need to call them explicitly...

Regards
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String one = new String("someString");
String two = new String("someString");
two.intern();

here firstly four objects are created
line 1:
2 objects one in the string pool and other in non string pool and one reference var
similarly,
line 2:
2 objects one in the string pool and other in non string pool and ref var

now about two.intern()
according to me this searches the string pool for the same String which is referred by two ,if its there it returns the String from the pool.

I have a doubt about intern() ,please clarify if i am wrong.
 
Venkat KG
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Just need to know after intern() how many no of objects will exists
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you explain me what do you mean by ...

non string pool

Regards
 
Purujit Saha
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friend harish shankarnarayan,
I think you are making things complex...
All Objects are created in the heap ...in the StringPool only references are stored not the object...

Plz go through the link & find the below mentioned lines there..
http://www.javaranch.com/journal/200409/Journal200409.jsp#a1

[
" What is the String Literal Pool? Most often, I hear people say that it is a collection of String objects. Although that's close, it's not exactly correct. Really, it's a collection of references to String objects. Strings, even though they are immutable, are still objects like any other in Java. Objects are created on the heap and Strings are no exception. So, Strings that are part of the "String Literal Pool" still live on the heap, but they have references to them from the String Literal Pool. "
]
 
harish shankarnarayan
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello kumar

sorry , imeant not in the String pool memory,
can u plz explain how many objects and wat exactly intern() does wat i have explained above is it right.
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by harish shankarnarayan:
[QB]String one = new String("someString");
String two = new String("someString");
two.intern();

here firstly four objects are created


No; the two literals ("someString") refer to the same instance of String. Only three String objects will be created in this code.

...this searches the string pool for the same String which is referred by two ,if its there it returns the String from the pool.


Furthermore, if the String object is *not* in the pool, it will be put there.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..Steve

In this code

String one = new String("someString");
String two = new String("someString");
two.intern();

if we comment out two.intern()..how many objects are created?

and where do they reside

Thanks
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think there is a difference between two.intern(); and two=two.intern();

String one = new String("someString");
String two = new String("someString");
At the end of this two Objects(object1 & object2) will be created on the heap.

Now if we use two.intern(); one more objct (object3) will be created on the heap and this object will be referenced from String Literal Pool. At this point of time none of the 3 objects are eligible for GC.

Instead of two.intern(), if we use two=two.intern(), our two will now point to object3 and not object2. Now object2 is eligible for GC.

Now if we write one=one.intern() it will not create another object in the heap as we have a reference to object3 in String Literal Pool. Now one also points to object3 and object1 will be eligible for GC

Thanks,
Prashanth
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

String one = new String("someString");
String two = new String("someString");
two.intern();

if we comment out two.intern()..how many objects are created?


Same answer: one String object for the literal, two others from the "new" keywords. All two.intern() will do is return a reference to the same instance referred to by the literal. You can prove this to yourself with code...

Hope this helps!
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Now if we use two.intern(); one more objct (object3) will be created on the heap and this object will be referenced from String Literal Pool.

No, a call to intern() will not create a new String object. It will search the pool for a String with the same character sequence. If it finds a match, it will return the reference to the match. Otherwise, it will be added to the pool, and its reference will be returned.

Hope this helps.
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Purujit Saha:
Whenever you declear these,

String one = new String("someString");
String two = new String("someString");
Two Object will be created on the heap.

But if you declear
two.intern(); then one object will be garbase collected & two will refer the String Object that is created in 1st line.


Just a couple of small corrections:

All objects are created on the heap; this includes instances of String referred to by literals, so after the first two lines, there will be three objects on the heap: one referred to by the literal, and the two created by those two lines. The String object referred to by the literal "someString" is pooled.

If you just call two.intern(), it doesn't do anything special to the String object that "two" points to. What happens is that Java searches pooled Strings for a matching character sequence. It will find one in this case - the pooled string referred to by the literal "someString", and return a reference to the pooled string. If you don't store this returned reference in a variable, it's just popped off the stack and discarded. Nothing happens to the object "two" points to. You can see this with the following code:
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


String one = new String("someString");//#1
String two = new String("someString");//#2
two.intern();//#3


As I know..
at line 1 & 2 one new object is created on the heap i.e total 2 objects.
At line 3: When u say intern on reference var 'two' following things happens :--

1) JVM looks in StringPool for any reference pointing to the string object with value "someString".
2) If it finds one, it simply returns the existing reference.
3) Otherwise it creates new String object on the heap with the value
"someString" , and place reference to it in StringPool.

Here actually we r not assigning the obj returned by intern()to any reference var, so it is by birth eligible for GC...

It is experimented....
So please don't write things just like that for fun...




[ November 16, 2005: Message edited by: Mahendar Reddy ]
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here actually we r not assigning the obj returned by intern()to any reference var, so it is by birth eligible for GC...


The call to intern() in the code you posted will return a reference to the pooled object referred to by "someString". It doesn't create any new objects.

If we don't use a literal, though...


[ November 16, 2005: Message edited by: Steve Morrow ]
 
Venkat KG
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Steve!
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice explanation Steve...
ThankQ
 
Purujit Saha
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friend steve, Thnx for your explanation, i got it.
But i would like to mention that most of the friends made the mistake that StringPool contains String Objects, Its wrong, The StringPool only contains the references only.Like all objects Strings are also resides in heap.

See the code below,

class B
{
public static void main(String arg[])
{

String one = new String("abc"); // Two objects created one for the literal & another for the new keyword, but both should reside in the heap.
String two = new String("abc");// One object will be created due to the new keyword & no further object will be created here due to literal as same literal string object is already there is the heap.

one=one.intern(); // Here the object created due to String constructor in first line is garbase collected & 'one' will refer the that String literal object created in first line.

System.out.println(one == two); // false

two=two.intern(); // Here the String object created in second line will be garbase collected & two will refer the String literal object created in first line.
System.out.println(two == one); // true
}
}
[ November 17, 2005: Message edited by: Purujit Saha ]
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..


....... garbase collected




So essentially intern() on string objects

(that are created by new)

will make the string as a literal.... since it would now be discarding the object created by new


Is that what we can infer from your post...
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Purujit, your example is good, but please be careful about statements such as this one, especially in the SCJP forum...

Here the object created due to String constructor in first line is garbase collected


This statement implies some certainty about GC that shouldn't be there. There is no guarantee that any object will be garbage collected in that code; it's quite likely that nothing will be garbage collected at all. It would be more properly phrased like the following:

Here the object created due to String constructor in first line is eligible for garbage collection...


Beware of questions that ask "how many objects will be garbage collected" versus "how many objects are eligible for garbage collection". It's a subtlety of the language that must be observed.

Hope this helps!
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So essentially intern() on string objects
(that are created by new)
will make the string as a literal....


It might not a good idea to think of it that way, as the term "string literal" has a very specific meaning with respect to Java. String literals are one of several kinds of constant expressions; they are references to String objects represented in source code by characters enclosed in double-quotes. They are resolved to an actual object through a procedure that guarantees that all literals containing the same character sequence refer to the same String object.

Calling intern() on String objects won't make the object a "literal". It will simply return a reference to a String object representing that character sequence that is also pooled so as to be shared.

Regards.
 
Purujit Saha
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, we can't say when an object will be garbase collected, but can say when an object is eligible for garbase collection.

Thanks Steve for your rectification.
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May i know what an intern method() does. Is it included in SCJP 1.4?
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Akhil Trivedi

Here the java doc says abt intern method.

Returns a canonical representation for the string object.When the intern method is invoked, if the pool already contains a string equal to this <code>String</code> object as determined by the {equals(Object)} method, then the string from the pool is returned.
Otherwise, this <code>String</code> object is added to the pool and a reference to this <code>String</code> object is returned.



Let me explain with the example

Stirng s = new String("Intern");
System.out.println(s.intern() == "Intern"); //TRUE
In the above statement we are creating two String Objects . Let we named the memory area as

S001 for the String literal "Intern"
S002 for the String s which has the content as "Intern"

in the second line s.intern() will look for the String content "Intern" which has the memory S001 . the found memory area (S001) will be return when ever the intern() method called by the String object has the content equals to "Intern"

see the output below u can get an idea

 
Akhilesh Trivedi
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Arul. But my second question still remains unanswered...

"Is it included in SCJP 1.4?"
 
Arul Prasad
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Akhil Trivedi


That i am not sure ...
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic