It's not a secret anymore!*
The moose likes Java in General and the fly likes For loop with wrapper objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "For loop with wrapper objects" Watch "For loop with wrapper objects" New topic
Author

For loop with wrapper objects

Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
Hi,

Just out of curiosity I was trying out the for loop when I found one thing that I could not explain myself. First lets have a look at the code

Code 1:

Code 2:

Code 1 compiles nicely , while code 2 doesn't. I thought that for loop does not auto-boxes Long . But this is not true since
compiles nicely.

The compiler stacktrace is

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types
required: java.lang.Long
found: int


If that be the case, then it should have been the case for code 1 too, but it isn't. Does anybody knows why this happen?


Programming is about thinking, NOT coding
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38449
    
  23
Why on earth are you running code which has a compiler error in? Have you been running that from an IDE? Didn’t it tell you there was a compiler error?

You would appear to be passing an int to something which requires a Long. You can box or you can widen, but converting an int to a Long would require both widening and boxing. You can’t do both together.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4369
    
    8

The problem is with the initialisation of the loop:
0 is an int literal, so this is asking the compiler to widen and then autobox, which it won't do (it will only autobox then widen). This doesn't apply in your first example (needs to widen but not box) or the third (needs to box but not widen).

If you change it to Long s = 0L, it will compile.
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
And just in case all this 'widening' and 'boxing' talk is foreign to some. Here's the JLS discussion on widening, and here's the discussion on boxing. There may be additional info of interest in the same area.


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
Matthew Brown wrote:The problem is with the initialisation of the loop:
0 is an int literal, so this is asking the compiler to widen and then autobox, which it won't do (it will only autobox then widen). This doesn't apply in your first example (needs to widen but not box) or the third (needs to box but not widen).

If you change it to Long s = 0L, it will compile.


thanks, how silly of me
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Campbell Ritchie wrote:Why on earth are you running code which has a compiler error in? Have you been running that from an IDE? Didn’t it tell you there was a compiler error?

Just out of curiosity, I tried this form NetBeans. It told me there were errors and offered me to run it anyway, with a checkbox to suppress the warning in the future...

I tried to run a class with compiler errors in a method which was not called. It finished successfully. So the javac produces a correct .class file even if there are compilation errors. (Fascinating. It opens up a whole new world of possible fubars!)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38449
    
  23
Martin Vajsar wrote: . . . Just out of curiosity, . . .
That’s all right. Just out of curiosity justifies anything . . .

. .. except possibly this!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7691
    
  19

Vishal Shaw wrote:Just out of curiosity I was trying out the for loop...

Just a word of warning for you: Your 'code 2' (assuming it was coded properly) will run orders of magnitude longer than 'code 1', and Long is only a shift key away from long, so make sure you get it right if you ever decide to do it for real.

You can't make the same mistake with int, since its wrapper is called Integer.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
 
subject: For loop with wrapper objects