next up previous
Next: Operations With Those Types? Up: Learning the Language Previous: Simple Java Sentences

Defining Our Own 'types'

So what are primitive data types good for? Well, the int type is useful for describing the number of people in a classroom or the number of doors on a car, the 'double' type is useful for describing temperature or grade point average, and so on. But what about the rest of the world? What would we use to describe a student, for example?

A student is a complicated thing, so let's select some simple things that we might want to use to describe a student, such as name, age, courses selected, fees owing, and whether the student holds a scholarship. Now recall that the compiler 'knows' how much memory and what encodings to use for built-in types, but what about our Student type? In this case, we have to tell the compiler how much memory to use and what encodings to use, and we do so by building our Student type out of primitive data types, which the compiler/hardware already 'know' about. Explicitly, we tell the compiler how much memory to allocate and what encodings to use with the following construction in Java, called a class definition:


public class Student {
    private String name;
    private int age;
    private String courses;
    private double feesOwing;
    private boolean scholarship;
}

For now let's ignore the keywords public and private, except to note that they are particularly useful when creating large software projects. Note a few things: first of all, we've introduced a word that we haven't seen before -- String. This is a datatype which is not quite built-in, and we certainly haven't defined it anywhere; you can think of it as a Very Useful addition to the language. It is a type that stores pieces of text like sentences, names, and so on. We'll look at it more closely later on, but this is a good enough description of it for now.

Secondly, we've used a variable of the int type to describe age because age is an integer (we don't care how many months older than 18 the student is,for example), variables of the 'String' type to describe the name and courses since these are pieces of text, a variable of the 'double' type to represent fees owing since this is a number that includes a decimal point (rational to be exact), and a variable of type 'boolean' to represent the existence of a scholarship since this either exists (true) or doesn't (false).

So what we've done is to create a compound type, called so because it is made up of a bunch of types that have been defined already such as primitive types or other compound types that have been defined. The types in a class definition are called attributes, member variables, fields, or properties. Now there is enough information for the compiler to be able to allocate memory for a 'Student'. Recall that the compiler knows how to allocate memory for the primitive types and if we've defined other compound types the compiler knows how to allocate memory for these; all the compiler has to do when it comes time to allocate memory for a Student is to allocate memory for each of the individual types in the compound type. But what about the 'String' type? We don't know how long the strings are going to be -- names of students tend to be of different lengths, for example. It turns out that the compiler doesn't allocate memory for the string here -- that's done elsewhere (where the string is defined, to be exact). What the compiler does is to allocate space for a reference to the string. Whenever the value of the string (the text of the string) is to be accessed, the reference is used to look up the value. Note that this class definition doesn't actual allocate the memory, it just lets the compiler know how to allocate the memory -- this is the recipe, not the meal.

The upshot is that this is all very similar to what goes on for one of the primitive types like int. In the case of int, the compiler 'knows' how much memory to allocate and how to encode values. In this case we are telling the compiler how it should allocate memory by making a class definition. But this is not enough. Recall that for int we also had operations that could be performed on an int or pair of ints such as increment, decrement, addition, and so on. We have no operations that we can perform on a Student... yet.


next up previous
Next: Operations With Those Types? Up: Learning the Language Previous: Simple Java Sentences
Chris Trendall
Copyright ©Chris Trendall, 2001. All rights reserved.

2001-12-09