File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is Math class IMMUTABLE? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is Math class IMMUTABLE?" Watch "Is Math class IMMUTABLE?" New topic

Is Math class IMMUTABLE?

Bin Zhao
Ranch Hand

Joined: Oct 04, 2000
Posts: 73
I have encounter two questions about this.
I want to make sure of it.
Oliver Grass
Ranch Hand

Joined: Nov 02, 2000
Posts: 65
yes, Math class is IMMUTABLE.
Math class is declared final....
hope that helps
Suresh Selvaraj
Ranch Hand

Joined: Nov 14, 2000
Posts: 104
java.lang.Math class is defined as final and hence it cannot be subclassed and is immutable.
Here is the heirarchy:
public final class Math extends Object.
Note: java.lang.String and all Wrapper classes
(Byte,Integer,Long,Double,Float...) are also immutable.
- Suresh Selvaraj

Suresh Selvaraj, (author of JQuiz)<br />SCJP2<br /><a href="" target="_blank" rel="nofollow"></a>
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
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,

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Prasad Ballari
Ranch Hand

Joined: Sep 23, 2000
Posts: 149
Thanks for enumerating the difference.
Jagdev Singh
Ranch Hand

Joined: Jan 12, 2010
Posts: 71

Nice post very useful.

Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4635

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.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
I agree. Here's the link:
subject: Is Math class IMMUTABLE?
It's not a secret anymore!