Friday, March 13, 2015

Arrays in java

Posted by Sona Mathews


1.     An array is a group of variables that have a common name.
2.     An array is an abstract data type.
3.     Arrays are stored in contiguous memory locations.
4.     They are fixed length entities i.e. they remain the same once they are created.
5.     Arrays of any type can have one or more than one dimensions.
6.     The element of an array can be accessed by its index also known as subscript.
7.     With the help of arrays we can group related information.
8.     Array is a collection of homogeneous elements with same name.

Example:
1.     The array name is c.
2.     The elements in the array are referred to as c [0], c [1], to c [11]. This means 12 elements from 0-11.

Arrays in java

Creating an array:
In Java arrays are implemented as objects.
In order to create an array we must first declare an array and then instantiate it using new operator.
Declaring/Instantiating an array:
     int c[] = new int[12];

This declaration and array creation create an array containing 12 int elements and store the reference in variable c.

The task can be performed in two steps:
            int c [];                       //declare the array variable
            c = new int [12];     //create the array; assign to array variable
            float [] salary = new float [100];           
1.     A salary is an array variable which stores the reference to the array which contains 100 float type values.
2.     The default values for an array of numeric elements are 0. For Boolean arrays are false. For char arrays are ‘\u0000’ (space). For an array of class type are null.

Initializing arrays:
Once an array is created, each element of an array is set to its default initial value according to its type.
User can also provide value other than the default value by using array initialization.
           data type [] array name = {value1, value2 …};
           int [] num = {5,10,15,20};

The above initialization is also equivalent:
int [] num = new int [5];
num [0] = 5;
num [1] = 10;
num [2] = 15;
num [3] = 20;
num [4] = 25;

How to get length of an array:
1.     The user can access the length of an array i.e. the number of elements it holds.
2.     By using its length field which is public and final implicitly.
3.     The length field is associated with each and every array which has been instantiated.
4.     Length field can be directly accessed using the array name and a dot operator because it is a public member.
        num.length;    //to access the number of elements in an array called num.

1.     Array with length zero is said to be an empty array.
2.     Length field of an array can also be used to control the numerical for loop which iterates over the elements of an array.

One dimensional array: They are a list of like-typed variables which have only one index or subscript.
Multidimensional array: Arrays of Arrays
1.     These are the arrays which contain more than one index and can be stored in more than multiple dimensions.
2.     The information is arranged in rows and columns.
3.     But Java does not directly support multidimensional arrays.

Two dimensional arrays: these arrays require two indexes to identify a particular element.
Left index refer to rows.
[0][0]
[0][1]
[0][2]
[0][3]
[0][4]
[1][0]
[1][1]
[1][2]
[1][3]
[1][4]
[2][0]
[2][1]
[2][2]
[2][3]
[2][4]
[3][0]
[3][1]
[3][2]
[3][3]
[3][4]

Right index refers to columns.
int two [] [] = new int [4] [5];
This allocates a 4 by 5 two dimensional array and this matrix is implemented as an array of arrays of int.
Read More

Tuesday, March 10, 2015

Control Statements in Java

Posted by Sona Mathews


Control Flow Statements are used to break the flow of execution of the program by employing the following categories:

·        Decision making (Selection statements)—if, nested ifs, if-else-if ladder, switch.
·        Looping (Iteration statements)—while, do-while, for.
·        Branching (Jump statements)—break, continue, return.

Selection statements: These statements works on the conditions which are known only at the run time.

1)    if statement: It is also known as conditional branch statement. The set of statements are executed if the condition is true.
·        Syntax:
if(condition) statement1;
else
statement2;

·        Explanation:
·        Each statement is enclosed within a pair of curly braces, which is also known as the Block.
·        The condition here can be any expression which returns a Boolean value i.e. True or False.
·        The else clause is optional here. If the condition is true, then statement1 is executed. Otherwise, if condition is false then else clause works i.e. statement2 is executed.

2)    Nested if statement: This means using one if or else if statement inside another if or else if statement.

·        Syntax:

if(condition1){
       //  executes when the condition1 is true
if(condition2){
     // executes when the condition2 is true
}
}


·        Note: The main thing to remember is that the else clause always refers to the nearest if clause that is within the same block.

3)    if-else-if ladder: This executes when particular conditions are true and else works when none of the above conditions are true.
if(condition)
   statement;
else if(condition)
   statement;
else if (condition)
   statement;
     .
     .
     .
     .
else
   statement;

·        Explanation:
·        The if statements are executed from the top down approach.
·        When one of the condition controlling if statements are true, the statements associated with particular if is executed and remaining ladder is bypassed.
·        If none of the condition is true then the final else statement will be executed. Final else clause acts as the default condition.
4)    switch statement: It is a multiway branch statement. It allows a variable to be tested for equality against a list of values. Value is called case.
switch(expression){
    case value 1:
        //statement sequence
    break;
   case value 2:
      //statement sequence
   break;
    .
    .
    .
    .
   case value n:
       //statement sequence
  break;
  default;
      //default statement sequence
}

·        Explanation:
·        The expression must be byte, int, short, char.
·        The values in the case must be of a type compatible with the expression.
·        The expression value is compared with the case value. If the match is found the statement sequence is executed of that particular case.
·        If none of the value is matched with the expression value then the default statement is executed.
·        The default statement is optional.
·        If there is no default and no case is matched then no action is taken.
·        The break statement is used to terminate the specific statement sequence.
·        The break statement is also optional. But it is always advised to use break in switch statements.

Looping statements: These are also known as the Iteration statements in Java. A loop executes repeatedly the same set of instructions until a termination condition is met.

1)    while loop: while loop is used to repeat a task for a certain amount of time. The set of statements are executed when the condition is true. The execution stops when the condition becomes false.
while(condition){
     //body of loop
}

·        Explanation:
·        The condition can be Boolean expression.
·        The body of the loop is executed till the condition remains true.
·        When the condition is false then the control passes to the next line of code following the loop.

2)    do-while loop: The difference between these two loops are that do-while evaluates its expression at the bottom of the loop instead of top, so the statements in the do block are executed at once.
do{
   //body of the loop
}while(condition);

·        Explanation:
·        It first executes the body of the loop and then evaluates the conditional expression.
·        If the expression is true the loop will repeat. Otherwise it terminates.

3)    for loop: for loop provides a compact way to iterate over a range of values. It loops until a particular condition is satisfied.

for(initialization; condition; iteration){
  //body of the loop
}
·        Explanation:
·        The initialization expression is used to initialize the loop and is always executed only once as the loop begins.
·        Next is that the condition is evaluated and if the condition is false the loop terminates and if it is true then the body of the loop is executed.
·        The iteration portion increments or decrements the loop variable.
·        The process repeats until the controlling expression becomes false.
Branching statements: Java supports three jump statements i.e. break, continue and return. These statements are used to transfer the control to another part of the program.
1)    Break statement: It can be used to exit a loop.
·        It is used to terminate the statements in switch.
·        It can be used as the form of “goto”.
·        User can break out of one or more blocks of code by using break.
·        The blocks are not required to be part of a loop or a switch, it can be any block.
·        break works with a label and provides benefits of goto.

break label;

·        Label is the name of the label which is used to identify the block of code.
·        The control is transferred out from the named block of code.
·        The labeled block of code should enclose the break statement.

2)    Continue statement: The continue statement transfers the control directly to the conditional expression that controls the loop in while and do-while loop.
·        In loop statements where early iteration is needed the continue statement provides a structured way for it.

3)    Return statement: This is the last control statement. It is used to return explicitly from a method.
·        It causes the control of program to transfer back to the invoker of the method.
·        It terminates the method in which it is executed.

Read More