0

I am confused how it's happening. Output is not as expected.

public class Test2 {

public static void main(String arg[]){
    int interval = 43200;
    long tempInterval = interval * 60000;
    System.out.println(tempInterval);
}}

Expected output is 2592000000 but I'm getting -1702967296. It might be naive question but I'm stuck with this.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
subro
  • 104
  • 2
  • 15
  • `60000` is `int`, and `interval` is `int`. So `interval * 60000` is `int * int` which is `int`. Storing that `int` result in `long` variable will not change its value. You probably need `60000L` literal which represents `long` type so you would end up with `int * long = long` (now where is that duplicate). – Pshemo Sep 18 '16 at 15:56
  • And http://stackoverflow.com/questions/17221254/1000-60-60-24-30-results-in-a-negative-number – Tunaki Sep 18 '16 at 15:58

1 Answers1

0

Add a L after 60000.

Java assumes that you're using int multiplication, which causes an int overflow before it's casted to a long.

xes_p
  • 501
  • 4
  • 14