Lesson: Data Types

Overview: 
Explore the concept of Data Types and in particular the data types in Java.
Objectives: 

Understand Data Types in general and the specific data types available in Java.

Content: 

As discussed in the previous lesson, variables in Java are typed, that is, we specify what kind of data can be contained in the variable. Java has two categories of data type, primitive types and reference types. Java has eight primitive types. Reference types allow you define your own data types (typically objects). The primitive data types are:

boolean stores only the idea of 'true' or 'false'
int stores a signed integer value 32 bits in size
short stores a signed integer value 16 bits in size
long
stores a signed integer value 64 bits in size
double stores a high precision decimal number
float stores a lower precision decimal number
byte stores a signed integer value 8 bits in size
char stores a single text symbol 16 bits in size

Primitive data types contain the simple numeric value of the appropriate type. Selecting a data type depends on the type of data you wish to store in a variable and also its numeric range or size. Primitive types default to a value of zero (false for boolean) if not initialized when created. Besides defining the type of data a variable can hold, the type also determines the memory space allocated for the variable. Typically, we only use int, double, float and boolean in robotics.

As you can see, there is some repetition among the types of data different primitives can be used for. In general, int will work for most tasks we have working with whole numbers, and double will work well for any decimal operations. It is possible to do nearly all of your programming without using the different sizes of data. Recall that when creating a variable, Java uses the type to decide how big the memory storage location should be. As should make sense, if you try to take a piece of data stored in a long type and put it into a variable with type int, there will be an error. The same goes for any mismatch of the size limitations of the different primitives.

The Java compiler and JVM will watch for type mismatches, that is, trying to put an incompatible value into a variable, like trying to set an int to a double value like this:  int x = 3.2; The compiler will flag this as an error and at run time this would result in an exception (run time error).

Watch this video for more about variables and data types. Go here, here or here for detailed discussions of each data type.

Reference data types contain references or pointers to more complex data like objects. We will learn more about objects and reference data types a bit later. Reference variables default to a special Java value called null if they are not initialized. For reference variables, which are supposed to point to a more complex data object, null means the variable is not pointing to anything.

Strings in Java are a hybrid data type, acting like both a primitive and reference data type. A String contains a list of characters and can be treated like a primitive data type but Strings also support methods that can be called to perform operations on the string of characters contained in the string. A String is really a special Java object. Go here and here for detailed discussions of Strings. Note that a String variable may not be pointing to anything, meaning it is un-initialized or null. A String variable may also point to an actual string object, which happens to contain no characters, which is called an empty or zero length string. Null and empty do not mean the same thing.

Navigation: