This question already has answers here:
concatenating string and numbers Java
(7 answers)
Closed 6 months ago.
I came across this question, and I can vaguely tell it seems to be handling the number part as: A + B * C --> A * B + B * C
similarly, there are 3 more examples
1)
jshell> System.out.println("Sum is: " + 3 + 10*0.008);
Sum is: 30.08
jshell> System.out.println("Sum is: " + 33 + 10*0.008);
Sum is: 330.08
jshell> System.out.println("Sum is: " + 33 + 1000*0.8);
Sum is: 33800.0
According to String Concatenation, shouldn't it be:
is "Sum is: 3.8"
is "Sum is: 33.08"
is "Sum is: 33800"
but it's not.
I remember we can even easily turn numbers into String by simply doing "" + number; and if it was "" + number + number --> would be ""numbernumber, not ""2number
How are these results produced exactly?
This is a problem of associativity. + is left associative. a + b + c is ((a + b) + c).
Similarly, a + b + c * d is
((a + b) + (c * d))
not
(a + (b + (c * d)))
You can treat (c * d) as "one term" here because * has higher precedence than +.
Normally this doesn't make much difference if a, b, c are numbers, but in your case
("Sum is: " + 3) + (10 * 0.008)
The first + operates on a String and int, so it does string concatenation, which makes the first parenthesised expression a string. This makes the second + also do string concatenation.
On the other hand, if it were
("Sum is: " + (3 + (10 * 0.008)))
The second + operates on an int and a double, so it does numeric addition.
I was asked in interview what will happen if below statement is compiled in java.
int x= 1+ +2+ +3+ +4+ +5;
It returns 15.
Can anyone explain how this goes?
+ and - can both also be unary operators, so 1 + +5 is the same as 1 + 5.
So expression would be translated into
int x = 1 + (+2) + (+3) + (+4) + (+5)
Trying to make a simple type math game for my 4 year old. Having issue with my first operation. I am trying to generate to random numbers then have number 1 and number 2 displayed for him to answer. Problem is println text - first number text - second number - text
Basic syntax i am trying to get to work:
int SIDES = 6;
int a = 1 + (int) (Math.random() * SIDES);
int b = 1 + (int) (Math.random() * SIDES);
System.out.println("What is " +a "+" +b "=");
That last line is not correct. Please help.
You are missing an addition operator for your String in println:
System.out.println("What is " + a "+" + b "=");
// ^ Missing '+' operator
So you should change it to this:
System.out.println("What is " + a + "+" + b "=");
This question already has answers here:
Java String Concatenation with + operator
(5 answers)
Closed 7 years ago.
what is the explanation of this precedence in strings in java?
public class PrecedenceInStrings {
public static void main(String[] args){
int x = 3;
int y = 5;
String s6 = x + y + "total";
String s7 = "total " + x + y;
String s8 = " " + x + y + "total";
System.out.println(s6 + "\n" + s7 + "\n" + s8);
}
}
output:
8total
total 35
35total
Java compiler processes operators + in your expressions left to right. When it comes to the first + in
x + y + "total"
it sees ints on both sides, so it performs an addition. When Java compiler processes the second +, it sees an int and a String, and interprets the operator as string concatenation.
In your second and third expressions the left-hand side of the + operator is a string, so all operators get interpreted as concatenations.
If you want to force a specific order of operations, use parentheses. For example, if you would like to get the total in your third example, parenthesize the addition, like this:
String s8 = " " + (x + y) + "total";
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
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 (+).
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
"*" 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
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.
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.
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.
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
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"
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