Numbers

Hi

I have been through Sun磗 tutorial and found something really weird to me. It is about Numbers.

The range for long values is:

-9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive).

But when I do this:

long value = 9223372036854775807;

I get this:

integer number too large: 9223372036854775807

What is wrong? Anybody helps?

[429 byte] By [charlles_cubaa] at [2007-11-27 0:29:06]
# 1
When you create a numeric literal, it is assumed to be an integer unless you append the 'L' character to indicate that it is a long
guitar_man_Fa at 2007-7-11 22:31:10 > top of Java-index,Java Essentials,Java Programming...
# 2
try this:long value = 9223372036854775807L;Message was edited by: guitar_man_F
guitar_man_Fa at 2007-7-11 22:31:10 > top of Java-index,Java Essentials,Java Programming...
# 3
Ok.long is an integer type, just as int.So if a literal numeric is assumed to be an integer what is the problem?
charlles_cubaa at 2007-7-11 22:31:10 > top of Java-index,Java Essentials,Java Programming...
# 4
Sorry I mean both int and long are integersint is a 32-bit integer while long is a 64-bit integerIt is correct isn磘 it?
charlles_cubaa at 2007-7-11 22:31:10 > top of Java-index,Java Essentials,Java Programming...
# 5
Ohh I got nowWhen You refer to integer you磖e talking specifically about int not about longIt it?
charlles_cubaa at 2007-7-11 22:31:10 > top of Java-index,Java Essentials,Java Programming...
# 6

> Ohh I got now

> When You refer to integer you磖e talking specifically

> about int not about long

> It it?

Yes, I'm sorry, I should have made that more clear.

The JLS defines an unqualified integer literal to be an int (32 bits). If you wish to provide long integer literal, you must add 'L' to the end of the literal, and the compiler will interpret the literal as a 64 bit integer.

guitar_man_Fa at 2007-7-11 22:31:10 > top of Java-index,Java Essentials,Java Programming...
# 7
Thanks you thank you guitar_manI got now.One more question?The same applies to double? Do I have to use letters in the end of the number?
charlles_cubaa at 2007-7-11 22:31:10 > top of Java-index,Java Essentials,Java Programming...
# 8
F and D.F means float. D means double.
KathyMcDonnella at 2007-7-11 22:31:10 > top of Java-index,Java Essentials,Java Programming...
# 9
Ok What is the normal range for Double. Not the extended-exponent valueDo I have to put a "D" in a Double literal?Thanks
charlles_cubaa at 2007-7-11 22:31:10 > top of Java-index,Java Essentials,Java Programming...
# 10

See JLS 3.10.2:

The largest positive finite float literal is 3.4028235e38f.

The smallest positive finite nonzero literal of type float is 1.40e-45f.

The largest positive finite double literal is 1.7976931348623157e308.

The smallest positive finite nonzero literal of type double is 4.9e-324.

KathyMcDonnella at 2007-7-11 22:31:10 > top of Java-index,Java Essentials,Java Programming...
# 11

> Thanks you thank you guitar_man

>

> I got now.

>

> One more question?

> The same applies to double? Do I have to use letters

> in the end of the number?

F/D for float vs. double. However, the difference is that with floating point types, the larger one (double) is the default.

1-- int

1L-- long

1.0 -- double

1D-- double

1.0D -- double

1F-- float

1.0F -- float

jverda at 2007-7-11 22:31:10 > top of Java-index,Java Essentials,Java Programming...
# 12

> One more question?

> The same applies to double? Do I have to use letters

> in the end of the number?

You get the same sort of treatment, only the platform favors the 64 bit number.

A floating point literal (unqualified) is assumed to be a double(64 bit) unless an 'f' is used to indicate that it is a float(32 bit).

- Adam

guitar_man_Fa at 2007-7-11 22:31:10 > top of Java-index,Java Essentials,Java Programming...
# 13
Thanks JVerg and guitar_man and the othersSumarising:Unqualified integer literal is assumed to be of type int.Unqualified floating point literal is assumed to be of type double.
charlles_cubaa at 2007-7-11 22:31:10 > top of Java-index,Java Essentials,Java Programming...
# 14
> Sumarising:> Unqualified integer literal is assumed to be of type> int.> Unqualified floating point literal is assumed to be> of type double.Correct.
jverda at 2007-7-11 22:31:10 > top of Java-index,Java Essentials,Java Programming...
# 15
Ok thank you
charlles_cubaa at 2007-7-21 19:45:42 > top of Java-index,Java Essentials,Java Programming...