What is the Output of this code segment
Explain the expected output of the following code segment?
public class Simplilearn
{
public static void main (String args[])
{
System.out.println(100 + 100 +“Simplilearn");
System.out.println(“E-Learning Company" + 100 + 100);
}
}
If you run the code segment you provided, the following will be printed to the console:
- "200Simplilearn"
- "E-Learning Company100100"
Here's why:
In the first line of code, you are using the "+" operator to concatenate (combine) three values: 100, 100, and "Simplilearn". The "+" operator has a higher precedence for arithmetic operations than for string concatenation, so the first two values (100 and 100) are added together to give 200. The result is then concatenated with the string "Simplilearn" to give the output "200Simplilearn".
In the second line of code, you are using the "+" operator to concatenate three values: "E-Learning Company", 100, and 100. Since the first value is a string, the "+" operator is treated as a string concatenation operator, and the other two values (100 and 100) are treated as strings as well. The result is the string "E-Learning Company100100".
I hope this helps! Let me know if you have any other questions.
Comments
Post a Comment