Win a copy of Event Streams in Action this week in the Java in General 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Different Data Types Operations Question

 
Ranch Hand
Posts: 159
1
Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that when having to do with operators in Java , and different data types, then the result is promoting to the larger of the data types
So, because double is larger than float, when having for example



The result z+w will be of double type.
If I don't out the F on the float data type, then the float is assumed to be double.
So why the bellow code does not compile??



Shouldn't the result just be double, if both z and w are considered double???
Instead it throughs

Arithmetic.java:26: error: incompatible types: possible lossy conversion from double to float
float w=2.1;
       ^
1 error



I cannot seem to understand that case can you help me?
Thanks
 
Bartender
Posts: 1244
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
Floating-point literals in Java default to double precision. To specify a float literal, you must append an F or f to the constant.

Ioanna Katsanou wrote:If I don't out the F on the float data type, then the float is assumed to be double.

No, in your first example here you appended F to floating point literal so It is assumed to be float.

So why the bellow code does not compile??  

Because here on line no 2 floating point literal 2.1 is by default double and you are trying to assign It to float so as you know range of double doesn't fit in float so gives compile time error which says "incompatible types: possible lossy conversion from double to float " pointing at line no 2 of your second example.
 
Bartender
Posts: 10772
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ioanna Katsanou wrote:I know that when having to do with operators in Java , and different data types, then the result is promoting to the larger of the data types


You need to be a bit careful with your terminology.

What you're posting about are specifically primitive data types, and even more specifically numeric primitives (ie, everything except boolean) and arithmetic operators (+, -, *, / and %). It also applies to bitwise operators, but I'd say that's beyond the scope of this question.

Basically, when Java does arithmetic on two primitives that are NOT the same type, it has 3 "native" modes:
1. It converts both arguments to int, and returns the result as an int.
2. It converts both arguments to long, and returns the result as a long.
3. It converts both arguments to double, and returns the result as a double.
and it chooses them in that order.
So:
1. If both types are integers (ie, not float or double) AND both are smaller than a long, it converts them to ints.
2. (Assuming 1 is false) If both types are integers, it converts them to longs.
3. (1 and 2 are false) It converts them to double.

Plug that into your question and see if it helps.

Winston
 
Saloon Keeper
Posts: 3408
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Winston, but that is not 100 % accurate as well. If x is a short and y a float, then in x * y x is first promoted to an int (correct) but then promoted to a float, not a double. The result is also a float. That is, if I understood my ocajp book correctly, which is not 100% certain...
 
Marshal
Posts: 65046
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shou‍ld find the details in this Java® Language Specification (=JLS) section. Under the heading binary numeric promotion, it says this:-

Otherwise, if either operand is of type float, the other is converted to float.

It would appear that the integer argument is converted directly to a float without passing go or collecting £200 becoming an int.

For those who are not familiar with the JLS@ beware. It can be very difficult to read.
 
Winston Gutkowski
Bartender
Posts: 10772
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:For those who are not familiar with the JLS@ beware. It can be very difficult to read.


That conversion also surprises me, since promotion of an int to a float could easily lose precision (with long to double, it's far less likely, and also unavoidable); but I bow to superior "interpretation".

Winston
 
Ranch Hand
Posts: 531
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Ioanna,

I would always put like f after a numeric value before assigning it to a float variable.

With best regards,

Anton.
 
Ioanna Katsanou
Ranch Hand
Posts: 159
1
Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to everyone I finally understood this !!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!