473,387 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Confusion in String.intern()

sumittyagi
202 Expert 100+
Hi All!

I am confused in the functioning of String.intern() function.
Expand|Select|Wrap|Line Numbers
  1. public class StringTest 
  2. {
  3.     public static void main(String[] args) 
  4.     {
  5.         String test2 = new String("ABC");
  6.         test2.intern();
  7.         String test1 = "ABC";
  8.         System.out.println("test1 == test2 is: " + (test1==test2));
  9.         System.out.println("test1 == test2 is: " + (test1==test2));
  10.     }
  11. }
  12.  
The output is:
test1 == test2 is: false
test1 == test2 is: false

Now the definition of String.intern() function says:
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

There is no String "ABC" in the pool when intern is called, so the same object is added to the pool; i.e. after call to intern, object referenced by test2 is in String pool. Now when test1 is created, then "ABC" exists in string pool so the same is returned from the pool and no new object is created.

So that means test1 and test2 references to the same object, so I am not able to understand why this strange result is comming.

Please guide me in this context.
Thanks in advance for any help.
Jul 25 '07 #1
4 1673
sumittyagi
202 Expert 100+
Also if I change my program like this:
Expand|Select|Wrap|Line Numbers
  1. public class StringTest 
  2. {
  3.     public static void main(String[] args) 
  4.     {
  5.         String test2 = new String("ABC");
  6.         test2 = test2.intern();
  7.         String test1 = "ABC";
  8.         System.out.println("test1 == test2 is: " + (test1==test2));
  9.         System.out.println("test1 == test2 is: " + (test1==test2));
  10.     }
  11. }
  12.  
Then result is fine:
test1 == test2 is: true
test1 == test2 is: true


But test2.intern() will be returning the same object as referenced by test2 as there is no String "ABC" in the pool, so the same object is added to the pool and returned. Then I think there is no difference in both cases.
Jul 25 '07 #2
JosAH
11,448 Expert 8TB
When you do this:

Expand|Select|Wrap|Line Numbers
  1. String test2= new String("ABC");
  2.  
There will be *two* Strings:

1) The literal String "ABC" which was in the pool already when the class was loaded;
2) A new String with the content "ABC" (not interned).

When you intern your test2 String, you should forget about that other String.
You didn't do so in your first example, you forgot the interned String instead.

In your current example test1 and test2 will both refer to the same (interned) String.

kind regards,

Jos
Jul 25 '07 #3
sumittyagi
202 Expert 100+
When you do this:

There will be *two* Strings:

1) The literal String "ABC" which was in the pool already when the class was loaded;
Ok!!!

That means whenever we create a new string with new keyword, then there are actually two objects created!
one for the parameter passed(which is a string object that gets added to the string pool) to the constructor and one for the new keyword, which is forcefully created.

That's great!
Thanks a lot Jos!
Jul 25 '07 #4
JosAH
11,448 Expert 8TB
Ok!!!

That means whenever we create a new string with new keyword, then there are actually two objects created!
one for the parameter passed(which is a string object that gets added to the string pool) to the constructor and one for the new keyword, which is forcefully created.

That's great!
Thanks a lot Jos!
You're welcome of course. So basically this is silly:

Expand|Select|Wrap|Line Numbers
  1. String foo= new String("bar");
  2.  
... because the following would've done the same using one less object:

Expand|Select|Wrap|Line Numbers
  1. String foo= "bar";
  2.  
kind regards,

Jos
Jul 25 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Daniel | last post by:
how to make two references to one string that stay refered to the same string reguardless of the changing value in the string?
11
by: Zeng | last post by:
Hello, Is it true that in C# we can't modify directly a character in a string? I can't find the method anywhere. I would expect something like this should work,but it doesn't string myStr =...
5
by: Chris Mullins | last post by:
I've spent some time recently looking into optimizing some memory usage in our products. Much of this was doing through the use of string Interning. I spent the time and checked numbers in both x86...
26
by: anonieko | last post by:
In the past I always used "" everywhere for empty string in my code without a problem. Now, do you think I should use String.Empty instead of "" (at all times) ? Let me know your thoughts.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.