We learn about Java variable scope, boxing/unboxing (wrapping) and Arrays
Scope of variables
You may notice that some lines are commented, because they cant be executed. Please un-comment them and try again, read the error messages.
public class W06aVarScope {
public static int a; //global
public static void main(String[] args) {
int b; //local to main() method
final int d = 1000;
System.out.println("Understanding variable scope.");
a = 100;
b = 200;
// c = 300; //cannot access from here
// d = 1500; //cannot do this
}
public static void varTest(){
int c; //local to vartest() method
a = 5000;
// b = 6000; //cannot access from here
c = 7000;
}
}
Box/unbox test
Lines 20 to 28 uses the primitive data type. You notice the number of ‘if’ statements required. Similar code can be written using the ‘Character wrapper class’. It’s more readable and has methods that make the job easy.
import java.util.Scanner;
/**
*
* @author azmeer
*/
public class W06bBoxTest {
public static void main(String[] args) {
System.out.println("Java boxing unboxing (wrapper) test");
Scanner in = new Scanner(System.in);
char choice;
String line;
System.out.print("Please enter your choice: ");
line = in.nextLine();
choice = line.charAt(0);
//using primitive data type
if (choice >= '1' && choice <= '9') {
System.out.println("You entered a number");
} else if (choice >= 'A' && choice <= 'Z') {
System.out.println("You entered a letter");
} else if (choice >= 'a' && choice <= 'z') {
System.out.println("You entered a letter");
} else {
System.out.println("You entered a symbol");
}
//using Character class (wrapper/box)
Character c = choice;
if (c.isDigit(c)) {
System.out.println("You entered a number");
}
if (c.isAlphabetic(c)) {
System.out.println("You entered a letter");
}
if (!c.isLetterOrDigit(c)) {
System.out.println("You entered a symbol");
}
}
}
Using the Number class
/**
*
* @author azmeer
*/
public class W06cNumberClass {
public static void main(String[] args) {
System.out.println("testing the Number class");
//autoboxing
Integer x = Integer.valueOf(9);
Double c = Double.valueOf(5);
Float a = Float.valueOf("80");
Integer b = Integer.valueOf("444", 16);
System.out.println(x);
System.out.println(c);
System.out.println(a);
System.out.println(b);
//unboxing
int n1 = Integer.parseInt("9");
double n2 = Double.parseDouble("5");
int n3 = Integer.parseInt("444", 16);
System.out.println(n1);
System.out.println(n2);
System.out.println(n3);
//number to string conversion/casting
Integer n4 = 5;
System.out.println(n4.toString());
System.out.println(Integer.toString(12));
}
}
Array usage
import java.util.Arrays;
/**
*
* @author azmeer
*/
public class W06dArrayTest {
public static void main(String[] args) {
System.out.println("Java Array test");
int[] marks = {22, 54, 15, 88, 31, 66, 45};
System.out.println("No of elements: " + marks.length);
System.out.println("Marks for 5th student: " + marks[4]); //elements starts from 0
//Using a for loop
System.out.println("\nUsing FOR loop");
for (int i = 0; i < marks.length; i++) {
System.out.println(marks[i]);
}
//Using an enhanced for loop (for-each)
System.out.println("\nUsing enhanced FOR loop");
for (int num : marks) {
System.out.println(num);
}
//sorting the array
Arrays.sort(marks);
System.out.println("\nSorted list");
for (int num : marks) {
System.out.println(num);
}
//fill with one value
Arrays.fill(marks, 0);
System.out.println("\nFIlled list");
for (int num : marks) {
System.out.println(num);
}
}
}