Machine-translated by Google. Questions are authored in English.
Top

Discussion

What will be the output of the program?

public class Fragment74
{
    static void show()
    {
        System.out.println("10" + 20 + 30);
        System.out.println(10 + 20 + "30");
    }

    public static void main(String[] args)
    {
        show();
    }
}

  • A.102030 then 102030
  • B.The program prints 102030 then 3030
  • C.102030 then 1020 30
  • D.The program prints 60 then 60

Answer: B

Addition is left associative. The first line starts with a String, so every following value is joined as text, giving 102030. The second adds 10 and 20 as ints first and only then meets the String, giving 3030. Same operands, different order, different answer.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.