Skip to content
Advertisement

why nano seconds have to use int instead of long in java

I have been doing this exercise and this is the code

JavaScript

Doesnt nanoseconds have to use long instead of int because the nanoseconds in the range?

Advertisement

Answer

That’s because like documentation says, we have a duration which consist of two fields, one is seconds and the other one is nanos. So when you ask for duration between, you get 2 values :

diff = seconds + nanos

So in this case, nanos only count up to 999,999,999 (0.99… seconds), so integer is enough.

So …

If you need duration in nanos, you’ll have to do something like this :

JavaScript

EDIT :

As mentioned in comments, there is an easier way in your case :

Both

JavaScript

And

JavaScript

would output you long formatted nanosecond duration

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement