问题
I'm just starting out in AP Comp sci in high school and I stumbled across a question regarding the + operator in strings
Why does
System.out.println ("number" + 6 + 4 * 5)
result in number620
whereas
String s = "crunch";
int a = 3, b = 1;
System.out.print(s + a + b);
System.out.print(b + a + s);
result in crunch314crunch?
Thanks
回答1:
Depends on It's precedence order
When two operators share an operand the operator with the higher precedence goes first. For example, 1 + 2 * 3 is treated as 1 + (2 * 3), whereas 1 * 2 + 3 is treated as (1 * 2) + 3 since multiplication has a higher precedence than addition (+).
回答2:
If you want to do any Math into System.out.println, wrap it with braces, because Java sees String at 1st place.
Try System.out.println ("number" + (6 + 4 * 5)).
For 2nd example use: System.out.print(s + (a + b));
in this case you have sum of a and b.
but in System.out.print(b + a + s); b and a stay at the 1st place. Compiler does a+b 1st and after add String, you don't need braces
回答3:
"*" has a higher operator precedence than "+", this means the expression "4 * 5" is calculated before the String concatenation happens.
See http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
回答4:
It has something to do with the operator precedence:
First, 4 * 5 = 20
Second, "number" is concatenated with 6, which is further concatenated with 20.
回答5:
On your first example, since you used a multiplicative operator, 4 is being multiplied to 5 before concatenated to other string.
For the second example, you started with 2 integer before the String which will be calculated first before concatenated to a String.
回答6:
Why does System.out.println ("number" + 6 + 4 * 5) result in number620
Because, * has higher precedence than +, so the result is number620.
String s = "crunch"; int a = 3, b = 1; System.out.print(s + a + b); System.out.println(b + a + s); result in crunch314crunch?
Here, + is used as operator overloading not as binary operation. So, '+' do concat operation, not sum operation. So, the result is crunch314crunch.
回答7:
It's about two things:
- operator precedence
- string concatenation vs addition
The + has the following rules:
int + int => int
int + string => String
String + int => String
String + String => String
That is, as soon as a String is involved, + means concatenation.
Operators with the same precedence are evaluated left to right. Therefore
String + int + int => String + int => String
but
int + int + String => int + String => String
The first case uses concatenation only, whereas the second uses addition in the first step.
In your first example, * has higher precedence than +, so the multiplication is performed first.
String + int * int => String + int => String
回答8:
It's all about operator precedence and their associativity.
Your first example: "number" + 6 + 4 * 5
Acc. to operator precedence * is calculated first, so it becomes "number" + 6 + 20
Now, Associativity for + is Left -> Right (L->R), so + becomes a concatenation operator cause it is used with String, so the expression becomes "number6" + 20, and then "number620"
(Actually the int are converted to String before concatenation)
Similarly, your 2nd example:
Only + operator and start execution from L->R
"crunch" + 3 + 1 = "crunch3" + 1 = "crunch31"
1 + 3 + "crunch" = 4 + "crunch" = "4crunch"
回答9:
According to your question and answer
explanation is
1.
A)System.out.println ("number" + 6 + 4 * 5);
B)System.out.println ("number6" + 4 * 5);
C)System.out.println ("number6" + 20);
D)System.out.println ("number620");
And it prints output like
number620
And Second one is
2.
A)System.out.print("crunch" + 3 + 1);
System.out.print(1 + 3 + "crunch");
B)System.out.print("crunch3" + 1);
System.out.print(4 + "crunch");
C)System.out.print("crunch31");
System.out.print("4crunch");
And it prints output with in a line, why because you have used print() statement
crunch314crunch
来源:https://stackoverflow.com/questions/18757312/operator-and-strings