🧰 Java Basics to Intermediate
Estimated reading: 3 minutes 26 views

πŸ“¦ Java Variables – Declare, Initialize & Use Variables in Java


🧲 Introduction – What Are Variables in Java?

Every Java program revolves around data β€” and variables are how we store and manipulate that data. Think of a variable as a container with a label, used to hold different kinds of information like numbers, text, or boolean values.

Whether you’re calculating tax or storing user input, understanding Java variables is a fundamental step.

By the end of this guide, you’ll be able to:

  • βœ… Declare and initialize variables
  • βœ… Understand Java’s variable types and naming rules
  • βœ… Use variables in expressions and output
  • βœ… Follow best practices for clean, readable code

πŸ”‘ What Is a Variable in Java?

A variable in Java is a named memory location that stores a value.

Syntax:

type variableName = value;

βœ… Example:

int age = 25;

βœ… Explanation:

  • int: data type (integer)
  • age: variable name
  • 25: value assigned to the variable

πŸ“š Types of Variables in Java

Java supports several types of variables, depending on where and how they are declared.

TypeScopeDefined InsideExample
LocalMethodMethod or blockint count = 10;
InstanceObjectClass (outside any method, without static)String name;
StaticClassClass with static keywordstatic int totalUsers;

πŸ’‘ Tip: Always initialize local variables before using them.


🏷️ Declaring and Initializing Variables

πŸ”Ή Declare First, Assign Later:

int score;
score = 100;

πŸ”Ή Declare and Assign Together:

String city = "Mumbai";

πŸ”Ή Multiple Variables of Same Type:

int x = 10, y = 20, z = 30;

⚠️ Warning: Avoid using undeclared or uninitialized variables β€” it causes a compile-time error.


πŸ’‘ Variable Naming Rules

βœ… Allowed:

  • Letters (a–z, A–Z)
  • Digits (0–9)
  • Underscore (_) and Dollar sign ($)

🚫 Not Allowed:

  • Names starting with a digit
  • Reserved keywords (class, int, etc.)

βœ… Examples:

Valid NamesInvalid Names
userName1user (starts with digit)
total_amountint (Java keyword)
MAX_SIZE@value (invalid symbol)

πŸ’‘ Best Practice: Use camelCase for variables (firstName, totalAmount).


πŸ“ Example: Using Variables in Java

public class UserProfile {
    public static void main(String[] args) {
        String name = "Alice";
        int age = 30;
        double height = 5.7;

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Height: " + height + " feet");
    }
}

πŸ–¨οΈ Output:

Name: Alice
Age: 30
Height: 5.7 feet

βœ… Explanation:

  • Different data types are used (String, int, double)
  • All values are printed using System.out.println()

πŸ§ͺ Java Variable Data Types (Quick Overview)

TypeDescriptionExample Value
intInteger numbers100, -45
doubleDecimal numbers3.14, -9.8
charSingle character'A', '9'
booleanTrue/false valuetrue, false
StringSequence of characters"Hello"

πŸ“˜ You’ll explore these in detail in the Java Data Types section.


🧠 Summary – Java Variables Essentials

Java variables are the foundation of storing and manipulating data. You’ve learned:

  • How to declare and initialize variables
  • Types of variables: local, instance, static
  • Naming rules and best practices
  • How to use variables in Java programs.

❓FAQs – Java Variables

❓ What is a variable in Java?

A variable is a container that holds a value β€” like a label on a storage box in memory.

❓ Can a Java variable name start with a number?

No. Variable names cannot begin with digits, but digits can be used after the first character.

❓ What happens if I don’t initialize a variable?

  • Local variables must be initialized before use β€” or you’ll get a compile error.
  • Instance variables get default values (0, false, null, etc.).

❓ What’s the difference between String and char?

  • char: stores a single character ('A')
  • String: stores a sequence of characters ("Apple")

Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Java Variables

Or Copy Link

CONTENTS
Scroll to Top