Java, Software Development

Java Primitives in The Object-Oriented Programing World

Java primitives are predefined basic data types that exist in the Java programming language. We do not need to create them as we would with our custom Java classes.

What Are Basic Data Types in Java?

Java primitive types hold primitive values. Java has these types – byte, short, int, long, float, double, boolean, and char.

The Integer Types – byte, short, int, and long

The byte, short, int, and long types are integer types, and they hold whole-number numeric values. These data types differ from each other on the range of values they can hold. Consider the following table.

Primitive TypeDescriptionRange
byteThe byte data type is an
8-bit signed two's
complement integer
-128 to 127 (inclusive)
shortThe short data type is a
16-bit signed two's
complement integer.
-32,768 to of 32,767 (inclusive)
intThe int data type is a
32-bit signed two's
complement integer
-2^31 to 2^31-1
longThe long data type is a
64-bit two's complement integer.
-2^63 to 2^63-1

Integer Literals

Integer literals are hard-coded values in our codes. Hence, literals. Consider the following examples.

The Floating-Point Types – float and double

The Java primitive float and double types are floating-point types, and they can hold non-integer fractional numbers, e.g., 12.88.

Primitive TypeDescriptionRange
floatThe float data type is a
single-precision 32-bit
IEEE 754 floating point
doubleThe double data type is a
double-precision 64-bit
IEEE 754 floating point.

Floating-Point Literals

Like integer literals, floating-point literals are hard-coded values that go with our codes. Consider the following examples.

One thing to note here is double is the default type of any floating-type literals. For example, 4.65. If we want to make floating-type literals of float type, we need to use the suffix F or f, e.g., 1.23F.

If we had changed, 1.23F to 1.23, we would get the following compile-time error.

The true/ false Type – boolean

A variable of a Java primitive boolean type can only hold the value true or false. Consider the following codes.

The output is as follows.

The Character Type – char

The Java primitive char type represents a single character (or more accurately, 16-bit Unicode character) within a pair of single quotes.

The codes display K.

The range of the character type is from '\0000'  (or 0) to '\uffff'  (or 65,535 inclusive). Does it mean we can use char like a numeric type? Kind of, but we need typecasting. Look at these example codes. Note that we need to use typecasting on line 3 because int is larger than the char type in terms of total range.

These codes will generate the following output on the console.

Java Primitives - Looping through char type range and displaying each character


Wrapper Types – Java Primitives’ Alter-egos

In general, our OOP codes cannot work directly with Java primitive types because they are not objects. We somehow need to convert them to their OOP-equivalent types, which we call Wrapper Types. Wrapper types are Java built-in classes that represent all the Java primitive types. Each primitive type has its Wrapper type.

Primitive TypeWrapper Type

This post is part of a reboot Java tutorial.

You Might Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *