This week's book giveaway is in the Kotlin forum.We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Doubt in long datatype

RajendraKr Kumar
Greenhorn
Posts: 6
Hello friend,
I have some doubt in long datatype, As i know any long datatype can store max value 9223372036854775807 which i calculated to be 63 bit long only. But by default long datatype can store 64 bit value and max capacity should be 18446744073709551615. I wanted to store 64 bit value(18446744073709551615) in long datatype but it is not being accepted.

long ts=18446744073709551615L;

It is giving error like "Integer number too large".

Mattias Ahlin
Greenhorn
Posts: 11
The long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive).

You're right that is can store 9223372036854775807 different numbers. Since half of them are negative, only half of them can be positive.

David Nemeskey
Ranch Hand
Posts: 52
Mattias: the explanation is great, but I guess you wanted to write

"You're right that is can store 18446744073709551616 different numbers. Since half of them are negative, only half of them can be positive."

RajendraKr Kumar
Greenhorn
Posts: 6
But how can i store this big value(18446744073709551615) in long datatype.

Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
You cannot, because that number falls outside the range of the long data type.

Kaydell Leavitt
Ranch Hand
Posts: 690
If you need 64 bits or more without the number being negative, you can use the class:

BigInteger

-- Kaydell