5

I have two functions. One works fine, while the other doesn't compile. Not able to spot the cause. Can you please help me here?

This works fine

 static byte method1() {
    final short sh1 = 2;
    return sh1;
    }

This one doesn't compile

static byte method2(final short sh2) {
return sh2;
}
vjy
  • 1,184
  • 1
  • 10
  • 24
Edi Nijam
  • 105
  • 3
  • You should have posted the compiler error message, and you should also have *read* the compiler error message, and tried to make sense of it, first. – user207421 Jun 18 '13 at 09:41
  • The compiler (infact the editor) clearly says, I have cast it to byte. But my question is more around, how is it working without a cast in first function but not in the second. – Edi Nijam Jun 18 '13 at 09:44

5 Answers5

12

In your first example, sh1 is final, and has a value which can be determined at compile time to be "castrated down" to a byte. In effect, it's a constant. If you remove the final, it won't compile anymore.

In your second example, it cannot be determined by the compiler that your method argument is "safe", you have to do an explicit cast, as other answers mentioned.

For the nitty gritty details, see here (JLS references and all). But a simple example:

final byte b1 = 127;
final byte b2 = 1;
final byte b = b1 + b2; // <-- FAIL: 128 greater than Byte.MAX_VALUE

final byte b1 = 12;
final byte b2 = 3;
final byte b = b1 + b2; // <-- SUCCESS: 15 fits
Community
  • 1
  • 1
fge
  • 119,121
  • 33
  • 254
  • 329
3

First Method:

First method you declare a final short variable whose value is 2 inside the range of byte. sh1 in that first method cannot hold any other value other than 2determined at the compile time itself. If you remove the final , you will see the compiler complaining.

Second Method:

You need to cast short to byte as this is a narrowing conversion and you have to assure the compiler that you do it deliberately and know its consequences because final short sh2 can have any value which should or shouldn't be in the range of byte.

static byte method2(final short sh2) 
{ 
   return (byte)sh2; 
}
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
1

Also as an experiment if you remove assignment from the first one e.g.

static byte method1() {
        final short sh1;
        return sh1;
        }

Then this doesn't compile as well. So just as the second one, the compiler isn't sure about the type safety because the value is passed to the function, same happens if you remove assignment from the first one.

voidMainReturn
  • 3,339
  • 6
  • 38
  • 66
0

“final” final at runtime.

In method 1

final short sh1 = 2;////  determined value at run-time.So compiles.

You have to cast it.You are passing short and returning as byte.

static byte method2(final short sh2) { 

        return (byte) sh2;

    }

Is "final" final at runtime?

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

Java is a strongly typed language. In strongly typed languages you have to perform explicit type casting. Data type mismatch will lead to errors in strongly typed languages.

In your code you are passing short and returning byte. So the code should be as follows,

static byte method2(final short sh2) 
{ 
    return (byte) sh2;
}
Deepu
  • 7,592
  • 4
  • 25
  • 47