Java Reference
In-Depth Information
This expression evaluates as follows:
"The sum = " + (12 + 16)
= "The sum = " + 38
= "The sum = 38"
Next, consider the expression:
12 + 26 + " is the sum"
This expression evaluates as follows:
12 + 26 + " is the sum"
= (12 + 26) + " is the sum"
= 38 + " is the sum"
= "38 is the sum"
Now consider the expression:
"The sum of " + 12 + " and " + 26 + " = " + (12 + 26)
Notice the parentheses around 12 + 26 . This expression evaluates as follows:
"The sum of " + 12 + " and " + 26 + " = " + (12 + 26)
2
=
"The sum of 12" + " and " + 26 + " = " + (12 + 26)
=
"The sum of 12 and " + 26 + " = " + (12 + 26)
=
"The sum of 12 and 26" + " = " + (12 + 26)
=
"The sum of 12 and 26 = " + (12 + 26)
=
"The sum of 12 and 26 = " + 38
=
"The sum of 12 and 26 = 38"
The following Java program shows the effect of the preceding statements:
// This program illustrates how the String concatenation works.
public class Example2_10
{
public static void main(String[] args)
{
System.out.println("The sum = " + 12 + 26);
System.out.println("The sum = " + (12 + 26));
System.out.println(12 + 26 + " is the sum");
System.out.println("The sum of " + 12 + " and " + 26
+ " = " + (12 + 26));
}
}
Search WWH ::




Custom Search