I know that implicit casting is done automatically by the compiler and virtual machine, and that explicit casting is needed to convert types of data when Java is not sure if the result will be valid. Casting should be done between primitives or between objects.
My question is: If my application uses both integers as well as long integers - should I use long
for both types? Or downcasting for long
? Or casting for int
?
Can usage of long for both types causes some problems?
I will probably go with the right datatype for right operations , long
if I need long
, int
if int
is enough for my requirements . Common sense would be to think using long
for int
probably will slow you down in general, but again this depends on the specific JVM implementation. You will have to decide based on the range which you want to target .
Again , if there are lots of casting involved in your program , then better to go for a datatype like long
which can accommodate a larger range than int
and save you from casts and loss of data in that process . Having said that , long
might be slower than int
although in modern processors and JVMs I doubt that might affect much .
May be , enlightened ones here can answer this question better.