Free Guides
Language Tutorials

Index

Control flow, exception handling and assertions
-
The type of expression of a switch statement must be non-long integral. All labels in a switch construct including the default label are optional. But there cannot be more than one default label. The case label values must be in range of type of switch expression.
-
Switch blocks may be nested and the case labels can be redefined inside them.
-
In the for loop header, two semicolons are mandatory. The 'crab' (;;) is commonly used to construct infinite loops. If in the for loop, pre-increment instead of post-increment is used, it makes no difference to the behavior of the loop.
-
The scope of a label is the entire statement prefixed by the label. So it cant be redeclared as a label inside the statement. A statement may have multiple labels. A declaration statement cannot have a label.
-
break() terminates the loop or block it is in, whereas continue() skips that particular iteration of the loop and continues with the next one.
-
All exceptions are derived from java.lang.Throwable class and have the methods getMessage() and printstacktrace(). RuntimeException, Error and their derivatives are called unchecked exceptions. All others are checked7. In particular Asserts throw unchecked AssertionError.
-
In a try-catch-finally construct, block notation is mandatory. There can be zero or more catch blocks but a maximum of one finally block. The catch and finally blocks must always appear in conjunction with the try and in that order.
-
After catching an exception, the control continues from the class which handled the exception. If the finally block throws an exception, then this is propagated regardless of how the try block or any catch block were executed. Also, the new exception overrules any previously unhandled exception. A value returned by a finally block will supersede other return statements.
-
Any exception propagated to a finally block is nullified by any return statement in the finally block.
-
When a subclass is created it can override a superclass methods, but the new throws clause can only throw a subset of the previous exceptions (including their subclasses)
-
To compile assertions use $ javac -source 1.4 programname.java and during running, enable it by either -enableassertions, or -ea flags. Disable by -disableassertions or -da. These options affect all non-system classes only8. For enabling or disabling system class assertions, use -enablesystemassertions, -esa andj -disablesystemassertion, -dsa.