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

Widening conversion

 
Ranch Hand
Posts: 234
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In understand that autoboxing occurs at compile-time i.e. in the code below, the compiler will automatically generate code to autobox 42

Does widening conversion also occur at compile-time? For example, in the code below, the datatype of the float literal is widened to a double. Does this occur at at compile-time or runtime?
 
Daniel Cox
Ranch Hand
Posts: 234
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never mind. I think I found the answer. Chapter 2 (Java in a Nutshell) seems to suggest that widening conversions are performed at runtime.

Widening conversions are performed automatically by the Java interpreter.

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's possible that if you're dealing with compile-time constants, as is in the case in your example, the compiler will do the conversion at compile time.
 
Marshal
Posts: 76472
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Cox wrote:. . . autoboxing occurs at compile-time . . .

What makes you think that? The compiler can verify that boxing is feasible for that code, but does not do it. Same for widening conversions. If you look in the Java® Language Specification=JLS (beware: the JLS can be difficult to read) it says it must be a primitive type, and lists the possible conversions. The compiler keeps track of the types of all variables, so it can verify that 879977.4F is a float literal and can undergo widening conversion to a double or boxing to a Float.

I see you have found the answer already: well done
 
Daniel Cox
Ranch Hand
Posts: 234
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Daniel Cox wrote:. . . autoboxing occurs at compile-time . . .

What makes you think that? The compiler can verify that boxing is feasible for that code, but does not do it.


When I said that autoboxing occurs at compile-time, I meant the compiler will generate "autoboxing" bytecode at compile-time, for example, at compile-time, the compiler will translate this source code new ArrayList<Integer>().add(2) to this bytecode new ArrayList<Integer>().add(Integer.valueOf(2))
 
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
Have you inspected the bytecode? Try this:-and thisNow print the bytecode out which you can do with the javap tool:-
javap -c MyClass
Then you can see what the differences are.
 
Daniel Cox
Ranch Hand
Posts: 234
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going by what the oracle documentation says. The compiler converts this code li.add(i) to this code li.add(Integer.valueOf(i)).

Consider the following code:

List<Integer> li = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
li.add(i);

...the compiler converts the previous code to the following at runtime:

List<Integer> li = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
li.add(Integer.valueOf(i));



My jad decompiler shows that this source code

gets compiled to
 
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

Daniel Cox wrote:I'm going by what the oracle documentation says. The compiler converts this code li.add(i) to this code li.add(Integer.valueOf(i)).


Actually, that is the Java tutorial, not the documentation. The JLS, on the other hand, suggests that boxing is purely a runtime event, which would mean that the compiler isn't involved at all.

I guess it's possible that a boxing event could generate slightly different byte-code, but it doesn't appear to be mandatory; so what you might be seeing is the implementation for a specific version of javac.

Winston
 
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

Daniel Cox wrote:. . .
My jad decompiler . . .

Forget about decompilers and use javap to inspect the bytecode. Much more accurate.
 
Daniel Cox
Ranch Hand
Posts: 234
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did as you suggested. I used the javac command to compile my Simple.java file and used the javap command to disassemble my Simple.class file. I don't claim to understand bytecode jargon but I'm convinced that the compiler converts this code numbers.add(123) to this code numbers.add(Integer.valueOf(123)).

The following source code

gets compiled to

The following source code produces the same bytecode.

 
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
Same result as I got; both exactly the same. So it would appear that you were right that code is created at compile‑time which prepares the way for boxing.
 
Winston Gutkowski
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

Campbell Ritchie wrote:Same result as I got; both exactly the same. So it would appear that you were right that code is created at compile‑time which prepares the way for boxing.


In which case, that would make the JLS wrong, wouldn't it? Or maybe, as I said, it's not a requirement (it doesn't explicitly state how it does the conversion at runtime; simply that is IS done then).

Winston
 
Daniel Cox
Ranch Hand
Posts: 234
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think that the JLS is wrong. It just says nothing about what happens at compile-time.
 
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
Agree; the JLS section in question doesn't say compile‑time or runtime or anything similar.
 
Doe, a deer, a female deer. Ray, a pockeful of sun. Me, a name, I call my tiny ad ...
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic