Java Variables

Storing and managing data in your Java programs

📦 What are Java Variables?

Java variables are containers that store data values. Think of them as labeled boxes where you can put different types of information like numbers, text, or true/false values.


public class VariableExample {
    public static void main(String[] args) {
        int age = 25;           // Integer variable
        String name = "John";   // String variable
        double price = 19.99;   // Double variable
        
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Price: $" + price);
    }
}
                                    

Output:

Name: John
Age: 25
Price: $19.99

Java Data Types

🔢

Integer (int)

Whole numbers without decimals

int count = 42;
int temperature = -10;
💰

Double

Numbers with decimal points

double price = 29.99;
double pi = 3.14159;
📝

String

Text and characters

String message = "Hello World";
String email = "[email protected]";

Boolean

True or false values

boolean isActive = true;
boolean isComplete = false;

🔹 Creating Variables

To create a variable in Java, you need to specify the data type and give it a name:

public class CreateVariables {
    public static void main(String[] args) {
        // Syntax: dataType variableName = value;
        
        int studentCount = 30;        // Integer
        double average = 85.5;        // Double  
        String schoolName = "ABC High"; // String
        boolean isOpen = true;        // Boolean
        
        // You can also declare first, then assign
        int grade;
        grade = 95;
        
        System.out.println("School: " + schoolName);
        System.out.println("Students: " + studentCount);
        System.out.println("Average: " + average + "%");
        System.out.println("Open: " + isOpen);
        System.out.println("Grade: " + grade);
    }
}

Output:

School: ABC High
Students: 30
Average: 85.5%
Open: true
Grade: 95

🔹 Variable Rules

Java has specific rules for naming variables:

✅ Valid Variable Names:

  • Must start with a letter, underscore (_), or dollar sign ($)
  • Can contain letters, numbers, underscores, and dollar signs
  • Cannot use Java keywords (like int, class, public)
  • Case-sensitive (age and Age are different)
public class VariableRules {
    public static void main(String[] args) {
        // ✅ Valid variable names
        int age = 25;
        String firstName = "John";
        double _price = 19.99;
        boolean $isValid = true;
        int number1 = 10;
        
        // ❌ Invalid variable names (these would cause errors)
        // int 2number = 5;     // Cannot start with number
        // String first-name;   // Cannot use hyphen
        // boolean class = true; // Cannot use Java keywords
        
        System.out.println("Age: " + age);
        System.out.println("Name: " + firstName);
        System.out.println("Price: $" + _price);
        System.out.println("Valid: " + $isValid);
        System.out.println("Number: " + number1);
    }
}

Output:

Age: 25
Name: John
Price: $19.99
Valid: true
Number: 10

🔹 Changing Variable Values

You can change the value of a variable after creating it:

public class ChangeValues {
    public static void main(String[] args) {
        int score = 0;           // Initial value
        System.out.println("Initial score: " + score);
        
        score = 50;              // Change the value
        System.out.println("Updated score: " + score);
        
        score = score + 25;      // Add to existing value
        System.out.println("Final score: " + score);
        
        String status = "Beginner";
        System.out.println("Status: " + status);
        
        status = "Advanced";     // Change string value
        System.out.println("New status: " + status);
    }
}

Output:

Initial score: 0
Updated score: 50
Final score: 75
Status: Beginner
New status: Advanced

🧠 Test Your Knowledge

Which data type is used to store whole numbers in Java?