Is 1/0 a Legal Java Expression

Is 1/0 a legal Java expression?

Is 1/0 actually a legal Java expression that should compile anytime anywhere?

Yes.

What does JLS say about it?

Nothing specific ... apart from saying that division by zero will result in a runtime exception. However, the JLS acknowledges that possibility of runtime exceptions in the following definition:

"A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following: ..."

(Emphasis added.) So the following would NOT compile:

switch(i) {
case 1:
case 1 + 1:
case 1 / 0: // compilation error.
}

If this is legal, is there a good reason for it?

Good question. I suppose that it is a way to throw ArithmeticException though that is hardly a plausible reason. A more likely reason for specifying Java this way is to avoid unnecessary complexity in the JLS and compilers to deal with an edge case that is rarely going to bite people.

But this is all by the by. The fact is that 1/0 is valid Java code, and no Java compiler should ever flag this as a compilation error. (It would be reasonable for a Java compiler to issue a warning, provided that there was a compiler switch to turn it off.)

Is `1/0` a constant expression in Java?

The compiler is doing constant folding (precomputing trivial literal expressions). This is a case where the expression "completes abruptly", to use the JLS verbiage, disqualifying it from meeting the definition of "constant expression". So it's not a bug, it's consistent with the JLS.

And yes, the expression doesn't evaluate to a value either (warning the user trying to do something like this that the result will not be a constant expression), but the compiler doesn't know that until it tries. Not evaluating to a value and completing abruptly would seem to go hand-in-hand.

Adding a variable declaration like

int x = 1 / 0;

doesn't cause a compiler error, it's the switch that forces the expression to be evaluated at compile time.

By the way I checked that this happens for version 7 of the Oracle and IBM JDKs too, it's not OpenJDK or JDK8 specific.

What does this boolean (number & 1) == 0 mean?

Keep in mind that "&" is a bitwise operation. You are probably aware of this, but it's not totally clear to me based on the way you posed the question.

That being said, the theoretical idea is that you have some int, which can be expressed in bits by some series of 1s and 0s. For example:

...10110110

In binary, because it is base 2, whenever the bitwise version of the number ends in 0, it is even, and when it ends in 1 it is odd.

Therefore, doing a bitwise & with 1 for the above is:

...10110110 & ...00000001

Of course, this is 0, so you can say that the original input was even.

Alternatively, consider an odd number. For example, add 1 to what we had above. Then

...10110111 & ...00000001

Is equal to 1, and is therefore, not equal to zero. Voila.

Is 'T.super' a legal expression as per JLS?

No, it's not. Refer to Chapter 19. Searching for the keyword super yields the following constructs:

  • wildcard bounds: extends T / super T;
  • explicit constructor invocation: super(args);
  • field access: [Typename.]super.field;
  • method invocation: [Typename.]super.method();
  • method reference: super::method.

The fact that it compiles may be considered a bug or a language extension, although there is no real difference between the two.

How to create a method to return 1 or 0 without using conditions?

If you are given only 0 and 1 then this could be simpler:

return 1 - value;

What does the += operator do in Java?

The "common knowledge" of programming is that x += y is an equivalent shorthand notation of x = x + y. As long as x and y are of the same type (for example, both are ints), you may consider the two statements equivalent.

However, in Java, x += y is not identical to x = x + y in general.

If x and y are of different types, the behavior of the two statements differs due to the rules of the language. For example, let's have x == 0 (int) and y == 1.1 (double):

    int x = 0;
x += 1.1; // just fine; hidden cast, x == 1 after assignment
x = x + 1.1; // won't compile! 'cannot convert from double to int'

+= performs an implicit cast, whereas for + you need to explicitly cast the second operand, otherwise you'd get a compiler error.

Quote from Joshua Bloch's Java Puzzlers:

(...) compound assignment expressions automatically cast the result of
the computation they perform to the type of the variable on their
left-hand side. If the type of the result is identical to the type of
the variable, the cast has no effect. If, however, the type of the
result is wider than that of the variable, the compound
assignment operator performs a silent narrowing primitive
conversion [JLS 5.1.3].

Why boolean in Java takes only true or false? Why not 1 or 0 also?

Java, unlike languages like C and C++, treats boolean as a completely separate data type which has 2 distinct values: true and false. The values 1 and 0 are of type int and are not implicitly convertible to boolean.



Related Topics



Leave a reply



Submit