This week's book giveaway is in the Kotlin forum.We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

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

Manish Bilung
Greenhorn
Posts: 13
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
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
Ranch Hand
Posts: 115
1
I still didn't get this example :P I have the same question as the OP

Piet Souris
Rancher
Posts: 1979
67
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
Sheriff
Posts: 23283
125
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
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: 115
1
OK thank you I just got this !!

Henry Wong
author
Sheriff
Posts: 23283
125
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
@Henry Ah! I see, I misunderstood. Thanks

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.