Java Questions Java Questions - 6 months ago 18
Java Question

Difference between int and Integer

What is the difference between

int
and
Integer
. Yes, one is
primitive
and another one is
wrapper
, what is the situation to use them correctly.

Also what is the difference between :

int i=0;
++i
and
i++

Answer

part 1

One example .. you can use Integer as the key of HashMap but you can't use int. Because an Object is needed.

So where you need an int value as an object there you need to use Integer class.

part 2

++i is pre increment i++ is post increment

for example

i = 0;
System.out.println(i++) //will print 0 then the i will be 1.

and

i = 0;
System.out.println(++i) // here i wil be incremented first then print 1.