🔤 Understanding Variables in Java: Data Types, Output & Examples

Abhi YadavAbhi Yadav
3 min read

By Abhi Yadav — documenting my Java journey, one concept at a time.


🧠 What Are Variables?

In Java, variables are containers that hold values — like numbers, text, or decimals — used during a program’s execution.

Think of variables as labeled storage boxes that store information your code needs to remember.


🧪 Example: Declaring & Printing Variables

File: variable.java

javaCopyEditpublic class Variable {
  public static void main(String[] args) {

    int x = 10;                  // Integer type variable
    double price = 55.50;        // Floating-point number
    String name = "Abhi";        // String (text)

    System.out.println("x = " + x);
    System.out.println("price = " + price);
    System.out.println("name = " + name);
  }
}

🧾 Output:

iniCopyEditx = 10  
price = 55.5  
name = Abhi

🧠 What’s Happening Here?

LineDescription
int x = 10;Stores the number 10 in a variable named x
double price = 55.50;Stores a decimal in price
String name = "Abhi";Stores text (a string) in name
System.out.println()Displays the values with labels

🧠 Java Data Types (Common Ones)

TypeExampleDescription
intint age = 20;Whole numbers
doubledouble pi = 3.14;Decimal numbers
StringString name = "Abhi";Text values
booleanboolean isOnline = true;true or false
charchar grade = 'A';Single character

💡 Naming Rules for Variables

✅ Must start with a letter (or _ or $)
✅ Cannot start with a number
✅ Case-sensitive (Price and price are different)
✅ Avoid Java reserved keywords (like int, class, etc.)


📌 Real-Life Analogy

Imagine you’re writing your name on a sticky note and placing it on a box.

  • Variable name = the label on the box (e.g., name)

  • Value = the item inside (e.g., "Abhi")


🎯 What I Learned Today

  • How to declare variables in Java

  • What data types are available

  • How to output values using System.out.println

  • Java’s strict syntax around types


📘 What’s Next?

Next up, I’ll explore how to take user input in Java using the Scanner class.

I’ll cover:

  • How to let the user enter their name, age, and other data

  • How to read different data types using nextLine(), nextInt(), and nextDouble()

  • How to print values that the user enters

Stay tuned — I’m keeping it simple and consistent 🔁


🧠 Blog by Abhi Yadav
🎯 Learning Java. Building in public.
📍 Follow me on X (Twitter)

1
Subscribe to my newsletter

Read articles from Abhi Yadav directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abhi Yadav
Abhi Yadav