• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Implicit Widening

 
j rab
Greenhorn
Posts: 16
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm currently studying for the OCA exam and came across a question on the Enthuware.

How is this assignment valid without the F or f post fix and why would this be considered Implicit widening conversion?

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
j rab wrote:How is this assignment valid without the F or f post fix and why would this be considered Implicit widening conversion?

Because any integer literal is of type long if it ends with the letter L or l; otherwise it is of type int. And an int can be implicitly widened to a float. The other way around (from float to int) will result in a compiler errorAnd it might be a bit weird, but a long can fit (implicitly converted, no explicit cast required) into a float (and a double) as well. You'll find a detailed explanation in this topic.

Please be aware that you could have precision loss if you convert a long to a doubleOutput: 9223372036854776000,000000 9223372036854775807

And for completeness: no precision loss with an int widened to a double, but widened to a float you don't get exactly the same maximum value of int. This code snippetprints: 2147483647 2147483648,000000 2147483647,000000

Hope it helps!
Kind regards,
Roel
 
j rab
Greenhorn
Posts: 16
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Roel, that does help. So because it was an int and because a int can easily fit into an float, it was implicitly casted to a float. But if it was a floating point number than it would have been a compile time error because default floating point numbers are double. So this assignment wouldn't work:

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
j rab wrote:So because it was an int and that a int can easily fit into an float it was implicitly casted to float. But if it was a floating point number than it would have been a compile time error because default floating point numbers are double.

Exactly!

This statement will indeed fail to compile due to the fact 0.23 is by default considered to be a double literal and you can't assign a double to a floatIn order to get rid of the compiler error, you have two alternative solutions; either use F or f (and create a float literal instead of a double literal) or add an explicit cast to float (or an integer data type like int or short, might of course cause precision loss). This cde snippet illustrates both alternatives

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic