Answer the question
In order to leave comments, you need to log in
Dynamic type conversion and overflow
Hello!
I'll start with a puzzle that can be found literally in the first chapter of Java™ Puzzlers: Traps, Pitfalls, and Corner Cases by Joshua Bloch and Neal Gafter:
public class LongTest {
public static void main(String ... args) {
final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000;
final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;
System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY);
}
}
Answer the question
In order to leave comments, you need to log in
Let's say if we have an int and an overflow occurs at some stage. Why not do it in order to allocate new memory at this moment, but already of type long and move the value of the old variable there.
Number overflow masking is a recognized mistake in the design of the Java language (we read the same Bloch).
Why not do it in order to allocate new memory at this moment, but already of type long and move the value of the old variable there
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question