About Us Contact Us Write for Us Advertise
Home > Java > Variables and Data Types in Java (Beginner's Guide)
Java

Variables and Data Types in Java (Beginner's Guide)

Learn how to store data in Java: what variables are, how to declare them, and the primitive data types (int, double, boolean, char) plus String — explained simply with examples.

Shiv Pandey
Shiv Pandey
Sep 17, 2026 | 76 views
Variables and Data Types in Java (Beginner's Guide)

Now that you can read a basic Java program (from Lesson 3), it's time to make your programs actually remember things — names, numbers, yes/no answers. In programming, we store data in variables, and every variable has a data type that says what kind of value it holds. This lesson makes both crystal clear.

What is a variable?

A variable is a named box in memory that stores a value you can use and change later. Think of it like a labelled container: the label is the variable's name, and inside is the value.

int age = 25;   // a box named "age" holding the number 25

That single line does two things:

  • Declares a variable: its type is int and its name is age.
  • Initializes it: stores the value 25 inside.

You can use it, then change it:

int age = 25;
System.out.println(age);   // prints 25
age = 26;                  // change the value (no type needed again)
System.out.println(age);   // prints 26

Data types: what kind of value?

Java is a statically typed language — every variable must have a type, and it can only hold values of that type. Java has two families of types: primitive types (the basic building blocks) and reference types (like String and objects).

The 8 primitive types

Type Stores Example
int Whole numbers int score = 90;
double Decimal numbers double price = 9.99;
boolean true or false boolean isOn = true;
char A single character char grade = 'A';
long Very large whole numbers long views = 9000000000L;
float Smaller decimals float temp = 36.6f;
short, byte Smaller whole numbers byte b = 100;

In everyday code, you'll mostly use int, double, boolean, and char. Note the small extras: long values end with L and float values end with f.

String: text

String holds text (a sequence of characters). It's not a primitive — it's a reference type — but you use it just as easily:

String name = "GyaanPost";
System.out.println("Hello, " + name);   // Hello, GyaanPost

Notice the + — with Strings it joins text together (called concatenation). Note the capital S in String.

A quick all-in-one example

String name = "Asha";
int age = 21;
double height = 5.4;
boolean isStudent = true;
char grade = 'A';

System.out.println(name + " is " + age + " years old.");

The var shortcut

Since Java 10, you can let Java figure out the type using var — as long as you assign a value immediately:

var city = "Delhi";   // Java infers String
var count = 10;         // Java infers int

It's convenient, but as a beginner, writing the explicit type is a great way to build understanding.

Naming your variables

  • Use clear, descriptive names: totalPrice, not tp.
  • Start with a lowercase letter and use camelCase for multiple words: firstName, isLoggedIn.
  • Names can include letters, digits, _ and $, but can't start with a digit or be a Java keyword.
  • Remember: names are case-sensitive (age ≠ Age).

A habit that saved me endless confusion early on: name variables for what they hold, not to save typing. Reading totalPrice six months later is effortless; reading x2 is a mystery. Your future self is the main person you're writing clear code for.

Key takeaways

  • A variable is a named box that stores a value; declare it with a type and a name.
  • Java has 8 primitive types — you'll mostly use int, double, boolean, char.
  • String stores text; + joins Strings together.
  • Use clear camelCase names, and remember Java is case-sensitive.

← Previous: Lesson 3 — Java Syntax Basics
Next: Lesson 5 — Operators in Java Explained →

Frequently Asked Questions

What is a variable in Java?

A variable is a named storage location that holds a value you can use and change later. You declare it with a type and a name, for example int age = 25; which creates an int variable named age holding the value 25.

What are the primitive data types in Java?

Java has 8 primitive types: int (whole numbers), double and float (decimals), boolean (true/false), char (a single character), and long, short, byte (other whole-number sizes). Beginners mostly use int, double, boolean, and char.

Is String a primitive type in Java?

No. String is a reference type, not a primitive, but it is used just as easily to store text. You can join Strings together using the + operator, and String is written with a capital S.

What is the var keyword in Java?

Since Java 10, var lets Java infer a variable type automatically from the value you assign, such as var city = "Delhi"; which becomes a String. It only works when you assign a value immediately, and beginners often benefit from writing explicit types first.

Related Articles

== vs .equals() in Java: The Difference Explained (Beginner Guide)
Java

== vs .equals() in Java: The Difference Explained (Beginner Guide)

Java vs Python: Which Should You Learn? (Beginner Guide)
Java

Java vs Python: Which Should You Learn? (Beginner Guide)

Java Design Patterns & Best Practices (Beginner Guide)
Java

Java Design Patterns & Best Practices (Beginner Guide)

Spring Data JPA: Databases Made Easy (Beginner Guide)
Java

Spring Data JPA: Databases Made Easy (Beginner Guide)