Java, Java Tutorial, Software Development

Chapter 8: Java Primitives in The OOP World

java Sort Enum Properties Monitor Directory File Lambda Expression parameter return types Validate XML String XSD ExecutorService AutoCloseable Try-With-Resources Convert LocalDateTime OffsetDateTime Run Codes Before Shutdown XML Tag Lambda PostgreSQL JDBC invoke JavaScript Applet SFTP Batch hang block main thread MySQL JDBC try-with-resources delete Unencrypted SoapFault Apache CXF Remove XML Element XSLT skip execute finally public static void main constructor fields Eclipse convert Iterator Stream StreamSupport XMLEncoder XMLDecoder rename P6Spy JDBC Example PDF Version JasperReports Run from Ant iReport Page Numbers Count entities persistence context hibernate

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 – [crayon-616b7049a88ea600443889-i/], [crayon-616b7049a88ec310792890-i/], [crayon-616b7049a88ed422343937-i/], and [crayon-616b7049a88ee610530163-i/]

The byte, short, int, and long types are integer types, and they hold whole-number numeric values. These data types differ from each other in the range of values they can have. 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 – [crayon-616b7049a88f8464596336-i/] and [crayon-616b7049a88f9181937356-i/]

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 [crayon-616b7049a8900458546979-i/]/[crayon-616b7049a8902337065038-i/] Type – [crayon-616b7049a8903109607150-i/]

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 – [crayon-616b7049a890b811675327-i/]

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

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 the entire 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.

Got comments, or suggestions? Please visit our Facebook page!

You Might Also Like