my dog learned polymorphism*
The moose likes Java in General and the fly likes String Immutable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "String Immutable" Watch "String Immutable" New topic
Author

String Immutable

Serena Zhou
Ranch Hand

Joined: Dec 13, 2003
Posts: 31
I know that "String is immutable". But how was that implemented? How if I want to have some class immutable like String?


Thanks!
Jon Entwistle
Ranch Hand

Joined: Feb 20, 2003
Posts: 118
Hi Serena,

The String class is final and so cannot be subclassed. Add this to the fact that no methods are provided to change the state of the String make it immutable.

I don't know about the windows JDK, but the linux distribution comes with the source files for all classes in a zip file if you are ever interested in seeing 'how they did it'

Regards,

Jon


SCJD, SCEA
Serena Zhou
Ranch Hand

Joined: Dec 13, 2003
Posts: 31
Thanks for your answer. Have another quesion here:

How to copy by value instead of by reference for object?
Jon Entwistle
Ranch Hand

Joined: Feb 20, 2003
Posts: 118
Hi Serena,

The best place to ask these types of questions is the java intermediate forum, as this forum is intended for questions related to the developer exam.

I am also not too sure what you mean by copy by value - if you mean pass by value to a method, you can only pass a reference to an object as a method argument in java (or a primitive).

If you mean how do you make a deep copy of an object (i.e. not just its reference) you should use clone().

Regards,

Jon
[ July 19, 2004: Message edited by: Jon Entwistle ]
Serena Zhou
Ranch Hand

Joined: Dec 13, 2003
Posts: 31
Thanks!
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11460
    
  94

I have moved this thread to the Java in General (Intermediate) forum.

Thanks Jon for your answers and for pointing Serena to better choices in forums.

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Serena,

You might like to take a look at David O'Meara's April 2003 JavaRanch Journal article, "Mutable and Immutable Objects".


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Java is purely pass by value.
Java does not give you access to objects, only object references.
http://www.xdweb.net/~dibblego/java/faq/answers.html#q21

A class being declared final does not have any effect on whether or not it is immutable - simply that no API is exposed that allows its state to be altered.
http://www.xdweb.net/~dibblego/java/faq/answers.html#q20

There is always a hack that allows access to private data (given that the SecurityManager permits it).
http://www.xdweb.net/~dibblego/java/trivia/answers.html#q1
Sure it's not nice and unjustifiable, but I've seen it done.


Tony Morris
Java Q&A (FAQ, Trivia)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Immutable