Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

Markup JavaScript Java O.O.P.L.

Computer Sci Home&Links

Tutorial 2 - Syntax & Grammar


Like many languages (eg. C, JavaScript, awk, perl) Java is based on a common syntax and grammar developed by the Bell Labs in the 60's. This makes it easy to cross over from one language to another based on program requirements, resources and politics. This tutorial on basic Java syntax and grammar (ie. programming in the small) assumes some introductory programming experience in another language.
lexical structure data types

literal constants variables and arrays

operators and expressions conditions,statements,blocks

Lexical Structure
The lexical structure of a programming language is the set of elementary rules that define what are 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: Java is case sensitive. Whitespace, tabs, and newline characters are ignored except when part of string constants. They can be added as needed for readability. Single line comments begin with // Multiline comments begin with /* and end with */ Documentary comments begin with /** and end with **/ Statements terminate in semicolons! Make sure to always terminate statements with a semicolon. Commas are used to separate words in a list Round brackets are used for operator precedence and argument lists. Square brackets are used for arrays and square bracket notation. Curly or brace brackets are used for blocks.

Keywords are reserved words that have special meanings within the language syntax. Identifiers are names for constants, variables, functions, properties, methods and objects. The first character mustbe a letter, underscore or dollar sign. Following characters can also include digits. Letters are A to Z, a to z, and Unicode characters above hex 00C0. Java styling uses initial capital letter on object identifiers, uppercase for constant ids and lowercase for property, method and variable ids. Note: an identifier must NOT be any word on the Java Reserved Word List.

Data Types
Java is a strongly-typed language. Its primitive data types are:
Type boolean char byte Bits Range 1 true, false 16 0 to 65,535 8 -128 to +127 short 16 -32,768 to +32,767 int 32 -231 to +2311 long 64 -263 to +2631 float 32 -3.4E+38 to +3.4E+38 (approx) double 64 -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: Simple datatypes are not part of the object hierarchy. Simple datatypes are passed by value and not by reference. Two methods can't refer to the same instance of a simple type. Some classes can only use members of another class and not a simple type.

The wrapped object lacks advanced methods needed for object manipulation

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, Character: 'c',

false

'\f' String: "Fred", "B and E" Null: null

Integer: 5, 0xFF (hexadecimal) 073 [leading zero] (octal), 5l (long), 0xFFlD (hex) 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. The available sequences are:
Seq Usage Seq \b backspace \\ \f formfeed \" \n \r \t Usage backslash double quote newline \' single quote carriage \### Octal return encoded character horizontal \uHHHH Unicode tab encoded character

Variables
Variables are temporary data holders. Variable names are identifiers. Variables are 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 or take on a value (initialized). Examples of each of the primitive datatypes available in Java are as follows:
byte x,y,z; declaration */ /* 08bits long, not assigned, multiple

short numberOfChildren; /* 16bits long */ int counter; long WorldPopulation; /* 32bits long */ /* 64bits long */

float pi; double avagadroNumber; boolean signal_flag; char c;

/* 32bit single precision */ /* 64bit double precision */ /* true or false only */ /* 16bit single Unicode character */

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
Arrays allow you to store 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[]; char c[][]; float [] f; Bowl shelfA[]; /* one dimension array */ /* two dimension array */ /* geek speak way */ /* 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[][] = { {"Adam","Eve"},{"Lucy","Ricky"},{"Fred","Ethel"},{"Bert","Ernie"}};

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: Boolean Comparison: Bitwise Comparison: Bitwise Assignment: Conditional: Object Creation: Class of Object: Casting of Type: == != > >= < <= ! & | ^ && || (&& are short circuit ops) ~ & | ^ (xor) << >> >>> &= |= ^= (xor) <<= >>= >>>= ? (eg (expr1) ? expr2 : expr3 ) new (eg int a[] = new int[10];) instanceof (var_type)

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.

Conditions, Statements and Blocks


Conditions are phrases that can be evaluated to a boolean value such as a comparison operator between two constants, variables or expressions used to test a dynamic situation. Examples are x <= 5 and bool_flag != true. Statements are complete program instructions made from constants, variables, expressions and conditions. Statementsalways 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! Field variables (aka properties) have defaults but initialization is good programming practice. Execution blocks are sets or lists of statements enclosed in curly brackets. Variables maintain their definition (or 'scope') until the end of the execution block that they are defined in. This is the reason why variable declaration andassignment can be a two step process. Note: It is a good rule of thumb 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 execution block. At times this is convenient but beginning programmers should avoid reuse (ie. 'overload') of variable names.

Tutorials - Table of Contents Tutorial 1 Objects,Applications& Applets Tutorial 2 - Syntax and Grammar Tutorial 3 - Control Flow Tutorial 4 Encapsulation & Classes Tutorial 5 - Inheritance & Polymorphism Tutorial 6 - Java Class Libraries Tutorial 7 - String Manipulation Tutorial 8 - Generics & Exceptions Tutorial 9 - File IO

Tutorial 10 GUI Swing Widgets Tutorial 11 Intermediat e Swing Tutorial 12 Advanced Swing Tutorial 13 Designing GUIs Tutorial 14 GUI Views and Studies Tutorial 15 Threads & Serialization Tutorial 16 Graphics and Imaging Tutorial 17 Java Networking

Tutorial 18 Debugging Appendices

JR's HomePage | Comments [jatutor2.htm:2011 04 15]

You might also like