Wednesday, February 15, 2012

Chapter -3 Flow Control

                                       Chapter -3 Flow Control

Flow control describes the run time order in which the statements are going to execute.


                                          Flow control

Selection stmts               Iterative stmts                   Transfer stmts
EX:                                 Ex:                                      Ex:
If                                     For                                     break
If else                            While                                    continue
Switch                           do while                                return
                                                                                  Try, catch, finally
                                                                                  Assertion


3.1. Selection statements





3.1.1. if else:
The else part and curly braces are optional
The valid argument for if statement is Boolean only.
With out curly braces under if only one statement is allowed but that statement never is
declarative statement.
The compiler always map the else statement which is nearest if which does not have
else already. Hence there is no dangling-else problem in java.
3.1.2. Switch:
The valid arguments for the switch statement are byte, char, short and int.
Curly braces are mandatory.
Both case and default are optional.
All the statements inside switch must be associated with some case or default. This
means independent statements are not allowed.
All the case labels must be in the range of switch argument.
All the labels must be compile time constants and it may be compile time constant
expressions also.
The case labels never be duplicated.
The default statement we can place anywhere in the switch but JVM always consider the
default after the cases only.



int x=1;
switch (x)
{
Case 1: System.out.println(“1”);
Case 2: System.out.println(“2”);
Case 3: System.out.println(“3”);
break;
default: System.out.println(“default”);
}
If x =2 then output is 2,3
In execution flow if JVM finds the matched case then from that case all the statements
will execute until end of the switch or break statement.


3.2. Iteration Statements
3.2.1. While( ):
This loop is preferable if we don’t know the number of iterations in advance.
The valid argument for the while loop is boolean.
Curly braces are optional.
With out curly braces only one statement is allowed but that statement never be
declarative.
final int x = 10;
while(x>15)
{
System.out.println(“hi”);
}
System.out.println(“Hello”);
This program will give compile time exception. Because unreachable statement.
3.2.2. Do While( ):
If we want to execute the loop body at least one time then we will go for do while( )
loop.
Curly braces are optional.
With out curly braces only one statement is allowed that statement never be declarative.
With out curly braces we should keep exactly one statement at least empty statement.
If the argument is compile time constant true then the statement after do while will
become unreachable statements.

3.2.3. for( ):
Curly braces are optional.
With out curly braces only one statement is allowed that statement never be declarative.
for(initialization;condition;incre/decrement)
Initialization section:
In initialization section we are not allowed to declare variables of different types.
For(int a= 10, byte b=20; ; ;) this is invalid.
We are allowed to declare the variables of some type but the type we should specify
only on time.
for(int i=0,int j=1; ; ;) is invalid.
for(int i=0,j=1; ; ;) is valid.
In the initialization part we are allowed to take any java statement including
System.out.println( ) also.
Conditional expression:
The conditional expression is optional. The default value is true.
The conditional expression is any java statement but it should return boolean.
All the three parts of for loop is optional.
for( ; ; ) is valid in java.
Break and Continue:
Break statement we can use
Inside loops (to break the loop)
inside switch statement (to come out from the switch)
inside labeled blocks (to come out from the block)
If we are using break anywhere else we will get a compile time exception saying break
outside switch or loop
Continue statement we should use only in the loops. If we are using anywhere else we
will get a compile time exception saying continue outside loop.












chapter 2 Continuation

2.9 Instance operator:

                 a instance of B
                        |               \
                        |                 \
                        |                  \
       Object Reference      Class Name or Interface Name 

 EX1: String s = “surya”
System.out.println(s intanceof String); ------------gives output as true.
Ex2: Thread t = new Thread();
System.out.println(t instanceof Thread);------------gives true
System.out.println(t instanceof Object);------------gives true
System.out.println(t instanceof Runnable);------------gives true
Ex3: Object o = new Object();
System.out.println(o instanceof String);-----------gives false
Note: The type of a and B must be related, otherwise compile time exception like
incompatible types.
The null instance of any thing, the result is always false.


2.10. Type cast operator:

Type casting: Implicit type casting and Explicit type casting


Implicit type casting                                 |          Explicit type casting
This is responsibility of compiler             |            This is responsibility of programmer
No loss of information                              |            This result gives loss of information
Implicit type casting we can called          |             Explicit type casting we can called as Narrowing

as widening 

byte------short----}------>
                                          int----long-----float----double.


char----------------}------->


From left to right is implicit type casting.
From right to left is explicit type casting.
int a = 10;
byte b = a; gives compile time exception like required: byte, found: int.
int a = 10;
byte b = (byte)a;
This is explicit type casting. i.e. we are converting int type to byte type.


2.11. Conditional operator:
int a = (Boolean condition)?value if true: value if false;
int a = (b>40)?30:20;

2.12. New operator:
This operator is used for creation of objects.
[ ] is used for declaring and creating arrays.
class Demo
{
public static void main(String args[])
{
System.out.println(m(1)+m(3)*m(4)/m(5)+m(6));
}
public static int m(int i)
{
return i;
}
}





Output is: 9
1 + 3 * 4 / 5 + 6 = 1 + 12 / 5 + 6 = 1 + 2 + 6 = 9.
Before applying any operator all the operands must be evaluated from left to right and
then operators have to be applied based on the precedence.

2.13. Precedence of java operators:


Operator Precedence
OperatorsPrecedence
postfixexpr++ expr--
unary++expr --expr +expr -expr ~ !
multiplicative* / %
additive+ -
shift<< >> >>>
relational< > <= >= instanceof
equality== !=
bitwise AND&
bitwise exclusive OR^
bitwise inclusive OR|
logical AND&&
logical OR||
ternary? :
assignment= += -= *= /= %= &= ^= |= <<= >>= >>>=