-1

I've a java File: Int.java

class Int {
   public static void main(String[] args){
        int i=1052254545;
        System.out.println(i* 10);
        System.out.println(i);
    }
}

I run it:

$java Int
1932610858
1052254545

Why? and How to correct?

phuclv
  • 37,963
  • 15
  • 156
  • 475

1 Answers1

2
    int i=1052254545;
    System.out.println(Integer.MAX_VALUE);
    System.out.println(i* 10);
    System.out.println(i);

Run this.. you will have the answer.

i*10 > maximum value of int

Now In Java you can't represent i*10 value correctly since it is out of int value range. If you try to represent a value witch not fall into range that will cause truncation of data.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115