|
Variables are locations in memory in which values can be stored. Each one has a name, a type, and a value.
Before we can use a variable, we have to declare it. After it is declared, we can then assign values to it. |
|
| Java actually has three kinds of variables: |
| 1. instance variables, |
| 2. class variables, |
| 3. And local variables. |
|
|
Instance variables, are used to define the attributes of a particular object. Class variables are similar to instance variables, except their values apply to all that class's instances (and to the class itself) rather than having different values for each object.
Local variables are declared and used inside method definitions,
Although all three kinds of variables are declared in much the same ways, class and instance variables are accessed and assigned in slightly different ways from local variables.
Java does not have global variables-that is, variables that are global to all parts of a program. Instance and class variables can be used to communicate global information between and among objects.
| Declaring Variables |
|
| To use any variable in a Java program, we must first declare it. Variable declarations consist of a type and a variable name: |
|
int myAge; String myName; boolean isTired; |
|
| Variable definitions can go anywhere in a method definition (that is, anywhere a regular Java statement can go), although they are most commonly declared at the beginning of the definition before they are used: |
|
public static void main (String args[]) { int count; String title; boolean isAsleep; ... } |
|
| We can string together variable names with the same type on one line: |
int x, y, z; String firstName, LastName; |
|
| We can also give each variable an initial value when we declare it: |
int myAge, mySize, numShoes = 28; String myName = "eBIZ"; boolean isTired = true; int a = 4, b = 5, c = 6; |
|
|
If there are multiple variables on the same line with only one initializer the initial value applies to only the last variable in a declaration.
We can also group individual variables and initializers on the same line using commas, as with the last example.
|
| Rules on Variable Names |
|
| Variable names in Java can start with a letter, an underscore (_), or a dollar sign ($). They cannot start with a number. After the first character, our variable names can include any letter or number. Symbols, such as %, *, @, and so on, are often reserved for operators in Java, so we should be careful when using symbols in variable names. The Java language uses the Unicode character set. The Java language is case sensitive, which means that uppercase letters are different from lowercase letters. This means that the variable X is different from the variable x, and a rose is not a Rose is not a ROSE. |
| Variable Types |
|
| In addition to the variable name, each variable declaration must have a type, which defines what values that variable can hold. |
|
| The variable type can be one of three things: |
• One of the eight primitive data types • The name of a class or interface • An array |
| Primitive Data Types |
|
| The eight primitive data types handle common types for integers, floating-point numbers, characters, and boolean values (true or false). |
| They're called primitive because they're built into the system and are not actual objects, which makes them more efficient to use. |
|
| Integer Type |
There are four Java integer types, each with a different range of values. All are signed, which means they can hold either positive or negative numbers. |
|
|
|
|
|
| Type | Size | Range |
| byte | 8 bits | -128 to 127 |
| short | 16 bits | -32,768 to 32,767 |
| int | 32 bits | -2,147,483,648 to 2,147,483,647 |
| long | 64 bits | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
|
|
|
|
| Floating-point type: |
Floating-point numbers are used for numbers with a decimal part. There are two floating-point types: float (32 bits, single precision) and double (64 bits, double precision). |
|
|
|
| Char type: |
The char type is used for individual characters. Because Java uses the Unicode character set, the char type has 16 bits of precision, unsigned.
| Constants |
|
| • Are similar to variables but, once initialized, their contents may NOT be changed? |
| • Are declared with the keyword final? |
| • By convention, have all capital letters in their identifier. This makes them easier to see within the code. |
|
|
|
| Example 1: |
| This program defines a number of constants and then displays some of their values. |
|
public class App { public static void main(String[] args) {
final boolean YES = true; final char DEPOSIT_CODE = 'D'; final byte INCHES_PER_FOOT = 12; final int FEET_PER_MILE = 5280; final float PI = 3.14F; final double SALES_TAX_RATE = .06; final String ADDRESS = "119 South Street";
// Display some of the values
System.out.println(INCHES_PER_FOOT); System.out.println(ADDRESS); } } |
|
|
|
|
| Example 2: |
| This program will not compile because an attempt is made to change the value of its constant. |
|
public class App1 { public static void main(String[] args) {
final double SALES_TAX_RATE = .06; SALES_TAX_RATE = .04;
// Display the sales tax rate
System.out.println(SALES_TAX_RATE); } }
Previous Page Scroll Up Next Page |
|
|
|
|
|
|
|