Results 1 to 5 of 5
Thread: What is Immutable in String
- 12-10-2009, 12:09 PM #1
What is Immutable in String
Dear All,
I learn in the Sun java tutorial, there was something like this :
What does it mean by immutable? The contents of the string can easily be changed.Note: The String class is immutable, so that once it is created a String object cannot be changed. The String class has a number of methods, some of which will be discussed below, that appear to modify strings. Since strings are immutable, what these methods really do is create and return a new string that contains the result of the operation.
Java Code:String newstrings; newstrings = " What do you mean"; System.out.println(newstrings); newstrings = " With that?"; System.out.println(newstrings);
Kindest Regards,Java Code:What do you mean With that?
Elektronika
- 12-10-2009, 12:14 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
No, that is not changing the contents of the String. That is changing the Object that the reference variable points to. A String is immutable in that you cannot change the contents of a String object, it has nothing to do with changing the value of a reference.
- 12-10-2009, 12:27 PM #3
Dear masijade,
So what does the
means?so that once it is created a String object cannot be changed
so before it is created it can be changed? and what does the changed here means...
Kindest Regards,
Elektronika
- 12-10-2009, 12:50 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
A String is an object that "wraps" a character array. What is "immutable" about a String is that you cannot change those contents. A variable is not a String. A variable is simply a placeholder in which a pointer to a String can be saved. Changing that pointer does not change the String, it simply changes which String the variable now points to. And before it is created there is nothing to change, of course.
See http://www.javaranch.com/campfire/StoryCups.jsp and http://www.javaranch.com/campfire/StoryPassBy.jsp
- 12-10-2009, 12:58 PM #5
Similar Threads
-
combine string[] into string like perl's join function
By tekberg in forum Advanced JavaReplies: 9Last Post: 02-23-2009, 01:05 PM -
What is an Immutable Class
By maheshkanda in forum New To JavaReplies: 3Last Post: 02-06-2009, 08:12 PM -
Let eclipse warn about a semicolon after an if statement and string == string?
By foobar.fighter in forum EclipseReplies: 5Last Post: 01-11-2009, 10:12 AM -
Using java.util.Scanner to search for a String in a String
By Java Tip in forum Java TipReplies: 0Last Post: 11-20-2007, 04:59 PM -
Strings are immutable yet they can be changed ?
By anjanesh in forum New To JavaReplies: 4Last Post: 05-19-2007, 03:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks