• No se han encontrado resultados

través de medidas preventivas y de las prestaciones y servicios necesarios La Ley establecerá los

T ABLA 3.9: L AS VARIABLES UTILIZADAS PARA MEDIR LA CAPTURA DEL REGULADOR

might not be as obvious if you hadn’t seen any other approach.

In the following discussion, to differentiate the simple use of the class construct used here, from its later use to create ADTs, we use the generic term recordin place of class. Logical Level

A record is a composite data type made up of a finite collection of not necessarily homogeneous elements called fields. Accessing is done directly through a set of named field selectors.

We illustrate the syntax and semantics of the component selector within the con- text of the following program:

TEAM

FLY

public class TestCircle {

static class Circle {

int xValue; // Horizontal position of center

int yValue; // Vertical position of center

float radius;

boolean solid; // True means circle filled

}

public static void main(String[] args) {

Circle c1 = new Circle(); c1.xValue = 5; c1.yValue = 3; c1.radius = 3.5f; c1.solid = true; System.out.println("c1: " + c1); System.out.println("c1 x: " + c1.xValue); } }

The above program declares a record structure called Circle. The main method instantiates and initializes the fields of the Circlerecord c1, and then prints the record and the xValuefield of the record to the output. The output looks like this:

c1: TestCircle$Circle[at]111f71 c1 x: 5

The Circlerecord variable (the circle object) c1is made up of four components (or fields, or instance variables). The first two, xValue and yValue, are of type int. The third, radius, is a float number. The fourth, solid, is a boolean. The names of the components make up the set of member selectors.

The syntax of the component selector is the record variable name, followed by a period, followed by the member selector for the component you are interested in:

If this expression is on the left-hand side of an assignment statement, a value is being stored in that member of the record; for example:

c1.xValue = 5; c1.xValue member selector period struct variable

84 | Chapter 2: Data Design and Implementation

If it is used somewhere else, a value is being extracted from that place; for example: output.println("c1 x: " + c1.xValue);

Application Level

Records are useful for modeling objects that have a number of characteristics. Records allow us to associate various types of data with each other in the form of a single item. We can refer to the composite item by a single name. We also can refer to the different members of the item by name. You probably have seen many examples of records used in this way to represent items.

We declare and instantiate a record the same way we declare and instantiate any Java object; we use the newcommand:

Circle c1 = new Circle();

Notice that we did not supply a constructor method in our definition of the Circle class in the above program. When using the class as a record mechanism it is not neces- sary to provide a constructor, since the record components are not hidden and can be initialized directly from the application. Of course, you can provide your own construc- tor if you like, and that may simplify the use of the record. If no constructor is defined, Java provides a default constructor that initializes the constituent parts of the record to their default values.

In the previous section we discussed how primitive types such as ints are handled “by value.” This is in contrast to how all nonprimitive types, including records or any objects, are handled. The variable of a primitive type holds the value of the variable, whereas a variable of a nonprimitive type holds a referenceto the value of the variable. That is, the variable holds the address where the system can find the value of the vari- able. We say that the nonprimitive types are handled “by reference.” This is why, in Java, composite types are known officially as reference types. Understanding the ramifi- cations of handling variables by reference is very important, whether we are dealing with records, other objects, or arrays.

The differences between the ways “by value” and “by reference” variables are han- dled is seen most dramatically in the result of a simple assignment statement. Figure 2.7 shows the result of the assignment of one intvariable to another intvariable, and the result of the assignment of one Circleobject to another Circleobject. Actual circles represent the Circleobjects in the figure.

When we assign a variable of a primitive type to another variable of the same type, the latter becomes a copy of the former. But, as you can see from the figure, this is not the case with reference types. When we assign object c2 to object c1, c1 does not become a copy of c2. Instead, the reference associated with c1 becomes a copy of the reference associated with c2. This means that both c1 and c2 now reference the same object. The feature section below looks at the ramifications of using references from four perspectives: aliases, garbage, comparison, and use as parameters.

Figure 2.7 Results of assignment statements

intA intA = intB intB 15 10 intA intB 10 10 c1 c2 c1 c2

initial state operation final state

c1 = c2

Java includes a reserved word null that indicates an absence of reference. If a ref- erence variable is declared without being assigned an instantiated object, it is automati- cally initialized to the value null. You can also assign nullto a variable, for example: c1 = null;

And you can use nullin a comparison: if (c1 == null)

output.println("The Circle is not instantiated");

Ramifications of Using References

Outline

Documento similar