Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-17-2007, 07:14 AM
Member
 
Join Date: May 2007
Location: Mumbai, India
Posts: 5
anjanesh is on a distinguished road
Strings are immutable yet they can be changed ?
Hi

Trying to get the hang of Java.

In the String doc, its mentioned :
Quote:
Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings.
But in that case how come this works ?

PHP Code:
String s "abcde"System.out.println(s);
"12345"System.out.println(s); 
Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-19-2007, 02:49 PM
Member
 
Join Date: May 2007
Posts: 5
fritz is on a distinguished road
because you are not changing the String, you are changing what the 's' reference points to.

in other words, 's' is NOT a String, it is a REFERENCE TO a String. 's' lives on the stack, and the String object containing "abcde" lives in the heap (let's not get into the String pool right now). when you run the line

s= "12345", a new String is created in the heap. 's' is changed to point to it, but the String "abcde" is still hanging around, unchanged.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-19-2007, 03:00 PM
Member
 
Join Date: May 2007
Location: Mumbai, India
Posts: 5
anjanesh is on a distinguished road
So when s references to 12345, does the string obj containing abcde get deleted from the heap ?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-19-2007, 03:41 PM
Member
 
Join Date: May 2007
Posts: 5
fritz is on a distinguished road
no.

String literals are a special case. whenever you have a double quoted string, it gets created in the string pool. these are not eligible for normal garbage collection.

the details are a little more than you need to know right now, but basically:

Strings are never changed. new strings can be created, and references re-assigned to them.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-19-2007, 04:08 PM
Member
 
Join Date: May 2007
Location: Mumbai, India
Posts: 5
anjanesh is on a distinguished road
So is this a bad way to go about ?
PHP Code:
String s "abcde"
"12345"
.
.
.
"somethingelse"
Is there an more efficient way (in terms of memory) to code this ?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
characters + strings Gilgamesh New To Java 3 03-02-2008 10:10 PM
Comparison of Strings Cero.Uno New To Java 3 02-11-2008 03:46 AM
Help with drawing strings! JavaInLove AWT / Swing 1 02-05-2008 04:39 AM
Why the panel text changed? ottawalyli AWT / Swing 1 12-17-2007 06:56 AM
Why the panel text changed? ottawalyli SWT / JFace 0 12-16-2007 05:16 PM


All times are GMT +3. The time now is 04:16 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org