-2
class Example{
    public static void main(String[] args){
        byte b1=10,b2=20,b3;
        b3=b1+b2;
        b3=b1+1;
        b3=b1*2;
        short s1=10,s2=20,s3;
        s3=s1+s2;
        s3=s1+1;
        s3=s*1;
        int x1=10,x2=20,x3;
        x3=x1+x2;
        x3=b1+b2;
        x3=b1+1;
        x3=b1*2;
        x3=s1+s2;
        x3=s1+1;
        x3=s1*1;

    }
}

When I tried to compile it give errors. I'm a beginner at java. So I need some help from you. Thank you.

Rojo
  • 2,749
  • 1
  • 13
  • 34
  • 2
    Java and Javascript are two completely different languages. When tagging a question, use one or the other and read the description of the tag as well – Rojo Jan 22 '21 at 19:54
  • I ran your code in REPL and it seems the errors are related to incompatibility between bytes and int. Is there some reason you're using byte instead of int? – Russ J Jan 22 '21 at 19:57
  • I think you may need to just stick with int here and do away with the byte and short. – Russ J Jan 22 '21 at 19:59
  • https://stackoverflow.com/questions/5193883/how-do-you-specify-a-byte-literal-in-java – Russ J Jan 22 '21 at 19:59

2 Answers2

2

The JVM has no arithmetic instructions defined for bytes or shorts. Thus the answer of the arithmetic operations become an int. You would need to cast this to a byte or short if attempting to store it in one.

class Example{
    public static void main(String[] args){
        byte b1=10,b2=20,b3;
        b3= (byte)(b1+b2);
        b3=(byte)(b1+1);
        b3=(byte)(b1*2);
        short s1=10,s2=20,s3;
        s3=(short)(s1+s2);
        s3=(short)(s1+1);
        s3=(short)(s1*1);
        int x1=10,x2=20,x3;
        x3=x1+x2;
        x3=b1+b2;
        x3=b1+1;
        x3=b1*2;
        x3=s1+s2;
        x3=s1+1;
        x3=s1*1;

    }
}

The JVM spec clearly says:

The Java virtual machine provides the most direct support for data of type int. This is partly in anticipation of efficient implementations of the Java virtual machine's operand stacks and local variable arrays. It is also motivated by the frequency of int data in typical programs. Other integral types have less direct support. There are no byte, char, or short versions of the store, load, or add instructions, for instance.

dajavax
  • 3,060
  • 1
  • 14
  • 14
0

Many of these errors are happening due to the fact that they are converted when the math happens.

class Example{
public static void main(String[] args){
    byte s = 1;  // unsure of what you wanted s to be however it wasn't defined.  I just set it to 1.
    //I added casts to accommodate for the side of the variable that it needed to be. 
    byte b1 = 10, b2 = 20, b3;
    b3 = (byte) (b1 + b2);
    b3 = (byte) (b1 + 1);
    b3 = (byte) (b1 * 2);
    short s1 = 10, s2 = 20, s3;
    s3 = (short) (s1 + s2);
    s3 = (short) (s1 + 1);
    s3 = (short) (s * 1);
    int x1 = 10, x2 = 20, x3;
    x3 = x1 + x2;
    x3 = b1 + b2;
    x3 = b1 + 1;
    x3 = b1 * 2;
    x3 = s1 + s2;
    x3 = s1 + 1;
    x3 = s1 * 1;

}
}
Dale
  • 1,613
  • 4
  • 22
  • 42