Data Types, Variables in Java

1) Data Types, Variables, and Constants in Java

Data Types

In Java, data types define the kind of data a variable can hold. They are categorized into two types:

  • Primitive Data Types: These are predefined by the language
    • byte: 8-bit integer (-128 to 127)
    • short: 16-bit integer (-32,768 to 32,767)
    • int: 32-bit integer (-2^31 to 2^31-1)
    • long: 64-bit integer (-2^63 to 2^63-1)
    • float: Single-precision 32-bit floating-point
    • double: Double-precision 64-bit floating-point
    • char: 16-bit Unicode character
    • boolean: Represents true or false
  • Reference Data Types: Used to store objects like arrays, strings, or user-defined classes.

Variables

A variable is a storage unit used to hold data values. Variables in Java must be declared with a type.

data_type variable_name = value;
int age = 25;
double price = 99.99;
char grade = 'A';
boolean isActive = true;

Constants

Constants are unchanging values that remain the same once defined. Declared using the final keyword.

final data_type CONSTANT_NAME = value;
final double PI = 3.14159;
final int MAX_USERS = 100;
Previous Next