Master Core Java Concepts!
Subscribe to Ram N Java for clear explanations, practical code examples, and essential Java tutorials!
Subscribe Now on YouTubeHow to Use Variables in Java
In Java, a variable is a container that holds a data value during the execution of a program. Every variable is explicitly assigned a data type that dictates what kind of information it can store in memory.
Java Data Types Categories
Java provides several primitive data types tailored for different categories of information:
- Integer Types:
byte(8-bit),short(16-bit),int(32-bit), andlong(64-bit) for whole numbers. - Floating-Point Types:
float(32-bit single-precision) anddouble(64-bit double-precision) conforming to IEEE 754 standards for fractional numbers. - Textual Type:
char(16-bit Unicode character) for individual characters. - Logical Type:
boolean(1 bit) representingtrueorfalse.
The Two Essential Steps to Use Variables
Working with variables in Java involves two fundamental steps:
1. Variable Declaration
Declaring a variable specifies its data type and assigns it a unique name (identifier). This tells the compiler to allocate appropriate memory for it:
int age; // 'int' is the data type, 'age' is the variable name
double salary;
char grade;
boolean isActive;
2. Variable Initialization
Initializing a variable means assigning it a valid starting value using the assignment operator (=):
int count = 100; // Container named 'count' holds the value 100
salary = 75000.50;
grade = 'A';
isActive = true;
Key Takeaways
✔️ Containers for Data: A variable stores data values that can be referenced and modified during program execution.
✔️ Two-Step Process: Always declare the variable with a data type and initialize it before use.
✔️ Type Safety: Java is strongly typed; you can only store values compatible with the declared data type.
java online training
ReplyDeletejava training