Live AI Powered DevOps with AWS
JavaData types

Variables

Why Do We Build Software?

Software exists to solve real-world problems, and every meaningful problem involves handling data.
This data may represent numbers, text, user input, sensor readings, or results of calculations. To work with this information effectively, programs need a way to store values temporarily in memory, and this is done using variables.

Variables allow Java programs to store, update, and reuse data while the program is running.

A variable is one of the most fundamental building blocks in programming. Mastering variables will help you write more powerful, flexible, and dynamic programs.

What is a Variable?

A variable is like a labeled container in memory that stores a piece of data.
When your program runs, the JVM allocates space in memory to hold the value.

Every variable has:

  • A name (also called an identifier)
  • A data type (defines what kind of value it can store)
  • A value (the actual data stored)

Example:

int age = 25;    // 'age' is a variable of type int

Here:

  • int → data type
  • age → variable name
  • 25 → value stored in the variable

You can change the value of a variable at any time as the program executes.

Naming Conventions

Java has specific rules for naming variables, and following good style makes your code easy to read and maintain.

Start Rules

A variable name must start with a letter, _, or $.
It can contain numbers after the first character.
It cannot start with a digit like 1value.

Meaningful Names

Choose descriptive names that represent the stored data, such as
studentName, accountBalance, speedLimit.

Camel Case Style

Java follows camelCase for variable names:
example → firstName, maxSpeed, totalMarks.

Avoid Special Symbols

Do not use illegal symbols like @, %, -.
Only letters, digits, _, and $ are allowed.

Good naming improves readability and reduces bugs.

Strongly Typed Language

Java is a strongly typed language, meaning every variable must have a declared data type. This helps the compiler prevent mistakes and ensures safer code.

Common data types include:

  • int → whole numbers
  • double → decimals
  • String → text
  • boolean → true or false

Example Code 1 – Direct Computation

public class Hello {
    public static void main(String[] args) {
        System.out.println(3 + 5);  
        System.out.println(8 + 7);  
    }
}

Here, values are used directly without storing them in variables. While this works, it doesn’t scale well for larger programs.

  • print → prints output on the same line
  • println → prints and moves to the next line

Example Code 2 – Using Variables

public class Hello {
    public static void main(String[] args) {
        int num = 3;  
        System.out.println(num);   // Prints 3
    }
}

Here, we store a value in a variable and then print it. This approach is more flexible because you can change the value of num anytime.

Example Code 3 – Storing Results

public class Hello {
    public static void main(String[] args) {
        int num1 = 3;
        int num2 = 5;
        int result = num1 + num2;
        System.out.println(result);   // Output: 8
    }
}

This example demonstrates:

  • Using multiple variables
  • Performing a calculation
  • Storing the result in another variable

Variables allow you to break larger problems into smaller steps.

Key Point Summary

  • Variables are containers that store temporary data in memory.
  • Each variable has a name, type, and value.
  • Java uses strong typing to prevent type-related errors.
  • Variables make programs reusable, readable, and more dynamic.
  • Calculations and logic become easier and clearer when using variables.

How is this guide?

Last updated on