Developer

Transitioning into OOP: What can you do with Java's primitive data types?

You can't learn a new language without knowing its available data types. Fortunately for those learning Java, the rules are always the same. Here's an overview of Java's primitive data types.


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


Data types
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.
Table A
Java primitive data types
Primitive type Size Range/accuracy
float 4 bytes 32-bit IEEE 754 single-precision
(seven decimal places)
double 8 bytes 64-bit IEEE 754 double-precision
(15 decimal places)
byte 1 byte -128 to 127
short 2 bytes -32,768 to 32,767
int 4 bytes -2,147,483,648 to 2,147,483,647
long 8 bytes -9,223,372,036,854,775,808 to
9,223,372,036, 854,775,807
char 2 bytes Entire Unicode character set
boolean 1 bit true or false
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.

 Wrapper classes
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.
Table B
Default values for Java's primitive data types
Type Default value
boolean false
byte 0
short 0
int 0
long 0
char '\u0000'
float 0.0
double 0.0
Java primitive data type default initial values

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:
byte: 0
short: 0
int: 0
long: 0
float: 0.0
double: 0.0
char: 0
boolean: false

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;

Summary
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.

Editor's Picks