# Java: Summary - Basic Elements

## Comments

`//` Everything to the end of the line is ignored.
`/*` Everything (possibly many lines) is ignored until a `*/`
`/**` Used for automatic HTML documentation generation by the javadoc program. `*/`

## Types

Types are divided into two categories. There are 8 primitive types: boolean (true and false); char (Unicode characters); byte, short, int, and long (integers); float and double (floating point). Object types are created whenever a class is defined.

## Integer Types

The integer types byte, short, int, and long are stored as two's complement, signed binary integers. char, which is technically also an integer type, is stored as an unsigned binary number. Expressions are computed as ints, so a cast is needed to store in a smaller type.

Type | Bytes | Range | Literals
byte | 1 | -128..+127 | none
short | 2 | -32,768..+32,767 | none
int | 4 | -2,147,483,648..+2,147,483,647 | 23, 0xAF
long | 8 | -9,223,372,036,854,775,808.. | 23L, 0xAFL

Operators: arithmetic, comparison, bitwise, assignment.

## Floating-point Types

The floating-point types, float and double, are stored in IEEE-754 format. Calculations may produce NaN (Not a Number) or +/- infinity. Calculations are done as doubles, so a cast is needed to store in a float.

Type | Bytes | Range | Accuracy | Literals
float | 4 | -3.4E38..+3.4E38 | 6-7 digits | 3.14F 6.02e23F
double | 8 | -1.7E308..+1.7E308 | 14-15 digits | 3.14 6.02e23

Operators: arithmetic, comparison, assignment.

## char Type

char type is a Unicode character stored as an unsigned number in two bytes (range 0..65,535 or '\u0000'..'\uFFFF'). char is an integer type.
Literals

- 'A' (single character)
- Unicode '\uxxxx' where x is a hexadecimal digit. Eg '\u0041'
- Octal '\nnn' where n is an octal digit.
- Escape combinations: '\n' newline, '\\' backslash, '\'' single quote, '\"' double quote, '\r' carriage return, '\t' tab, '\b' backspace, '\f' form feed.

## boolean Type

boolean is used to store the values true or false. The storage is unspecified.
Operators: logical, ==, !=, assignment.

## Identifiers

- Start identifiers with an alphabetic character (a-z or A-Z), and continue with alphabetic, numeric (0-9), or '_' (underscore) characters. Do not use $.
- Second words in a name should start with an uppercase letter.
- Do not use keywords (see below).
- Class and interface names should start with an uppercase letter (Graphics, ActionListener, JButton, ...)
- Variable and method names should start with a lowercase letter (repaint(), x, ...).
- Constants should be all uppercase with underscores between words (BoxLayout.X_AXIS, Math.PI, ...).

## Keywords

[types] `boolean byte char double float int short true false` [flow] `if else while for do continue switch case break default assert try catch finally throw return` [OOP] `class extends implements instanceof interface new null super this` [declarations] `final import native package private protected public static throws transient volatile void` [other] `strictfp synchronized goto const`

## Variables

Variables may be local, instance (field), or static (class) variables. Formal parameters are local variables that are assigned values when the method is called.

| local | instance | static / class
Where declared | In a method. | In class, but not in a method. | In class using `static` keyword.
Initial value | Must assign a value before using. | Zero for numbers, null for objects, false for boolean. Can be initialized in constructor. | Zero/null/false or initialized with static initializer.
Visibility | Only in the same method. | `private`: Only methods in this class. Default: All methods in same package. `public`: Anyone can see it. | Same as instance.
Created | When method is entered. | When an instance of the class (object) is created with `new`. | When program is loaded.
Destroyed | When the method returns. | When there are no more references to the object. | When program terminates.

Copyright 2004 Fred Swartz Last update: 2004-08-18