• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

String Immutable

 
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!
 
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!
 
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • 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
 
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".
 
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.
 
So you made a portal in time and started grabbing people. This tiny ad thinks that's rude:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic