Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 11th, 2012, 06:11 PM
Iinweed Iinweed is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 1 Iinweed User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 49 m 3 sec
Reputation Power: 0
Question Can someone help me understand strings please?

Everything I read about strings tells me that they are immutable - i.e. once created they cannot be changed.

That being the case, can someone please explain in idiot terms what is going on here?

To the uninitiated (me!) it looks like str1 is being changed. Obviously I am not understanding something basic here, but as I have just started programming I have no idea what.

Any help would be appreciated!


Code:

public class Test {

	public Test() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String str1 = "I can't be changed once created!";
		System.out.println(str1);
		str1 = "Now I have been changed";
		System.out.println(str1);

	}

}

Reply With Quote
  #2  
Old December 12th, 2012, 03:01 AM
Aurum84 Aurum84 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 74 Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 23 h 5 m 49 sec
Reputation Power: 17
Your code does not compile, yet, because you are missing a return type in the Test() method. (which isn't used in this example). When I run the program I get:

Code:
I can't be changed once created
Now I have been changed


What I have understood from the immutability (spelling?) of a Java String object, is that the String itself will not change, but the reference to its value will. In contrast to the use of the reserved word 'final' which disallows an object to mutate its reference to its value.

Look at the text on wikipedia:
*** begin quote from http://en.wikipedia.org/wiki/Immutable_object ***
A classic example of an immutable object is an instance of the Java String class.
Code:
String s = "ABC";
s.toLowerCase();

The method toLowerCase() will not change the data "ABC" that s contains. Instead, a new String object is instantiated and given the data "abc" during its construction. A reference to this String object is returned by the toLowerCase() method. To make the String s contain the data "abc", a different approach is needed.
Code:
s = s.toLowerCase();


*** End Quote ***
Comments on this post
stdunbar agrees!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Can someone help me understand strings please?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap