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

Trail: Learning the Java Language
Lesson: Language Basics

Answers to Questions and Exercises: Variables

Answers to Questions

  1. Question: Which of the following are valid variable names?
    int
    anInt
    i
    i1
    1
    thing1
    1thing
    ONE-HUNDRED
    ONE_HUNDRED
    something2do
    
    Answer:
    int
    anInt
    i
    i1
    1
    thing1
    1thing
    ONE-HUNDRED
    ONE_HUNDRED
    something2do
    
  2. Answer the following questions about the BasicsDemo(in a .java source file) program.
    1. Question: What is the name of each variable declared in the program? Remember that method parameters are also variables.
      Answer: args, sum, current
    2. Question: What is the data type of each variable?
      Answer: The data type of args is String[], which means that it's an array of strings. sum and current are both declared as int, which is the integer type.
    3. Question: What is the scope of each variable?
      Answer: args has scope within the entire main method. sum is a local variable that is valid from its declaration to the end of the main method. current is available from its declaration to the } that closes the for loop.

Answers to Exercises

Exercises:
  1. Modify the MaxVariablesDemo(in a .java source file) program so that aBoolean has a different value.
  2. Rewrite the MaxVariablesDemo program to display the minimum value of each integer data type. You can guess what the names of these variables are or you can look them up in the API documentation.
  3. Can you guess the name of a method in the Character(in the API reference documentation) class that you can use instead of isUpperCase to determine the capitalization of a character? Modify the MaxVariablesDemo program to use that method instead of isUpperCase.
Solution:

Here's our solution to all three exercises, called MinVariablesDemo(in a .java source file), with the differences shown in red:

public class MinVariablesDemo {
    public static void main(String args[]) {

        // integers
        byte smallestByte = Byte.MIN_VALUE;
        short smallestShort = Short.MIN_VALUE;
        int smallestInteger = Integer.MIN_VALUE;
        long smallestLong = Long.MIN_VALUE;

        // real numbers
        float smallestFloat = Float.MIN_VALUE;
        double smallestDouble = Double.MIN_VALUE;

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

        // display them all
        System.out.println("The smallest byte value is " + smallestByte);
        System.out.println("The smallest short value is " + smallestShort);
        System.out.println("The smallest integer value is " + smallestInteger);
        System.out.println("The smallest long value is " + smallestLong);

        System.out.println("The smallest float value is " + smallestFloat);
        System.out.println("The smallest double value is " + smallestDouble);

        if (Character.isLowerCase(aChar)) {
            System.out.println("The character " + aChar + " is lower case.");
        } else {
            System.out.println("The character " + aChar + " is upper case.");
        }
        System.out.println("The value of aBoolean is " + aBoolean);
    }
}

Previous Page Lesson Contents Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.