afsq
afsq

Reputation: 3

Integer literals

I can't understand why in Java you can assign to a float value a double value ,for example :

float number=456.7F;

but you can't do the same for integers and float numbers, such as :

int numb=56798434L;

In C++ These things seem to work.

Upvotes: 0

Views: 48

Answers (2)

qwertz
qwertz

Reputation: 14792

You cannot assign a value of a type with higher capacity to a type of lower capacity without casting. long can store larger numbers, so it can't be assigned to int.

Upvotes: 0

Tagir Valeev
Tagir Valeev

Reputation: 100249

The 456.7F literal is not a double value, it's float value. The double suffix is d (or absence of suffix) and it also produces a compilation error:

float f = 456.7d; 

Java consistently requires an explicit casting for number transformation which may result in overflow or precision loss.

Upvotes: 4

Related Questions