Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
Greenhorn
Posts: 13
Eclipse IDE Firefox Browser Chrome
  • 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
 
Bartender
Posts: 1248
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • 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.

     
    Ranch Hand
    Posts: 167
    1
    Oracle 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
     
    Bartender
    Posts: 4001
    156
    • 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...
     
    author
    Posts: 23878
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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: 1248
    86
    Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
    • 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
    Ranch Hand
    Posts: 167
    1
    Oracle Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    OK thank you I just got this !!
     
    Henry Wong
    author
    Posts: 23878
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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: 1248
    86
    Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @Henry Ah! I see, I misunderstood. Thanks
     
    The only cure for that is hours of television radiation. 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
      Bookmark Topic Watch Topic
    • New Topic