Java summary: Basics

Everything indicated with a are details that you??probably won't need.


// 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 are divided into two categories. There are 8 primitive types: boolean (true and false); char; byte, short, int, and long (integers); float and double (floating point). Object types are created whenever a class is defined. Arrays are objects.

Integer Types

The integer types byte, short, int, and long are stored as two's complement, signed binary integers.?? The leftmost bit represents the sign. 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 are float and double. 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 stored as an unsigned number (integer type) in two bytes (range 0..65,535).
  • 'A' (single character)
  • Unicode '\uxxxx' where x is a hexadecimal digit. Eg '\u0041'
  • 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 (variable, class or method names)

  • Start identifiers with an alphabetic character (a-z or A-Z), and continue with alphabetic, numeric (0-9), or '_' (underscore) characters. Do not use $.
  • Do not use keywords (see below).

Standard Naming:

  • Class and Method names should start with an uppercase letter
  • Variable names should start with a lowercase letter, start class attributes with m and class attributes with an s.
  • Constants should be all uppercase with underscores between words.
  • Second words in a name should start with an uppercase letter.

Keywords (defining the Java language)

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


Variables may be local, attributes (field), or class variables. Parameters are local variables that are assigned values when the method is called.

local in method attribute 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. All methods in the same package. If it is declared public, other classes can see it. If it is protected, only child classes, and for private, only this class. Same as fields.
Lifetime Created when the method is called. Destroyed when the method returns. Created when an instance of the class (object) is created with new. Destroyed after there are no more references to the object (garbage collection). Program lifetime.