String Pool and Immutability

Hi every1.

I am learning Java String. I have two questions. the question is relevant to following code.

Q1.
String s1 = "Hello";
String s2 = "Hello";
String s3 = new String("Hello");

Now when I do s1==s2 the result is true. That's fine as I expected as they are pointing to the same literals.

But if I do s1==s3 the result is false. Why? I tought that for string class if you have String s1=""; and using String s = new String(""); are same its just which style you prefer. But now I think they are different.

Can  someone please tell me what is going on here.

Q2.
As I read, String pool is not garbage collected. So I thought maybe I can make my system to run out of memory. So I wrote a small program which keeps generating distinct string at run time. So as I think all these strings must be stored in the pool and never get garbage collect. But my system never went out of memory. I am running this program for more than two hours now. Why is that? Is the pool garbage collected?

Thank you,
Dharmesh
LVL 6
dharmesh_amityAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
>>But my question is when I create new strings at runtime where is new string stored

I answered that above:

>>Using new String allocates a new String object on the heap

(correcting the typo). Then the gc comes along and gc's the heap

0
 
CEHJCommented:
Q1
>>But now I think they are different

They are. Using new String allocates a new Stirng object on the heap. The reference will be different to the first one, hence the comparison failure.

Q2 Maybe you've got a lot of memory? ;-) Seriously it depends on how you're doing it. Use new as above and you should  run out of memory eventually

0
 
sciuriwareCommented:
Q2: tha garbage collector can not collect what is not garbage: what's in the pool is available for the rest
of your program and is not garbage!

;JOOP!
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
dharmesh_amityAuthor Commented:
I think I should not have so much memory for running the program for more have two hours. I have tried to create new object before and system will go out of memory after smetime usally in just a few minutes.

I also think that new is different, but I want to know what exactly is going under the hood.

Thank for your response.
Dharmesh
0
 
dharmesh_amityAuthor Commented:
To sciuriware,

But my program is pointing to only one string at any given time. But I have created many strings at runtime and string pool is not garbage collected. So does this mean if I create new strings at runtime they are not allocated in the string pool?
0
 
CEHJCommented:
>>Use new as above and you should  run out of memory eventually

Actually if the VM is well tuned, it may well be difficult to bring this about even if you try as garbage collection may well keep up with you enough. Setting the thread priority to as high as possible should help though ;-)
0
 
dharmesh_amityAuthor Commented:
I understand that the garbage collector is keeping up. But my question is when I create new strings at runtime where is new string stored are they stored in string pool? and if it is stored in string pool then my system should run out of memory.

0
 
sciuriwareCommented:
The pool can't be garbage collected as those Strings (and other things) must remain available.

But now, why are you so worried? Those few Strings of you will not blow your computer.
SWING for example will need much more memory.

At the moment I run an application that uses 520 Mb and it's certainly not the Strings that make it up.

;JOOP!
0
 
dharmesh_amityAuthor Commented:
I am worried because I want to understand the concepts. My objective is not to blow my computer. But I want to know because if you know when your computer blows, you can write much efficient and less error prone programs.

And I really think that the computer should run out of memory when I am creating so many string.

Dharmesh
0
 
CEHJCommented:
8-)
0
 
OmindraCommented:
class Comp
{
      public static void main(String[] args)
      {
            String s = "Hello";  
            String s2 = "Hello";

            if (s==s2)
            {  
                  System.out.println("Equal without new operator ==");  
            }  
                          if (s.equals(s2))
            {  
                  System.out.println("Equal without new operator equal method");  
            }  
                         
            String t = new String("Hello");  
            String u = new String("Hello");

            if (t==u)
            {  
                  System.out.println("Equal with new operator==");  
            }
                          if (t.equals(u))
            {  
                  System.out.println("Equal with new operator equal method");  
            }
                         
      }
}  
Answer:
      Equal without new operator ==
      Equal without new operator equal method
      Equal with new operator equal method

State:
1. Because of the way Java conserves resources by re-using identical strings that are created without the new             operator s and s2 have the same "address" and the code does output the string
2. The == operator  check object's contents, not reference.
3. The creation of two strings with the same sequence of letters without the use of the new keyword will create          references to the same String in the Java String pool. The String pool is Java's way of conserving resources.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.