0

When i am trying to subtract a smaller negative value from a bigger negative value ,int type is returining a positive value.

can anyone explain why?

Here is the code:

public class Test {

public static void main(String[] args) {
    int a=-2147483648;
    int b =a-18;
    System.out.println(b);


}

} output: 2147483630

  • What you're observing is called "overflow". The `int` data type has a finite range. When it exceeds that range, it starts back at the other end of that range. – David Jul 19 '17 at 13:57
  • No.it isn't In that question.if limit exceeds .then count goes in reverse oreder.Here value is right.but sign is wrong,,, – Nishith Lakhlani Jul 19 '17 at 14:00
  • And in what way is your observation different? – David Jul 19 '17 at 14:01
  • ok.got it....... if u surstract 1 from -2147483648 ,it prints 214783648 which is maximum range on positive side/.Thanks – Nishith Lakhlani Jul 19 '17 at 14:04

0 Answers0