What is legal Java? — Variables, Objects and Values

Values

There are two types of values in Java.

Primitive types

The primitive types are booleans, true and false, and numerics. Numerics come in several flavors.

The integer numerics differ in size and signedness. The floating point numbers differ in range and precission.

Primitive values are stored completely in little boxes.

Reference types

Reference types have their own little boxes, but these boxes don’t hold values. They hold references. Frequently several references “point” to the same thing. Generally we don’t even speak of reference types as having a value.

There are four kinds of reference types.

Class instances and arrays are always introduced in the first programming course. They refer to objects which can have methods and members and are assocated with a class, or more precisely a hierarchy of classes and often a few interfaces. (We generally don’t think about “interface” types since every object must belong to a class but may not be associated with an interface.)

There’s a lot to learn about objects. It would be best to just look at Section 4.3.1 of the language reference.

Type variables do play an important part in the second programming course but there will be plenty of time to worry about that.

Boxing

One confusing property of Java is that every primitive type has a corresponding wrapper class that allows the primitives to pretend to be objects.

Primitive typeWrapper class
booleanBoolean
byteByte
shortShort
intInteger
longLong
charCharacter
FloatFloat
doubleDouble

The wrapper classes do contain a few useful methods, but they are not easy to use. If you want to reverse the bits of an int variable x, you can call Integer.reverse(x).

As of Java 5, autoboxing has been supported in Java. This allows you to type less when using the wrapper classes, but it can make programs a bit harder to understand.

Variables

The Java tutorial on variables lists four kinds of variables.

The Java language reference has its own list of seven.

We will go to the source references for a bit more discussion.

One picture

Many people, like the instructor, really need to draw it to understand it. We will use the memory diagrams of Western Carolina’s introductory programming courses to make some nice drawings.

Take this seriously. It really will appear on the test.