When I read Java Language Specification (JLS8) > Binary Compatibility, one of a set changes that doesn't break binary compatibility is:
Changing methods or constructors to return values on inputs for which they previously either threw exceptions that normally should not occur or failed by going into an infinite loop or causing a deadlock
I don't understand this idea.
Please help clarify and give an example to demonstrate it.
Changing methods or constructors to return values on inputs for which they previously either threw exceptions that normally should not occur
Existing code:
public int square(int x) {
if (x % 2 == 0) {
return x * x;
} else {
throw new IllegalArgumentException("Cannot square odd numbers");
}
}
Example of a compatible change that satisfies above rule:
public int square(int x) {
return x * x;
}
Example of an incompatible change:
public int square(int x) {
if (x % 2 == 1) {
return x * x;
} else {
throw new IllegalArgumentException("Cannot square even numbers");
}
}
or failed by going into an infinite loop or causing a deadlock
Before:
public int square(int x) {
if (x % 2 == 0) {
return x * x;
} else {
while (true)
;
}
}
Example of a compatible change that satisfies above rule:
public int square(int x) {
if (x % 2 == 0) {
return x * x;
} else {
// Even this is binary compatible (although bad form as the method
// name is no longer self-explanatory.)
return x * x * x;
}
}
I think you get the picture.
The practical meaning of the statements is:
It's a fancy way of saying something that make a lot of common sense.