How can this code be explained ? byte b1 = 5; works fine . When Integral literals are considered by default to be 32 bit literals then 5 is represented in 32 bits . Is it not an error to put 32 bits explicitly into a 8 bit variable . Somebody please help me in understanding this . (With reference to float f = 1.3 giving an error because all floating point literals are 64 bit values )
Cheers !
nupur dhawan
Ranch Hand
Joined: Jun 26, 2002
Posts: 71
posted
0
this works fine as byte b1 = 5, assigns b1 a value of 5 which is within the range of a byte , that is , -127 to 128 .
Nupur. <br />SCJP2.
nupur dhawan
Ranch Hand
Joined: Jun 26, 2002
Posts: 71
posted
0
4.2.1 Integral Types and Values The values of the integral types are integers in the following ranges: For byte, from -128 to 127, inclusive For short, from -32768 to 32767, inclusive For int, from -2147483648 to 2147483647, inclusive For long, from -9223372036854775808 to 9223372036854775807, inclusive For char, from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535
Howard Roarke
Greenhorn
Joined: Jun 28, 2002
Posts: 6
posted
0
I understand the limits for Integer data types and all . My question is this In Java float f = 1.0 is not allowed . Why ? Because 1.0 is considered by default to be a 64 bit double. It has also been told that integer literals are by default 32 bit literals . What I am asking is why does the same rule not apply here ?
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
posted
0
Smart Word, Welcome to JavaRanch! We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy. Thanks Pardner! Hope to see you 'round the Ranch!
Alex D, Welcome to JavaRanch! We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy. Thanks Pardner! Hope to see you 'round the Ranch!
Marilyn de Queiroz
Sheriff
Joined: Jul 22, 2000
Posts: 9033
10
posted
0
Originally posted by Smart Word: I understand the limits for Integer data types and all .
My question is this
In Java float f = 1.0 is not allowed . Why ? Because 1.0 is considered by default to be a 64 bit double. It has also been told that integer literals are by default 32 bit literals . What I am asking is why does the same rule not apply here ?
Because they are stored differently. An int is stored like 0000 0000 0000 0000 0000 0000 0000 0005 so losing a few leading zeros to store it (the constant) in a byte is ok.
However, floats and doubles are stored in something similar to scientific notation so they can't be "chopped off" to fit into a smaller slot.
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.