TYPES, VALUES AND VARIABLES(11)
TYPES, VALUES AND VARIABLES(11)
KINDS OF VARIABLES:
6. Exception handler parameter: an exception handler parameter is created each time an exception is caught by the catch clause of a try statement. The new variable is initialized with the actual object associated with the exception. The exception handler parameter ceases to exist when execution of the block associated with the catch clause is complete.
public class ExceptionHandler {
String name;
void writeName(String typed){
try{
name = typed;
}
//exception handler created here. variable of
//type NumberFormatException
catch (NumberFormatException n){
System.out.println("Wrong entry, please.");
}
}
}
7. Local Variable: is declared by a local variable declaration statement. When the flow of control enters a block or for statement, a new variable is created for each local variable declared in a local variable declaration statement immediately contained in a block or for statement.
A local variable declaration statement may contain an expression which initializes the variable. The local variable with an initializing expression is not initialized however until the local variable declaration statement that declares it is executed. The local variable effectively ceases to exist when the execution of the block or for statement is complete.
public class ArrayComponents {
//an array of integers as components
int[] five = new int[5];
//this method takes all the default values
//according to jls, 0 and prints them out.
void defaultvalue(){ //local variable declaration block
for (int i=0;i<5;i++ ){ //new variable i, for statement
System.out.print("The ");
switch(i){
case(0): System.out.print("first ");break;
case(1): System.out.print("second ");break;
case(2): System.out.print("third ");break;
case(3): System.out.print("fourth ");break;
case(4): System.out.print("fifth ");break;
default: ;break;
}
System.out.println("value is: "+five[i]);
}
}
}
Final Variables: a variable declared final may be assigned to only once. Never assign a once assigned final variable unless it has been unassigned prior to this.
A blank final is a final variable without an initializer in its declaration.
A final variable referring an object refers only to the object which returns its value. See example.
public class FinalExample {
static String bobby = "friend";
static final String drumboy = bobby; //refers to object
public static void main(String[] args){
System.out.println("Final drumboy is: "+drumboy);
//let's change the object
bobby = "enemy";
//but final will still refer to the first object
//you see why final is very useful?
System.out.print("Final drumboy after object reference change is: ");
System.out.println(drumboy);
}
}
Initial values of variables:
Every variable in a program must have a value before usage.
1. each class variable, instance variable, or array component is initialized with a default value when it is created.
- for type byte, the default is zero, (byte)0
- for type short, (short)0
- for type int, 0
- for type long, default is 0L
- for type float, default is positive zero, 0.0f
- for type double, default is positive zero, 0.0d
- for type char, the default value is the null character, i.e ‘\u0000’
2. each method parameter is initialized to the corresponding argument value provided by the invoker of the method
3. each constructor parameter is initialized to the corresponding argument value provided by a class instance creation expression or explicit constructor invocation.
4. an exception-handler parameter is initialized to the thrown object representing the exception.
5. a local variable must be explicitly given a value before use, either by initialization or assignment, in a compiler verifiable way using the rules for definite assignment.
No comments:
Post a Comment