1

I know what a wrapper class is, they wrap primitive types (e.g. int, double, etc) to objects of their respective class.

But I want to know with a Java Code Example which can explain me practically.

With wrapper class and with out wrapper class what it will do.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • But I want to know in Collections, we can have an ArrayList, but not an ArrayList why??? – – user3383175 Mar 24 '15 at 10:52
  • 1
    It's possible to implement an `ArrayList` of primitives, but it's not provided in the standard library - see for example [this question](http://stackoverflow.com/questions/1301588/java-vector-or-arraylist-for-primitives). However, you can't have an `ArrayList` because generic types don't support primitives, as explained [here](http://stackoverflow.com/questions/2721546/why-dont-java-generics-support-primitive-types) – DNA Mar 24 '15 at 10:54

4 Answers4

1

A simple one line answer could be that you can have ArrayList<Integer> but you can not an ArrayList<int>. They are used for collections, polymorphism etc. So I think Java designers wanted to make things simple and thats why they used the concept of wrapper class.

Java is an object-oriented language and as said everything in java is an object. But what about the primitives? They are sort of left out in the world of objects, that is, they cannot participate in the object activities, such as being returned from a method as an object, and being added to a Collection of objects, etc. . As a solution to this problem, Java allows you to include the primitives in the family of objects by using what are called wrapper classes.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • But I want to know in Collections, we can have an ArrayList, but not an ArrayList why??? – – user3383175 Mar 24 '15 at 10:57
  • See http://stackoverflow.com/questions/2721546/why-dont-java-generics-support-primitive-types – DNA Mar 24 '15 at 10:58
  • @user3383175:- The answer to you why is simple, generic types dont support primitive types. As I said in my answer, Java designers wanted to make things simple. Check the Oracle docs:- https://docs.oracle.com/javase/tutorial/java/generics/restrictions.html#instantiate – Rahul Tripathi Mar 24 '15 at 10:59
1

When the Java language was "invented" people thought that having primitive types int, long, ... would avoid performance issues. 15+ years back, there were no efficient JIT compilers; so it made a huge difference if you had to create an array for 10000 ints ... or for 10 000 Integer objects.

On the other hand, Java wants to be a object-oriented language. Therefore those primitive types came with accompanying classes. You are correct in the sense: if there would be no primitive tpyes, just Integer, Long, ... and so on, the Java type system would be more consistent, much simpler ... but back in 1997 this simple type system would have been to "expensive" regarding performance. And unfortunately people didn't think about allowing primitive types in bytecode ... but not having them in the Java language itself (and the compiler converting Integer to int internally).

The main usage nowadays is the fact that the Java compiler does autoboxing (automated conversion between primitive and "object based" types); so you can write stuff like:

Map<Integer, String> someMap = ...
someMap.put(5, "string")
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • But I want to know in Collections, we can have an ArrayList, but not an ArrayList why??? – user3383175 Mar 24 '15 at 10:50
  • 1
    That is because of the nature of java generics. Java generics only work for reference tpyes. See https://docs.oracle.com/javase/tutorial/java/generics/restrictions.html#instantiate – GhostCat Mar 24 '15 at 10:56
1

Wrapper classes are used to convert any data type into an object. The primitive data types are not objects; they do not belong to any class; they are defined in the language itself. Sometimes, it is required to convert data types into objects in Java language

What is a Wrapper class?

A wrapper class wraps or encloses a data type and gives it an appearance of an object. You can also get the primitive datatype from the object.

Observe the following example.

 int x = 100;
    Integer iObj = new Integer(x); 

The int data type (x) is converted into an object (iObj) with the help of an Integer class. This can be used whever an object is required.

The following code can be used to unwrap the object iObj and obtain the primitive datatype.

int y = iObj.intValue();
System.out.println(y); // prints 100 

intValue() is a method of Integer class that returns an int data type.

Why Wrapper classes?

To convert primitive data types into objects and vice versa.

Manish Kothari
  • 1,702
  • 1
  • 24
  • 34
  • But I want to know in Collections, we can have an ArrayList, but not an ArrayList why??? – – user3383175 Mar 24 '15 at 10:58
  • for why collections cannot store primitive data types and only objects you can refer to one of the posts i came across: [Why can Java Collections not directly store Primitives types?](http://stackoverflow.com/questions/2504959/why-can-java-collections-not-directly-store-primitives-types) – Manish Kothari Mar 24 '15 at 11:08
0

Wrapper types exists in Java primarily for two reasons:

  • to allow type conversion in an object-oriented fashion
  • all Collection types used generic java Objects, so if you need a List of, say floating point numbers, you can't use simple float numbers, because they are not objects. Also, they don't inherit from java.lang.Object

So you can have an ArrayList<Integer> because Integer extends from Object (as every other object inside the JVM). But you can't have an ArrayList because int is not an object type: it's just a simple int variable, without methods, instance variables, etc.

Since Java 1.5 you have automatic boxing and unboxing of primitive types into their correspondent wrapper types, which makes everything a little bit more confusing. Consider this listing:

List list = new ArrayList<Integer>()

list.add(new Integer(10))
list.add(20)

Here you see that this list only uses Integer objects. In the last line the compiler is putting the simple value 20 inside a new Integer object to save you some typing. This is a little syntax sugar but the idea remains: list can only use Integer objects.

Diego Freniche
  • 5,225
  • 3
  • 32
  • 45
  • There is a little whole in your explanation: primitive types and wrappers where part of Java from day 1. Collections were added years later. Thus the wrappers were NOT invented to enable Collections. – GhostCat Mar 24 '15 at 11:02
  • You're right, Integer (and all other wrapper classes) where added in 1.0. Collections where added in 1.2. Since 1.2 was released at the end of 1998 [reference](http://en.wikipedia.org/wiki/Java_version_history), it is safe to explain that way to a beginner what purpose the wrapper classes serve – Diego Freniche Mar 24 '15 at 11:11