0

Here is code from Java champs.

class Compound
{
    public static void main (String args[])
    {
        int k = 12;
        k /= --k;
        System.out.println(k);
        k *= k++;
        System.out.println(k);
        k *= ++k;
        System.out.println(k);
    }
}

The options given are

  • 1 It will compile successfully and display 1 followed by 2 and then 3 as an output.
  • 2 It will compile successfully and display 1 followed by 1 and then 2 as an output.
  • 3 It will compile successfully and display 1 followed by 3 and then 4 as an output.
  • 4 It will generate compile time error.

The output is option-2. Can someone explain, how come it evaluates to option-2?

msrd0
  • 7,816
  • 9
  • 47
  • 82
benz
  • 4,561
  • 7
  • 37
  • 68
  • 4
    Take it one step at a time. `12 / 11` is `1.09`, which is 1 when casted to int. Thats why `1` prints for the first statement. Do the rest of the calculations, and youll get your answer. `num++` will increase number and return the previous value (so the value it was at before incremenating), while `++num` returns the incremeanted value – Vince Nov 02 '14 at 19:52
  • 4
    Use a pen and paper. Work it out. Or step through it with a debugger. – Boris the Spider Nov 02 '14 at 19:52

3 Answers3

4

Things you need to know:

  • k(operator)=value is basically k=k(operator)value (more info here).
  • ++ is incrementation -- is decrementation. There exists pre, and post versions of this operators where:

    • x++ (post-incrementation) will first return current value of x then will increment x (if x=2 print(x++) would first pass 2 to method then increment x to become 3)
    • ++x (pre-incrementation) will first increment value of x then will return it
  • ++ -- have higher precedence than * / and these have higher precedence than =

  • dividing integers will also return integer so 7/3 will become 2 not 2.33333...
  • in argument1 (operator) argument2 (operator) argument3 argument1 will be evaluated before argument2, and argument2 will be evaluated before argument3 (in other words they are evaluated from left to right)
  • variable = expression before putting value of expression into variable it will need to be evaluated first, so x=x+1 first x+1 expression will need be calculated, then x can be set with result of this evaluation.

so you can rewrite your code as

k/=--k; -> k = k / --k; -> k = (k / (--k))
k*=k++; -> k = k * k++; -> k = (k * (k++))
k*=++k; -> k = k * ++k; -> k = (k * (++k))

So at start with k=12.

k = (k / (--k))
k = (12 / (--k)) lets replace first k with its current value
k = (12 / 11) pre-decrement --k will first decrement k to 11, then return decremented value k = 1 because we are dividing integers 12/11 result is 1 not 1.0909...

So now k=1. Time for k*=k++;

k = (k * (k++))
k = (1 * (k++))
k = (1 * (1)) post-increment k++ first returns current value of k which is 1 then increments it
k = 1 1*1=1, nothing special here

So k is still 1. Time for k*=++k;

k = (k * (++k))
k = (1 * (++k)) since k=1 k = (1 * (2)) ++k first increments then returns value, so k becomes 2 and this value is returned
k = 2 1*2 = 2 nothing special here.

Community
  • 1
  • 1
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • thanks so much. It helped me clarify the concepts. Though i have a question, when previous value is returned, where does temporary incremented value gets stored ? – benz Nov 03 '14 at 05:47
  • 1
    I assume that you are asking about `x++`. It mostly works like this: current value of `x` is passed to processor (or in case of `print(x++)` to local variable of `print` which is separate than used `x`) and new (incremented) value is stored in `x` variable. – Pshemo Nov 03 '14 at 15:03
2
int k=12;
k/=--k;

//i.e. k=k/--k; i.e. k=12/11; i.e k=1

System.out.println(k);
k*=k++;

/*i.e. k=k*k++; i.e. k=1*1; and after this statement value of k is still 1,
  inspite of k++,

  since postfix version increase k by 1 after evaluation of k*k++, 
  but just after that 1 is assigned to k overwriting its previous value.

*/

System.out.println(k);

k*=++k;
//i.e. k=k*(++k); i.e. k=1*2;

System.out.println(k);
AsSiDe
  • 1,826
  • 2
  • 15
  • 24
1
int k = 12;
k /= --k;
System.out.println(k);
k *= k++;
System.out.println(k);
k *= ++k;
System.out.println(k);

The first equation is 12 / 11. --k will decrease k, and return the decreased value (which is 11). 12 / 11 is 1.09. When casted to int (happens implcitly), the decimal places are omited, and you're left with 1. The 1 gets printed to the console.

The second equation is 1 * 1. k++ incremeants k, then returns the previous value (the value it was before the incremeant, so 1). 1 gets printed to the console; now you know why you have the first two 1s printing out.

The final equation is 1 * 2. By now, you should know how incremeanting works. The answer is printed to the cosole. Thats why the answer is option #2

Vince
  • 14,470
  • 7
  • 39
  • 84