Ayush Basak
Ayush Basak

Reputation: 459

Why is int being cast to float in java?

In this code

    public void display(int x, double y){
        System.out.println("Double");
    }
    public void display(int x, float y){
        System.out.println("Float");
    }
    public static void main(String[] args){
        prog01 p = new prog01();
        p.display(4,5); //First Output
        p.display(4,5.0); // Second Output
    }

The outputs are:

Float
Double

The second output is understandable, but why is 5 being cast to float when it should have given an error?

Upvotes: 0

Views: 99

Answers (1)

varman
varman

Reputation: 8894

Widening Casting (Implicit) happens, byte -> short -> int -> long -> float -> double. Here you pass integer, Since it doesn't have long as a type, the next one is float. That's why it's giving you float

Upvotes: 1

Related Questions