Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method overloading shows 'incompatible type error' with float but not with double

I tried this method overloading code and I got the error

no suitable method found for add(double,double)

The code:

class Adder {
    static float add(float a, float b) {
        return a + b;
    }

    static int add(int a, int b) {
        return a + b;
    }
}

class TestOverloading1 {
    public static void main(String[] args){
        System.out.println(Adder.add(11.5, 11.5));
        System.out.println(Adder.add(27, 21));
    }
}

On writing, 11.5f in params, this works well.

I understood the differences between float and double from here and here.

So, why does Java take the parameters as double datatype by default? Is the higher precision of double behind such a bias?

I am aware that it takes the double by default. But, I wish to know what is the reason behind this?

like image 965
Shachi Avatar asked Sep 04 '17 09:09

Shachi


People also ask

How do I fix incompatible type error?

Swapping the inappropriate constructor call with an instance of the correct type solves the issue, as shown in Fig. 6(b). Fig. 6 also serves as an example to show how the incompatible types error is, in fact, a generalization of the method X in class Y cannot be applied to given types error explored in [4].

What is the difference between double and float type of data?

float and double both have varying capacities when it comes to the number of decimal digits they can hold. float can hold up to 7 decimal digits accurately while double can hold up to 15.

What is the difference between a double and a float in Java?

Size: Float is of size 32 bits while double is of size 64 bits. Hence, double can handle much bigger fractional numbers than float. They differ in the allocation of bits for the representation of the number. Both float and double use 1 bit for representing the sign of the number.

Can we add float with double?

To answer your question, you can add a float to a double and vice versa. Generally, the result will be made into a double , and you will have to cast it back to a float if that is what you want.


2 Answers

A floating point literal without any suffix (such as 11.5) is of type double by definition (similarly an integer literal without any suffix is of type int).

A double parameter is not acceptable for a method that accepts float arguments (since a casting from double to float may result in loss of data, and therefore the compiler won't perform such casting automatically).

On the other hand, 11.5f is a float literal, so you can pass such literals to your add(float a,float b) method.

like image 157
Eran Avatar answered Oct 03 '22 19:10

Eran


doing

Adder.add(11.5,11.5)

is the same as

double a = 11.5;
double b = 11.5;
Adder.add(a, b)

that doesnt match the parameters in the static method

static float add(float a,float b){return a+b;}  

so you are required to: cast those literals to float:

Adder.add(11.5f, 11.5f );

or declare a and b as float

float a = 11.5;
float b = 11.5;
Adder.add(a, b)
like image 38
ΦXocę 웃 Пepeúpa ツ Avatar answered Oct 03 '22 19:10

ΦXocę 웃 Пepeúpa ツ