|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
- Question: Which of the following are valid variable names?
Answer:int anInt i i1 1 thing1 1thing ONE-HUNDRED ONE_HUNDRED something2doint anInt i i1 1 thing1 1thing ONE-HUNDRED ONE_HUNDRED something2do- Answer the following questions about the
BasicsDemoprogram.
- Question: What is the name of each variable declared in the program? Remember that method parameters are also variables.
Answer:args,sum,current- Question: What is the data type of each variable?
Answer: The data type ofargsisString[], which means that it's an array of strings.sumandcurrentare both declared asint, which is the integer type.- Question: What is the scope of each variable?
Answer:argshas scope within the entiremainmethod.sumis a local variable that is valid from its declaration to the end of themainmethod.currentis available from its declaration to the}that closes theforloop.
Exercises:Solution:
- Modify the
MaxVariablesDemoprogram so that
aBooleanhas a different value.- Rewrite the
MaxVariablesDemoprogram 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.- Can you guess the name of a method in the
Characterclass that you can use instead of
isUpperCaseto determine the capitalization of a character? Modify theMaxVariablesDemoprogram to use that method instead ofisUpperCase.Here's our solution to all three exercises, called
MinVariablesDemo, 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); } }
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.