-1

Can anybody explain it to me why the value of n1 is not increasing by 1 in this case, or in other words why the mathematical operation isn't showing 1.

package com.company;

public class Main {

    public static void main(String[] args) {

        System.out.println(isNumber(20, 21));
    }
    public static double isNumber (double n1, double n2){

        double calc = n1++ / n2;
        return calc;
    }
}
deceze
  • 510,633
  • 85
  • 743
  • 889
Shpend Palushi
  • 597
  • 8
  • 21
  • 1
    it's the post-increment operator, which increases the value after returning the result. `++n1` would return an incremented number in the calculations. – Rogue Jul 24 '17 at 13:06
  • 1
    You have tagged this with [tag:post-increment]… you do understand what the "post" in "post-increment" means as opposed to *pre-increment*…? – deceze Jul 24 '17 at 13:07
  • 3
    Possible duplicate of [Pre & post increment operator behavior in C, C++, Java, & C#](https://stackoverflow.com/questions/6457130/pre-post-increment-operator-behavior-in-c-c-java-c-sharp) – Alex Shesterov Jul 24 '17 at 13:07
  • 1
    just print n1 before returninc calc an you will see that it really is incremented. However, n1 isn't returned so the change is unnoticed – Quickbeam2k1 Jul 24 '17 at 13:07
  • i bet you get calc= `20/21`. If yes think about the what the name of that operator **post**-increment could mean ;) – Davide Spataro Jul 24 '17 at 13:08
  • @DavideSpataro: I'd rather see that written as `20.0 / 21`, or `20.0 / 21.0`. – Bathsheba Jul 24 '17 at 13:11
  • @Bathsheba you're right, it makes a hell of a difference! – Davide Spataro Jul 24 '17 at 13:20

2 Answers2

2

The result will be a little less than 1.

n1++ / n2 is evaluated as 20.0 / 21.0 with the side-effect of n1 being increased by 1, meaning that n1 will then be exactly 21.0 as incrementing a floating point double by 1 from 20.0 is exact. This will have no effect on the value of n1 in the caller since parameters are passed by value in Java.

Did you want ++n1 / n2?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

The value is increased, but after the execution of line double calc = n1++ / n2;.

That happens because you used a post increment operator.

public class Main {

    public static void main(String[] args) {
        System.out.println(isNumber(20, 21));
    }

    public static double isNumber (double n1, double n2){
        double calc = n1++ / n2;
        System.out.println(n1); // Prints 21
        return calc;
    }
}

Your code is equivalent to

    public static double isNumber (double n1, double n2){

        double calc = n1 / n2;
        n1 = n1 + 1;
        return calc;
    }

If you need to apply the increment before the division, you need to apply the preincrement operator as follow:

double calc = ++n1 / n2;

Using the preincrement operator the code is equivalent to:

public static double isNumber (double n1, double n2){
    n1 = n1 + 1;
    double calc = n1 / n2;
    return calc;
}
Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56