Strange behaviour of the increment operators in Java? - java

I have to pieces of code:
int m = 4;
int result = 3 * (++m);
and
int m = 4;
int result = 3 * (m++);
After the execution m is 5 and result is 15 in the first case, but in the second case, m is also 5 but result is 12. Why is this the case? Shouldn't it be at least the same behaviour?
I'm specifically talking about the rules of precedence. I always thought that these rules state that parantheses have a higher precedence than unary operators. So why isn't the expression in the parantheses evaluated first?

No - because in the first case the result is 3 multiplied by "the value of m after it's incremented" whereas in the second case the result is 3 multiplied by "the initial value of m before it's incremented".
This is the normal difference between pre-increment ("increment, and the value of the expression is the value after the increment") and post-increment ("remember the original value, then increment; the value of the expression is the original one").

The difference is when the result is assigned to m.
In the first case you have basically (not what it really does, but helps to understand)...
int result = 3 * (m=m+1);
In the second case you have
int result = 3 * m; m = m +1;

This is the definition of the operators: m++ evaluates to m, then increments m. It's a "post-increment". Parentheses around it don't change the fact that the operator evaluates to the variable, and also increments it afterward.

Think of it as "increment and get" and "get and increment." For instance, see AtomicInteger, which has the methods incrementAndGet() and getAndIncrement().

Related

How are y=x++ and y=x-- different when the assignment operators has the least priority?

I'm new to java.
Just found out that in the expression,
y=x++, y assumes the value of x and x becomes +1.
Forgive me if I sound stupid, but according to the order of precedence, assignment operators is at the end. So isn't x++ supposed to happen first followed by the assignment. Thanks in advance.
Q: So isn't x++ supposed to happen first followed by the assignment.
A: Yes. And that is what happens. The statement y = x++; is equivalent to the following:
temp = x; // | This is 'x++'
x = x + 1; // | (note that 'temp' contains the value of 'x++')
y = temp; // This is the assignment.
But as you see, the order of the operations (++ and =) doesn't affect what the operations actually do.
And therefore ...
Q: How are y=x++ and y=x-- different when the assignment operators has the least priority?
A: They aren't.
Operator precedence and evaluation order are two different concepts and they're not related.
If you have a() + b() * c(), that doesn't mean that b() and c() get invoked first because * has a higher precedence than +. a() still gets evaluated first. Evaluation order is typically left-to-right unless stated otherwise.
The Java programming language guarantees that the operands of
operators appear to be evaluated in a specific evaluation order,
namely, from left to right.
https://docs.oracle.com/javase/specs/jls/se14/html/jls-15.html#jls-15.7
y=x++ is assigning the value of x to y and then x gets incremented. The x++ operation is called a post-increment.
Here's some code that you can run for illustration:
int x = 0;
System.out.println("Illustration of post-increment");
System.out.println(x);
System.out.println(x++);
System.out.println(x);
int y = 0;
System.out.println("Illustration of pre-increment");
System.out.println(y);
System.out.println(++y);
System.out.println(y);

Does promotion occur before increment in Java?

I've seen this question in OCA questions and need to know why it outputs 90 and not 100.
Here is the code:
int x = 9;
long y = x * (long) (++x);
System.out.println(y);
So, what I think this would do is, firstly, increment x (because that's what happens at first right?) and then it would do the type promotion and take left x which is 10, turn it into long and multiply those two longs. Right?
No. The operands of each operator are evaluated from left to right. Therefore the first operand of the * operator, x, is evaluated before the second operand (long) (++x). Therefore 9 is multiplied by 10.

Use of postfix/prefix expressions

I understand in prefix/postfix, there is no need to bother about precedence of operators.
But where do we use prefix/postfix expressions?
Are they internally converted by the compiler?
Is there a way I could write expression in prefix/postfix and the compiler evaluates it for me?
EDIT: Ff you're actually talking about postfix (RPN) notation, then no, the compiler uses infix notation for expressions. You could write an algorithm that converts an RPN expression into an infix expression, try this
Postfix and prefix operators are just like a binary operator, but they work on only one operand.
Postfix increment (the increment occurs after the variable is evaluated):
int x = 1;
int y = x++; // y = 1 but after the assignment occurs x = 2
int z = x; // z = 2
Prefix increment:
int x = 1;
int y = ++x; // y = 2 and before the assignment occurs x = 2
int z = x; // z = 2
The rules that apply to the increment operator also apply to the decrement operator.
Prefix NOT (inverts a boolean expression):
boolean a = true;
boolean b = !a; // b = false, a = true
I think I have understood the question correctly, but I'm not sure what you mean about the compiler "evaluating" them "for you".
I suspect that prefix and postfix operations and notations are mostly useful for theories these days, but they have been used "for real". See e.g. https://en.wikipedia.org/wiki/Stack_machine, https://en.wikipedia.org/wiki/Forth_%28programming_language%29, http://linux.about.com/library/cmd/blcmdl1_dc.htm, and https://en.wikipedia.org/wiki/Reverse_Polish_notation#Hewlett-Packard
Few languages use postfix notation (the only one I know of would be PostScript), but the resulting CPU instructions would be the same as for infix notation:
a = (3 + 4) * 5
or
a = * + 3 4 5
Would be converted (in an imaginary, very simple, instruction sets):
add r0 3 4 # r0 = 3 + 4
mul r1 r0 5 # r1 = r0 * 5
Your CPU does not care about the initial notation, all it accepts is a set of « basic » instructions, so the compiler has to convert the notation you use to instructions understandable by your CPU.

operation on post++ and --pre operator

I am wondering with post and pre increment and decrement operation.
what I know in Java precedence of post operator is high and associativity is left-to-right.while associativity of pre operator is right-to-left
Oracle Java Tutorial
but my code showing me undesired result-
public class Bunnies {
static int count = 3;
public static void main(String[] args) {
System.out.println(--count*count++*count++);//out put is 12 expected 48
//associativity of post is higher so should be evaluated like this-
//--count*3**count++ count is 4 now
//--count*3*4 count is 5 now
//4*3*4=48
count = 3;
System.out.println(--count*++count*++count); //out put is 24 expected 120
//associativity of pre is right to left so should be evaluated like this-
//--count*++count*4 count is 4 now
//--count*5*4 count is 5 now
//4*5*4=120
count = 3;
System.out.println(-- count*count++);// out put is 4 expected 9
//--count*3 count is 4 now
//3*3=9
}
}
Order of evaluation of subexpressions is independent of both associativity and precedence.
The subexpression of the multiplication are evaluated from left to right, so when doing --count*count++*count++, you evaluate --count then count++ and finally count++.
And as the pre operator is evaluated first, --count will be decremented before its evaluation. In the same way, as the post operator is evaluated lately, count++ will be incremented after its evaluation.
The precedence only help the compiler to create a correct abstract syntactic tree.
For example, when doing ++count*2, the compiler use the precedence to know the expression is (++count)*2 and not ++(count*2). In the same way, when doing ++count*count--, the expression is (++count)*(count--) and not (++(count * count))-- or whatever. But then, during the evaluation of the multiplication ++count is evaluated before count--.
Hope this help you :)
I just found a great answer about expression evaluation in C# and Java here, enjoy :)
System.out.println(--count*count++*count++);
= 2 * 2 * 3 = 12
count = 3;
System.out.println(--count*++count*++count)
= 2*3*4 = 24
count = 3;
System.out.println(-- count*count++);
= 2 * 2 = 4
Pre increment/decrement
++/-- X first increments/decrements then does the operation.
Post increment/decrement
X ++/-- first the operation is done, then increment / decrement.
I take the first one.
System.out.println(--count*count++*count++);//out put is 12 expected 48
2 * 2 * 3 = 12
pre * post * post
count = 3:
Example 1:
--count*count++*count++ equals (--count)*(count++)*(count++)
(--count) = 2
(count++) = 2 (you increment it AFTER you do something with it)
(count++) = 3 ... count was incremented from before
2*2*3 = 12
Example 2:
--count*++count*++count equals (--count)*(++count)*(++count)
--count = 2
++count = 3
2 * 3 * 3 = 24
Example 3:
(--count)*(count++)
--count = 2
2 * 2 (the count++ gets changed afterwards)
Keep in mind that you have to watch the multiplicaton operator

What is the difference += and +?

I was asked a question what the difference is between having counter += 5 and counter + 5 in the java programming language. I said they essentially do the same thing but I did not know how to explain why. I felt one was considered a shorthand representation of the same problem but now thinking about it more, I feel I am not correct. Can anyone give me a simple explanation the difference between them?
counter += 5 modifies counter. counter += 5 can be used as a statement (e.g. line of code) on its own, because it does something (increments counter by 5).
counter + 5 does not modify anything. counter + 5 can only be used as an expression within a statement, because it doesn't do anything on its own.
Here is some code that demonstrates the difference:
int counter = 1;
System.out.println(counter + 5); // 6
System.out.println(counter); // 1
// counter + 5; // not a valid statement
counter += 5; // counter is now 6
System.out.println(counter); // 6
System.out.println(counter += 5); // 11
System.out.println(counter); // 11
counter += 5 is assigning the value of whatever counter was plus 5 to the counter variable while counter+5 is return the result of 5 plus counter but the counter variable stays the same.
+= operator
For example you have a a variable counter that equals 3. When you do counter += 5, you are actually assigning a new value to the variable counter. so if counter was 5 and you do counter+=3, counter will now equal 8.
+ operator without the equal
In this case if your counter equals 3. when you do counter+3 it will return 8 for that instance but your counter will still be 3.
Code to demonstrate the differences:
int counter = 3;
counter += 5;
int y = 0;
/*this will return 8, since the result of 5 and counter was newly assigned to
counter*/
System.out.println(counter);
//resetting counter value.
counter =3;
y = counter+5;
//here counter still remains at 3, because there wasn't anything assigned to it
System.out.println(counter);
//will return 8, because you assigned the result of counter and 5 to y.
System.out.println(y);
Only the JLS holds the true answer!
(Assuming count is a numeric type. If it's a String, for example, then everything everyone told you above is wrong at the time I wrote this.)
+= operator
JLS 15.26.2 Compound Assignment Operators:
A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.
following,
[...] the saved value of the left-hand variable and the value of the right-hand operand are used to perform the binary operation indicated by the compound assignment operator.
[...] the result of the binary operation is converted to the type of the left-hand variable, subjected to value set conversion (§5.1.13) to the appropriate standard value set (not an extended-exponent value set), and the result of the conversion is stored into the variable.
(emphasis mine). In the above above notation, E1 and E2 will perform the operation indicated by += (meaning E1 + E2). The result is stored in E1.
+ operator
JLS 15.18.2. Additive Operators (+ and -) for Numeric Types:
The binary + operator performs addition when applied to two operands of numeric type, producing the sum of the operands.
Note that there is no assignment here.
counter += 5 is the one that is shorthand, but it's shorthand for counter = counter + 5. counter + 5 is just an expression, what you get is a value that is 5 greater than the value of counter, but this value is just left behind and nothing is done with it. In order for something else to happen, an additional operator needs to be present, such as the =, or assignment operator. This operator takes an expression on the right hand side and an identifier on the left, evaluating the expression and assigning the result to the identifier. Without assignment, values don't change, and even when you use methods that seem to change values without assignment (String.append() for example) there is an assignment hidden in the code of the function. As an added fact, counter += 5 can be reduced (if you really want to call it reducing it) to counter++ used 5 times.

Categories