What will be the result of the following code compilation and execution?

public class Test {
    public static void main(String[] args) {
        byte a = 3;
        short b = 4;
        compute(a, b);
    }

    public static void compute(short x, short y) {
        System.out.println("Short: " + (x + y) + (x + y));
    }
}
Explanation
"Short: " + (x + y) + (x + y) is composed of two + operators that are left-associative. That means that this expression is equivalent to (("Short: " + (x + y)) + (x + y)). When the line is one of the arguments of the + operator, JVM performs a string concatenation. See. JLS 15.18.1

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback