Chapter Four, Sethi

Data Representation

Troll peano notation: "Trolls traditionally count like this: one, two three...many, and people assume this means they can have no grasp of higher numbers. They don't realize that many can be a number. As in: one, two, three, many, many-one, many-two, many-three, many many, many-many-one, many-many-two, many-many-three, many many many, many-many-many-one, many-many-many-two, many-many-many-three, LOTS."

Terry Pratchett "Men At Arms", p.127

Java data types are similar to C with some important differences. There are two main types of data in Java: primitive data types, which are passed by value and Java classes and arrays which are passed by reference. Pointer manipulation is not allowed. It is not possible for the program to get the memory location of an object.

Java is statically typed: the type of every object is known at compile time. However the Java run time system keeps track of all objects, their types and relationships. Java is late-binding which is required for an OO language in which classes may be subclassed and proper method to run for an object may not be known until run time. Unlike C++, Java prevents the programmer from using uninitialized variables by refusing to compile (in the case of uninitialized local variables) or automatically initializing to a default value ( in the case of fields).

There are no global variables in Java. Variables must be defined within a class (where they are called fields) or within a method (where they are called local variables). Primitive data types (e.g. int and long) have set sizes which are not platform dependent.

Java is a strongly typed language requiring explicit conversions where a simple cast could be used in a language like C or C++.

As an example of the types of data representations available in Java, lower case boolean is a primitive data type in Java, upper case Boolean is a class defined by the Java language. A boolean cannot be automatically converted to Boolean.

There are a number of String layouts available in Java. For example the built in String type, String and an array of primitive characters, char[].

Strong type casting, while useful in eliminating programmer errors, results in a loss of flexibility. A conversion from char[] to String, from byte[] to int or from boolean to Boolean requires a method invocation.