• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

the answer to a question!!

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following represent immutable classes?
a) String b) Double
c) StringBuffer d) Math
//can you rectify the answer for me? thank you!!
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a good question! I guess all of them are true since all the classes mentioned (String,Double,StringBuffer,Math) are immutable, (I state this basing on the fact that these classes are declared final).
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which ones do you think are immutable?
We cannot rectify your answers because you have not given us any
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One I can tell you (for leandro) StringBuffer is mutable. You can take a StringBuffer and change its contents. For example, if sb references a StringBuffer, and you do sb.reverse(), then sb refers to the same StringBuffer but the contents are reversed.
 
Erico Doon
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you all for your reply!!
but i am still confused. Maybe i have not understood the meaning of the word "immutible".
it means this class cannot be derived or the content cannot be changed? please help me!
these all class are final!!
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by leandro oliveira:
That's a good question! I guess all of them are true since all the classes mentioned (String,Double,StringBuffer,Math) are immutable, (I state this basing on the fact that these classes are declared final).


Don't get final classes confused with immutable classes. A class being declared as final means that the class can not be extended. However, a class that is said to be immutable is a class which, once you create an instance of it, that instance can not be changed.
In this case, String and Double are immutable, while StringBuffer and Math are not.
I hope that helps,
Corey
 
Erico Doon
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you ,Corry !! the clods have disappeared.
but the answer is Math.
maybe the answer is wrong!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hold on Erico! Corey, java.lang.Math is final and consists only of zillions of static methods. You cannot even construct a Math object. So Math is safely considered to be an immutable class.
[ March 17, 2003: Message edited by: Barry Gaunt ]
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the difference of immutable classes and immutable objects??? I thought immutable classes were classes that could not be extended, and it had something to do with polymorfism... Please clarify!!!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My interpretation of an immutable class is one from which only immutable (unchangable) objects can be constructed. You can't construct any objects from Math so by a logical quirk it can be considered immutable. (See above for other characteristics of the Mathclass.)
Once you have constructed a String (or Double) object, you cannot change what's "inside" it. The methods of these classes only allow you to create new modified versions of the original object.
StringBuffer objects are not immutable because their contents can be changed. If you are given a StringBuffer object, then you can change its contents. You are still holding the same StringBuffer object but its contents may have been reversed or appended to.
[ March 17, 2003: Message edited by: Barry Gaunt ]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the Sun web site:
Bloch defined an immutable class as a programming element whose instances cannot be modified. "All the information contained in each instance is provided when it is created and is fixed for the lifetime of the object," Bloch said.
By this defintion only String and Double are immutable classes. StringBuffer's instances can be modified and Math can't have any instances.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Thomas Paul:
From the Sun web site:
Bloch defined an immutable class as a programming element whose instances cannot be modified. "All the information contained in each instance is provided when it is created and is fixed for the lifetime of the object," Bloch said.
By this defintion only String and Double are immutable classes. StringBuffer's instances can be modified and Math can't have any instances.


This is exactly why I didn't include Math in the immutable class group - the contructor is private - you can never make an instance of it, but I suppose an argument could be made either way depending upon the definition you want to use for immutable. But, as immutability has to deal with the instances of a class and not really the class itself, I would have to exclude Math from the immutable classes list. However, I doubt such an ambiguous case would appear on the exam.
Thanks for the great reply, Tom.
Corey
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll throw Math out the window for this question.
Who's going to throw the author of the question (not Erico) off the roof?
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you think of "immutable" as "read-only" it will clarify a few things in the above discussion.Rattan
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you guys! I understand it clearlly!!!
thx!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic