Declaring Variables

2 min read ·

What Does Declaring a Variable Mean?

Declaring a variable means:
  • Telling Java what type of data you want to store
  • Giving that variable a name
When you declare a variable, memory space is reserved, but no value is stored yet.

Basic Syntax

The general syntax for declaring a variable in Java is:
Example:
Here:
  • int, double, char, String → Data types
  • age, salary, grade, name → Variable names

Important

Declaration only creates the variable. It does not assign any value.


Runnable Example – Declaring Variables

Here is a complete Java program that declares variables correctly:
This code will compile and run without errors.

Declaring Multiple Variables of Same Type

You can declare multiple variables of the same type in one line:
Runnable example:

Common Data Types Used While Declaring

Data TypeUsed For
intWhole numbers
doubleDecimal numbers
charSingle character
StringText
Example:

Important Rules

  • Variable name cannot start with a number.
  • Java is case-sensitive (age and Age are different).
  • Semicolon ; is compulsory.
  • Variable name should not be a Java keyword.