Java Data Types

A data type of a variable in java determines the values it may contain, how much memory is required to store this data, plus the operations that may be performed on it. By creating variables of different data types, we can store integers, floating point numbers, boolean values etc.

Java programming language supports two types of data types primitive and Non-primitive data types. A primitive data type is predefined by the language and is named by a reserved keyword. In this tutorial, we will learn about primitive data types. There are eight primitive data types supported by Java programming language:


Java Primitive Data Types
Data types are fundamental building blocks in any programming language, defining the nature of variables and the operations that can be performed on them. Java, being a statically-typed language, requires explicit declaration of data types for variables. In this tutorial, we will delve into the various data types in Java, covering primitive data types, reference data types, and how to effectively use them in your programs.

byte

The byte data type is an 8-bit signed integer. It has a range of -128 to 127. This type is often used when working with raw binary data or in situations where memory conservation is critical.
  • Keyword byte is used to declare variables.
  • The byte data type is an 8-bit signed two's complement integer.
  • The range of byte data type if from -128 to 127(inclusive).
  • We can use variables of byte data types instead of integer data type to save memory. We should use byte data types only when we know the range of values we are dealing with will always be in the range of byte data type.
For Example:
byte count = 100;  

short

The short data type is a 16-bit signed integer with a range of -32,768 to 32,767. It is useful when dealing with a wider range of values compared to byte.
  • Keyword short is used to declare variables.
  • The short data type is a 16-bit signed two's complement integer.
  • The range of short data type is from -32,768 to 32,767 (inclusive).
  • We can use variables of short data types instead of integer data type to save memory.
  • As the range of short data type is more than byte, we can use it to store values beyond the range of byte variables.
For Example:
short i = -15000, j = 12000;  

int

The int data type is a 32-bit signed integer, and it is one of the most commonly used data types in Java. It has a range of approximately -2 billion to 2 billion.
  • Keyword int is used to declare integer variables.
  • The int data type is a 32-bit signed two's complement integer.
  • The range of int data type is from - 2,147,483,648 (-2^31) to 2,147,483,647(2^31 -1)(inclusive).
  • We can use variables of int data types if we are not concerned about memory or when you are dealing with number whose values are beyond range of short data types.
For Example:
int score = 5236521;  

long

The long data type is a 64-bit signed integer. It is used when dealing with large integer values that exceed the range of int.
  • Keyword long is used to declare variables.
  • The long data type is a 64-bit two's complement integer.
  • The range of long data type is from -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1)(inclusive).
  • We can use variables of type long when we are dealing with number larger than the range of int data types.
For Example:
long amount = 645374587L;

float

The float data type is a 32-bit floating-point number. It is suitable for representing decimal numbers with moderate precision.
  • Keyword float is used to declare variables.
  • The float data type is a single-precision 32-bit IEEE 754 floating point.
  • float data type takes less memory than double but float variables cannot store very precise values like double.
For Example:
float length = 345.6f;

double

The double data type is a 64-bit floating-point number, providing higher precision than float. It is commonly used for calculations requiring more accuracy.
  • Keyword double is used to declare variables.
  • The double data type is a double-precision 64-bit IEEE 754 floating point.
  • Double data type is default for decimal numbers.
  • double data types takes more memory than float but are more precise than float.
For Example:
double area = 543.76;

boolean

The boolean data type has only two possible values: true or false. It is fundamental for decision-making in control flow statements.
  • Keyword boolean is used to declare variables.
  • The boolean data type has only two possible values: true and false.
  • We use this variable as a flag to store binary information.
  • It stores 1 bit of information but it's actual size is not defined properly.
For Example:
boolean isComplete = true;

char

The char data type represents a 16-bit Unicode character. It can store any character from the Unicode character set.
  • Keyword char is used to declare variables.
  • The char data type is a single 16-bit Unicode character.
  • The range of char data type is from '\u0000' (0) to '\uffff'(65,535)(inclusive).
  • We use char data type to store one character.
For Example:
char c = 'A';

Default Values of Data Types

It is not compulsory to initialize a variable while declaration. If we don't initialize a variable the compiles assigns a default value as per the data type of the variable. However, it is a good programming practice to initialize a variable during declaration. Here are the default values of various data types:

Data Type Default Value
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
char '\u0000'
boolean false

Here is the Java program to demonstrate the declaration, initialization and printing of primitive data types.

public class DataTypes {
    public static void main(String args[]) {
        byte b = 100;
        System.out.println("Byte : " + b);

        short s = 50;
        System.out.println("Short : " + s);

        int i = 5236521;
        System.out.println("Int : " + i);

        long l = 645374587L;
        System.out.println("Long : " + l);

        float f = 345.6f;
        System.out.println("Float : " + f);

        double d = 543.76;
        System.out.println("Double : " + d);

        boolean bl = true;
        System.out.println("Boolean : " + bl);

        char c = 'A';
        System.out.println("Char : " + c);
    }
}
Output
Byte : 100
Short : 50
Int : 5236521
Long : 645374587
Float : 345.6
Double : 543.76
Boolean : true
Char : A

Implicit and Explicit Type Casting

In Java, data type conversions can be broadly categorized as implicit (automatic) and explicit (manual) type casting.

Implicit Type Casting

Implicit type casting occurs when a smaller data type is converted to a larger data type. Java automatically performs this conversion without the need for explicit instructions.

 int myInt = 50;
 long myLong = myInt; // Implicit casting from int to long
    

Explicit Type Casting

Explicit type casting is required when converting a larger data type to a smaller one. This conversion may result in data loss, and it requires manual intervention.

 double myDouble = 3.14;
 int myInt = (int) myDouble; // Explicit casting from double to int
    


Conclusion: Mastering Data Types in Java

Understanding data types is fundamental to effective Java programming. Whether working with primitive data types for simple values or reference data types for complex structures, choosing the right data type is essential for efficient memory usage and program correctness.

As you continue your Java journey, experiment with different data types, practice type casting, and explore the nuances of each type in various scenarios. Mastering data types will empower you to write robust, efficient, and maintainable code in Java, contributing to your growth as a proficient programmer.