Java Variables – Examples

Welcome to The Coding College, your go-to platform for learning programming concepts! In this post, we’ll explore Java variables with practical examples. By the end, you’ll have a solid understanding of how to declare, initialize, and use variables effectively in Java.

What Are Variables in Java?

A variable is a container that holds data values during the execution of a program. It has:

  • Name: The identifier for the variable.
  • Type: The data type of the variable (e.g., int, String, float).
  • Value: The data stored in the variable.

Example 1: Declaring and Initializing Variables

Code:

public class VariableExample {
    public static void main(String[] args) {
        int age = 25;         // Declares an integer variable
        double salary = 45000.75; // Declares a double variable
        String name = "John"; // Declares a string variable

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Salary: " + salary);
    }
}

Output:

Name: John  
Age: 25  
Salary: 45000.75

Example 2: Changing Variable Values

You can reassign values to variables.

Code:

public class UpdateVariable {
    public static void main(String[] args) {
        int number = 10;
        System.out.println("Initial number: " + number);

        number = 20; // Update the variable
        System.out.println("Updated number: " + number);
    }
}

Output:

Initial number: 10  
Updated number: 20 

Example 3: Using Variables in Calculations

Variables can store data used for calculations.

Code:

public class CalculationExample {
    public static void main(String[] args) {
        int num1 = 15, num2 = 25;
        int sum = num1 + num2; // Adding two variables

        System.out.println("Sum: " + sum);
    }
}

Output:

Sum: 40

Example 4: Declaring Multiple Variables in One Line

Java allows declaring multiple variables of the same type in one line.

Code:

public class MultipleVariables {
    public static void main(String[] args) {
        int x = 5, y = 10, z = 15;

        System.out.println("x: " + x + ", y: " + y + ", z: " + z);
    }
}

Output:

x: 5, y: 10, z: 15

Example 5: Using Constants

A variable declared with the final keyword becomes a constant and cannot be modified.

Code:

public class ConstantExample {
    public static void main(String[] args) {
        final double PI = 3.14159;
        System.out.println("Value of PI: " + PI);

        // Uncommenting the line below will cause an error
        // PI = 3.14; // Error: cannot assign a value to final variable PI
    }
}

Output:

Value of PI: 3.14159

Example 6: Concatenating Strings with Variables

Combine text and variables to create meaningful outputs.

Code:

public class StringConcatenation {
    public static void main(String[] args) {
        String firstName = "Jane";
        String lastName = "Doe";

        String fullName = firstName + " " + lastName; // Combine variables
        System.out.println("Full Name: " + fullName);
    }
}

Output:

Full Name: Jane Doe

Example 7: Variable Scope

The scope of a variable determines where it can be accessed.

Code:

public class VariableScope {
    public static void main(String[] args) {
        int outerVariable = 10; // Declared in the main method

        if (true) {
            int innerVariable = 20; // Declared inside if block
            System.out.println("Inner Variable: " + innerVariable);
        }

        System.out.println("Outer Variable: " + outerVariable);
        // Uncommenting the line below will cause an error
        // System.out.println(innerVariable); // Error: innerVariable not accessible here
    }
}

Output:

Inner Variable: 20  
Outer Variable: 10

Example 8: Default Values of Variables

Class-level variables get default values, while local variables must be initialized.

Code:

public class DefaultValues {
    static int defaultInt;
    static double defaultDouble;
    static String defaultString;

    public static void main(String[] args) {
        System.out.println("Default int: " + defaultInt);
        System.out.println("Default double: " + defaultDouble);
        System.out.println("Default String: " + defaultString);
    }
}

Output:

Default int: 0  
Default double: 0.0  
Default String: null

Key Takeaways

  • Variables are fundamental to Java programming.
  • Use meaningful names to enhance code readability.
  • Be mindful of scope and initialization.
  • Constants are defined using the final keyword.

For more programming tips, tutorials, and examples, visit TheCodingCollege.com. We’re here to make coding simple and enjoyable for everyone! 🚀

Got questions or suggestions? Drop them in the comments below. Happy coding! 😊

Leave a Comment