Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the main difference between primitive type and wrapper class?

What is the difference between these two lines?

    int pInt = 500;

and

    Integer wInt = new Integer(pInt);

Or

    Integer wInt = new Integer(500);
like image 716
Bernard Avatar asked Nov 12 '12 07:11

Bernard


People also ask

What is the difference between primitive and wrapper class?

Wrapper class creates an object and primitive does not create object. Wrapper classes are used with Collections to represent type. Wrappers have methods and can hold memory address/null and primitives hold default values. Primitives are fast compare to wrapper classes as there is no overhead of methods or object.

What is the difference between primitive types and class types?

int , float , double , long , short , boolean and char are examples of primitive data types. You can't invoke methods on these data types and they don't have a high memory footprint, which is their striking difference from classes. Everything else is a class (or class-like in the case of interfaces and enums).

Which is better primitive or wrapper class in Java?

Verdict. Generally, choose primitive types over wrapper classes unless using a wrapper class is necessary. Primitive Types will never be slower than Wrapper Objects, however Wrapper Objects have the advantage of being able to be null.

What is the difference between a primitive type and an object type?

Primitive values are immutable — they cannot be changed after being created. Object references, however, are mutable and can be changed. Since objects are stored as references, special care must be taken when duplicating objects and when performing equality checks on objects.


2 Answers

None.

That's the exact same thing. In the first case you just have a supplementary variable.

Note that with autoboxing you rarely need to have both an int and an Integer variables. So for most cases this would be enough :

int pInt = 500;

The main case where the Integer would be useful is to distinguish the case where the variable is not known (ie null) :

Integer i = null; // possible
int i = null; // not possible because only Object variables can be null

But don't keep two variables, one is enough.

like image 170
Denys Séguret Avatar answered Jan 04 '23 13:01

Denys Séguret


In Java, an instance of a primitve class holds the actual value of the instance, but instance of a wrapper class holds a reference to the object. i.e. The address of the place where the object would be found.

When you write a program with this line:

Integer integer = 500;

The compiler changes it to this:

Integer integer = new Integer(500);

This process is called autoboxing. That is automatically putting a primitive-instance in a "box" of Integer. Hence, output of the following program:

public class PrimitiveToObject {
    public static void main(String[] args) {
        printClassName(1);
        printClassName(1L);
        printClassName((char)1);
    }
    public static void printClassName(Object object){
        System.out.println(object.getClass());
    }
}

is this:

class java.lang.Integer
class java.lang.Long
class java.lang.Character

Also this:

int i = integer;

changes into this:

int i = integer.intValue();

This is called unboxing.

As you can see above, the dot operator(.) is used on the variable named integer but not on i. That is: a wrapper's object can be dereferenced, but not a primitive instance.

Boxing and unboxing may slow down the program a little bit. Hence, to a newbie, wrappers may look like added burden, but they are not so. Wrappers are used at places where the object needs to be a reference type. eg: Map<Integer,String>map=new HashMap<Integer,String>(); is a valid statement, but Map<int,String>map=new HashMap<int,String>(); is not a valid statement.

Another typical case where wrapper is very useful:
In MySQL, NULL is a valid entry for a column of INT type. But in Java, int cannot have a null value, Integer can. This is because in SQL NULL symbolises Not Available. So if you are using JDBC to insert integer values in a MySQL table, a null in your java program will help in inserting NULL in the MySQL table.

A wrapper class can also be useful in a case similar or anologous to this:

Boolean decision; // Using wrapper for boolean.
if("YES".equalsIgnoreCase(consent))
    decision = Boolean.TRUE; // In favour
else if("NO".equalsIgnoreCase(consent))
    decision = Boolean.FALSE; // Not in favour
else if("CAN'T SAY".equalsIgnoreCase(consent))
    decision = null; // Undecided
like image 41
Abhishek Oza Avatar answered Jan 04 '23 12:01

Abhishek Oza