Java – Data Types4 min read

What are Data Types in Java?

Data types specify the varying sizes and values in the variables that can be stored. That is every variable is assigned by data-types according to the need. And based on their respective data-types, the operating system allocates memory to that data-types.

There are two data types in Java:

  • Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double.
  • Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.

1. Primitive data types:

These are the most basic data types available in the Java language.
There are eight primitive data-types available by Java:

Data type SizeDescription
byte 1 byteStores whole numbers from -128 to 127
short 2 bytesStores whole numbers from -128 to 127
char 2 bytesStores whole numbers from -32,768 to 32,767
int 4 bytesStores a single character/letter or ASCII values
long 8 bytesStores whole numbers from -2,147,483,648 to 2,147,483,647
float 4 bytesStores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
double 8 bytesStores fractional numbers. Sufficient for storing 6 to 7 decimal digits
boolean 1 bitStores true or false values

These are put into four group:

  • Integers: This group includes byte, short, int, and long, which are for whole-valued signed numbers.
  • Floating-point numbers: This group includes float and double, which represent numbers with fractional precision.
  • Characters: This group includes char, which represents symbols in a character set, like letters and numbers.
  • Boolean: This group includes boolean, which is a special type for representing true/false values.

Integers:

  • byte:
    It is an 8-bit signed two’s complement integer. The byte data type is used to save memory in large arrays.
    Its minimum value is -128 and the maximum value is 127. Its default value is 0.
    Example:
    byte x = 15;
    byte y = -12;
  • Short:
    The short data type is a 16-bit signed two’s complement integer. A short is 2 times smaller than an integer. Its minimum value is -32,768 and the maximum value is 32,767. Its default value is 0. And is also used to save memory.
    Example:
    short x = 1500;
    short y = -1200;
  • int:
    The int data type is a 32-bit signed two’s complement integer. It is a default data-type for integral values.Its minimum value is – 2,147,483,648 and maximum value is 2,147,483,647. Its default value is 0.
    Example:
    int x = 25;
    int y = -35;
  • long:
    The long data type is a 64-bit two’s complement integer. Its value-range lies between -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807 (2^63 -1) (inclusive). Its default value is 0. long is used when int is not large enough to store the value.
    Example:
    long
    x = 200000L;
    long y = -300000L;

Floating Point Types:

  • float:
    The float data type is a single-precision 32-bit IEEE 754 floating-point. It is used when the user needs to save memory in large arrays of floating-point numbers. float is never used for precise values such as for currency. Its default value is 0.0d.
    Example:
    float x = 234.5f;
  • double:
    The double data type is a double-precision 64-bit IEEE 754 floating-point. The double is generally used for decimal values just like float. The double also should never be used for precise values, such as currency. Its default value is 0.0d.
    Example:
    double x = 13.99d;

Characters:

The char data type is a single 16-bit Unicode character. The size of a character is 2 bytes. It is used to store single characters and must be enclosed within single quotes as shown in the example. Example:


Boolean:

A boolean keyword is used to declare with the boolean data-type and represents one bit of information that is either true or false.
Example:


2. Non-Primitive Data Types:

The non-primitive data types include Strings, Classes, Interfaces, and Arrays. These are also known as reference types because they refer to objects.
Non-primitive types are created by the user/programmer and are not predefined by Java except one that is String and can have null values whereas primitive data types are predefined in Java and always has a value.

To learn about Non-Primitive Data Types click on the following:
Strings, Classes, Interfaces, Arrays.


MORE

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More