Java From Beginner To Advanced

0% completed

Previous
Next
Variable Declarations and Initialization

In this lesson, we learn how to declare and initialize variables in Java. A variable is like a container that stores data.

Each section below includes its own complete code example that you can run to see the concepts in action.

What Is a Variable?

A variable is a named space in memory used to store a value that may change during program execution.

Note: In this lesson, we explain the idea of a variable. Code examples in later sections will show how to declare and initialize them. We will learn about variable data types in the next lesson.

Declaring Variables

Declaring a variable tells Java that a variable exists with a given name and data type.

Syntax

type variableName;
  • type: The kind of data (e.g., int, String).
  • variableName: The identifier you choose for the variable.

Rules for Declaring Variable Names in Java

When declaring variables in Java, it's essential to follow specific naming conventions and rules to ensure your code is readable, maintainable, and free from errors. Below are the key rules and best practices for naming variables in Java:

  • Start with a Letter, Underscore (_), or Dollar Sign ($):

    • Variable names must begin with a letter (A-Z or a-z), an underscore (_), or a dollar sign ($).
    • Example: int age;, double _salary;, String $name;
  • Subsequent Characters Can Include Letters, Digits, Underscores, or Dollar Signs:

    • After the first character, variable names can contain letters, digits (0-9), underscores, or dollar signs.
    • Example: int count1;, double total_amount;, String user$Name;
  • Case-Sensitive:

    • Java variable names are case-sensitive, meaning myVariable, MyVariable, and MYVARIABLE are considered distinct.
  • Cannot Use Reserved Keywords:

    • Variable names cannot be Java reserved keywords such as int, class, public, static, void, etc.
    • Incorrect Example: int class = 5; // Error
    • Correct Example: int myClass = 5;
  • No Spaces Allowed:

    • Variable names must be a single continuous string without spaces. Use camelCase or underscores to separate words.
    • Incorrect Example: int my variable; // Error
    • Correct Examples: int myVariable;, int my_variable;
  • Should Not Start with a Digit:

    • Variable names cannot begin with a digit (0-9). However, digits can be used after the first character.
    • Incorrect Example: int 1stPlace; // Error
    • Correct Example: int firstPlace;, int place1;
  • Avoid Using Special Characters Except _ and $:

    • Only underscores (_) and dollar signs ($) are permitted as special characters in variable names. Avoid using other special characters like @, #, %, etc.
    • Incorrect Example: int total@Amount; // Error
    • Correct Example: int total_Amount;, int total$Amount;

Example

Java
Java
. . . .

Explanation:

  • The variable number is declared as an integer.

Initializing Variables

Initializing a variable means giving it an initial value at the time of declaration.

Syntax

type variableName = value;

Here, value is assigned to the variableName variable.

Example

Java
Java

. . . .

Explanation:

  • The variable number is initialized with 10.
  • The variable message is initialized with "Hello, Java!".
  • Both values are printed to the console.

Multiple Variable Declarations

You can declare several variables of the same type on one line, and you can initialize them together or separately.

Example

Java
Java

. . . .

Explanation:

  • Variables a, b, and c are declared and initialized in one line.
  • Their values are printed individually.

In our next lesson, we will cover Data Types in Java. Data types determine the kind of values a variable can store, such as numbers, characters, and text.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next