Java

Data Types

Java / Data Types

Data Types

Data Types

In Java programming, data types define the type of data that a variable can store and also specify the size and value range of that data. Each data type is predefined, which makes Java a statically typed and strongly typed language. This means every variable must be declared with a specific data type before it is used.

Data types help the compiler understand what kind of value a variable holds, such as a number, character, or logical value (true/false).

Types Β (Java data types are mainly divided into two categories:)

1. Primitive Data Types

Primitive data types are the basic data types used to store simple values directly in memory.

  • boolean β†’ true or false values
  • char β†’ single character (e.g., 'A')
  • byte β†’ small integer values
  • short β†’ short integer values
  • int β†’ integer numbers
  • long β†’ large integer numbers
  • float β†’ decimal numbers
  • double β†’ large decimal numbers

2. Non-Primitive Data Types

Non-primitive data types are used to store complex data and multiple values. These are also called reference types.

  • String β†’ sequence of characters (text)
  • Array β†’ collection of similar data elements
  • Class β†’ blueprint for objects
  • Interface β†’ used to achieve abstraction

Data Type Size Range

byte1 byte (8 bits)-128 to 127
short2 bytes (16 bits)-32,768 to 32,767
int4 bytes (32 bits)-2,147,483,648 to 2,147,483,647
long8 bytes (64 bits)-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float4 bytes (32-bit floating point)Β±1.4E-45 to Β±3.4E38
double8 bytes (64-bit floating point)Β±4.9E-324 to Β±1.8E308
char2 bytes (16-bit unsigned)0 to 65,535 (Unicode characters)
booleanNot precisely definedtrue or false

Example

public class DataTypesExample {
Β  Β public static void main(String[] args) {

Β  Β  Β  Β String name = "Hello";
Β  Β  Β  Β int number = 100;
Β  Β  Β  Β float price = 10.5f;
Β  Β  Β  Β char grade = 'A';
Β  Β  Β  Β boolean isJavaEasy = true;

Β  Β  Β  Β System.out.println("String: " + name);
Β  Β  Β  Β System.out.println("int: " + number);
Β  Β  Β  Β System.out.println("float: " + price);
Β  Β  Β  Β System.out.println("char: " + grade);
Β  Β  Β  Β System.out.println("boolean: " + isJavaEasy);
Β  Β }
}

Technology
Java
want to connect with us ?
Contact Us