Variables and Data Types
What are Variables in Java
Variables are containers used to store data values in a Java program. Each variable has a name, a data type, and a value. Variables allow you to store and reuse data efficiently throughout your program.
google.com, pub-8434817042454839, DIRECT, f08c47fec0942fa0
Example:
int age = 25;
Primitive Data Types in Java
Primitive data types are the most basic data types provided by Java. They store simple values.
Types of Primitive Data Types
- int – used to store whole numbers
- float – used to store decimal values
- double – used for high precision decimal values
- char – used to store a single character
- boolean – used to store true or false values
Example:
int number = 10;
float marks = 85.5f;
double salary = 50000.75;
char grade = ‘A’;
boolean isJavaEasy = true;
float marks = 85.5f;
double salary = 50000.75;
char grade = ‘A’;
boolean isJavaEasy = true;
Non-Primitive Data Types in Java
Non-primitive data types are reference types and can store more complex data.
Types of Non-Primitive Data Types
- String – stores text
- Arrays – stores multiple values
- Classes – user-defined data types
- Objects – instances of classes
Example:
String name = “Groot Academy”;
Variable Declaration and Initialization
Declaration
int number;
Initialization
number = 10;
Combined Declaration and Initialization
int number = 10;
Rules for Naming Variables
- Variable names must start with a letter, underscore (_), or dollar sign ($)
- Cannot start with a number
- Cannot use Java keywords
- Java is case-sensitive
- Use meaningful and readable names

