Aura
Aura

Reputation: 61

What is the real long type range in java?

According to every reference I've ever found, the long type in Java ranges from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807. I've written the following code below,

public static void main(String[] args){
    long x = 12 * 24 * 60 * 1000 * 1000; 
    long y = 12 * 60 * 1000 * 1000;
    System.out.print(x / y); 
}

The expected result is 24; But the output was 0.

Upvotes: 1

Views: 491

Answers (2)

Frank
Frank

Reputation: 15641

Your literals are int's...

And that gives that x = 100130816 instead of 17280000000

Here you go:

public static void main(String[] args) {
        long x = 12L * 24L * 60L * 1000L * 1000L;
        long y = 12L * 60L * 1000L * 1000L;
        System.out.print(x + " " + y + " " + x / y);

}

The result is 24

P.S. i solved this the first time, but you deleted the Question...

Upvotes: 3

PermGenError
PermGenError

Reputation: 46428

in your code the 12 * 24 * 60 * 1000 * 1000 is evaluated as an ints. and then you are assigning it to long.

try

long x = 12L * 24 * 60 * 1000 * 1000; 
    long y = 12L * 60 * 1000 * 1000;
    System.out.print(x / y); 
}

Upvotes: 3

Related Questions