2

What happened to my code? The following code worked for integer type of data, but couldn't work for byte type of data.

public class Exchange {
    public static void main(String[] args) {
    //int a = 23, b = 44;
    byte a = 23, b = 44;
    a = a + b;
    b = a - b;
    a = a - b;
    System.out.println("a=" + a + "b=" + b);
    }
}

I know that the data-type byte can hold data of the range -2^(8-1) to -1+2^(8-1). But I'm using 23 & 44, so it's less than 127.

Here I got error message "incompatible types: possible lossy conversion from int to byte".

3
  • And if you were adding a=127 and b=127 would the result fit in one byte? Commented Feb 20, 2016 at 4:51
  • @NawinMandal Use Alt+Enter keywords, it helps you to show you a way. In your case I just copied your code and cast your arithmetic operation before running that application. Commented Feb 20, 2016 at 5:07
  • alternative: a += b; b = (byte) (a-b); a -= b; -- compound assignments (like +=) automatically do the casting -- the second calculation can be replaced by b -= a; b *= -1; or b += a-2*b; to make it even more unclear, I mean, make it not using casts -- I would still prefer using a temporary variable, instead of a bunch of operations just to save one stack position Commented Aug 1 at 18:16

4 Answers 4

4

If you want to perform an arithmetic operation on byte and assign it back to a byte variable you should explicitly let the compiler know that "you know what you're doing" otherwise you'll get the the error that you're losing information by converting int (the outcome of the arithmetic operation) to byte (on the left side).

To fix this, cast the outcome of the arithmetic operation back to byte:

    byte a = 23, b = 44;
    a = (byte) (a + b);
    b = (byte) (a - b);
    a = (byte) (a - b);
Sign up to request clarification or add additional context in comments.

Comments

2

Simply, Cast the result of your arithmetic operation to byte as follows:

public class Exchange {
    public static void main(String[] args) {
    //int a = 23, b = 44;
    byte a = 23, b = 44;
    a = (byte) a + b;
    b = (byte) a - b;
    a = (byte) a - b;
    System.out.println("a=" + a + "b=" + b);
    }
}

Tip:- Use Alt+Enter for hints

3 Comments

stackoverflow.com/questions/14125843/… explains exactly why the widening on arithmetic occurs.
@user508633, that link is not working for me! It's maybe helpful, but I found convoluted.
@NawinMandal doing "alt-enter" without understanding the meaning of what you're doing is a bad habit for a developer. Pro tip: take the time to go over the link in the first comment, make sure you understand what widening means otherwise, you're doomed to run into nasty bugs in the future...
2

Corrected code is here

 public static void main(String[] args) {
    //int a = 23, b = 44;
    byte a = 23, b = 44;
    a = (byte) (a + b);
    b = (byte) (a - b);
    a = (byte) (a - b);
    System.out.println("a=" + a + "b=" + b);
}

Comments

0

Because, in java all the arithmetic expressions are by default treated as "int" data type, that means whatever the expression you write in RHS will be promoted to "int" type, unless it contains even more wider type data, so implicit conversion from int to byte is not possible, you need to cast explicitly or use matching data type.

byte a = 23, b = 44;
a = (byte) (a + b);
b = (byte) (a - b);
a = (byte) (a - b);

2 Comments

I would remove the all from "all the arithmetic expressions" (for example when any long, float or double variable is involved, it is not treated as int {not mentioning String, since that would not be arithmetic} )
Yes I agree, I also mentioned "unless it contains even more wider type data"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.