What are Variables?
Variables in Java are containers that store data values. Think of them as labeled boxes where you can put different types of information. Before using a variable, you must declare it by specifying its type and name.
Variable Declaration Syntax
dataType variableName = value;
Example:
int age = 25;
String name = "John";
Data Types in Java
Java has two main categories of data types:
1. Primitive Data Types
These are the basic data types built into Java. They store simple values and are not objects.
Numeric Types:
- byte
- Size: 8 bits
- Range: -128 to 127
- Use: Small integer values
- Example:
byte temperature = 23;
- short
- Size: 16 bits
- Range: -32,768 to 32,767
- Use: Medium integer values
- Example:
short population = 12000;
- int
- Size: 32 bits
- Range: -2^31 to 2^31-1
- Use: Most common for whole numbers
- Example:
int score = 95000;
- long
- Size: 64 bits
- Range: -2^63 to 2^63-1
- Use: Very large whole numbers
- Example:
long population = 7800000000L;
Floating-Point Types:
- float
- Size: 32 bits
- Precision: 6-7 decimal digits
- Example:
float price = 19.99f;
- double
- Size: 64 bits
- Precision: 15-16 decimal digits
- Example:
double pi = 3.14159265359;
Other Primitive Types:
- char
- Size: 16 bits
- Stores: Single character/letter
- Example:
char grade = 'A';
- boolean
- Size: 1 bit
- Stores: true or false
- Example:
boolean isActive = true;
2. Reference Data Types
These are more complex types that refer to objects.
- String
- Stores text
- Example:
String message = "Hello World";
- Arrays
- Stores multiple values of the same type
- Example:
int[] numbers = {1, 2, 3, 4, 5};
- Classes
- User-defined types
- Example:
Student student = new Student();
Variable Naming Rules
- Must start with a letter, underscore (_), or dollar sign ($)
- Can contain numbers but cannot start with them
- Case-sensitive (age and Age are different variables)
- Cannot use Java keywords
- Should use camelCase convention (firstName, lastName)
Variable Scope
- Instance Variables
- Declared inside class but outside methods
- Accessible throughout the class
public class Example {
int instanceVar = 10; // Instance variable
}
- Local Variables
- Declared inside methods
- Only accessible within that method
public void method() {
int localVar = 20; // Local variable
}
- Static Variables
- Shared across all instances of a class
- Declared with ‘static’ keyword
public class Example {
static int staticVar = 30; // Static variable
}
Best Practices
- Always initialize variables before using them
- Use meaningful variable names
- Choose appropriate data types based on your needs
- Use constants (final keyword) for values that won’t change
- Keep variable scope as narrow as possible
Common Examples
// Basic variable declarations
int age = 25;
double salary = 50000.50;
String name = "John Doe";
boolean isEmployed = true;
// Array declaration
int[] scores = new int[5];
// Constant declaration
final double PI = 3.14159;
// Multiple variables of same type
int x = 1, y = 2, z = 3;
These fundamentals form the basis of Java programming. Understanding data types and variables is crucial for writing efficient and error-free code.