• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Ajith's trips/traps in the FAQ

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
1.It has been given that Math class being an option for immutable classes is a trap. I know Math class cannot be extended nor can it be instantiated. What do you mean by immutable classes?
2. instanceOf is different from instanceof
instanceof is an operator
Is there any mehod by name instanceOf or is it just a caution to be aware of the case?
------------------
Regards,
Shree
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) an immutable class is one that cannot be changed. A String is an example of this. you cannot actually change a String object once it is created. when you use a method of String class like concat() it actually returns a new String. Same with s=s+"a"; you create a new String and tell s to point at the new String instead of the old one. A StringBuffer on the other hand is not immutable. A StringBuffer object can be modified. Since Math class cannot be instantiated there can be no object to modify.
2)if you write:
if(s instanceOf String)
you will get a compile error of some sort
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.It has been given that Math class being an option for immutable classes is a trap. I know Math class cannot be extended nor can it be instantiated. What do you mean by immutable classes?
I think it is important to differentiate between immutable classes and immutable objects. Math is a final class so it cannot be extended and behavior of any of it's functions cannot be overriden. String objects are immutable (String class is also final). The reason Math cannot be instantiated is because it does not provide any public constructors.
2. instanceOf is different from instanceof
instanceof is a keyword and instanceOf is not. In java no keyword contains a capital letter.
Hope that was of help.
Regards,
Abhijit
 
shree vijay
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, both of you.
I wanted to be sure about the distinction between immutable objects and final classes and Abhijit clarified it
------------------
Regards,
Shree
 
You can't expect to wield supreme executive power just because
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic