Primitive and Reference Type Variables


Primitive and Reference Type Variables

There are two general classes of variables in Java ”primitive and reference. Primitive variables, also referred to as value types, are memory locations that hold the value of a primitive type. The primitive types in Java are boolean , byte , char , double , float , int , short , and long . Their general characteristics are shown in Table 8.1.

We can see from Table 8.1 that primitive variables store one value. The value can be a byte, a Unicode character, a number, or a boolean (true or false). The boolean type is very useful in that you can define logical variables that can control the flow of execution. You can code statements like "If the variable is true, do one thing. If it is false, do something else." Boolean variables are not available in Fortran or C.

Java defines three types of integer primitives of varying memory requirements and range. Java does not support unsigned integer types. There are two types of floating point primitives. The mostly commonly used numerical primitive types are int and double . Java uses the Unicode encoding scheme to represent character data. The Unicode encoding scheme represents each character as a 16-bit integer value and can represent all international character sets.

Table 8.1. Primitive Types

D ATA TYPE

D ESCRIPTION

S TORAGE R EQUIREMENTS

boolean

Logical value that is either true or false

1 bit

byte

One byte of data

1 byte

char

A Unicode character

2 bytes

short

An integer value between “32,768 and 32,767

2 bytes

int

An integer value between “2,147,483,648 and 2,147,483,647

4 bytes

long

An integer value between “9,223,372,036,854,775,808 and “9,223,372,036,854,775,807

8 bytes

float

A single-precision floating point value between 3.4e-38 and 3.4e+38

4 bytes

double

A double-precision floating point value between 1.7e “308 and 1.7e+308

8 bytes

We can also see from Table 8.1 why the type must be specified when the variable is declared. The primitive types require different amounts of memory and the compiler must know how much memory to allocate. Memory for primitive types is allocated on the part of memory known as the stack.

The second major variable type is the reference type. A reference type variable is a storage location that holds either a null reference or a reference to an object of the same type as the variable. Java arrays are also reference types. The value of a reference variable is the memory address at which the data associated with the object is stored. A reference is comparable to a pointer in C or C++. Unlike C or C++, you cannot use a Java reference type variable to directly access memory. You manipulate a reference variable by using the variable name rather than the reference itself.

Reference variables are different from primitive variables in that instance methods can be called on them. Indeed, instance methods can only be called outside of the class in which they are defined by using an object reference. The syntax for an instance method call is the reference variable name, a period, the method name, and the argument list. This process is dependent on the access available for the method. See Chapter 9 for further discussion on instance methods .

Reference variables can be used to access an object's fields. The syntax is the same as for calling methods: the variable name, a period, and the field name. This type of access is dependent on the access modifier given to the field. See the "Access Modifiers" section of this chapter for more details.

Java passes arguments to methods by value. What this means for primitive variables is that the value of the variable is passed to the method rather than a reference to the variable itself. Any changes made to the value inside the method are not reflected in the value stored by the variable. A primitive variable is not an object and cannot be passed to a method that takes an object as an input parameter. There are numerical wrapper classes that can be wrapped around a primitive variable. These are discussed in more detail in Chapter 15.

When a reference variable is passed to a method a copy of the reference to the object is passed to the method. This value is a pointer to the original object, so any changes made to the object through its reference in the method will be reflected in the original object.

Example: Using Primitive and Reference Variables

This example demonstrates how to declare, initialize, and manipulate both primitive and reference variables. The concepts shown in this example are explained in more detail later in this chapter. The SimpleGas5 class declares three primitive ( pressure , temperature , perfectGas ) and one reference ( name ) variable. Two constructors are provided to initialize the variables. The first constructor takes no arguments and gives the variables default values. The second constructor initializes the variables by assigning them the same value as the arguments passed to the method.

 public class SimpleGas5 {   private double pressure, temperature;   private String name;   private boolean perfectGas;   public SimpleGas5() {     pressure = 101325.0;     temperature = 207.4;     name = "air";     perfectGas = true;   }   public SimpleGas5(double p, double t, String nm,                     boolean pg) {     pressure = p;     temperature = t;     name = nm;     perfectGas = pg;   }     // These methods return the values of the variables   public double getPressure() {     return pressure;   }   public double getTemperature() {     return temperature;   }   public String getName() {     return name;   }   public boolean isPerfectGas() {     return perfectGas;   } } 

To use the SimpleGas5 class, we will write a driver program. The driver creates a SimpleGas5 variable and calls two of the methods defined in the SimpleGas5 class. The data members of the SimpleGas5 object are initialized by calling the four-argument constructor.

 public class GasDriver5 {   public static void main(String args[]) {     //  Create a SimpleGas5 object.  Initialize its     //  data members by calling a constructor     SimpleGas5 air =             new SimpleGas5(21.95, 207.8, "air", true);     System.out.println("gas name is " + air.getName());     System.out.println("pressure is " +                           air.getPressure());    } } 

Output ”

 gas name is air pressure is 21.95 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net