| Java Keywords | |||||
|---|---|---|---|---|---|
| abstract | boolean | break | byte | case | catch |
| char | class | const | continue | default | do |
| double | else | extends | final | finally | float |
| for | goto | if | implements | import | instanceof |
| int | interface | long | native | new | package |
| private | protected | public | return | short | static |
| strictfp | super | switch | synchronized | this | throw |
| throws | transient | try | void | volatile | while |
| assert | enum | ||||
| Legal Identifiers |
|---|
| Must start with a letter, $, or underscore. |
| After the first character, can contain any letter, currency character, underscore, or number. |
| No limit to the number of characters it can contain. |
| Can't use a Java keyword as an identifier. |
| Case sensitive - foo and FOO are two different identifiers. |
| Naming Conventions |
|---|
| If a variable consists of only one word, spell it in all lowercase letters. |
| If a variable consists of more than one word, capitalize the first letter of each subsequent word. |
| If a variable stores a constant value, use all capital letters, and separate words with underscore. Example: static final int NUM_GEARS = 6 |
| For class names, the first letter should be capitalized, and if it consists of several words, capitalize the first letter of each subsequent word. |
| For interfaces, the names should be adjectives like Runnable, or Serializable. |
| For methods, the first letter should be lowercase, and then normal camelCase rules should apply. In addition, the names should contain a verb, such as "getBalance", "doCalculation", "setBalance". |
| For enums, all letters should be uppercase. |
| JavaBean Naming Conventions |
|---|
| For most properties, the getter method's prefix must be "get". For boolean properties, the prefix may be "get" or "is". |
| The setter method's prefix must be "set". |
| For both getter and setter methods, use camelCase for the method name. |
| Setter methods must be public, take an argument of the desired type, and return void. |
| Getter methods must be public, take no arguments, and return the desired type. |
| Listener register methods must use the prefix "add", followed by the listener type - ie, "addActionListener()". |
| Listener unregister methods must use the prefix "remove", followed by the listener type - ie, "removeActionListener()". |
| The type of listener to be added or removed must be passed as the argument to the method. |
| Listener method names must end with the word "Listener." |
| Source File Rules |
|---|
| There can be at most one public class per source file. |
| If a public class exists in the source file, the name of the file must be the name of the class, with '.java' as a suffix. Example: A file containing "public class MyClass" must be named "MyClass.java". |
| If there are no public classes in the source file, you may name it anything that doesn't match a class in the file, with a '.java' suffix. |
| If the class is in a package, the package statement must be the first line in the source code file. |
| If there are import statements, they must be after the package statement if one exists, and before everything else. |
| Class Access Modifiers | |
|---|---|
| default (no modifier) | Visible to classes in same package. |
| public | Visible to classes in all packages. |
| Class Non-Access Modifiers | |
|---|---|
| final | Class can not be overridden. Cannot use with abstract. |
| abstract | Class can never be instantiated - must be overridden. Cannot use with final. Abstract classes can have non-abstract methods. Abstract methods in class must be prefixed with "abstract", and end in a semicolon instead of curly braces. |
| strictfp | Methods in class uses strict floating point calcuations. |
| static | For nested classes only, creates a class that will exist independently of any instances created for the class. |
| Class Member Access Modifiers | ||||
|---|---|---|---|---|
| Including methods, variables, and inner classes. | ||||
| Visibility | Public | Protected | Default | Private |
| From the same class | Y | Y | Y | Y |
| From any class in the same package | Y | Y | Y | |
| From a subclass in the same package | Y | Y | Y | |
| From a subclass outside the same package | Y | Y (through inheritance) | ||
| From any non-subclass class outside the package | Y | |||
| Non-Access Modifiers | |
|---|---|
| for Class Methods | |
| final | Method cannot be overridden. Cannot use with abstract. |
| abstract | Method must be overridden. Cannot use with final, private, or static.
Abstract methods in class must be prefixed with "abstract", and end in a semicolon instead of curly braces. If even one method is marked abstract, the class MUST be marked as abstract, as well. |
| strictfp | Method uses strict floating point calculations. |
| static | Marks a method that can be used without an instance of this class. |
| synchronized | Method can be accessed by only one thread at a time. |
| native | Method is implemented in platform-dependent code. The body must be a semicolon, like an abstract method, indicating that the implementation is omitted. |
| for Instance Variables | |
|---|---|
| final | Once assigned, the variable becomes a constant, and can never be reassigned.
Can also be used on arguments in a function signature. Example: public void doIt(final int x). |
| static | Marks a variable that can be used without an instance of this class. |
| transient | Skip this variable when serializing the object. |
| volatile | The thread accessing the variable must always reconcile its own private copy of the variable with the master copy in memory. |
| for Local Variables | |
|---|---|
| final | Once assigned, the variable becomes a constant, and can never be reassigned.
Can also be used on arguments in a function signature. Example: public void doIt(final int x). No other access modifier can be applied to local variables! |
| Interfaces |
|---|
| Similar to a class, but use the word "Interface" instead of "Class". |
| Interfaces are abstract, whether it is marked that way or not. |
| May extend other interfaces, but not classes. |
| Interface Methods |
|---|
| Must contain only public abstract methods (no actual methods). |
| All methods in interface are public and abstract, whether they are marked that way or not. |
| Interface methods can't be static, final, strictfp, or native. |
| Interface methods must end in a semicolon, not curly braces.
This implies curly braces anywhere in an interface is an error - watch out for them! |
| Interface Variables |
|---|
| All variables are public, static, and final, whether they are marked that way or not.
To make this easier to remember, type interface variables in all caps, with underscores separating the words. This will jog your memory that they actually ARE static final constants (and public to boot!), whether you mark them that way or not. |
| Enum Rules |
|---|
| Enums constants should be in all caps. |
| Enums can be declared as a top level class, or an inner class, but not within a method. |
| It is optional to put a semicolon at the end of the enum declaration. |
| You can compare enums with == or .equals, but < and > don't work. |
| You must specify all enum values at compile time - it is not possible to add to the enum values at run time. |
| They can be simple, such as
enum CoffeeSize { TALL, GRANDE, VENTI} // <-- no semicolon or they can look like almost like a full blown class, but with the word "enum" instead of "class", such as
enum CoffeeSize {
|
| Range of Numeric Primitives | |||||
|---|---|---|---|---|---|
| Type | Bits | Bytes | Min Range | Max Range | Valid as array subscript |
| byte | 8 | 1 | -128 | 127 | Y |
| short | 16 | 2 | -32,768 | 32,767 | Y |
| char | 16 | 2 | 0 | 65,535 | Y |
| int | 32 | 4 | -2,147,483,648 | 2,147,483,647 | Y |
| long | 64 | 8 | -9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 | N |
| float | 32 | 4 | N | ||
| double | 64 | 8 | N | ||