This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Numeric Promotion (Java OCA 8 Programmer I Study Guide, Sybex)

 
Manish Bilung
Greenhorn
Posts: 13
Chrome Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends,

Lets have a look at the code snippet on page 56 - Boyarsky and Selikoff below:



As per the book the code will not compile as floating-point literals are assumed to be double, unless postfixed with an f, as in 2.1f.

But for the following code snippet:

What is the data type of x * y / z?


First, the short gets converted to int.
Then the int gets converted to Float.
Then multiplication happens.
Then while dividing float and double why don't we observe an error because as per the book : the code will not compile as floating-point literals are assumed to be double, unless postfixed with an f, as in 2.1f.


Regards,
Manish
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manish Bilung wrote:as per the book : the code will not compile as floating-point literals are assumed to be double, unless postfixed with an f, as in 2.1f.

  • The book meant to say for assignment of that literal value 2.1 to a variable of type float.

  • Suppose If you try to assign floating-point literals means 10.00 ( which is assumed to be double because It is not postfixed with an f, as like 10.00f ) to variable of float type.
    Example: float x = 10.00; this will cause error because you are assigning double value to a float type variable where double is larger data type than float.

    So to compile It successfully either you have to postfix that floating-point literal i.e. 10.00 with f Or do narrowing conversion like below shown.
    float x = 10.00f; will compile as postfixed with f.
    float x = ( float )10.00; will compile because of explicit narrowing conversion which converts double literal to float and then assigns to x.


    Manish Bilung wrote:Then while dividing float and double why don't we observe an error

  • Because of the  division operator / between float and double operand, binary numeric promotion is performed which says

  • JLS 5.6.2. Binary Numeric Promotion wrote:Widening primitive conversion (ยง5.1.2) is applied to convert either or both operands as specified by the following rules:
    If either operand is of type double, the other is converted to double.
    Otherwise, if either operand is of type float, the other is converted to float.
    Otherwise, if either operand is of type long, the other is converted to long.
    Otherwise, both operands are converted to type int.


    Here one operand is of type double i.e. z so the other ( Result of x * y which is float ) is converted to double.

     
    Ioanna Katsanou
    Greenhorn
    Posts: 15
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I still didn't get this example :P I have the same question as the OP
     
    Piet Souris
    Rancher
    Posts: 1529
    33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And I didn't understand why 'short x = 14'  compiled in the first place, since 14 is an integer and so we have a narrowing conversion... in this case, since 14 fits into a short, this narrowing is allowed. Jls 5.2. Difficult stuff...
     
    Henry Wong
    author
    Marshal
    Pie
    Posts: 22113
    88
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ioanna Katsanou wrote:I still didn't get this example :P I have the same question as the OP


    Well, since this is a two months old topic, that has been answered -- if you are still confused, it may be a good idea to elaborate a bit on your doubt.

    Henry
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Piet Souris wrote:And I didn't understand why 'short x = 14'  compiled in the first place, since 14 is an integer and so we have a narrowing conversion... in this case, since 14 fits into a short, this narrowing is allowed. Jls 5.2. Difficult stuff...
    May be because an integer literal in Java can either be of type long ( if it is suffixed with L or l ) Or type int only, so although 14 fits in the range of short It is an integer literal of type int so implicit narrowing is done i.e. int to short.
     
    Ioanna Katsanou
    Greenhorn
    Posts: 15
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    OK thank you I just got this !!
     
    Henry Wong
    author
    Marshal
    Pie
    Posts: 22113
    88
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ganesh Patekar wrote:May be because an integer literal in Java can either be of type long ( if it is suffixed with L or l ) Or type int only, so although 14 fits in the range of short It is an integer literal of type int so implicit narrowing is done i.e. int to short.


    Actually, I Piet already knows the details -- as he mentioned the Java Language Specification, section 5.2 (assignment conversions). I just read the comment as it is kinda difficult to understand (and not that Piet was confused).

    Henry
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @Henry Ah! I see, I misunderstood. Thanks
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic