What is the difference in usage of primitive and wrapper data type and what is the need of wrapper data type?
Asked Answered
B

3

5

I searched it all over the web, but all the answers just consisted of the difference. I know the difference, but I don't understand the difference in their applications.

For example, suppose we have to take two floating values, if we use double, we can easily compare using a==b, whereas if we use Double, we will have to use a.equals(b).

Backwoods answered 5/4, 2017 at 9:7 Comment(1)
Hopefully the == comparison on floats is just an example :) #1088716Mooney
E
7

You can find it on blog

1. First

Double is a reference type so you can use it as template argument

For Example :

public class Tmp<T> {
    public Tmp() {
    }
}

If you want to create a class like That.

Then you have to pass reference type, while creating object of in. For example

new Tmp<Integer>()

You will get an error if you create object like :

new Tmp<int>()

2. Second

Only because of Wrapper classes it is possible to do generic datatype programming.

For example bellow method accept any kind of number (Byte, Integer, Double, Short, Float, Long, BigDecimal, BigInteger, AtomicInteger, AtomicLong) and return the Integer addition of that numbers.

public Integer add(Number a, Number b){
    return a.intValue() + b.intValue();
}

3. Third

In earlier version of Java is not supporting AutoBoxing and AutoUnboxing. So, if You use that version of Java then you can easily differentiate the both.

For example if you use Java 1.4 or earlier version then:

Integer a = 1; // Auto Boxing(Not Works)
Integer a2 = new Integer(2); // Boxing (It Works)

4. Fourth

The Storage of both also differ Primitive types are stored in Stack while reference types are store in Heap

5. Fifth

You can use functionality of that class like parsing string to Integer, Double, etc and use consents of the same.

Here are the functions and consents of Integer class

enter image description here

6. Sixth

You can serialize Integer while it is not possible with int

7. Seventh

You can pass Integer as a RMI method but the same is not possible with int

Note : Both Integer and int can be part of another object in RMI argument in fact inside the Integer class they store value in int.

8. Eighth

Variable of int is mutable (It is not the case with final int) while Integer is immutable. It will create new object when we change the value.

Eskimo answered 5/4, 2017 at 9:21 Comment(0)
S
4

There is more than that behind the scenes. One of the reasons is how the Collections API is developed in Java...

Consider that you just can not do anything like:

List<int> myList 

in Java. You will need the wrapper for that

List<Integer> myList 

because the Collections work with objects and not primitives.

On the other hand, wrappers are objects that offer the developers a nice group of methods/constants that make "faster and easier" some operations like:

int x = 17;

Now, thanks to the wrapper you can do:

String pars= Integer.toBinaryString(x);
pars= Integer.toHexString(x);
Integer.MAX_VALUE;
Integer.highestOneBit(x);

Without that you will get a pain in the neck, since the primitive x has not much helpful for you to do that.

Schroer answered 5/4, 2017 at 9:9 Comment(1)
On the side note, there are implementations of "Primitive Collections for Java" for instance here: pcj.sourceforge.netErin
G
2

When you use Collections you have to use Objects and not primitives.

List<double> list = new ArrayList<double>; // not allowed
List<Double> list = new ArrayList<Double>; // allowed
Map<double, String> map = new HashMap<double, String>(); // not allowed
Map<Double, String> map = new HashMap<Double, String>(); // allowed

Or if you want that your Double can be null. Useful for example with Hibernate's entities and with DTO or POJO

private int number; // default: 0
private Integer number; // default: null

Double allows you to do quick cast

double.intValue();
double.toString();
Gobbet answered 5/4, 2017 at 9:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.