• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

immutable classes

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi..
which of the foll classes are immutable and why?
A. Math
B. String
C. StringBuffer
D. Boolean
E. Object
the answers given are String and Boolean.
I know that a String object is immutable whereas a StringBuffer object is not.
Does it mean that a Boolean object, once created, is immutable too?
thanks for the help.
Ramani.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ramani,
Boolean is a wrapper class. All wrapper classes are immutable. So, Integer, Float, Double, Character, etc. are also immutable.
Hope this helps.
But I have a doubt. Isn't Math an immutable class? (Since you can't instantiate it and also since all its fields are static and final.)
[This message has been edited by srikrish (edited September 14, 2000).]
 
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Math class is defined as
public final class Math extends Object
Also it does not have a constructor.
How does the issue of immutability arize when an Object of Math class cannot be instansiated.
so Math class is not one of the answers
------------------
Regds.
Rahul P. Mahindrakar
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My answers would be a, b, c, d
Btw , Rahul, Math class does have a constructor but it is private. You can very this by using the javap utility.
Please let me know if there is anything wrong my answers.
-sampaths77
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Math class cannot be instantiated because it has a private constructor( and not because it is declared as final! ).
Immutability is usually defined with regard to the contents of the object ie., the instance data. As srikrish has noted, since all the fields in the Math class are static and final you cannot change them!. Hence Math is not an immutable class.
Ajith
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks for the clarification Ajith! I misunderstood the concept of immutability
So, the answers given are right - Only String and Boolean are immutable.
Thanks again Ajith
-sampaths77

 
Rahul Mahindrakar
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sampaths77,
Yes indeed the Math class has a private constructor. Actually when i made my reply i was thinking of why a class that has all static method's would require a constructor.
Now i realize that if no constructor is provided by the Math class then the compiler will provide one which has default access. This can be checked by creating a class with no constructor and decompiling it. This would create objects of the Math class which the designer of the Math class did not want anyone to undertake. So a private constructor has been provided.

reply
    Bookmark Topic Watch Topic
  • New Topic