Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
An object stores its state in variables.You must explicitly provide a name and a type for each variable you want to use in your program. The variable's name must be a legal identifier an unlimited-length sequence of Unicode characters that begins with a letter. You use the variable name to refer to the data that the variable contains. The variable's type determines which values it can hold and what operations can be performed on it. To give a variable a type and a name, write a variable declaration, which generally looks like this.
Definition: A variable is an item of data named by an identifier.In addition to the name and type that you explicitly give a variable, a variable has scope. The section of code where the variable's simple name can be used is the variable's scope. The scope is determined implicitly by the variable declaration's location; that is, where the declaration appears in relation to other code elements. You'll learn more about this in the Scope section.type nameThe boldface type in the following program, called
MaxVariablesDemo
, highlights all the variable declarations in the program.This is the output from this program.public class MaxVariablesDemo { 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 uppercase."); } else { System.out.println("The character " + aChar + " is lowercase."); } System.out.println("The value of aBoolean is " + aBoolean + "."); } }The following sections further explore the various aspects of variables:The largest byte value is 127. The largest short value is 32767. The largest integer value is 2147483647. The largest long value is 9223372036854775807. The largest float value is 3.4028235E38. The largest double value is 1.7976931348623157E308. The character S is uppercase. The value of aBoolean is true.The
- Data Types
- Variable Names
- Scope
- Variable Initialization
- Final Variables
- Summary of Variables
- Questions and Exercises: Variables
MaxVariablesDemo
program uses two items with which you might not yet be familiar and are not covered in variable discussion: several constants namedMAX_VALUE
and anif
-else
statement. EachMAX_VALUE
constant is defined in one of the number classes provided by the Java platform and is the largest value that can be assigned to a variable of that numeric type. These classes are covered in the Numbers section. Theif
-else
statement is covered later in this chapter in The if and else Statements section.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.