Monday, November 16, 2009

Basics: Literals, arraycopy(), Statements

Literals
A literal is a source code representation of a fixed value; literals are represented directly in the code without requiring computation. In general, its possible to assign a literal to any primitive type variable as shown below:

boolean answer = false;
char initial = 'D';
String[] name = "Jackson";

Literals assigned to 'integer' type variables can be expressed using decimal, octal, or hexadecimal number systems. Either of the 3 systems will give the same value. 'Char' and 'String' type literals may contain unicode characters. If unicode is not supported by the system, then with the help of "Unicode Escape" i.e "\u" suffixed with the appropriate code will display the corresponding unicode character.

"S\u00ED" is "Si"

Char literals are always used with single quotes while String literals are used with double quotes.

Copying Arrays
The System class has an arraycopy method that you can use to efficiently copy data from one array into another:

public static void arraycopy(Object sourceArray,
int startingPoint,
Object targetArray,
int targetStartingPoint,
int lenght)

The values in sourceArray, beginning from startingPoint upto the length specified are copied into the targetArray from the targetStartingPoint of the targetArray

Sample statement for arraycopy() method:
"System.arraycopy( copyFrom, 2, copyTo, 0, 7 );"


Classification of Statements
A statement is a single unit of execution. Statements can be classified into 3 types with each having subtypes:
1. Expression Statements
    o assignment statements
    o increment statements
    o method invocation statement
    o object creation statement
2. Declaration Statements
3. Control Flow Statements
    o decision-making statements
    o looping statements
    o branching

Branching Statements
'continue' and 'break' are the two branching statements. Each of them has 2 forms 'labeled' and 'unlabeled'. The unlabeled statements are used only to terminate or skip the innerblocks while the labeled statement blocks are used to terminate or skip the outerblocks.

The Syntax for labeled break and continue statements is:
label:
    OuterBlock
      InnerBlock
        break or continue;

No comments:

Post a Comment