473,804 Members | 2,246 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Confusion in String.intern()

sumittyagi
202 Recognized Expert New Member
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 1689
sumittyagi
202 Recognized Expert New Member
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 Recognized Expert MVP
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 Recognized Expert New Member
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 Recognized Expert MVP
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
1648
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
1832
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 = "blah'; myStr = 'e';
5
1712
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 and x64, and have published the results here: http://www.coversant.com/dotnetnuke/Default.aspx?tabid=88&EntryID=24 The benefits for our SoapBox suite of products are pretty compelling, memory wise. Before I roll the changes into our...
26
2802
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
9714
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9594
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10351
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10096
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9174
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7638
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
3834
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.