• Post Reply Bookmark Topic Watch Topic
  • New Topic

When does automatic promotion will happen from float to double in java.  RSS feed

 
hemkar kumar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Currently, I am learning method overloading where I came to know that in case compiler unable to find required method with the float argument than compiler will try to promote arguments to next level that is double.But in my case, automatic promotion is not happening instead I am getting compile time error mentioned below.

   
I have two classes one contains the main method and other contains overloaded methods.

TestClass.java


MainClass.java
  
 
hemkar kumar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, i found the issue. instead of double (primitive type), i am using Double (Wrapper class) in the overloaded method as argument type.

Anyway thanks for your valuable time and reading my issue.
 
Andrea Binello
Ranch Hand
Posts: 62
5
Eclipse IDE Java Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hemkar kumar wrote:instead of double (primitive type), i am using Double (Wrapper class) in the overloaded method as argument type.

Yes, and to be clear/complete, the Java Language Specification 8 in §5.3 "Invocation Contexts" states that a "loose" invocation can be:

- a boxing conversion (§5.1.7) optionally followed by widening reference conversion

This means that the compiler can implicitly convert a float (primitive) to a Float (wrapper) or eventually to an Object. In other words:

float --> Float (OK)
float --> Float --> Object (OK)
float --> double --> Double (NO!! cannot do this implicitly)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!