2

I have the problem with following code:

     byte b=34;  //successfully compiled
     b=b+10;     //compile time error
     int x=34;   //success
     b=x;        //compile time error

Why is b=34 compiled successfully, while integer literal is by default int but b=x gives compile time error while x is int ?

apresh
  • 151
  • 1
  • 7

7 Answers7

2

The problem is that byte b is a byte of 8 bits and int x is a 32 bits integer value.

Therefore, your code b=b+10 can be translated as

b = 00100011b + 00000000000000000000000000001010b;

As you can see, the JVM will cannot force byte b to an int (by shoving 32 bits value into an 8 bit registry), so there's type incompatibility, if typecasting is not done (the same applies for b=x).

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
2

With out casting it's not possible, see The Elite Gentleman answer.

do casting this way

byte b=34;
b=(byte)(b+10);
int x=34;
b=(byte)x;
subodh
  • 6,136
  • 12
  • 51
  • 73
1

due to possibly loss of precision you should code as follows

 byte b=34;
b=(byte)(b+10);
int x=34;
b=(byte)x;
Sheo
  • 1,034
  • 12
  • 24
1

In your code-snippet the b=x; is not assignable.

From the JLS 5.2 Assignment Conversion.

If the type of the expression cannot be converted to the type of the variable by a conversion permitted in an assignment context, then a compile-time error occurs.

Narrowing primitive conversion may be used if all of the following conditions are satisfied:

  1. The expression is a constant expression of type byte, short, char or int.
  2. The type of the variable is byte, short, or char.
  3. The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable.
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • 1
    `b=b+10` also has an issue... why doesn't `b+=10`? (Since you already have the specification open...) –  Dec 13 '11 at 07:32
1

When you add int to byte, the byte will be implicitly converted into int but the conversion int to byte will not be done implicitly, it should be done explicitly.

byte b=34;
b=(byte)(b+10);
int x=34;
b=(byte)x;
dku.rajkumar
  • 18,414
  • 7
  • 41
  • 58
  • So why is `b += 10;` perfectly legal? :) –  Dec 13 '11 at 07:31
  • 1
    b += 10 is perfectly legal, because when we use +=, implicitly type cast is done. first right hand operation is performed and the result is typecasted to the variable type of left hand side. @pst – Zohaib Dec 13 '11 at 07:47
  • 1
    thats the operator += functionality. all operators of the form 'op=' cast their result to the type of the left-operand . http://www.janeg.ca/scjp/oper/assignment.html – dku.rajkumar Dec 13 '11 at 07:52
0

Because integer datatype can hold much bigger values than byte and there is no trivial rule to convert int yo byte.

In your case:

b = (byte)x;

will help,

Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114
0

well byte range is -128 to 12y so you are able to set the value in the while the arithemetic operation you need to convert that byte to integer bcoz it will consider as integer value not as byte value.

you can't directly type cast from integer to byte bcoz integer size is larger than the byte that's why you getting compile time error

check this link for result how the effect on conversion from integer to byte How does Java convert int into byte?

Community
  • 1
  • 1
Pratik
  • 30,639
  • 18
  • 84
  • 159