All Exams Test series for 1 year @ ₹349 only
Question

What will be the output of the following Java program?

class test

{public static void main (String args [ ])

{

System.out.println(20 + 30 + "Java");

System.out.println("Java" + 20 + 30);

}

}

The correct answer is

50Java

Java2030

Analyzing the Output of a Java Program with String Concatenation

The given Java program demonstrates how Java handles the '+' operator when combining numbers and strings. The '+' operator in Java performs both arithmetic addition and string concatenation.

Here is the code snippet provided:

class test{
    public static void main (String args [ ]){
        System.out.println(20 + 30 + "Java");
        System.out.println("Java" + 20 + 30);
    }
}
    

Step-by-Step Explanation of the Program's Output

Let's analyze each line of output generation:

First `System.out.println` Statement: `System.out.println(20 + 30 + "Java");`

  • In Java, operators have precedence. The '+' operator is evaluated from left to right.
  • The first operation is `20 + 30`. Since both operands are integers, arithmetic addition is performed.
  • \(20 + 30 = 50\)
  • The expression now becomes `50 + "Java"`. One operand (`50`) is a number, and the other (`"Java"`) is a string. When the '+' operator encounters a string, it performs string concatenation.
  • The number \(50\) is converted into its string representation `"50"`.
  • Then, `"50"` is concatenated with `"Java"`.
  • The result of this concatenation is `"50Java"`.
  • This string `"50Java"` is printed to the console.

Second `System.out.println` Statement: `System.out.println("Java" + 20 + 30);`

  • Again, the '+' operator is evaluated from left to right.
  • The first operation is `"Java" + 20`. Since the first operand (`"Java"`) is a string, the '+' operator performs string concatenation.
  • The number \(20\) is converted into its string representation `"20"`.
  • Then, `"Java"` is concatenated with `"20"`.
  • The result is `"Java20"`.
  • The expression now becomes `"Java20" + 30`. One operand (`"Java20"`) is a string, and the other (`30`) is a number. String concatenation is performed again.
  • The number \(30\) is converted into its string representation `"30"`.
  • Then, `"Java20"` is concatenated with `"30"`.
  • The result is `"Java2030"`.
  • This string `"Java2030"` is printed to the console on a new line.

Summary of Output

Based on the analysis:

  • The first line of output is `50Java`.
  • The second line of output is `Java2030`.

The combined output will be:

50Java
Java2030
    

Comparing with Options

Let's compare our derived output with the given options:

Option Output Matches Analysis?
1 <p>50 Java</p><p>Java 20 30</p> No (spaces and second line calculation)
2 <p>20 30 Java</p><p>Java 50</p> No (calculations and spaces)
3 <p>2030Java</p><p>Java50</p> No (calculations)
4 <p>50Java</p><p>Java2030</p> Yes

The output matches Option 4.

Revision Table: Java Program Output and String Concatenation

Concept Description Example
Arithmetic Addition When '+' is used with two numeric operands, it performs addition. \(10 + 5 = 15\)
String Concatenation When '+' is used with at least one string operand, it joins the operands together as strings. Non-string operands are converted to strings first. "Hello" + "World" results in "HelloWorld"
"Value: " + 100 results in "Value: 100"
Operator Precedence and Associativity Determines the order in which operators are evaluated. '+' has left-to-right associativity. In a + b + c, if 'a' is a string, evaluation proceeds as (a + b) + c performing concatenation from the start. If 'a' and 'b' are numbers and 'c' is a string, evaluation proceeds as (a + b) + c performing addition first, then concatenation.

Additional Information: Understanding Java Operators

It's crucial to understand how Java operators work, especially the '+' operator which is overloaded to perform different functions based on the data types of its operands.

  • Arithmetic Operators: These include '+', '-', '*', '/', '%'. They work on numeric types (integers, floating-point numbers).
  • String Concatenation Operator: The '+' operator is used to join two or more strings. If one operand is a string, the other operand is automatically converted to a string before concatenation.
  • Evaluation Order: Expressions are evaluated based on operator precedence and associativity. In an expression with multiple '+' operators, evaluation typically happens from left to right. The type of the first operation often dictates the type of subsequent operations in a chain involving strings. For example, `number + number + string` performs addition first, then concatenation. `string + number + number` performs concatenation, then further concatenation.

This example clearly shows how the presence and position of a string operand change the behavior of the '+' operator in a chain of operations.

Was this answer helpful?

Important Questions from Java

  1. When an applet begins, which of the following sequence methods is CORRECT?

    1) start()

    2) paint()

    3) init()

  2. In Java, the 'PreparedStatement' interface is a sub interface which is used to execute __________ query.

  3. Which of the following is NOT a java primitive type?

  4. Which of the following is/are the rules to declare variables in Java?

  5. What is the use of 'javac' command?

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App