Java Data Types

Data types in programming define the range of values and sizes that can be held in variables or constants. Java is known for being a statically and strongly typed language due to its predefined data types. In Java, there are two main categories of data types:

  • Primitive Data Types: These encompass boolean, char, byte, short, int, long, float, and double.
  • Non-Primitive Data Types: These consist of Classes, Interfaces, String, and Arrays.

Let's understand in detail.

Java Primitive Data Types

Primitive data types in Java serve as the fundamental units for data manipulation, constituting the foundational data types.

In Java, there are mainly eight primitive data types which are as follows.

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

Java is classified as a statically typed programming language, which requires the declaration of all variables before they are used. This entails specifying both the type and name of the variable. Now, we will explore each data type individually.

1. Boolean Data Type

In the Java programming language, the boolean data type is used to store a single bit of data that can have one of two values: true or false. The boolean data type occupies a size of 1 byte, which is equivalent to 8 bits.

The boolean data type is utilized for holding the outcomes of logical conditions or expressions. Unlike primitive types such as int or double, boolean does not possess a defined size or range. Typically, it is represented as a single bit, although the actual implementation might differ depending on the platform.

Syntax:

Example

boolean flag;

Example

Example

Boolean a = false;

Boolean b = true;

2. Byte Data Type

In Java, the byte data type is a fundamental data type that signifies an 8-bit signed integer in two's complement form. It can hold values ranging from -128 to 127, with a default value of 0.

The byte data type is frequently utilized for handling raw binary data or when there is a need to conserve memory, as it consumes less memory compared to larger integer types such as int or long.

Syntax:

Example

byte size;

Example

Example

byte a = 10;

byte b = -20;

3. Short Data Type

The short data type in Java is a fundamental data type that signifies a 16-bit signed two's complement integer. It can hold values within the range of -32,768 to 32,767.

Just like the byte data type, the short data type is utilized when there is a need to conserve memory but a higher level of precision than byte is necessary. The default value assigned to a variable of type short is 0.

Syntax:

Example

short var;

Example

Example

short a = 10000;  

short b = -5000;

4. int Data Type

In Java, the int data type is a fundamental data type that signifies a 32-bit signed integer in two's complement form. It covers a spectrum of values starting from -2,147,483,648 to 2,147,483,647.

The int data type is widely utilized and serves as a fundamental data type. It is primarily employed for storing integers without any fractional component. By default, its initial value is set to 0.

Syntax:

Example

int myInt = 54;

In Java, integer variables are defined using the int keyword. For instance, by declaring int myInteger = 54; you create an int variable called myInteger and set its initial value to 54. These int variables are suitable for arithmetic operations, can be assigned to different int variables, and are applicable in conditional statements.

In Java SE 8 and beyond, the int data type can be utilized to denote an unsigned 32-bit integer, with values ranging from 0 to 2^32-1. The Integer class can be employed to treat the int data type as an unsigned integer.

Example

Example

int a = 100000;  

int b = -200000;

5. long Data Type

The long data type in Java is a fundamental data type that signifies a 64-bit signed two's complement integer. It encompasses a broader spectrum of values compared to int, spanning from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. By default, it is assigned a value of 0.0L or 0.0l.

The long data type is utilized when the integer data type is insufficient to store the intended value or when a broader spectrum of integer values is required.

Syntax:

Example

long num = 15000000000L;

long num = 9,223,372,036,854,775l

The long data type is frequently utilized in scenarios demanding extensive integer values, like in scientific calculations, financial software, and operating systems development. It offers enhanced accuracy and a wider scope compared to int, making it ideal for situations where int falls short.

Example

Example

long a = 5000000L;  

long b = -6000000L;

Note: In Java SE 8 and later versions, we can use the long data type to represent an unsigned 64-bit long number. It has a minimum value of 0 and a maximum value of 2 64 -1.

6. float Data Type

The float data type in Java is a fundamental data type that signifies 32-bit IEEE 754 floating-point numbers with single precision. While it can cover a broad spectrum of decimal values, it is unsuitable for handling precise figures like currency. By default, its initial value is 0.0f or 0.0F.

The float data type proves to be beneficial for scenarios requiring a wider range of values with less emphasis on precision.

Syntax:

Example

float num = 67;

The float data type is known for its capacity to express a broad spectrum of values, encompassing both positive and negative extremes, as well as exceptionally small and large values. Nonetheless, its precision is constrained to around 6-7 significant decimal digits, making it unsuitable for scenarios that demand precise decimal accuracy.

Example

Example

float f = 234.5f;

7. double Data Type

In Java, the double data type is a fundamental data type that denotes double-precision 64-bit IEEE 754 floating-point numbers. By default, it initializes to 0.0. This data type offers a broader spectrum of values and enhanced accuracy in contrast to the float data type. Due to this, it is ideal for scenarios that demand precise handling of decimal values.

Syntax:

Example

double num = 75.658d;

An important benefit of the double data type is its capacity to depict a broader spectrum of values with enhanced accuracy when contrasted with float. It is capable of precisely illustrating values with around 15-16 significant decimal figures, making it appropriate for tasks demanding high precision like financial computations, scientific analysis, and graphic design programming.

Example

Example

double d = 12.3;

Note: If accuracy is the most important concern, it is suggested not to use float and double data types; use the BigDecimal class instead.

8. char Data Type

In Java, the char data type is a fundamental data type that stands for a solitary 16-bit Unicode character. This data type is capable of holding any character from the extensive Unicode character set. This capability enables Java to facilitate internationalization and the display of characters from diverse languages and scripts.

Syntax:

Example

char alphabets = 'J';

char a = 60, b = 61, c = 62;

The char data type is frequently utilized for storing characters like letters, numbers, and symbols. Additionally, it can be employed for mathematical calculations since the Unicode values of characters are interchangeable with integers.

Example

Example

char c = 'A';

For instance, manipulating the Unicode values of char variables can be done by performing addition or subtraction operations on them.

Type Default Values Size Range of Values Example
boolean false 8 bits true, false true, false, 0, 1
byte 0 8 bits -128 to 127 -
char \u0000 16 bits Characters Representation of ASCII values0 to 255 'z', '\u0041', '\11', '\\', '\', '\n'
short 0 16 bits -32,768 to 32,767 -
int 0 32 bits -2,147,483,648to2,147,483,647 -5,-1,0,6,8
long 0 64 bits -9,223,372,036,854,775,808to9,223,372,036,854,775,807 -7L,-2L,0L,1L,2L
float 0.0f 32 bits up to 7 decimal digits 3.14f, 89.09F, -9.3F
double 0.0 64 bits up to 16 decimal digits 6.98765e300d , -567899e-600d , 2e2d

Java Primitive Data Type Example

File Name: Main.java

Example

Example

public class Main

{

    public static void main(String args[])

    {

        boolean flag = true;

        char ch = 'z';

        int num = 1234;

        byte size = 2;

        short srt = 78;

        double value = 2.4546778;

        float temp = 3.8f;

        long val = 1888889;

        System.out.println("boolean: " + flag);

        System.out.println("char: " + ch);

        System.out.println("integer: " + num);

        System.out.println("byte: " + size);

        System.out.println("short: " + srt);

        System.out.println("float: " + value);

        System.out.println("double: " + temp);

        System.out.println("long: " + val);

    }

}

Output:

Output

boolean: true

char: z

integer: 1234

byte: 2

short: 78

float: 2.4546778

double: 3.8

long: 1888889

Non-Primitive Data Types in Java

Within the Java programming language, reference data types are utilized to contain intricate objects instead of basic values. These data types store memory addresses or references that indicate the precise memory location of the object. Understanding this differentiation is crucial as it impacts the storage, transfer, and manipulation of these data types within Java applications.

1. Class

In Java, a widely used non-primitive data type is the class, which serves to instantiate objects. Objects are representations of classes, embodying their attributes and functionalities such as fields and methods.

An illustration of this concept could involve the creation of a Person class aimed at depicting an individual. This class could include attributes such as the individual's name, age, and address, along with functions to assign and retrieve these attributes.

Syntax:

Example

class Main

{

//code

}

2. Interface

Interfaces play a crucial role as a non-primitive data type within Java programming. They serve as blueprints that outline the requirements a class must fulfill when implementing the interface, without dictating the specific implementation details. By leveraging interfaces, developers can achieve abstraction and facilitate multiple inheritance in Java, enhancing the flexibility and reusability of classes.

Syntax:

Example

// interface

interface Shape {

  public void draw(); // interface method (does not have a body)

  public void color(); // interface method (does not have a body)

}

3. Arrays

Arrays represent a core composite data type in Java, enabling the storage of numerous values of identical types within a singular variable. They possess a predetermined size set upon creation and can be retrieved utilizing an index. Arrays are frequently utilized for storing value lists or portraying matrices and various multi-dimensional data constructs.

Syntax:

Example

int[] arr = { 1, 2, 3, 4, 5 };

4. String

In Java, a string represents a series of characters, essentially functioning as a character array. Unlike a character array in Java, which consists of individual char-type elements, a string is intended to store a sequence of characters within a single variable. It is essential to note that, unlike in C/C++, Java strings do not require termination with a null character.

Syntax:

Example

// string without using new operator 

String s = "Hello World"; 

// String using new operator 

String str = new String("Austria");

5. enum

Within Java, there are additional non-primitive data types available, including enums and collections. Enums play a role in establishing a group of named constants, offering a means to depict a stable set of values. Collections, on the other hand, consist of a system of classes and interfaces that deliver flexible data structures like lists, sets, and maps, allowing for adjustments in size according to requirements.

Syntax:

Example

enum Grade {

  FIRST,

  SECOND,

  THIRD

}

In Java, non-primitive data types play a crucial role in the development of intricate and adaptable software. These data types enable the creation and manipulation of objects, establishment of connections between objects, and representation of elaborate data structures.

Java Non-Primitive Data Type Example

File Name: Main.java

Example

Example

enum Color {

    RED,

    GREEN,

    BLUE;

}

public class Main {

    public static void main(String[] args) {

        String str = "Hello";

        int[] arr = { 1, 2, 3, 4, 5 };

        Color clr = Color.BLUE;

        System.out.println(clr);

        System.out.println(str);

        for (int member: arr) {

        System.out.print(member+", ");

        }

    }

}

Output:

Output

BLUE

Hello

1, 2, 3, 4, 5

Why char uses 2 bytes in Java, and what is \u0000 ?

Java employs the Unicode standard rather than the ASCII encoding system. The character \u0000 represents the bottom end of the Unicode range. For a more in-depth understanding of Unicode, please refer to the following section.

Input Required

This code uses input(). Please provide values below: