Java Variables
In Java, variables are containers for storing data values. Each variable must be declared with a data type before you can use it. The data type specifies the size and type of values that can be stored in the variable.
Declaring Variables
To declare a variable in Java, you specify the data type followed by the variable name. For example:
int age;
Here, int
is the data type and age
is the variable name. You can also initialize a variable at the time of declaration:
int age = 25;
Data Types
Java has different types of variables, which can be categorized into two groups:
- Primitive Data Types: These include
byte
,short
,int
,long
,float
,double
,char
, andboolean
. - **Reference Data Types`: These include objects, arrays, and strings.
Examples
Integer Variable
public class Main {
public static void main(String[] args) {
int age = 30; // Declaring and initializing an integer variable
System.out.println("Age: " + age);
}
}
Double Variable
public class Main {
public static void main(String[] args) {
double price = 19.99; // Declaring and initializing a double variable
System.out.println("Price: " + price);
}
}
Character Variable
public class Main {
public static void main(String[] args) {
char grade = 'A'; // Declaring and initializing a character variable
System.out.println("Grade: " + grade);
}
}
Boolean Variable
public class Main {
public static void main(String[] args) {
boolean isJavaFun = true; // Declaring and initializing a boolean variable
System.out.println("Is Java fun? " + isJavaFun);
}
}
String Variable
public class Main {
public static void main(String[] args) {
String greeting = "Hello, World!"; // Declaring and initializing a string variable
System.out.println(greeting);
}
}
Multiple Variables
You can declare multiple variables of the same type in a single statement, separated by commas:
public class Main {
public static void main(String[] args) {
int a = 5, b = 10, c = 15;
System.out.println(a + " " + b + " " + c);
}
}
Final Variables
If you want to declare a constant variable, you can use the final keyword. The value of a final variable cannot be changed once it is initialized.
public class Main {
public static void main(String[] args) {
final int YEAR = 2024;
System.out.println("Year: " + YEAR);
}
}
Variable Scope
The scope of a variable is the part of the program where the variable is accessible. In Java, there are three types of variables based on their scope:
- Local Variables: Declared inside a method or block.
- Instance Variables: Declared inside a class but outside any method, with no
static
keyword. - Class/Static Variables: Declared with the
static
keyword inside a class, outside any method.
Local Variable Example
public class Main {
public static void main(String[] args) {
int x = 5; // Local variable
System.out.println(x);
}
}
Instance Variable Example
public class Main {
int instanceVar = 10; // Instance variable
public static void main(String[] args) {
Main obj = new Main();
System.out.println(obj.instanceVar);
}
}
Static Variable Example
public class Main {
static int staticVar = 20; // Static variable
public static void main(String[] args) {
System.out.println(Main.staticVar);
}
}