This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Math class immutable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Math class immutable" Watch "Math class immutable" New topic
Author

Math class immutable

Charlie Swanson
Ranch Hand

Joined: Jan 29, 2001
Posts: 213
If the math class an immutable class?
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Charlie,
Yes as well as all the wrapper classes (Integer, Boolean, etc.)
Regards,
Manfred.
Ravindra Mohan
Ranch Hand

Joined: Mar 16, 2001
Posts: 216
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.
Kevin Yip
Ranch Hand

Joined: Oct 17, 2000
Posts: 110
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.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Kevin Yip
Ranch Hand

Joined: Oct 17, 2000
Posts: 110
Hi Jane,
I agree with you. But I was talking about Math class, though.
V Srinivasan
Ranch Hand

Joined: Aug 16, 2000
Posts: 99
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
Farhan Tariq
Ranch Hand

Joined: May 14, 2001
Posts: 54
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
V Srinivasan
Ranch Hand

Joined: Aug 16, 2000
Posts: 99
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).]
Farhan Tariq
Ranch Hand

Joined: May 14, 2001
Posts: 54
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
Jyotsna Umesh
Ranch Hand

Joined: May 09, 2001
Posts: 94
Originally posted by Charlie Swanson:
If the math class an immutable class?

Hi everyone!
I think Math class is not immutable and is a final class. Only wrapper classes and String are mutable. If I am wrong please correct me.
Jyotsna

[This message has been edited by Jyotsna Umesh (edited June 08, 2001).]
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Only wrapper classes and String are mutable.
Wrapper and String objects are immutable.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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.
Marcellus Tryk
Ranch Hand

Joined: May 31, 2001
Posts: 64
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.

V Srinivasan
Ranch Hand

Joined: Aug 16, 2000
Posts: 99
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
 
 
subject: Math class immutable
 
Similar Threads
Is Math class IMMUTABLE?
Math
Tript/traps for SCJP
Is the Math class considered immutable?
immutable objects vs classes