• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

OCA 8 Numeric Promotion

 
Greenhorn
Posts: 9
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm studying the Jeanne and Scott's book.
On the example on page 61:


I do not understand why the third line shows compilation error.
I debug it on Netbeans and shows that x and y are short variables.
I understand that, in the third line, they are converted to int  but, what I do not understand, is why I get error in this line if in line 1 an int was converted to a short without any error.
Thanks
 
Marshal
Posts: 26626
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you multiply two short variables together, there is no guarantee that the product will be inside the numeric range supported by "short". The compiler implements that by requiring a cast to short in that code.

Now you may be able to look at other code in the class and determine that the product will in fact fit into a short variable, but the compiler doesn't do that. That's because it would be far too complicated to do so -- sure, it might work for that simple example but there are many more possibilities. So where does a compiler-writer draw the line? Naturally the compiler isn't going to analyze any other code to see if a cast is required.

And anyway, how do you know there isn't another thread running which changes the values of x and/or y before the multiplication takes place? I suppose that in this case you might be able to demonstrate that, but asking a compiler to do so is not practical.
 
Master Rancher
Posts: 3903
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:When you multiply two short variables together, there is no guarantee that the product will be inside the numeric range supported by "short". The compiler implements that by requiring a cast to short in that code.


Well yes - but then, when you multiply two ints together, there's no guarantee that the product (that is, the correct product) will be inside the numeric range supported by int.  And yet, the compiler allows this sort of thing all the time, silently ignoring any overflow.  The more relevant reason is, int gets special treatment compared to short, because Java assumes that most/all math operations will be implemented using at least 32 bits of hardware, so it immediately converts any binary operation between two integral types to be result int, unless at least one operand is a long, in which case it uses long instead.  Or unless there's a float or double involved... but then, I did say "integral types".

Paul Clapham wrote:Now you may be able to look at other code in the class and determine that the product will in fact fit into a short variable, but the compiler doesn't do that. That's because it would be far too complicated to do so -- sure, it might work for that simple example but there are many more possibilities. So where does a compiler-writer draw the line? Naturally the compiler isn't going to analyze any other code to see if a cast is required.



But the compiler does look at some code to make this determination.  That's why lines 1 and 2 were able to compile.  The difference is, (a) those lines were variable declarations with initializer expressions, and (b) everything in the initializer expression was a compile-time constant, which meant the whole expression could be evaluated at compile time.  Those two conditions are necessary to allow the compiler to decide that yes, it's OK to convert an int to short, because it's within the allowed range.  Otherwise, it's not possible.  For line 3, because neither x nor y were constants, the compiler will not allow the conversion, even though it was allowed in lines 1 and 2.

Fernando: for comparison, try this:

Will this compile?  Why, or why not?  Try it and see if your answer is correct.  
 
Fernando Albornoz
Greenhorn
Posts: 9
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried the code and compiled.



I finally understood why the third line of this code does not show an error. It's because the compiler knows that he is evaluating two constants.
The compiler evaluates a line of code at a time.
Thank you very much for a very clear and educational explanation.
 
Marshal
Posts: 73021
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done working the problem out

Fernando Albornoz wrote:. . .
The compiler evaluates a line of code at a time.
Thank you very much for a very clear and educational explanation.

No, the compiler doesn't evaluate code. It parses code and converts it into bytecode, but the evaluation is done by the JVM at runtime. You can try single file execution and write java Foo.java but even then the compiler doesn't evaluate any code. It compiles it and the JVM then starts and runs the bytecode.
 
Fernando Albornoz
Greenhorn
Posts: 9
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank very much for the clarification.
I'm learning a lot in this forum.🤗
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic