• Post Reply Bookmark Topic Watch Topic
  • New Topic

HOW WRAPPER CLASSES ADD FUNCTIONALITY  RSS feed

 
mara thamizhan
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is sai who posted that doubt!
I came to know that using wrapper classes one can change a primitive value into a object!

The reason behind this conversion is that it will add functionality like we can store those primitive objects in collection classes like vectors,list,sets etc.

What i want is that can u plz explain with the help of a javaprg how storing primitive objects in collections classes enhance functionality.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose that somewhere in your program you need to store a list of numbers. You decide to use ArrayList to store the numbers.

Now there's a problem: ArrayList can only store objects, but primitives are not objects. So you cannot store ints directly in an ArrayList.

The solution is: You wrap each int in a java.lang.Integer object, and you store those Integer objects in the ArrayList. So the wrapper class Integer makes it possible to store a list of numbers.
 
Mike Noel
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why would you want an ArrayList instead of int[]? Because ArrayLists are resizable while arrays (int[]) are not.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!