Data types in Java

Introduction

The data type is used to inform the compiler of the type of data stored in a variable. Java has support for different data types that can be broadly classified into primitive data types and non-primitive (Reference) data types.

Primitive data types

A primitive data type is a built-in type in Java that can store a single value at a time. Java supports several primitive types which are broadly classified into four categories which are as follows,

Integer

 These types store signed integers. The types in this category are byte, short, int, and long. Signed integers can store positive as well as negative values such as -20.

The following table describes the four Integer types,

Integer typeDescription Bit sizeDefault valueRange
byte Stores a small amount of data.  Used for streaming data from a network file.  Used for processing raw binary data.8-bit (1 byte)0-128 to 127
short The least used data type.  Stores smaller numbers such as Id.16-bit (2 bytes)0-32,768 to 32,767
int Most commonly used.  Stores whole numbers. Most commonly used.  Stores whole numbers.32-bit (4 bytes)0-2,147,483,648 to 2,147,483,647
long Used for a number larger than int type.  For example, the results of large scientific calculations.64-bit (8 bytes)0L-9,223,372,036,854, 775,808 to 9,223,372,036,854, 775,807

Float

Float types store floating-point numbers or real numbers with fractional precision. The float types supported by Java are float and double.

The following table gives a brief description of the float types,

Float typeDescriptionBit sizeDefault valueRange
floatSupports single-precision value.  Used to store numbers with less precision.  For example,  float PI, area;32-bit (4 bytes)0.0f3.4e-038 to 3.4e+038
double Supports double-precision value.  Used for larger mathematical calculations requiring more accuracy.  For example, double bigData;64-bit (8 bytes)0.0d1.7e-308 to 1.7e+308

In Java, all numeric types are signed, that is, they store both positive and negative values. The size of each type is the same and standardized for all platforms.

Character

The character type is used to store symbols such as letters and numbers. It supports only one type, char.

Bit-size: 16-bit (2 bytes) Unicode character
Range: 0 (‘\u0000’) to 65,535 (‘\uffff’)
Default value: 0 (‘\u0000’)
For example,
char gender = ‘M’;

Boolean

The Boolean type is used to indicate true or false. It supports only one type, Boolean. Its size is 1 bit and the default value is false.

For example, boolean selected = true;

Non-primitive data types

Java also supports several non-primitive and reference types, namely, String, Array, Class, Interface, etc.

String

The String class is defined in the java.lang package and is used to store a sequence of characters also called a string. None of the primitive data types have the support for storing more than one character in a variable. The String class provides this feature.

For example, String str = “Stores a string”;

Here, str is not a primitive type of variable but an object of type String. The value is specified within double quotes. The value of the str variable remains constant throughout. Initializing the variable with a new value, internally creates a new String object. Thus, String objects are immutable.

Reference types

A reference type stores the address of an object. In Java, arrays and objects represent the reference types. When an object or array is created, the address of the memory assigned to it is stored in the reference variable.

Array

It is a collection of values of the same type. For example, the score of players can be stored in an array.

Class

It is a structure that encapsulates data members and methods within it.

Interface

It is a reference type similar to a class in Java that contains abstract methods, default methods, etc.

The following example shows the use of different data types,

package mypkg;
public class Customer {
  public static void main(String[] args) {
    // Declaring variables
    int custId; // variable of type Integer 
    float payableAmt; // variable of type Float 
    // Declaring and initializing the variables 
    double creditScore = 456790.897; // variable of type Double 
    char premiumCust = 'Y'; // variable of type Character 
    boolean paymentStatus = false; // variable of type Boolean 
    String address = "10, NorthDriven Park"; // variable of type String 
    // Initializing the declared variables 
    custId = 1007;
    payableAmt = 10487.50f;
    // Displaying the values of variables on the console 
    System.out.println("Customer Id: " + custId);
    System.out.println("Address: " + address);
    System.out.println("Order Amount: " + payableAmt);
    System.out.println("Premium Customer?: " + premiumCust);
    System.out.println("Credit Score: " + creditScore);
    System.out.println("Payment Status: " + paymentStatus);
  }
}

Output

Customer Id: 1007
Address: 10, NorthDriven Park
Order Amount: 10487.5
Premium Customer?: Y
Credit Score: 456790.897
Payment Status: false

Here, different types of variables are declared and initialized with appropriate values. Note that the meaningful names have been used for variables based on the type of value they store.

Leave a Comment