0

I missed this question three times on a test. I just can't seem to grasp how to solve this. Any help is much appreciated.

    public class Test {
         public static void main(String[] args) {
             int j = 0;
             int i = ++j + j * 5;

             System.out.println("What is i? " + i);
        }
    }
icktoofay
  • 126,289
  • 21
  • 250
  • 231
  • 1
    Have you tried running the code to se what value i will have? – Vini.g.fer Aug 30 '15 at 03:21
  • 4
    The reason it returns 6 is that `++j` is [pre-increment](http://stackoverflow.com/questions/2371118/how-do-the-post-increment-i-and-pre-increment-i-operators-work-in-java) which increases the value of `j` to 1, and it does it "pre" calculating this line so that when this line is calculated, the value of `j` is already 1. And 1 + 1 * 5 = 6. – Nir Alfasi Aug 30 '15 at 03:21
  • 2
    The real lesson: never write this kind of code. – dnault Aug 30 '15 at 04:21
  • Thanks alfasin - makes sense now! – Abner Romero-Salceda Sep 02 '15 at 21:39

6 Answers6

3

For:

int i = ++j + j * 5;

The variable j is per-incremented by 1 with the expression ++j, that is equal to j+1, changing the value of j to 1. Then following the rules for the order of operations in java, the multiplication is executed (j * 5) or (1 * 5) at this stage, so currently i = 5. Finally to the product of the multiplication j is added (j + 5) or (1 + 5) = 6.

You can check this page where this is explain in a simple way:

http://introcs.cs.princeton.edu/java/11precedence/

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
Towerss
  • 629
  • 2
  • 12
  • 26
1

As per Java's operator precedence and evaluation order, i is equal to 6. i is set equal to j which is pre-incremented to 1, and added to the product of itself and 5, which is 6.

Nooble
  • 562
  • 7
  • 17
0

The thing to note here is the difference between j++ and ++j. See this: The difference between ++Var and Var++

Keeping this in mind, ++j will return 1, and change j's value to 1. So the next part, j*5 is equal to 1*5, which is 5. So overall ++j + j*5 gives you 6.

Community
  • 1
  • 1
Zarwan
  • 5,537
  • 4
  • 30
  • 48
0

Java has an operator precedence rule which means for any mathematical expression in Java which contains operators, postfixes(j++) and prefixes(++j) are evaluated first. There is also left and right associativity which is how your statement is parsed. Mathematical operators such as +,-,/,* have left to right associativity. Postfixes and Prefixes have right to left associativity.

Looking at the Java Grammar File one finds that

The value of the prefix increment expression is the value of the variable after the new value is stored

So, ++j is 1 for the current statement.

The value of the postfix increment expression is the value of the variable before the new value is stored.

So, j++ is 0 for the current statement.

So, (++j) + j * 5 after prefix evaluation becomes (1 + (1 * 5)) = 6.

[Edit]

Thus, statement such as j++ + j * 5 will give you 5 as an answer because j becomes 1 after j++(postfix increment) but j++ itself remains 0.

Community
  • 1
  • 1
0

if ++j appears in the statement, the value of j is first incremented and then the statement is executed. But if j++ appears in the statement, first statement is executed and then the value is incremented.

To simply solve execution part, you can ignore ++ sign and simplify it. But don't forget the order of increment and simplification.

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
0

According to java operator precedence, ++ operator has higher precedence than + and * operators. Here's what happens, step by step:

First ++j happens. Since j is 0, ++j increments j to 1. Now the expression ++j + 5 * j has become 1 + 5 * 1.

Now we have two operators, + and *. Which one is applied first? See the operator precedence chart and you will find out that * has higher precedence than + . So first 5 * 1 takes place first. After multiplication the expression looks like 1 + 5.

Adding 1 to 5, we get 6. So....i = 6 is the result.

If u want to know more about ++ operator, there are some decent videos on youtube.

Wololo
  • 841
  • 8
  • 20