Have a question?
Message sent Close
View Categories

What is Java Variable?

What is Java Variable?

Declaration and Initialization #

📄
filename.js
dataType variableName;

📄
filename.js
dataType variableName = value;

📄
filename.js
public class Main {
    public static void main(String[] args) {
        // Declaration of variables
        int age;
        double salary;
        String name;

        // Initialization of variables
        age = 25;
        salary = 50000.50;
        name = "John Doe";

        // Output the values of the variables
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Salary: " + salary);
    }
}

Explanation #

  1. int age;
  • This declares an integer variable named age.

  • This declares a double variable named salary, which can hold decimal values.

  • This declares a String variable named name, which can hold a sequence of characters.