Java Verities (Eternal Truths)

Ahem. That might be a bit expansive. Let's try this:

Cool Things to Know About Java--Things That Could Let You Answer a SCJP Exam Question in Ten Seconds, and Get on with Questions that Require More Time

Sample: A question contains the statement:

if ( a = b )
    x = y;

The questions won't be: "Is this legal?" Or, "Do you think somebody was asleep here?" The question will be about (ostensibly) something else altogether. Given appropriate declarations, the statement will compile. The problem is, the programmer almost certainly didn't mean that. The = should have been ==. You need to have radar closely tuned to the bandwidth of such issues. Here's the start of a list, things you can check quickly, and maybe save a bunch of time.

Please suggest additions. And a better name that "Java Verities," which is the best I can do right now.

  1. Is the public static void main right? It could be static public void main and everything would be fine. But if the  static is missing the code will compile just fine, but fail at execution. How about the (String[] args) part? (String args []) is fine, and fred or sam or SecondBattleOfBullRun instead of args--but the [] better be there. If not it will compile fine; a method can have a String argument instead of a String array argument. But it won't execute. There couldn't possibly be more than one question on the whole exam about this--but if you miss it, that's painful.
  2. When numeric primitives of different type are compared, the narrower value is promoted to the type of the wider, then the comparison is made.
  3. In arithmetic involving byte, char, short, int, or long: if there is a long, the other operand is promoted to a long. In all other cases, everything is promoted to an int and the arithmetic is done. This applies to each operator and its two operands. In a complicated expression, the rule applies at each (binary) operator. Parenthesis force inner operations to be done first, as always.
  4. A class can't be protected or private.
  5. Interfaces and interface members have implicit characteristics, which you can declare or not. E.g., an interface method is implicitly public, with or without public in front of it; therefore any class implementing an interface method must be marked public.
        a. An interface is implicitly abstract.
        b. Interface methods are implicitly public and abstract. (There are no concrete methods in an interface.)
        c. Interface constants are implicitly public, static, and final. (There are no interface variables except constants.)

 

 

To be continued, preferably with your suggestions. And corrections, of course. It is really tricky to get generalizations like these absolutely right. On the Sun exam, there is no such thing as partial credit!

Back to Dan McCracken's  Home Page