vico
vico

Reputation: 18171

Integer+Integer in java 1.4

Integer i1 = new Integer(10);
Integer i2 = new Integer(20);
Integer i3 = i1+i2;

Why in Java 1.7 this code works fine, but in java 1.4 I have error:

The operator + is undefined for the argument type(s) java.lang.Integer, java.lang.Integer

This is autoboxing issue or operator "+" is defined for Integer in java 1.7?

How to proceed Integer+Integer in java 1.4 then?

Upvotes: 3

Views: 920

Answers (4)

kosa
kosa

Reputation: 66637

Because autoboxing and unboxing is introduced from java 5 onwards. Java 1.4 can't understand that syntax.

How to proceed Integer+Integer

You need to get primitive int using intValue() and then do addition on primitive values.

Upvotes: 14

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136042

Autoboxing/Unboxing is done by javac. If we decompile .class we will see how exactly this is done:

    Integer i1 = new Integer(10);
    Integer i2 = new Integer(20);
    Integer i3 = Integer.valueOf(i1.intValue() + i2.intValue());

Upvotes: 0

Grammin
Grammin

Reputation: 12205

Integer in 1.4 doesn't autobox. Integer in 1.7 does.

You want something like:

int i3 = i1.intValue() + i2.intValue();

Upvotes: 3

Bill the Lizard
Bill the Lizard

Reputation: 405775

Java 1.4 did not have autoboxing of Integer variables. That came along in Java 1.5.

See What's New in Java 1.5: Autoboxing/Unboxing

Prior to that you had to do things like:

Integer i1 = new Integer(10);
Integer i2 = new Integer(20);
Integer i3 = new Integer(i1.intValue() + i2.intValue());

Upvotes: 11

Related Questions