Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

For loop with wrapper objects

 
Vishal Shaw
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vishal Shaw
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Vajsar wrote: . . . Just out of curiosity, . . .
That’s all right. Just out of curiosity justifies anything . . .

. .. except possibly this!
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic