The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search

Trail: Learning the Java Language
Lesson: The Nuts and Bolts of the Java Language

Questions and Exercises: Variables

Questions

Here's the Sort(in a .java source file) program again:
public class Sort {
    public static void main(String[] args) {
        int[] arrayOfInts = { 32, 87, 3, 589, 12, 1076,
                              2000, 8, 622, 127 };

        for (int i = arrayOfInts.length; --i >= 0; ) {
            for (int j = 0; j < i; j++) {
                if (arrayOfInts[j] > arrayOfInts[j+1]) {
                    int temp = arrayOfInts[j];
                    arrayOfInts[j] = arrayOfInts[j+1];
                    arrayOfInts[j+1] = temp;
                }
            }
        }

        for (int i = 0; i < arrayOfInts.length; i++) {
            System.out.print(arrayOfInts[i] + " ");
        }
        System.out.println();
    }
}
  1. Identify all of the variables in the program.
  2. What is the name of each variable?
  3. What is the data type for each variable?
  4. What is the scope of each variable?

Exercises

Recall the MaxVariables(in a .java source file) program.
public class MaxVariables {
    public static void main(String args[]) {

        // integers
        byte largestByte = Byte.MAX_VALUE;
        short largestShort = Short.MAX_VALUE;
        int largestInteger = Integer.MAX_VALUE;
        long largestLong = Long.MAX_VALUE;

        // real numbers
        float largestFloat = Float.MAX_VALUE;
        double largestDouble = Double.MAX_VALUE;

        // other primitive types
        char aChar = 'S';
        boolean aBoolean = true;

        // display them all
        System.out.println("The largest byte value is " + largestByte);
        System.out.println("The largest short value is " + largestShort);
        System.out.println("The largest integer value is " + largestInteger);
        System.out.println("The largest long value is " + largestLong);

        System.out.println("The largest float value is " + largestFloat);
        System.out.println("The largest double value is " + largestDouble);

        if (Character.isUpperCase(aChar)) {
            System.out.println("The character " + aChar + " is upper case.");
        } else {
            System.out.println("The character " + aChar + " is lower case.");
        }
        System.out.println("The value of aBoolean is " + aBoolean);
    }
}
  1. Rewrite it to display the minimum value of each integer data type. You can either guess at the names of the class variables within each type wrapper class that provides the minimum, you can look them up in the javadocs, or you can calculate the values based on what you know about the size of the primitive types.
  2. Can you guess the name of a different method in the Character class that determines whether or not a char is a upper case? Use that method in the program.
  3. Modify the program so that aBoolean has a different value.
Check your answers.(in the Learning the Java Language trail)

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search