|
|
Tips
- an empty source file will compile without error
- if a .java file does not contain a public class or interface it can have any name
- a single-type import will take precedence over an import-on-demand
- import-on-demand types do not increase the size of the compiled code ie only the types actually used are added to the code
- while import-on-demand adds no overhead to the compiled code, they can slow down the speed of the compile
- a constructor body can include a return statement providing no value is returned
- any method can throw a Runtime or Error exception without declaring it in the throws clause
- methods having the same name and parameter types do not have the same signature unless the parameter types are listed in the same order
- main() can be declared final
- main() is inherited and can be overridden if not declared as final
- args[0] references first command line argument after the application name ( arrays in Java are zero-based)
- main() can be declared public static void ... or static public void ...
- the variable name does not have to be args; can be anything as long as the type is String[]
- variables can have the same name as a method or a class
- only field variables are automatically initialized to their types default value; local variables must be explicitly initialized
- arrays are initialized to the default value of their type when they are created, not declared, even if they are local variables
- array index operator [] has highest level of precedence
- integer variables can be used as array dimension values
- postfix/prefix operators have the highest level of precedence
- remember that when the postfix operator is used in an expression, the current value of the variable is used
- a class may be assigned to an Interface type if the class implements the interface or one of it's sub-interfaces
- you cannot cast a primitive type to an object reference, or vice versa
- you cannot cast a boolean type to another primitive type
- String operations whose result does not alter the original string (ie calling toUpperCase() on a String that is already in uppercase) return the original string reference; otherwise they return a reference to a new String
- Strings are immutable; the original String value can never be changed
- all the primitive type wrapper classes override the Object.equals() method to compare the value of the objects; the default Object.equals() method checks if the variables reference the same object
- you do not have to have a default statement in a
switch() block
- the default statement in a
switch() blcok can appear anywhere in the construct, does not have to be last
- all sections of the
for() loop are optional
finalize() can only be executed once on any object
Traps
- code with package or import declarations given in wrong order
- more than one package declaration
- file with more than one public class or interface declaration
- filename.java does not match name of public class declared in the file
- single-type imports for two classes in different packages but with the same simple name
- single-type import with the same simple name as a class defined in the source file
- attempting to import a package vs a type ie import java.util vs import java.util.*
- class attempting to extend more than one other class
- class declared both final and abstract
- an interface method declared as native or synchronized
- an interface method declared as static
- subclass with default constructor when the superclass does not have a no-args constructor or it's no-arg constructor has a throws clause
- constructor declared with a return type
- an abstract method also declared private, native, final, synchronized, or strictfp
- an abstract method declared in a non-abstract class
- a native or abstract method with a method body
- method returning a type which is not convertible to the declared return type
- a void method returning a value
- a static method referencing this or super
- main() declared other than according to the standard convention
- local (automatic) variables declared with a modifier other than final
- identifiers names beginning with a number or # sign
- main listed as a possible keyword
- capitalized words listed as possible keywords; particularly wrapper classes Integer, Boolean, etc
- C/C++ keywords listed as possible Java keywords
- an empty string vs null as the default value for a String object
- incorrect array declaration statements, particularly:
arrayType [#] varName;
- incorrect array initialization statements, particularly:
arrayType[] varName = new arrayType[2];
varName = { value, value, value };
- negative values for array index
- long value for array index
- array declaration used as an array creation statement
- variables of primitive type handled as Objects
- using the char literals \u000A or \u000D in comments or Strings
- String literal "c" assigned to char type
- using == operator to compare values of two different string reference variables
- variables requiring narrowing conversion being passed to methods without using a cast
- assigning a typed byte or short variable to a char variable
- floating point operation throwing an ArithmeticException
- Bitwise operator precdence is: & ^ |
- assigning subclasses with the same parent to each other
- assigning a parent class to a subclass without a cast
- result of an integer operation on byte or short types being assigned to a byte or short without an explicit cast
- a non-boolean value used for operand1 in a ternary expression
- using == to compare the contents of two different String objects
- using a new value based on a short-circuit operation that was never evaluated
- code that results in a primitive value being changed in a method (can't happen)
- code that results in an unchanged object value when it was changed in a method
- failing to cast a value to match a method parameter type ie assuming narrowing conversion on a method call
- a non-boolean value used in a loop or
if( ) statement
- using the assignment operator '=' vs '==' in an loop or
if() statement
- using an expression vs a value promotable to int in a
switch() block
switch() blocks with duplicate case values
switch() blocks with incorrectly 'typed' case statements
switch() blocks with missing break statements (unintentionally causing code to fall through to next case)
- attempting to access a variable declared in the initialization outside of the for-loop
for()loop with incorrect initialization expression
for()loop with a non-boolean expression
- a question that targets a specific object for garbage collection (can't be done)
- a question that presumes to force the gc to run (can only suggest it run)
|