I assume there's no difference after compiling between the different casting types for simple data types? (float)1 and 1F, for example? Just curious :>
Compiling with javac, no, both of those expressions would be resolved to the same bytecode.
However from a bytecode perspective they
are different.
Example 1:(float)1 occupies 2bytes :-
1F occupies just 1byte :-
Javac would always evaluate both expressions to the second bytecode sequence - which is optimal.
However - if the value '1' was replaced with the value '5', things become a little different.
Example 2:(float)5 still occupies 2 bytes :-
however,
5F occupies a total of 4+2 bytes! :-
1 2 3 4
| constants pool Entry: [1] = 5.0f (4bytes)
ldc 1 |
In this instance, Javac would again generate bytecode that matched the second case.
However in this case, it is clearly not size-optimal.
The reason for the difference, is the bytecode instruction set contains 6 single bytecode instructions for pushing the integer constants (-1 through to +5) onto the stack,
where-as it has only 3 single bytecode instructions for pushing float constants (0 through to +2) onto the stack.
Incidentally, this is a good site for a clear overview of the instruction set:
http://homepages.inf.ed.ac.uk/kwxm/JVM/codeByNo.html