Java Data Types

Java Data Types

Understanding primitive data types in Java programming

Introduction

Data is information that is stored and processed by a computer device.

A data type is used to represent a piece of data/information in programming. The importance of a data type is to store data in a computer program.

Categories of Data Types

Java classifies data types into two categories: Primitive data types and Nonprimitive data types.

  1. Primitive data types: byte, short, int, long, double, float, Boolean and char.

  2. Nonprimitive data types: classes, interfaces and arrays.

In this article, we are going to learn about primitive data types, what each of them is and what they can do.

Primitive Data Types

Primitive data types are the most basic data types in Java programming language. They are the foundation of Java data types as all other data types are built upon them.

There are 8 primitive data types in total

  • Byte

  • Short

  • Int

  • Long

  • Float

  • Double

  • Char

  • Boolean

Byte Data Type

The byte data type is an 8-bit signed two’s complement integer. The range (amount of numbers it can take) of a byte is from a minimum value of -128 to a maximum value of 127(inclusive). Its default value is 0.

The byte’s range is limited but it is useful when memory saving is important. A byte can also be used in place of an int.

It is advisable to use this data type if you are certain that the value of its variable will not exceed its maximum value of 127.

Example:

byte myByte = 50;
byte anotherByte = -110;

Short Data Type

The short data type is a 16-bit signed two’s complement integer. The minimum value of a short is -32,768 and its maximum value is 32,767(inclusive). It has a default value of 0.

The short data type can take more values than the byte as it is two times larger than a byte.

Like the byte, the short is also useful for saving memory, especially in situations where memory management is important.

Example:

short myShort = 35000;

Int Data Type

An int data type is a 32-bit(4-byte) signed two’s complement integer. It has a range of -2,147,483,648 minimum values to 2,147,483,647(inclusive) maximum values. The int has a default value of 0. The high range of the int data type makes it a widely used default data type for storing values unless there is an emphasis on memory management.

Example:

int myInt = 270000;

Long Data Type

The long data type is a 64-bit(8-byte) two’s complement integer. It has a minimum value of -2^64 and a maximum value of 2^63-1. Its default value is 0. The long data type should be used when you need a range of values wider than those provided by an int.

Example:

long myLong = 2000000L;

Float Data Type

A float data type is a single-digit precision 32-bit IEEE 754 floating point. It is a data type used to store decimal values and it gives about 6 decimal digits of precision.

The float is recommended for use if you want to conserve memory in large arrays of floating point numbers. Note that a float should never be used to store precise values such as currency.

Example:

float myFloat = 123.6f;

Double Data Type

The double data type is a double-precision 64-bit IEEE 754 floating point. As with float, it is also used to store decimal values and it gives about 15 decimal digits of precision. The double data type is the default choice for storing decimal values.

As aforementioned, the double data type should not be used for precise values such as currency.

Example:

double myDouble = 2.6;

Boolean Data Type

The boolean data type is used to store two possible values: true and false. A boolean is used to store conditions that are either true or false. The default value of a boolean is false.

The boolean data type represents one bit of information but it does not have a precisely defined size.

Example:

boolean theSkyIsBlue = true;

Char Data Type

The char data type is a single 16-bit(2-byte) Unicode character. It has a minimum value of ‘\u0000’ (or 0) and a maximum value of ‘\uffff’ (or 65,535 inclusive). It has no negative values.

A char(short for character) data type is used to represent a single character such as a letter or a symbol.

Example:

char myRating = ‘A’;

Conclusion

In this article, we discussed the concept of data types in Java programming. Data types are used to represent different kinds of data and determine how the data is stored and processed in a computer program.

Java has two categories of data types: primitive and non-primitive. Primitive data types are the basic building blocks of Java and include byte, short, int, long, float, double, char, and boolean.

Understanding the different data types and their characteristics is essential for proper data handling and memory management in Java programming.