What will be displayed, if the following code is compiled and executed:

public class Main {
    public static void main(String args[]) {
        System.out.println("2 + 2 = " + 2 + 2);
Actions are executed in order from left to right. Due to the fact that the string is the first operand, the concatenation is performed.
In order to get a mathematically correct example, you can modify the order of operations execution with the help of parentheses:
System.out.println("2 + 2 = " + (2 + 2));

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Sign Up Now
or Subscribe for future quizzes