Adding Byte and Short

32 Views Asked by At

I added a Byte variable to a Short variable, however I had to add (Short) for the line of code to compute. I thought this was not needed as I'm adding a smaller data type to a bigger one. Why is this?

What i thought was correct.

        byte NumberOfShoes = 5;
        short Laces = 10;
        short ShoePacks = NumberOfShoes + Laces;
        System.out.println(ShoePacks);

What was correct.

        byte NumberOfShoes = 5;
        short Laces = 10;
        short ShoePacks = (short) (NumberOfShoes + Laces);
        System.out.println(ShoePacks);
0

There are 0 best solutions below