Java Data Types

Data types in Java are used to specify the type of data that can be stored in a variable. In Java, there are two main categories of data types:

  1. Primitive Data Types
  2. Non-Primitive Data Types

Primitive Data Types: The primitive data types in Java are boolean, char, byte, short, int, long, float, and double. These data types are used to store simple values such as true/false, single characters, integers, and decimal numbers.

Non-Primitive Data Types: The non-primitive data types in Java include classes, interfaces, and arrays. These data types are used to store more complex data structures such as objects, collections, and arrays.

While primitive data types are predefined by Java and are built into the language, non-primitive data types are created by the programmer and are often used to build custom data structures and classes.

Primitive Data Types in Java

Primitive data types serve as the fundamental building blocks for data manipulation in the Java programming language. These data types are considered to be the most basic and essential types available in Java, as they are used to represent simple and straightforward data values.

There are 8 primitive data types present in Java:

  • Boolean data type
  • char data type
  • byte data type
  • short data type
  • int data type
  • long data type
  • float data type
  • double data type

Data Type Default Value Default Size
boolean false 1 bit
char ‘\u0000’ 2 bytes
byte 0 1 byte
short 0 2 bytes
int 0 4 bytes
long 0L 8 bytes
float 0.0f 4 bytes
double 0.0d 8 bytes

Boolean Data Type

In Java, the Boolean data type is used to represent variables that can only have two possible values: true or false. This data type is often utilized for simple flags that keep track of conditions that are either true or false.

While the Boolean data type represents only one bit of information, its precise “size” cannot be defined since the size of a bit is not fixed and can vary depending on the platform and compiler being used.

Example:

Boolean one = false

Char Data Type

In Java, the char data type is used to represent a single 16-bit Unicode character. This data type has a value range that spans from ‘\u0000’ (which is equivalent to 0) to ‘\uffff’ (or 65,535 inclusive).

The char data type is primarily used to store individual characters, such as letters, numbers, punctuation marks, and symbols. By using Unicode encoding, Java is capable of representing characters from a wide range of writing systems, including those that use non-Latin scripts.

Example:

char letterD = 'D'

Byte Data Type

The byte data type is a primitive data type in Java that represents an 8-bit signed two’s complement integer. Its value range spans from -128 to 127 (inclusive), with a minimum value of -128 and a maximum value of 127. The default value of a byte data type is 0.

One of the primary uses of the byte data type is to conserve memory in large arrays where memory savings is critical. By using bytes, developers can save space because a byte data type is four times smaller than an integer. Additionally, the byte data type can be used as a substitute for the “int” data type in situations where a smaller range of values is required, and memory usage needs to be optimized.

Example:

byte a = 20, byte b = -80

Short Data Type

The short data type in Java is a 16-bit signed two’s complement integer. Its value range spans from -32,768 to 32,767 (inclusive), with a minimum value of -32,768 and a maximum value of 32,767. The default value of a short data type is 0.

Similar to the byte data type, the short data type can also be used to save memory in situations where memory usage needs to be optimized. Using short data types can lead to memory savings since a short data type is two times smaller than an integer data type.

Example:

short s = 20000, short r = -10000

Int Data Type

In Java, the int data type is a 32-bit signed two’s complement integer. Its value range spans from -2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive), with a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647. The default value of an int data type is 0.

Typically, the int data type is used as the default data type for integral values unless memory usage is a concern. Since the int data type offers a wide range of values, it is often suitable for a wide variety of use cases. However, in situations where memory usage is critical, developers may choose to use smaller data types like byte or short data types to optimize memory usage.

Example:

int a = 200000, int b = -400000

Long Data Type

The long data type in Java is a 64-bit two’s complement integer. Its value range spans from -9,223,372,036,854,775,808 (-2^63) to 9,223,372,036,854,775,807 (2^63 -1) (inclusive), with a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807. The default value of a long data type is 0.

The long data type is typically used when a wider range of values is needed than what can be provided by the int data type.

Example:

long a = 100000L, long b = -200000L

Float Data Type

In Java, the float data type is a 32-bit IEEE 754 single-precision floating point. Its value range is unlimited, meaning it can represent a wide range of decimal numbers. However, it should be noted that the float data type is not recommended for use in applications that require precise values, such as financial calculations or currency conversions.

It is suggested to use a float instead of a double when there is a need to save memory, especially when working with large arrays of floating point numbers. The default value of a float data type in Java is 0.0F.

Example:

float f1 = 434.5f

Double Data Type

In Java, the double data type is a 64-bit IEEE 754 double-precision floating point. Like the float data type, it can represent a wide range of decimal values with an unlimited value range.

The double data type is commonly used for decimal values and is recommended for applications that require high precision, such as scientific calculations. However, it should not be used for precise values such as currency.

The default value of a double data type in Java is 0.0d.

Example:

double d1 = 19.3

We encourage you to follow tutorials.freshersnow.com for further learning opportunities, such as Java data types.