• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Immutable

 
Serena Zhou
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Serena Zhou
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answer. Have another quesion here:

How to copy by value instead of by reference for object?
 
Jon Entwistle
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11887
203
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serena,

You might like to take a look at David O'Meara's April 2003 JavaRanch Journal article, "Mutable and Immutable Objects".
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic