Why does java have an int and int Integer data type and can I move data from one to another?

Possible duplicate:
Why can Integer and int be used interchangeably?

I am trying to understand the difference between the two. Can I declare something as an int, and then compare this with the number I entered in Integer? Also why Java has two. Why not just combine them?

Can someone help me by showing me an example with line code 3-4, how each one is used?

+5
source share
6 answers

intprimitive is not an object. Arrays allow primitives:

int[] array = new int[10];

but there are no generics:

List<int>  //won't compile

- . Integer, Object. , Integer null, .

, , . , int AnyVal, extend AnyRef ( Any).

+5

. boxing Java. , , Java 9.

+2
  • int Integer - . int Integer. intValue Integer.
  • , . Java , . , , int . Integer.
+2

- , int - . , , . , .

Java . - :

Integer.toString()

, :

int.toString()

.

+2

int , Integer - . ArrayList<int>, ArrayList<Integer>.

+1

All primitive types in Java have their own class analogues (classes derived from Object), such as Boolean, Long, etc. This is called boxing. For an explanation see, for example, here .

+1
source

All Articles