Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
[PENDING: This page has not yet been updated.]
- 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
BasicsDemo
program.
- 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 ofargs
isString[]
, which means that it's an array of strings.sum
andcurrent
are both declared asint
, which is the integer type.- Question: What is the scope of each variable?
Answer:args
has scope within the entiremain
method.sum
is a local variable that is valid from its declaration to the end of themain
method.current
is available from its declaration to the}
that closes thefor
loop.
Exercises:Solution:
- Modify the
MaxVariablesDemo
program so thataBoolean
has a different value.- 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.- Can you guess the name of a method in the
Character
class that you can use instead ofisUpperCase
to determine the capitalization of a character? Modify theMaxVariablesDemo
program 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-2005 Sun Microsystems, Inc. All rights reserved.