-2
int number; 
number = 80; 

number = (int)math.sqrt(number++) 
System.out.println(number) 

This is the above code. I am new to programming and have a decent under standing of mathematics.

I am aware the SQRT is 8. However in this case i am confused why the answer is not 9.

I am aware of the ++VAR AND VAR++ and how it can affect the answer however given that the PLUS is above the print code i am confused why one is not added to the eight then the number 9 is printed.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
JAVAJoseph
  • 31
  • 9
  • `8.944...` becomes `8` when casted to `int`. This has nothing to do with the actual printing with `println()`. You are saving this value in the variable `number`, and **then** incrementing it. The increment never gets saved to `number`. – RaminS Oct 29 '16 at 13:15
  • Hello, Its joe, Thanks for helping me understand this concept. Regaurds, joe – JAVAJoseph Oct 30 '16 at 15:12

4 Answers4

3

The update ++ makes to number in the right-hand operand of the assignment is overwritten by assigning the result of the overall expression back to number.

This is covered by JLS§15.26.1. Here's what happens in that assignment expression:

  1. The left-hand side is determined to be a variable
  2. The right-hand side of the assignment expression is evaluated:
    1. The value 80 is read from number
    2. number is incremented to 81 (this doesn't matter)
    3. math.sqrt is called with the value 80 read above
    4. The result of math.sqrt is converted to an int
  3. The result from #2 above (8) is stored in number

Note the order of Steps 2.1 and 2.2 above. That's because you've used the postfix number++. The order would be reversed (and the result different) if you used ++number.

The ++ on number on the right-hand side has no effect whatsoever on the result of the expression. (In Java. In some other langauges, this would be considered Undefined Behavior allowing a compiler to do whatever it wants.)

In particular (since you mentioned expecting to get the value 9), the ++ doesn't get applied after the result of the assignment is stored in number. It gets applied immediately after reading the value of number when passing it into math.sqrt.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

number++ postincrements, meaning the expression is evaluated to 80 inside the square root function.

Square root of 80 is strictly less than 9 so its integer conversion (done with (int)) is 8.

Pierre Arlaud
  • 4,040
  • 3
  • 28
  • 42
1
number = (int)math.sqrt(number++) ,

Here First expression is evaluated,then number is increased, thats' why.

If you change it to

number = (int)math.sqrt(++number) ,

then it will work

Naruto
  • 4,221
  • 1
  • 21
  • 32
  • Hello, Thanks very much for your reply. This was an answer which confused me on a past paper for my Uni course. So i was mainly just concerned with why i got the result i did rather than fixing the script. However thanks a lot for your reply joe – JAVAJoseph Oct 30 '16 at 15:10
0

In java, post increment effects the variable when the expression is evaluated, not after the statement is run. So the variable gets incremented to 81, previous value 80 is passed to sqrt, and finally gets assigned 8.

Tamas Hegedus
  • 28,755
  • 12
  • 63
  • 97