• 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

wrapper class

 
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May i know why Integer is called both immutable and wrapper class? can we create our own wrapper classes?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is immutable because once an Integer object is created, its value can never change. There are no methods in class Integer to change the value that the Integer object contains. This has certain advantages, one of them being that it's easier to use in multi-threaded applications: you don't have to worry about multiple threads trying to change the value of an Integer object at the same time.

It's called a wrapper class because it's a very simple class that does not much more than store an int value. The word "wrapper class" really doesn't have any special meaning.

Ofcourse you can create your own wrapper classes. For example, you could write a class that is a lot like the java.lang.Integer class:

Note, however, that special compiler magic such as autoboxing is not going to work with your own class like it does with Integer.

 
reply
    Bookmark Topic Watch Topic
  • New Topic