35

What is the difference between them?

l is an arraylist of Integer type.

version 1:

int[] a = new int[l.size()];
for (int i = 0; i < l.size(); i++) {
    a[i] = l.get(i);
}
return a;

version 2:

int[] a = new int[l.size()];
for (int i = 0; i < l.size(); i++) {
    a[i] = l.get(i).intValue();
}
return a;
Timeless
  • 7,338
  • 9
  • 60
  • 94

8 Answers8

52

l.get(i); will return Integer and then calling intValue(); on it will return the integer as type int.

Converting an int to Integer is called boxing.
Converting an Integer to int is called unboxing
And so on for conversion between other primitive types and their corresponding Wrapper classes.

Since java 5, it will automatically do the required conversions for you(autoboxing), so there is no difference in your examples if you are working with Java 5 or later. The only thing you have to look after is if an Integer is null, and you directly assign it to int then it will throw NullPointerException.

Prior to java 5, the programmer himself had to do boxing/unboxing.

  • 2
    Prior to Java 5, the programmer would also need to cast to Integer before calling intValue. – Thilo Jul 15 '12 at 09:04
9

As you noticed, intValue is not of much use when you already know you have an Integer. However, this method is not declared in Integer, but in the general Number class. In a situation where all you know is that you have some Number, you'll realize the utility of that method.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
5

The Object returned by l.get(i) is an instance of the Integer class.

intValue() is a instance method of the Integer class that returns a primitive int.

See Java reference doc... http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#intValue()

Afshin Moazami
  • 2,092
  • 5
  • 33
  • 55
  • plus: in version 1 the JVM applies autoboxing to convert the `Integer` instance into a primitive type – hage Jul 15 '12 at 08:17
  • what is the potential dangerous if i'm not calling .intValue() in my case. – Timeless Jul 15 '12 at 08:18
  • As mentioned by hage, Autoboxing should do the necessary for you not to worry. Read this article about it, http://docs.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html – Stephen Mason Jul 15 '12 at 08:21
  • @hage, the auto boxing is done by the compiler and not the JVM. – Steve Kuo Jul 15 '12 at 12:11
3

Java support two types of structures first are primitives, second are Objects.

Method that you are asking, is used to retrieve value from Object to primitive.

All java types that represent number extend class Number. This methods are in someway deprecated if you use same primitive and object type since [autoboxing] was implemented in Java 1.5.

int - primitive

Integer - object

Before Java 1.5 we was force to write

int i = integer.intValue();

since Java 1.5 we can write

int i = integer;

Those methods are also used when we need to change our type from Integer to long

long l = integer.longValue();

2

Consider this example:

Integer i = new Integer(10);
Integer j = new Integer(10);
if (!(i == j)) {
    System.out.println("Surprise, doesn't match!");
}
if (i.intValue() == j.intValue()) {
    System.out.println("Cool, matches now!");
}

which prints

Surprise, doesn't match!
Cool, matches now!

That proves that intValue() is of great relevance. More so because Java does not allow to store primitive types directly into the containers, and very often we need to compare the values stored in them. For example:

oneStack.peek() == anotherStack.peek() 

doesn't work the way we usually expects it to work, while the below statement does the job, much like a workaround:

oneStack.peek().intValue() == anotherStack.peek().intValue()
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
  • This answer deserves +1. I was stuck in one of the DSA question, later realize that I wasn't using .intValue(). – sn- May 31 '21 at 14:00
1

get(i) will return Integer object and will get its value when you call intValue().In first case, automatically auto-unboxing happens.

UVM
  • 9,776
  • 6
  • 41
  • 66
0

It's just a convenience method for getting primitive value from object of Number: http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Number.html

Consider the code:

Integer integerValue = Integer.valueOf(123);
float floatValue = integerValue.floatValue();

The last line is a convenient method to do:

float floatValue = (float)(int)integerValue;

Since any numeric type in Java can be explicitly cast to any other primitive numeric type, Number class implements all these conversions. As usual, some of them don't make much sense:

Integer integerValue = Integer.valueOf(123);
int intValue = integerValue.intValue();
int intValue2 = (int)integerValue;
int intValue3 = integerValue;
Andrey Agibalov
  • 7,624
  • 8
  • 66
  • 111
0

They are exactly the same. As other posters have mentioned, you can put either the Integer object or the int primitive into the array. In the first case, the compiler will automatically convert the Integer object into a primitive. This is called auto-boxing.

Jorn
  • 20,612
  • 18
  • 79
  • 126
  • so, java will convert Integer to int automatically by calling .intValue() in this case? from a reference to value type? – Timeless Jul 15 '12 at 08:21