• 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

Is Math class IMMUTABLE?

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have encounter two questions about this.
I want to make sure of it.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
yes, Math class is IMMUTABLE.
Math class is declared final....
hope that helps
cheers
Oliver
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
java.lang.Math class is defined as final and hence it cannot be subclassed and is immutable.
Here is the heirarchy:
java.lang.Object
|
java.lang.Math
public final class Math extends Object.
Note: java.lang.String and all Wrapper classes
(Byte,Integer,Long,Double,Float...) are also immutable.
- Suresh Selvaraj
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suresh,
Immutability has nothing to do with being able to subclass a class. These two are totally different things.
Immutable means the contents cannot be changed once the class is instantiated and initialized with some value. For example, when you create an instance of <code>Double</code> with a value 2.3, you will not be able to make it hold another value, say 5.7. It is like putting something into a box and sealing it. The box remains sealed with the original content. There is no way you can re-open it and put something else inside. That is immutability.
Extensibility or subclassing is a different thing altogether. When a class is declared final, it means you will have to feel satisfied with its current implementation and use it as it is. By no means you will be able to specialize the class by subclassing and add desired behaviour to it. When someone designed the class he felt good about the initial design and thought it may not be necessary for somone to extend it. Hence it was made final
A final class may be mutable and my favourite example is the <code>StringBuffer</code> class. You cannot extend the <code>StringBuffer</code> class but you can change the contents of a <code>StringBuffer</code> instance any number of times after it is created.
Hope that helps,
Ajith
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajith
Thanks for enumerating the difference.
regards
Prasad
 
Ranch Hand
Posts: 71
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice post very useful.

Regards
Jagdev
 
Saloon Keeper
Posts: 15491
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, there is no such thing as an immutable class. A class is a class is a class. You can't change a class.

Objects can be immutable. A class can have mutable or immutable instances. The math class however has no instances. It's a utility class.

From a theoretical point of view, you could consider the Math class an instance of the Class class. In this case Math should be considered mutable, because it has a method which' output changes between invocations: the random() method.
 
Today's lesson is that you can't wear a jetpack AND a cape. I should have read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic