Datatypes in Java
Hey there! In this guide, we'll explore the different data types available in Java. Understanding data types is crucial for writing efficient and clear Java code. Let's dive in!
- In Java, variables are containers that hold data and are defined by specifying a type followed by the variable name.
- Variables must be declared before use and can hold various data types.
Video Explanation

1. Declaring Variables in Java
- To declare a variable in Java, you specify its type, followed by the variable's name.
int age;
double salary;
char grade;
2. Initializing Variables in Java
- Variables can be initialized with a value at the time of declaration.
int age = 25;
double salary = 45000.50;
char grade = 'A';
3. Types of Variables in Java
a. Integer Variables (int, byte, short, long)
- Holds whole numbers, both positive and negative.
int x = 10; byte y = 20; // Range: -128 to 127
short z = 30000; // Range: -32,768 to 32,767
long w = 123456789L; // Suffix 'L' for long literals
b. Floating-Point Variables (float, double)
- Represents real numbers, with
floatusing less precision thandouble.
float pi = 3.14f; // Suffix 'f' for float literals
double gravity = 9.81;
c. Character Variables (char)
- Stores a single character enclosed in single quotes.
char initial = 'A';