• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Math class immutable

 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the math class an immutable class?
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Charlie,
Yes as well as all the wrapper classes (Integer, Boolean, etc.)
Regards,
Manfred.
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kevin,
Actually, the constructors in Wrapper classes are coderanch, 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
 
Kevin Yip
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jane,
I agree with you. But I was talking about Math class, though.
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).]
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Only wrapper classes and String are mutable.

Wrapper and String objects are immutable.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic