Any program that doesn’t make use of data is going to be pretty boring, not to mention useless. Therefore, one of the things that you must learn as you pick up a new language is what data types are available and how you can use them. In this article, we will look into the primitive data types defined by Java. We will discuss their default initialization values, their range of values, and some of the things you can and can't do with them.
Catch up on past articles covering the transition to OOP with Java
- · "Transitioning into object-oriented programming using Java"
- · ”Intro to OOP: Understanding classes and objects”
- · "Intro to OOP: A closer look at inheritance and polymorphism"
- · "Intro to OOP: Restricting access to object properties"
- · "Intro to OOP: Java scope rules"
- · “Transitioning into OOP: Intricacies of Java object construction and initialization”
Data types are essentially abstractions that represent memory locations. Programmers work with many different data types: some defined by the programming language, some defined in external libraries, and some defined by the programmer. Many languages depend on the specific machine and compiler implementation for data-type attributes, such as word and integer size. Java's virtual machine, on the other hand, specifically defines all aspects of its built-in data types. This means that no matter what low-level, operating system a Java Virtual Machine (JVM) is running on, the attributes of the data types will remain the same.
Primitive data types
Primitive data types are irreducible, built-in data types, defined by a programming language, that represent real numbers, characters, and integers. Larger, more complex data types are defined using combinations of primitive data types. Many times, there are hardware equivalents of primitive data types. For example, an int primitive type is sometimes stored in a 32-bit hardware register. Java provides an assortment of primitive data types to represent numbers and characters.
Primitive data types are usually grouped into four categories: real numbers, integers, characters, and booleans. Some of these categories contain multiple primitive types. For instance, Java defines two primitive types, float and double, within the real numbers category; it defines four primitive types, byte, short, int, and long, within the integer category; it defines one type, char, within the character category; and it defines one type, boolean, within the boolean category. Table A details Java's primitive data types.
All numeric variables in Java are signed, and Java prevents arbitrary casting between data types. Only casts between numeric variables are allowed. For example, a boolean cannot be cast to another data type, and no other data type can be cast to a boolean.
Since all primitive data types are precisely defined in Java, and since direct memory access is not possible, the sizeof operator isn't provided.
Java's primitive data types are not objects. To treat a Java primitive data type in an object-oriented fashion, you need to wrap it in a class first.
Java provides built-in wrapper classes for Byte, Short, Boolean, Character, Integer, Double, Float, and Long. These wrapper classes provide some very handy utility methods. For example, the Byte, Float, Integer, Long, and, of course, Double classes all provide a doubleValue() method that converts the value stored in an instance of each class to a Double. Also, all of the wrapper classes provide a static, valueOf(String s) method that converts a given String to the corresponding primitive type. The code snippet in Listing A demonstrates some uses of the wrapper classes.
Initializing primitive data types
In Java, primitive data types declared as member variables of a class are automatically initialized to their default values unless explicitly declared otherwise. Primitive data types declared local to a method are not automatically initialized and will cause the compiler to throw an error similar to this: "Variable x may not have been initialized." Table B defines the default values for Java's primitive data types.
The code snippet in Listing B shows all of Java's primitive data types used as member variables of the Initialization class The example also shows an int variable declared locally to the constructor of the Initialization class. Without any modifications to the code snippet, a compiler error will be thrown when the code is compiled.
Once the offending lines (those referencing the uninitialized variable error) are removed or commented out, the following results will be displayed when the code is successfully compiled and executed:
We could have explicitly initialized our member variables to other values as follows:
byte b = 5;
short s = 123;
int i = 1234;
long l = 12345;
float f = 123.45f;
double d = 12345.678;
char c = 'A';
boolean z = true;
Java defines a full set of primitive data types. In addition, Java removes the hardware and compiler dependencies found in other languages, allowing the programmer to concentrate on other issues. In our next article, we will discuss some of the commonly used, complex data types defined by Java and see how they can be exploited to handle typical programming situations.