• Post Reply Bookmark Topic Watch Topic
  • New Topic

wrapper class  RSS feed

 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May i know why Integer is called both immutable and wrapper class? can we create our own wrapper classes?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!