Question on Assigning a Wrapper Object to a primitive

F

fxtrad

Hello,

I'm a java noob and every book I read on the subject was very explicit
on that you can't do the following:

int i = new Integer(1);

Since primitive types (double, float, int, etc) can not hold nor
reference an Object.

Yet.. This compiles & runs fine!!

Here is my javac version:
$ javac -version
javac 1.5.0_13

I'm a little confused. Anyone?

Thanks -
 
A

Arne Vajhøj

I'm a java noob and every book I read on the subject was very explicit
on that you can't do the following:

int i = new Integer(1);

Since primitive types (double, float, int, etc) can not hold nor
reference an Object.

Yet.. This compiles & runs fine!!

Here is my javac version:
$ javac -version
javac 1.5.0_13

I'm a little confused. Anyone?

I think your books covers Java pre-1.5.

Java 1.5 added auto conversion between int and Integer
(boxing and unboxing).

Arne
 
W

Wayne

Hello,

I'm a java noob and every book I read on the subject was very explicit
on that you can't do the following:

int i = new Integer(1);

Since primitive types (double, float, int, etc) can not hold nor
reference an Object.

Yet.. This compiles & runs fine!!

Here is my javac version:
$ javac -version
javac 1.5.0_13

I'm a little confused. Anyone?

Thanks -

Since version 1.5 Java has supported a feature known
as "auto-boxing". This is the automatic conversion of
primitive types to/from the java.lang.* wrapper objects.

In your case, the compiler sees your attempt to assign
an Integer object to a primitive variable, and
automatically invokes Integer.intValue() for you.

In previous versions of Java that code would indeed
show an error message. You would manually have to
invoke that method:

int i = new Integer(1).intValue();

Of course this isn't the best example of this new
feature. It is indeed useful, however it is confusing
to (newer) programmers whenever a compiler automatically
adds code for you.

So your books are correct, a primitive variable such
as "i" in "int i" can not hold a reference to any
object.

-Wayne
 
F

fxtrad

Since version 1.5 Java has supported a feature known
as "auto-boxing". This is the automatic conversion of
primitive types to/from the java.lang.* wrapper objects.

In your case, the compiler sees your attempt to assign
an Integer object to a primitive variable, and
automatically invokes Integer.intValue() for you.

In previous versions of Java that code would indeed
show an error message. You would manually have to
invoke that method:

int i = new Integer(1).intValue();

Of course this isn't the best example of this new
feature. It is indeed useful, however it is confusing
to (newer) programmers whenever a compiler automatically
adds code for you.

So your books are correct, a primitive variable such
as "i" in "int i" can not hold a reference to any
object.

-Wayne


I see - thanks guys.

I wish they left this "convenience" to the IDE (Eclipse, etc.),
instead of building it into the language. I dislike such code
generation where they're going out of their way for "added
convenience" - vaguely feels like VB :)
 
L

Lew

I see - thanks guys.

I wish they left this "convenience" to the IDE (Eclipse, etc.),
instead of building it into the language. I dislike such code
generation where they're going out of their way for "added
convenience" - vaguely feels like VB :)

Without going into detail, there are some emergent phenomena from the
interaction of auto[un]boxing and other features. You can get surprising
results from its interaction with variable argument lists in methods, for example.

There's nothing wrong with building convenience into a language, but some of
Java's convenience features do sport a set of interesting consequences that
cause a lot of programmer squawk. To put some of that in consequence, at
least the rules are documented, whereas that has not always been clear in
programming languages of the past. I remember a FORTRAN dialect that limited
scope of loop variables to the loop body, but only implicitly, and no other
FORTRAN dialect I knew then had that feature.

It seems that every programming language causes programmer squawk, and also
programmer religious fervor. Java inspires both.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top