| ## Java 1.1 Unleashed # - Appendix A - Java Language Summary by Laura Lemay revised by Billy Barron This appendix contains a summary or quick reference for the Java language, as described in this book. NOTE: This is not a grammar overview, nor is it a technical overview of the language itself. It's a quick reference to be used after you already know the basics of how the language works. If you need a technical description of the language, your best bet is to visit the Java Web site (`http://java.sun.com`) and download the actual specification, which includes a full BNF grammar. Language keywords and symbols are shown in a `monospace font`. Arguments and other parts to be substituted are in italic monospace. Optional parts are indicated by brackets (except in the array syntax section). If there are several options that are mutually exclusive, they are shown separated by pipes (`|`) like this:
    [ public | private | protected ] type varname
## Reserved Words The following words are reserved for use by the Java language itself (some of them are reserved but not currently used). You cannot use these terms to refer to classes, methods, or variable names:
`abstract` | `do` | `import` | `public` | `try`
`boolean` | `double` | `instanceof` | `return` | `void`
`break` | `else` | `int` | `short` | `volatile`
`byte` | `extends` | `interface` | `static` | `while`
`case` | `final` | `long` | `super` |
`catch` | `finally` | `native` | `switch` |
`char` | `float` | `new` | `synchronized` |
`class` | `for` | `null` | `this` |
`const` | `goto` | `package` | `throw` |
`continue` | `if` | `private` | `throws` |
`default` | `implements` | `protected` | `transient` |
## Comments
    /* this is the format of a multiline comment */
    // this is a single-line comment
    /** Javadoc comment */
## Literals
number | Type `int`
`number[l | L]` | Type `long`
0xhex | Hex integer
0Xhex | Hex integer
0octal | Octal integer
`[` number `].`number | Type `double`
number`[ f | f]` | Type `float`
number`[ d | D]` | Type `double`
`[ + | - ]` number | Signed
number`e`number | Exponent
number`E`number | Exponent
```character`'` | Single character
`"`characters`"` | String
`""` | Empty string
`\b` | Backspace
`\t` | Tab
`\n` | Line feed
`\f` | Form feed
`\r` | Carriage return
`\"` | Double quote
`\'` | Single quote
`\\` | Backslash
`\uNNNN` | Unicode escape (`NNNN` is hex)
`true` | Boolean
`false` | Boolean
## Variable Declarations
`[ byte | short | int | long ]` varname | Integer (pick one type)
`[ float | double ]` varname | Floats (pick one type)
`char` varname | Characters
`boolean` varname | Boolean
`classname` varname | Class types
`type` varname`,` varname`,` varname | Multiple variables
The following options are available only for class and instance variables. Any of these options can be used with a variable declaration:
`[ static ]` variableDeclaration | Class variable
`[ final ]` variableDeclaration | Constants
`[ public | private | protected ]` variableDeclaration | Access control
## Variable Assignments
variable`=` value | Assignment
variable`++` | Postfix Increment
`++`variable | Prefix Increment
variable`--` | Postfix Decrement
`--`variable | Prefix Decrement
variable `+=` value | Add and assign
variable`-=` value | Subtract and assign
variable`*=` value | Multiply and assign
variable `/=` value | Divide and assign
variable`%=` value | Modulus and assign
variable`&=` value | `AND` and assign
variable`| =` value | `OR` and assign
variable`^=` value | `XOR` and assign
variable`<<=` value | Left-shift and assign
variable`>>=` value | Right-shift and assign
variable`>>>=` value | Zero-fill, right-shift, and assign
## Operators
arg`+` arg | Addition
arg`-` arg | Subtraction
arg`*` arg | Multiplication
arg`/` arg | Division
arg`%` arg | Modulus
arg `<` arg | Less than
arg`>` arg | Greater than
arg`<=` arg | Less than or equal to
arg`>=` arg | Greater than or equal to
arg `==` arg | Equal
arg`!=` arg | Not equal
arg`&&` arg | Logical `AND`
arg`||` arg | Logical `OR`
`!` arg | Logical `NOT`
arg`&` arg | `AND`
arg`|` arg | `OR`
arg`^` arg | `XOR`
arg`<<` arg | Left-shift
arg`>>` arg | Right-shift
arg `>>>` arg | Zero-fill right-shift
`~` arg | Complement
`(`type`)`thing | Casting
arg`instanceof class` | Instance of
test`?` trueOp`:` falseOp | Ternary (if) operator
## Objects
new`` class`();` | Create new instance
new class`(`arg,arg,arg`...)` | New instance with parameters
`new` type`(`arg,arg,arg...`)` | Create new instance of an anonymous class
`Primary.new` type`(`arg,arg,arg...`)` | Create new instance of an anonymous class
object`.`variable | Instance variable
object`.`classvar | Class variable
Class`.`classvar | Class variable
object`.`method`()` | Instance method (no arguments)
object`.`method`(`arg,arg,arg`...)` | Instance method
object`.`classmethod`()` | Class method (no arguments)
object`.`classmethod`(`arg,arg,arg`...)` | Class method
Class`.`classmethod`()` | Class method (no arguments)
Class`.`classmethod`(`arg,arg,arg`...)` | Class method
> ## Arrays NOTE: The brackets in this section are parts of the array creation or access statements. They do not denote optional parts as they do in other parts of this appendix.
> type`` varname`[]` | Array variable
> type`[]` varname | Array variable
> `new` type`[`numElements`]` | New array object
> `new` type`[] {`initializer`}` | New anonymous array object
> array`[`index`]` | Element access
> array`.`length | Length of array
## Loops and Conditionals
`if (` test`)` block Conditional
`if (` test `)` block Conditional with `else` else block
`switch (`test`) {` | `switch` (only with `int`
`case` value`:` statements | or `char` types)
`case` value `:` statements |
`...` |
`default :` statement |
`}` |
`for (`initializer`;` test`;` change `)` block | `for` loop
`while (` test `)` block | `while` loop
`do` block | `do` loop
`while (`test`)` |
`break [` label `]` | Break from loop or switch
`continue [` label `]` | `continue` loop
`label:` | Labeled loop
## Class Definitions
`class` classname block | Simple class definition
Any of the following optional modifiers can be added to the class definition:
`[ final ] class` classname block | No subclasses
`[ abstract ] class` classname block | Cannot be instantiated
`[ public ] class` classname block | Accessible outside package
`class` classname`[ extends` Superclass`]` block | Define superclass
`class` classname`[ implements` interfaces`]` block | Implement one or more interfaces
## Method and Constructor Definitions The basic method looks like this, where returnType is a type name, a class name, or `void`:
`returnType methodName() block` | Basic method
returnType methodName`(`parameter`,` | Method with parameter`, ...)` block parameters
`` Method parameters look like this:
    type parameterName
Method variations can include any of the following optional keywords:
`[ abstract ]` returnType methodName`()` block | Abstract method
`[ static ]` returnType methodName`()` block | Class method
`[ native ]` returnType methodName`()` block | Native method
`[ final ]` returnType methodName`()` block | Final method
`[ synchronized ]` returnType methodName`()` block | Thread lock before executing
`[ public | private | protected ]` returnType methodName`()` | Access control
Constructors look like this:
classname`()` block | Basic constructor
classname`(`parameter, parameter, parameter`...)` block | Constructor with parameters
`[ public | private | protected]` classname`()` block | Access control
In the method/constructor body, you can use these references and methods:
`this` | Refers to current object
classname`.this` | Refers to a particular inner class object
`super` | Refers to superclass
`super.`methodName`()` | Calls a superclass's method
`this(...)` | Calls class's constructor
`super(...)` | Calls superclass's constructor
type`.class` | Returns the class object for the type
`return [` value`]` | Returns a value
## Importing
`import` package.className | Imports specific class name
`import` package`.*` | Imports all classes in package
`package` packagename | Classes in this file belong to this package
`interface` interfaceName `[ extends` anotherInterface `]` block |
`[ public ] interface` interfaceName block |
`[ abstract ] interface` interfaceName block |
## Guarding
`synchronized (` object `)` block | Waits for lock on object
`try` block | Guarded statements
`catch (` exception `)` block | Executed if exception is thrown
`[ finally` block`]` | Always executed
`try` block | Same as previous example (can
`[ catch (` exception`)` block`]` | use optional `catch` or `finally`
`finally` block | but not both)
| ©Copyright, Macmillan Computer Publishing. All rights reserved.