autoboxing is dangerous

I had a nasty typo in my code similar to the following:

int i=0;...."abc".substring(i<2 ? 1 : null);

This is semantically equivalent to

"abc".substring(i<2 ? 1 : ((Integer)null).intValue());

A decent compiler would evaluate the immutable expression ((Integer)null).intValue() at compile time

and would make a compile time error.

I wonder why this is not the case..

To identify this type of error I am planing to run javap on all classes of my project and analyze it with a shell script.

Has somebody a better idea ?

[581 byte] By [christo4711a] at [2007-10-3 3:32:25]
# 1

> I had a nasty typo in my code similar to the

> following:

>

> int i=0;...."abc".substring(i<2 ? 1 : null);

>

that's no typo, that's a logic error. I've never seen anyone type "null" instead of "0" accidentally.

I've seen them mistakenly THINK they should use one where the other is needed though.

> This is semantically equivalent to

> "abc".substring(i<2 ? 1 :

> ((Integer)null).intValue());

>

It's not.

> A decent compiler would evaluate the immutable

> expression ((Integer)null).intValue() at compile time

>

No it wouldn't.

> and would make a compile time error.

> I wonder why this is not the case..

>

Because you need to think for yourself for a change.

> To identify this type of error I am planing to run

> javap on all classes of my project and analyze it

> with a shell script.

>

Go ahead, should keep you busy enough to not bother us with such nonsense.

> Has somebody a better idea ?

Learn Java...

jwentinga at 2007-7-14 21:26:39 > top of Java-index,Developer Tools,Java Compiler...
# 2

> I've never seen anyone type "null" instead of "0" accidentally.

Every programmer makes an error at some point.

We are all human beings.

Fortunately there are excellent tools to

identify coding errors. I think that Findbugs is excellent. It did

identify this problem.

There were no further occurrence. I double checked by running a shell

script on all disassembled classes.

Here is another stupid code line. It looks almost innocent in

the context of a larger code block when null and false are both

shaded red in syntax highlighting.

boolean b = ...

JLabel l= ...

l.setEnabled(i<2 ? b : null);

instead of

l.setEnabled(i<2 ? b : false);

> This is semantically equivalent to

>It's not.

In Sun JDK both is compiled so that the static method intValue()

is called from an Integer object.

Are you using a different compiler ?

Please check by decompiling.

>>A decent compiler would evaluate the immutable

>>expression ((Integer)null).intValue() at compile time

>No it wouldn't.

I do not agree.

The method intValue() does not have side effects.

On an immutable object it could safely be evaluated once at compile time.

Currently, Sun javac already makes compile time evaluations to a limited extend.

>Learn Java...

Thanks for you advise. Which book ?

My conclusion reconsider each line trice and use Findbugs frequently.

christo4711a at 2007-7-14 21:26:39 > top of Java-index,Developer Tools,Java Compiler...