Well, NO.. The Math is NOT is not IMMUTABLE. String , and all Wrapper classes viz, Boolean, Integer, Float, Double are. Please note StringBuffer is not immutable. Hope this clears the doubt. Ravindra Mohan.
All the constructors in the Math class are private. Therefore you can't have any Math class instance objects. As such, we cannot say Math class is immutable or not. My understanding is that when we say, for example, String is immutable, we're actually saying that the String object is immutable.
Hi Kevin, Actually, the constructors in Wrapper classes are public, not private. However, their fields are <code>final</code> so once a wrapper object is created there is no way to change it's value. The classes are also declared <code>final</code> and may not be extended. Hope that helps. ------------------ Jane Griscti Sun Certified Programmer for the Java� 2 Platform
Dear Friends, As Manfred said Math class is immutable. Generally mutable immutable are talked to fields and objects, because state of these two are can be changed if they are mutable. Methods are either overloaded or overridden. Since Math class is final you cannot instantiate the Math class or Extend it so that you can override or overload the methods defined in Math class. Since these things are immposible with Math class, it is immutable. In Math class method E and PI are final methods and it does't take any parameter also, rest all methods are non-final methods and all of them will take parameters to generate the result and return. Please clarify me if I am wrong. Thanks & Regards V.Srinivasan
Hi everyone!! My thinkin' is different (hope it is correct). "Immutability" has no relationship with the keyword "final". Because immutability means what will be the effect on the actual value of the object. So when we say String class is immutable that means when we use methods of String class e.g -------------------- String s = new String("abc"); s = s.concat("def"); -------------------- So in above, immutability means "if the value returned by s.concat("def") is changed as compare to "abc" then make new object with value "abcdef" and if no change in the value then "s" remain reference to "abc". So I think immutability is clear to u. But in case of Math class this is not the case because they actually change the value of that object or variable and previous value lost, so Math class is not Immutable. tell me if i'm wrong Farhan
Joined: Aug 16, 2000
Hi Farhan, You have cleverly stored the concatenated value in the same variable name s. Just you have overwritten the variable reference to the newly assigned value, that's all (variable s refering to "abc" gets lost and points to new reference "abcdef"). String object created with new operator creates a separate string (no pool business), so it cannot be changed, use as it is or get it garbage collected and create a new one. Actually you cannot change the value of s through any method. In Math class all most all the time we just use the Math class's methods, since there is no field except method parameters. And methods are just to generate the result of the parameters, that's all. Please clarify me if I am wrong. Thanks & regards, V.Srinivasan [This message has been edited by V Srinivasan (edited June 08, 2001).]
Joined: May 14, 2001
Hi Srinivasan!! You r right because use of Math class is just as some functions or work perform on our values. So Math class is Mutable because Variable say this to methods of Math class, "hey! do this to my value" , "hey! do that to my value" (Haa Haa Haa Haa explanation joke). The main purpose of Math class is the format of given value (means to change the value) whether its abs(), round(), sin(), cos() etc. I hope u clear (crystal) Regards Farhan
The WORD immutable does not apply to classes. It applies to objects. A String OBJECT is immutable, not the class String. The Math class has a private constructor. Therefore preventing any crazy people from making any little Math objects. No objects = immutable word is not applicable.
"JavaRanch, where the deer and the Certified play" - David O'Meara
The Math class doesn't store any value therefore it has no value that can be changed. So saying that the Math class is mutable is meaningless. As Cindy said mutable/immutable refers to objects but you can't create a Math object.
I agree with Cindy - You can't instantiate a Math object so immutability is not even relevant. An object is immutable if you can't change its state (you can easily create your own immutable classes*). The final keyword is irrelevant - except if a class has only final variables it would be immutable). One other minor point - someone stated you can't instantiate an object of a final class - that's not true. * I'm calling a class immutable if you can only create immutable objects from it.
Joined: Aug 16, 2000
Thanks Tod, I am clear now, final keyword is not irrelevant in Math class, it prevents from extending it, we cannot subclass Math class. As Cindy said Math class has private constructor, it plays a vital role, we cann't instantiate an object out of Math class at all. Thanks & regards, V.Srinivasan