Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

For loop with wrapper objects

 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 76472
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 76472
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic