Funding for 'IT Lab' Project, Phase 1: Progress of sticker sales. Purchase a sticker to help us reach our target.Updated: 2010-02-28 11:53
10.7%
Sun Certified Programmer for the java 2 platform study guide


by Kasun Herath



Welcome everyone, to the second in the series of articles aimed at helping those who intend to sit for the sun certified programmer for java 2 platform (SCJP). Today we will cover the section 2 of the exam objectives, namely flow control.

 Have a look at the objectives here.

http://www.sun.com/training/catalog/courses/CX-310-065.xml

 

Section 2: Flow Control.

 

In a programming language flow control means controlling the order in which the statements would be executed. Any programming language is supposed to offer flow control techniques. The most common of them are if statement, for loop, switch statement. Apart from that java offers a technique called exceptions. We will first look at the simplest of them all, the if statement.

 

IF statement

If statement is used to decide the flow of the execution depending on a given condition.

A basic if statement could be written as this


If (boolean expression){
        //statements
    }


The statements within the if statement would only be executed if the boolean expression is true.

Normally this boolean expression is a comparison. For example if we want some statement to be run only if a variable x is greater than 10 it could be implemented as,


if(x > 10){
        //statement
    }

If we want to write two execution paths depending on value the of variable x it could be accomplished using the else statement.

 if(x > 10){
        //statements
    }
else{
        //statements
    }

If we want to execute many different execution orders depending on the value of x we could add if-else statements.

if(x == 10 ){

        //execution order 1

    }

 

else if(x == 12){

        //execution order 2

    }

 

else if(x == 20){

        //execution order 3

    }

 

else{

        //execution order 4

    }

Hint:

When comparing a value in java the comparison operator should be used ( == ) rather than (=) which is the equalization operator.

Hint:

When comparing a string value use the equals method of the class String rather than the comparison operator. The reason for that would be covered in a later chapter.

ex

if (new String(“colombo”).equals(“COLOMBO”)){

            //statements}


Switch Statement

 Switch statement is an alternative to multiple if statements. Here is how to use a switch statement.



Int x = 10;

 

    switch(x){

        case 10:

            System.out.println(“x is 10”);

            break;

        case 20:

            System.out.println(“x is 20”);

            break;

        case 30:

            System.out.println(“s is 30”);

            break;

        default:

            System.out.println(“x is niether 10 or 20 or 30”);

    }



We must give the variable to be compared inside the switch statement and the values to be compared should be given with the keyword 'case'.

 

Hint:

Unlike in the if statement only char, byte, short, int and enums could be provided to the switch statement.

 

Loops

 

Loops are used to iterate the execution of a set of statements depending on a given condition.

The most basic loop is called the while loop.


while(condition){

        //statements

    }


The loop will iterate until the condition expression becomes false.

The mostly used loop is the for loop.

for (int i=0; i<10; i++){

        //statements

    }


This is how a for loop runs. First the variable i is initialized to 0. Then the condition statement is evaluated. Since 0 is less than 10 the statements are run once. Then i is incremented by once. After the first iteration the condition is again evaluated. Now i is 1, since 1 is less than 10 the set statements are run again. Then the i is incremented again. Likewise the set of sentences would be run until i becomes 10.

 

Probably the least used loop is the do-while loop.

      do{

        //statements

    }while(condition)


This is very much like the while loop except that the set of statements are executed once before the condition is evaluated.

 

Exceptions

 

Exceptions are abnormal situations which could crash the whole program. These situations could be a result of simple program bugs to hardware failures. When an abnormal situation arises an exception is thrown, to avoid the crashing of the program. An exception handler catches the thrown exception. They execute a set of given instructions to avoid the crashing of the program. The statements that could result in an exception are placed inside a try block. The try block is followed by exception handlers for every possible exception that could be thrown.

 

Let’s take an example where a number is divided by another number. If the dividing number is zero the result is unknown and program could crash if we somehow use the result. To avoid that if the divding number is zero an ArithmeticException is thrown.


try{

        int i=0;

 

        result a/i;

        }

catch (ArithmeticException arithmeticException){

            result = 100; 

        }

catch(NumberFormatException numberException){

            result = 100;

        }

If we suspect the code in the try block could thrown an ArithmeticException or a NumberFormatExceptoin we place two exception handlers to catch them. When the number is divided by the zero an ArithmeticException would thrown stopping other statements of the try block from executing. The exception would be caught by the required exception handler and the handler would be run.

 

On some occasions we need some specific statements to run whether or not an exception is thrown. The finally block is for such use. We place the finally block right after all the catch blocks, and the finally block would always execute whether or not any exception is thrown.

try{

        int i=0;

 

        result a/i;

        }

catch (ArithmeticException arithmeticException){

            result = 100; 

        }

finally{

            //some required statements

    }


Below are some commonly used exceptions

 

ArrayIndexOutOfBoundsException

When an used index for array is invalid

IllegalArgumentException

When an argument of a method is in a different format than it expected

NullPointerException

When attempting to use a object which is null

NumberFormatException

When a method receives a number not in the format it expects it

ClassCastException

When a class is casted to another class which cannot be casted

StackOverflowError

When memory is over flown

NoClassDefFoundError

When the JVM can’t found a required class file.

 


Share/Save
No votes yet

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options