HTML Markup | JavaScript | Java | Home & Links

Tutorial 1 - Structure / Syntax

Java applications are based on a structure and syntax common to many languages (eg C, JavaScript, awk, perl). This makes it easy to cross over from one language to another based on program requirements, resources and politics. Apps are compiled into bytecode and executed by a run-time interpreter.

Lexical Structure

The lexical structure of a programming language is the set of elementary rules that define the tokens or basic atoms of the program. It is the lowest level syntax of a language and specifies what is punctuation, reserved words, identifiers, constants and operators. Some of the basic rules for Java are:

Data Types

The primitive Java data types are:

Type BitsRange
boolean1true, false
char160 to 65,535
byte8-128 to +127
short16-32,768 to +32,767
int32-231 to +231-1
long64-263 to +263-1
float32-3.4E+38 to +3.4E+38 (approx)
double64-1.8E+308 to +1.8E+308 (approx)

Type wrappers are classes used to enclose a primitive datatype into an object. This is sometimes necessary because:

The Number class wrapper has subclasses of Byte, Short, Integer, Long, Double and Float. Each of these subclasses have constants MAX_VALUE and MIN_VALUE and methods for converting from and to strings such as: parseInt(str[, radix]), parseLong(str[, radix]), valueOf(str[, radix]), and toString(value).

The Character class wrapper has many methods available, some of which are: isLowerCase(char c), isUpperCase(char c), isDigit(char c), isLetter(char c), isLetterOrDigit(char c), isWhitespace(char c), toLowerCase(char c), toUpperCase(char c), and compareTo(String g).

The Boolean class wrapper allows passing Boolean values (true and false) by reference.

Literal Constants

Literal constants are values that do not change within a program. Numeric constants default to integer or double unless a suffix is appended. Note that a character can be represented by an ASCII equivalent. The literal types are:

  • Boolean: true, false
  • Character: 'c', '\f'
  • String: "Fred", "B and E"
  • Null: null
  • Integer: 5, 0xFF (hexadecimal),
    073 [leading zero] (octal), 5l (long)
  • Floating Point: 2.543, -4.1E-6 (double)
    2.543f, 8e12f (float)

Note: Do not use leading zeros to format integers as this can cause an unintended octal meaning. Use spaces instead!

Escape (aka backslash) sequences are used inside literal strings to allow print formatting as well as preventing certain characters from causing interpretation errors. Each escape sequence starts with a backslash. Available sequences are:

SeqUsage SeqUsage
\bbackspace \\backslash
\fformfeed \"double quote
\nnewline \'single quote
\rcarriage return \###Octal encoded character
\thorizontal tab \uHHHHUnicode encoded character


Variables are temporary data holders. Variable names are identifiers and must be declared with a datatype. Java is a strongly typed language as the variable can only take a value that matches its declared type. This enforces good programming practice and reduces errors considerably. When variables are declared they may or may not be assigned an initial value such as int counter;. Multiple variables of the same datatype may be declared in a single statement such as byte x,y,z;

Variables can be made constant or read only by prepending the modifier final to the declaration. Java convention uses all uppercase for final variable names.


Arrays allows storing several related values in the same variable (eg. a set of marks). Declaration of an array only forms a prototype or specification for the array. Multi-dimensional arrays are considered to be arrays of arrays (of arrays...). Note that in a declaration the brackets are left blank.

int i[];       /* one dimension array */
char c[][];    /* two dimension array */
float [] f;    /* geek speak way */
Bowl shelfA[]; /* array of objects */

Array memory allocation is assigned explicitly with the new operator and requires known static bounds (ie. number of elements). The number of items in an array can then be determined by accessing its length property. For a two dimensional array named m, m.length gives the number of elements in its first dimension and m[0].length gives the number of elements in its second dimension.

Array1=new int[5]; //previously declared, now created
int markArray[]=new int[9]; //declaration and allocation at same time
int grades[]=new int[maxMarks]; //maxMarks must be a positive integer

Array initialization can take place at declaration. Size of the array is determined by the contents.

String flintstones[]={"Fred", "Wilma", "Pebbles"}; //init values
String pairs[][]={

Operators and Expressions

Operators are actions that manipulate, combine or compare variables. They fall into several categories as follows:

Assignment:            = += -= *= /= %=
Arithmetic:            + - * / % (modulus) ++ (increment) -- (decrement)
String Concatenation:  +
Comparison-Relational: > >= < <=
Comparison-Equality:   == !=
Bitwise Comparison:    ~ & | ^ (xor) << >> >>>
Bitwise Assignment:    &= |= ^= (xor) <<= >>= >>>=
Boolean Comparison:    ! & | ^ && ||
Object Creation:       new (eg int a[] = new int[10];)
Class of Object:       instanceof
Casting of Type:       (var_type)
Ternary:               ? (eg (bVal) ? tExpr : fExpr)

The logical-and operator (&&) returns a boolean. If A is true, A && B evaluates to B. If A is false, A && B evaluates to A. This is short-circuit logic. If A is false, B isn't even evaluated.

The logical-or operator (||) is similar. A || B evaluates to A if A evaluates to true and to B if A evaluates to false.

Note: Changes in data type are done explicitly using a cast operation. For example a = (int) b; assumes a is of type int and b is of another type.

Expressions are phrases used to combine values and/or operands using operators to create a new value. One example of an expression is 5 + 3.

Statements and Blocks

Statements are complete program instructions made from constants, variables, expressions and conditions. Statements always end with a semicolon. A program contains one or more statements.

Assignment statements use an assignment operator to store a value or the result of an expression in a variable. Memory allocation is done at the time of assignment. Primitive datatypes have static allocation with size determined by their type. Simple examples include first_name="Fred"; and count +=;

Variables may be assigned an initial value when declared. This is considered good programming practice. Examples are boolean fileOpenFlag=true;, int finalScore=null; and final float PI = 3.14159;

Array variables can use a shortcut method of initial value assignment. Examples are:

int v[]={2,4,20}; //declaration/creation/assignment in one step!
int m[][]={{2,3,4}, {4,5,6}, {1,1,1}}; // two dimensional array

Local variables must be assigned a value prior to use. There is no default assumption. Failure to initialize will cause a compiler error! Properties (aka field variables) have defaults but initialization is good programming practice.

Blocks are sets or lists of statements enclosed in curly brackets. Variables maintain their definition (or 'scope') until the end of the block that they are defined in. This is why variable declaration and assignment can be a two step process.

Note: It is good programming practice to declare variables in as nested a scope as possible to limit the chance of spurious assignment.

Beware: A variable's content can be hidden by redeclaration of its name within a nested block. At times this is convenient but beginning programmers should avoid reuse (ie. 'overload') of variable names.

JR's HomePage | Comments [jatutor1.htm:2016 02 09]