Issue about "binary compatibility"

197 views Asked by At

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.

2

There are 2 answers

6
Erwin Bolwidt On BEST ANSWER

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:

  • You can add functionality that makes your method do something it couldn't do before successfully
  • But you cannot change existing behaviour for already-valid inputs while remaining binary compatible.

It's a fancy way of saying something that make a lot of common sense.

3
Shubham Chaurasia On

Not very sure about it but what I can infer from it is this.
Suppose you had some method which would accept some integer and return some integer as a result.

int myMethod(int arg);

Now what JLS speaks is

here is a list of some important binary compatible changes that the Java programming language supports:

.....
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.

Suppose for some invalid input (like your method was only designed to work on positive integers and you passed other than that), the method either throws some exception (either as a part of validation or otherwise) or it enters results into some undefined behavior like infinite loop etc.

So for these kinds of input (invalid input) if you want to return a value indicating the same then it does not break the binary compatibility is what the JLS is saying I think. Eg. you want to return -1 or something like that to indicate the passage of obsolete arguments (rather than throwing or having an undefined behavior). It's implementation specific and depends how you want to handle such inputs.