aspose file tools*
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


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is Math class IMMUTABLE?" Watch "Is Math class IMMUTABLE?" New topic
Author

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
Hi,
yes, Math class is IMMUTABLE.
Math class is declared final....
hope that helps
cheers
Oliver
Suresh Selvaraj
Ranch Hand

Joined: Nov 14, 2000
Posts: 104
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

Suresh Selvaraj, (author of JQuiz)<br />SCJP2<br /><a href="http://www.decontconsulting.com" target="_blank" rel="nofollow">www.decontconsulting.com</a>
Ajith Kallambella
Sheriff

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


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
Ajith
Thanks for enumerating the difference.
regards
Prasad
Jagdev Singh
Ranch Hand

Joined: Jan 12, 2010
Posts: 71

Nice post very useful.

Regards
Jagdev
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is Math class IMMUTABLE?