10 Mar 1996

Primitive Data Types

Java variables are assigned a data type in one of four basic categories:

  1. Arithmetic (fixed or floating point numbers)
  2. Character (actually two byte Unicode characters)
  3. Boolean (having values of true or false)
  4. Access (a cross between a pointer and a handle)

Arithmetic

The integer arithmetic types are:

byte (1 byte)
short (2 bytes)
int (4 bytes)
long (8 bytes)

The floating point types are float (4 bytes) and double (8 bytes).

Unlike C, there are no unsigned data types, although there is an unsigned shift operation which provides the only important function where the signed/unsigned distinction really makes a difference. Do not attempt to store the number 50000 in a short. Its not worth the effort.

The Java language converts one arithmetic type to another arithmetic type in all the expected circumstances. If a conversion reduces the number of bits and loses data, no error is signalled.

Character

Java character variables and strings support the international two byte character set commonly known as Unicode. This allows strings to hold not only has all the characters in Western and Eastern Europe, but also Hebrew, Arabic, Cyrillic, and 30,000 characters belonging to Chinese/Japanese/Korean. The first 128 codes of the Unicode standard are the ordinary ASCII characters. ASCII characters are converted to Unicode by simply adding a high order byte of zero, and the Unicode representation of ordinary ASCII characters can be converted to plain ASCII by just dropping the high order byte.

In theory, the Java language tolerates programming in any of the recognized languages. While conventional languages define terms like alphabetic, numeric, and alphameric in US terms, Java defined a "digit" to be not only "0" to "9" but also the corresponding characters for 0-9 in the languages of Thailand, Tibet, Laos, Malaysia, and several Indian dialects. Note, however, that the Unix, NT, and MacOS operating systems on which Java has initially been implimented have little support for the editing and storage of Unicode disk files.

Variables declared to be of type char, and data objects derived from characters (particularly String objects) may lose information if converted to something declared to be of type byte. In practice, however, the current version of the I/O library converts all characters and Strings to bytes before writing them to Unix or NT disk files, and it reads Unix or NT files into a byte array and then explodes it to Unicode during input.

Unlike C, a char variable is not arithmetic. It can, however, be converted by simple assignment to variables of type byte or short, and byte and short can be converted to char. This is one place where the unsigned nature of arithmetic types can be an issue, since the value of a char variable is 16 bits without a sign.

Boolean

A variable of type boolean has the possible values true or false. Unlike other languages, boolean values are not associated with 0 or 1, may not be converted directly to any number, and have no ordering. There are some trivial expressions to get around this restriction, but the language still makes it.

Reference

The other primitive type is a reference. A reference variable replaces what every other language calls a pointer. The problem is that pointers normally contain a memory address value for something, while a Java reference variable probably contains a name or handle that locates an object only indirectly through some sort of table lookup.

A reference variable contains a reference value that designates a dynamically allocated object. The reference value is created by the new operation that dynamically allocates the object. It is not possible to say much more about this until the concepts of classes and objects have been covered.

Types that Don't Exist

There are no user defined enumeration types. The same effect can be accomplished by declaring constant (final) values of type byte (or maybe short).

There are no bit strings or bit fields.

Arrays, strings, structs, structures, records, and unions are all handled by classes and objects described later.

Continue Back PCLT

Copyright 1996 PC Lube and Tune -- Java H. Gilbert