5

CASE 1

byte b = 7; // why don't I need to cast 7 to byte in this case? byte b = (byte)7;
System.out.println(b);

Output: 7

CASE 2

static void fun(byte b) {
   System.out.println(b);
}

public static void main(String []args) {
   fun(7); // Compiler gives error because a cast is missing here.
}

Output: Compilation error: method fun(byte) is not applicable for the argument (int).

My question is: How come in case 1, 7 is implicitly cast to byte from an int, while in case 2 it is forcing the programmer to cast it explicitly?

7 is still in the range of byte.

Please suggest.

sstan
  • 35,425
  • 6
  • 48
  • 66
Smith
  • 151
  • 3
  • 11

4 Answers4

2

In case 1, you are implicitly casting to String, which is what gets pumped out to the standard output. Your function on the other hand, requires a byte, not an integer.

Pherion
  • 165
  • 10
  • You also missed the point of the question I'm afraid. – sstan Jun 30 '15 at 18:29
  • I don't know. The question is "Why in case one is the cast to int implicit, when in case 2 its not?" By pointing out that you are not in fact casting to int in case one, I think that answers the question. – Pherion Jun 30 '15 at 18:30
  • The question is: why in the first case, he is not forced to write: `byte b = (byte)7;` with the cast in front of the literal `7`. But why in the second case, he does have to. – sstan Jun 30 '15 at 18:31
  • Ah right! My fault. In this case I am unsure as to the answer! – Pherion Jun 30 '15 at 18:34
  • Well, enjoy the upvotes, because you're still getting them :) – sstan Jun 30 '15 at 18:49
  • 1
    There is no implicit cast to `String` anywhere in their code examples. – Sotirios Delimanolis Jun 30 '15 at 18:56
1

In fun(7), 7 is an Integer literal.

Try this:

fun((byte)7); // explicit cast, compile fine

Now come to your question:

In case 1 byte b = 7; and System.out.println(b); are in same scope. As 7 fits in a byte so no need for explicit casting and println(b) just cast the byte to string and print 7.

But in In case 2, they are not in same scope. You call fun with Integer 7 from one scope where the called function in another scope is only take byte instead int. So it doesn't matter whether 7 fits in a byte or not, hence compiler raise error (request for explicit casting) before the program execute System.out.println(b);.

mazhar islam
  • 5,561
  • 3
  • 20
  • 41
1

Numeric literals such as 7 are treated as integers. So normally, as in your 2nd case, you are required to cast to byte to pass it where a byte is expected.

fun((byte)7); // You are forced to cast here.

However, your first case is special, because you are using this integer literal in a variable initialization statement. In this case, there is a rule in the JLS that allows for an implicit cast to take place.

byte b = 7; // special JLS rule applies, the cast is done for you automatically.

JLS Spec

In addition, if the expression is a constant expression (§15.28) of type byte, short, char, or int:

A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.

Community
  • 1
  • 1
sstan
  • 35,425
  • 6
  • 48
  • 66
  • Thanks, this one looks more appropriate reason as why compiler behaves weird. – Smith Jun 30 '15 at 18:55
  • @Deepak: I'm happy to hear you say that. I've been a bit surprised by the voting results on this particular question. Even if the question has been marked as a duplicate, feel free to mark the answer as accepted if you think it's appropriate. – sstan Jun 30 '15 at 20:42
0

By default 7 is an int literal. in first case :- byte b = 7; we are explicitly assigning the int literal 7 to byte b so implicitly java handles it & assigns the corresponding value of int 7 to byte which is also 7, meaning the casting/conversion is done implicitly by java.

But in second case fun(7); the 7 here is int type by default , but your method fun(byte) expects byte as an parameter not int, and as java is strictly type casted it gives the error.

AnkeyNigam
  • 2,810
  • 4
  • 15
  • 23