Shamal Sandeep
Shamal Sandeep

Reputation: 263

final casting concept doesn't apply for overloading

In my casting class, teacher taught us an interesting fact as follows.

class Casting {
    public static void main(String args[]){
        int i = 10;
        byte b = i;
        System.out.println(b);
    }
}

We got an error

java:5: possible loss of precision

And then we changed the code as follows

class Casting1 {
    public static void main(String args[]){
        final int i = 10;
        byte b = i;
        System.out.println(10);
    }
}

10

We got the correct output. As for the reason, he told that when we modify a variable final the variable is stored in the smallest data type possible. In this case was a byte. That's the reason that we were able to cast that without using the cast keyword.

But when we use method overloading like this,

class A {
    void m(int i){
        System.out.println("int");
    }
    void m(byte b){
        System.out.println("byte");
    }
    public static void main(String args[]){
        A a1 = new A();
        final int i = 10;
        a1.m(i);
    }
}

I get the output int. If the final variables are stored in the lowest possible data type, it should be byte. So I tried the following code without overloading.

class A {
    void m(byte b){
        System.out.println("byte");
    }
    public static void main(String args[]){
        A a1 = new A();
        final int i = 10;
        a1.m(i);
    }
}

java:9: m(byte) in A cannot be applied to (int)

What's the reason for this? Is there any point that I have misunderstood?

Upvotes: 12

Views: 97

Answers (4)

Phil Anderson
Phil Anderson

Reputation: 3146

This bit isn't quite correct...

"As for the reason, he told that when we modify a variable final the variable is stored in the smallest data type possible. In this case was a byte."

It doesn;t store it as a byte, it stores it as an int, BUT it is effectively a constant so when Java compiles the line byte b = i; it knows for sure that the value will be 10, which doesn't need casting.

Upvotes: 2

Maroun
Maroun

Reputation: 95958

If the conversion is part of an assignment, and the value can fit into a byte, the compiler performs the conversion automatically for you.

The JLS clearly explains that is a special case that only applies to assignment and not to conversions in other contexts.

It's worth mentioning that byte is useful only when you program for embedded devices or dealing with files/networks. byte and int occupy the same space because variables addresses are aligned.

Upvotes: 0

Smallware
Smallware

Reputation: 72

You are mixing up memory space of variables and their type. Calling the method m(...) will first of all check the type of the paramether variable. Here it is an int so it will chose the according overloaded method, no matter the size of the int in memory.

Although I really apreciate you first example that brings the light into one of the characteristics of the final identifier.

Upvotes: 2

Atsby
Atsby

Reputation: 2327

Is there any point that I have misunderstood?

Yes. The search for which method to apply depends on the types of the arguments. Unlike the case of assignments, there's no conversion attempt for method arguments (at least, there wasn't before autoboxing was added to the language, which adds another set of arbitrary rules).

Upvotes: 0

Related Questions