Skip to main content

Method overloading parameter as float, double and wrapper.

Question?  What is the output of below code ?

 Other question 

will it be compile ?

will there be an error at runtime for ambiguity?
   

public class RedHatQuestion
{
  public void printData(float f)
  {
    System.out.println("inside float");
  }
  
  public void printData(double d)
  {
    System.out.println("inside double ");
  }
  
  public void printData(Double d)
  {
    System.out.println("Inside object double");
  }
  
  public static void main(String[] args) {
    RedHatQuestion question = new RedHatQuestion();
    question.printData(3.1415);
  }
}

It will compile as there is no syntax error.
Also there is no runtime exception as there no runtime binding created. 
See we have only compile time binding/overloading not overriding.

Answer : inside double.

Explanation : in java for floating value we have default data type as double. Every float value is double unless specified.

You might have encounter compilation error for below statement.

float PI = 3.1415; error : type mismatch cannot convert from double to float.

then you had changed it to either (float) 3.1415 or 3.1415F

Comments