I am trying to figure out why the following code gives two different results
I tried the followingL
int x = 4, y = 4;
System.out.println(x + --x);
System.out.println(--y + y);
And It outputs 7 6. From my knowledge, preincrement has a higher precendence than addition so it should decrease the value of x/y regardless of it's value in the expression but this is clearly not the case. Can anyone please explain this to me?
System.out.println(x + --x);
System.out.println(--y + y);
This can be re-written as:
x + --x => x + (x = x-1) => 4 + 3 = 7
--y + y => (y = y-1) + y (y here is already decreased in value) = 3 + 3 = 6
Related
Consider the following expressions:
int x = 5, y = 5, z;
z = y + x * y-- + ++x;
According to my calculations the value of z should be 40 considering ++ and -- have higher precedence than +.
So z = 4 + 6 * 5 + 6 = 40.
Running the code yields a result of z = 36. How does Java arrive at that result?
Also, what role does associativity (right to left) play here?
You must interpret it from left to right, so :
so z = y(5) + x(5) * y--(still 5, since the decrement will apply afterwards) + ++x(which is 6 because the increment applies before the evaluation of x)
The result being 36.
Operator precedence tells you how to parse the expression. It doesn't tell you anything about evaluation order.
There are two ways to look at this, you can either look at it as implicit braces, or as how the AST looks like.
If you look at it through the PoV of using braces, you end up with this:
z = y + x * y-- + ++x;
<=> z = y + (x * (y--)) + (++x);
<=> z = 5 + (5 * 5) + 6 // y-- is still 5 (4 afterwards), ++x is 6
<=> z = 5 + 25 + 6
<=> z = 36
If you look at it through the AST lens, you get
+
/ \
y +
/ \
* x++
/ \
x y--
Now if you go through that AST in evaluation order (left-right), you see that the leftmost y is still 5. On the right tree, we have again a +, which is the result of the multiplication and ++x. The multiplication is 5 * 5, since y-- is still 5 at that point, and y is 4 afterwards. And finally, ++x is 6 and x is 6 afterwards. Putting together, 5 + 25 + 6 = 36.
The AST is always evaluated in-order, operator precedence tells you how the AST looks like (stronger binding operators at the bottom, weaker binding one at the top).
int x = 5, y = 5, z;
z = y + x * y-- + ++x;
In this example var-- and var++ will be executed after z. And --var and ++var will be executed before z. In your case
z = 5 + 5 * 5 + 6 = 36
If you rewrite your expression to z = y + x * --y + ++x it will be z = 5 + 5 * 4 + 6 = 40.
If you rewrite your expression to z = y + x * y-- + x++ it will be z = 5 + 5 * 5 + 5 = 35.
This question already has answers here:
Java: sum of two integers being printed as concatenation of the two
(10 answers)
Closed 5 years ago.
This is my java code
public class exercise {
public static void main(String[] args) {
int x = 8;
int y = 4;
System.out.println("x + y = " + x + y);
System.out.println("x * y = " + x * y);
System.out.println("x + x + y =" + x + x + y);
double z = x / y;
System.out.println("z = " + z);
}
}
it's supposed to look like this:
x + y = 12
x * y = 32
x + x + y = 20
z = 2.0
but when I run it with eclipse this is the result I get:
x + y = 84
x * y = 32
x + x + y =884
z = 2.0
As you can see 8 + 4 definitely != 84
As well as 8 + 8 + 4 != 884
It looks like the eclipse typed the values 8 and 4 in the first line, and didn't add them together, the same thing with the third line it just typed 8 and 8 and 4, not adding them together.
Do you have any idea how to fix this problem?
You need to put brackets around arithmetic operation
System.out.println("x + y = " + (x+y));
System.out.println("x * y = " + (x*y));
System.out.println("x + x + y ="+( x+x+y));
I have this piece of code in JAVA 8:
int x=3;
int y = --x + x++ + --x ;
System.out.print("y: " + y + " x: "+x);
As I understand it should be split like this:
y = 2 + x++ + --x; x = 2
y = 2 + 2 + --x; x = 2
y = 2 + 2 + 1; x = 1
y = 5; x = 2
And it prints y: 6 x: 2
What is the order of operation in here?
It's because of the way pre and post increments work, it gets evaluated like this:
1. y = --x + x++ + --x ;
2. y = 2 + (2)++ + --(3);
3. y = 2 + 2 + 2;
4. y = 6
After 2 decrements and 1 increment, x becomes 2.
I have problem with JAVA code, that is I have a code:
class test {
public static void main(String[] args){
int x = 0;
int y = 0;
while( x < 5 ){
y = y - x;
System.out.print(x + "" + y + " ");
x += 1;
}
}
}
and I compile it in Command Prompt and I get this:
00 1-1 2-3 3-6 4-10
Where I expected output to be:
00 11 23 36 210
I don't know where the problem is.
Thanks for any responses.
The - in y = y - x; means subtraction.
Most likely you intended
y = y + x;
or
y += x;
Also most likely you expected.
00 11 23 36 410
the hyphen you see is not a hyphen but rather minus sign as y eventually becomes negative (based on ur code)
one way of doing it is by changing printout to be a positive value:
System.out.print(x + "" + y + " ");
to:
System.out.print(x + "" + Math.abs(y) + " ");
Your y is producing negative values:
Your code:
int x = 0;
int y = 0;
while( x < 5 ){
y = y - x;
System.out.print(x + "" + y + " ");
x += 1;
}
Let's try to debug it:
first iteration y = 0-0 = 0
second iteration y = 0-1 = -1
third iteration y = -1-2 = -3
fourth iteration y = -3-3 = -6
fifth iteration y = -6-4 = -10
As you can see, you're basically going opposite to your expected result which is a good indication that you're using an opposite operator on some variable, in this case y-x should be y+x.
Change y = y-x to y = y+x
This question already has answers here:
Ternary operator, syntax error when using assignment
(4 answers)
Closed 8 years ago.
I have the following piece of code. This is how I understand it.
In the first case, the ternary operator returns the value of y because x=4 and the print statement prints 5, as expected.
In the 2nd case, the ternary operator first assigns the value of y to x and then returns that value. Again, it prints 5, as expected.
In the 3rd case, the ternary operator has x=y to the left of the : and x=z to the right of the :. I would expect this to behave much like the 2nd case. However, this statement does not even compile.
Any help in understanding this will be much appreciated.
public class Test {
public static void main(String[] args) {
int x = 4;
int y = 5;
int z = -1;
x = (x == 4) ? y : z; // compiles and runs fine
System.out.println(x + " " + y + " " + z);
x = (x == 4) ? x = y : z; // compiles and runs fine
System.out.println(x + " " + y + " " + z);
x = (x == 4) ? x = y : x = z; // Does not compile
System.out.println(x + " " + y + " " + z);
}
}
Assignment has lower precedence than a ternary expression, so this expression:
(x==4)?x=y:x = z;
can be thought of as:
((x==4)?x=y:x) = z;
Which obviously won't compile because you can't assign a value to something that isn't a variable.
Add parenthesis to control the order of evaluation
x = (x == 4) ? (x = y) : (x = z); // Does compile.
Note the above is equivalent to
if (x == 4) {
x = (x = y);
} else {
x = (x = z);
}
Which will (as a side effect) of assigning a value to x assign the value assigned to x to x. In other words, your ternary is equivalent to
x = (x == 4) ? y : z;
or
if (x == 4) {
x = y;
} else {
x = z;
}
The ternary is specified in JLS-15.25. Conditional Operator ? :.