Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does java have an int and int Integer datatype and can I move data from one to another? [duplicate]

Tags:

java

Possible Duplicate:
Why can Integer and int be used interchangably?

I am trying to understand the difference between these. Can I declare something to be an int for example and then compare this with a number that I put in an Integer? Also why does Java have the two. Why not just combine these?

Can someone help me by showing me a 3-4 line code example of how each is used?

like image 733
Alan2 Avatar asked Jun 16 '12 16:06

Alan2


People also ask

Is int and Integer same in Java?

In Java, int is a primitive data type while Integer is a Wrapper class. int, being a primitive data type has got less flexibility. We can only store the binary value of an integer in it. Since Integer is a wrapper class for int data type, it gives us more flexibility in storing, converting and manipulating an int data.

What's the difference between int and integer?

The major difference between an Integer and an int is that Integer is a wrapper class whereas int is a primitive data type. An int is a data type that stores 32-bit signed two's complement integer whereas an Integer is a class that wraps a primitive type int in an object.

Does int inherit from object Java?

From "Primitive Data Types": "Primitive types are special data types built into the language; they are not objects created from a class." That, in turn, means that no, int doesn't inherit from java.

Which is faster int or integer in Java?

Use int when possible, and use Integer when needed. Since int is a primitive, it will be faster.


Video Answer


3 Answers

int primitive is not an object. Arrays allow primiteves:

int[] array = new int[10];

but generics don't:

List<int>  //won't compile

This is the primary reason to use wrapper classes these days. Also you can use Integer where Object is expected. Finally Integer can have null value if you want to implement optionality.

Note that there are some languages that dealt with that inconsistency. In c# you have value types, in scala Int class extend from AnyVal class while normal objects extend AnyRef (both of these extend from Any).

like image 102
Tomasz Nurkiewicz Avatar answered Nov 15 '22 00:11

Tomasz Nurkiewicz


See boxing of types in Java. There is talk of making them exactly the same starting with Java 9.

like image 36
pjulien Avatar answered Nov 14 '22 23:11

pjulien


  • Primitive int type and Integer class are different types. You can't compare directly a primitive int with a Integer object. You need to get intValue from the Integer object.
  • Yes, this is a required feature. Java and some other languages has these kinds of object wrappers for primitive types to handle the situations where an object is required. For example, a collection class will expect objects, you can't use primitive int with them. So you will need a Integer wrapper.
like image 27
taskinoor Avatar answered Nov 15 '22 00:11

taskinoor