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

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …