Results 1 to 20 of 20
Thread: Strings and Immutable
- 06-11-2010, 02:28 AM #1
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Strings and Immutable
Okay so I heard that java Strings are Immutable, Constants. fair enough, but that means they cannot change right?
So how come I can do this:
has mystr not being changed?? if I ran a println on mystr i know i would get the meplusonethousand output??? why is this? or do I just not get immutable lolJava Code:String mystr="me"; mystr="meplusonethousand";
- 06-11-2010, 02:31 AM #2
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
You're confusing the object with the variable. Variables, such as mystr, can refer to different objects, but the String object itself is immutable. This is why String methods such as replaceAll don't change the underlying object that calls the method, but rather must return a new String with the changes.
- 06-11-2010, 02:50 AM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
but I thought that Strings were objects, that is what I find so confusing about the whole thing, they are not simple data types in java.
Are Strings ultimately pointers that just change addresses and the methods they invoke behind the scences just accomplish this or have I it all wrong again???
What is the difference between a String, and a StringBuilder I think would clarify this for me.
- 06-11-2010, 03:08 AM #4
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
They are objects, and String variables are reference variables.
Again, you're confusing String objects with the variables. The variables are reference variables which can refer to a String object, can refer to null, and can have the object they refer to changed by the assignment operator. In this they are no different from other reference variables.Are Strings ultimately pointers that just change addresses and the methods they invoke behind the scences just accomplish this or have I it all wrong again???
A StringBuilder is a completely different object that allows you to create Strings, that is mutable.What is the difference between a String, and a StringBuilder I think would clarify this for me.
- 06-11-2010, 03:18 AM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Okay thanks for the information so far curmudgeon, I think I have a grasp what you are saying but one thing still baffles me, is the JPassword field, it comes as a char array, presumably because char arrays don't wander about in memory, but how come if a String can be set to null is it necessary to have a JPassword as a char array???
Is this not the same as changing the contents of the String variable???
e.g String password=new String(whateverCharArray);
password=null;
where is the seciurity risk after this?
- 06-11-2010, 03:21 AM #6
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
I'm not sure of the answer to this. I do know that many String objects are held in the String pool so that they can be re-used, and they may remain in this pool whether or not the variable that references them is set to null, so perhaps this has something to do with this question, but I can't say for sure.
- 06-11-2010, 03:25 AM #7
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Variables, such as mystr, can refer to different objects, but the String object itself is immutable. This is why String methods such as replaceAll don't change the underlying object that calls the method, but rather must return a new String with the changes.
Okay after rereading this explanation I do understand the String class, however I do not obviously understand memory if a String variable is assigned null after being previously a confidential password and a char array which was a JPasswordField becomes Arrays.fill(""); etc. that the JPasswordField Array of chars is fundamentaly safer than a String variable??
- 06-11-2010, 03:30 AM #8
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
If a String variable holds a password and is then set to null, the String object itself still exists until it is collected by the garbage collector, something which may take a long time and which you have little control over. So using a mutable object such as a char array that can be cleared is safer.
- 06-11-2010, 03:37 AM #9
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
the String object still holds its previous assignment of the variable even though the variable has changed its contents???
- 06-11-2010, 03:40 AM #10
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
Yes, absolutely. This is what we've been discussing this whole time -- Strings are immutable. Again, the variable may be made to refer to a different String object or null, but the original String object itself is immutable and does not change until it is destroyed by GC.
- 06-11-2010, 03:41 AM #11
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
I suggest that you sleep on this. I think that it will all be more clear to you if you give your brain time to mull it over. At least that's how my brain works.
Last edited by curmudgeon; 06-11-2010 at 03:43 AM.
- 06-11-2010, 03:44 AM #12
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
- 06-11-2010, 03:47 AM #13
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
You'll have to ask the Java gods this question. I've no idea why things are made the way their made, but so far it seems to work pretty well for me.
You're quite welcome. You seem to be asking the right questions.Thanks for all the replies curmudgeon. I learned a little bit more today which is always my goal :)
- 06-11-2010, 03:59 AM #14
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Just one last question which has been annoying me this whole thread, why are other instances of a class called objects i.e MyClass obj = new MyClass()
and Strings (assuming they are instances which maybe they are not, since you call them variables like an int or a double) are declared like String mystr; which would seem to me reading would be like "mystr is an instance of the class String"
is the implementation just different for Strings?
- 06-11-2010, 07:37 AM #15
No, your understanding is wrong. String is a class like any other. A variable that refers to a String object is a reference variable, just like any variable that refers to any object.
The only special treatment given to String is that the mathematical addition operators (+ and +=) can be used to concatenate Strings.
db
edit And that String literals are automatically interned in the String pool, and other Strings can be specifically interned by invoking intern()
- 06-11-2010, 10:00 AM #16
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Isn't there some web site description somewhere that explains objects and references in terms of cups? That might help, if anyone has it handy.
- 06-11-2010, 10:58 AM #17
- 06-11-2010, 11:15 AM #18
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
That's the one.
- 06-18-2010, 05:00 AM #19
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Thanks for the links, I think I understand it a bit more I'm just not 100% on the differences between how class instances and primitives types are treated by memory.
- 06-18-2010, 07:22 AM #20
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Objects live in a heap, in one of the separate spaces managed by the garbage collector. Primitives can be members of objects so they can also leave in the heap; primitives can also be local variables and are stored on the stack and finally, static final primitives can even be part of the (virtual) machine code.
kind regards,
Jos
ps. this was an entirely different question so it belonged in its own thread.
Similar Threads
-
It is possible in Strings..?
By mlibot in forum New To JavaReplies: 1Last Post: 03-12-2010, 05:30 AM -
What is Immutable in String
By elektronika in forum New To JavaReplies: 4Last Post: 12-10-2009, 12:58 PM -
Passing Strings ?
By aldo1987 in forum New To JavaReplies: 17Last Post: 04-24-2009, 05:03 AM -
What is an Immutable Class
By maheshkanda in forum New To JavaReplies: 3Last Post: 02-06-2009, 08:12 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