Programming BasicsBasicsbeginner

Variables and Data Types Explained Simply

Variables and data types explained with everyday analogies and small Java and Python programs side by side.

Published
TL;DR – Quick Answer

A variable is a named place in memory that stores a value, such as age = 20. A data type tells the computer what kind of value it is, such as a whole number, a decimal, text or true/false. Java makes you declare the type, while Python works it out from the value.

On This Page

Every program works with data: a student's name, their marks, whether they passed. To work with data, a program needs a place to keep it and a way to know what kind of data it is. Those two ideas are variables and data types.

This page explains both with everyday examples. Each idea has the same small program in Java and in Python, so you can see how two popular languages handle it.

What is a variable

A variable is a named box in the computer's memory that holds a value.

Think of the labelled jars in a kitchen. One jar says "Sugar", another says "Rice". The label tells you what is inside, and you can empty a jar and refill it. In a program:

  • The label is the variable's name, such as age.
  • The contents is the value, such as 20.
  • Refilling the jar means giving the variable a new value.

Here is the same idea in both languages.

Java

public class VariableDemo {
    public static void main(String[] args) {
        int age = 20;
        System.out.println("Age: " + age);

        age = 21;
        System.out.println("Next year: " + age);
    }
}

Python

age = 20
print("Age:", age)

age = 21
print("Next year:", age)

Output (same for both):

Age: 20
Next year: 21

The = sign does not mean "equal" as in maths. It means "store the value on the right in the box on the left". So age = 21 replaces 20 with 21.

What is a data type

A data type tells the computer what kind of value a variable holds. It matters because the computer handles each kind differently.

Think of a form at a bank. The "Name" box takes letters. The "Amount" box takes only numbers. The "Account type" box has a tick for Savings or Current. Each box accepts one kind of data, and that makes the form easy to check.

You can add two numbers. You cannot sensibly add a number to a yes/no tick. Data types let the computer catch such mistakes. Databases use the same idea: when you create a table in MySQL, every column gets a data type such as INT or VARCHAR.

Common data types in Java and Python

Kind of data Example value Java type Python type
Whole number 20, -5 int (also long, short, byte) int
Decimal number 8.75 double (also float) float
Text "Hyderabad" String str
Single character 'A' char No separate type; use a str of length 1
True or false true / True boolean bool

A few details to remember:

  • Java writes true and false in lower case. Python writes True and False with a capital letter.
  • In Java, char uses single quotes ('A') and String uses double quotes ("A"). Python accepts either quote for text.
  • A Java int holds values from about minus 2.1 billion to plus 2.1 billion. Use long for bigger numbers. A Python int can grow as large as memory allows.

Declaring variables: Java vs Python

This is the biggest difference between the two languages.

Java wants you to write the type before the name. This is called static typing. The compiler checks the types before the program runs.

public class StudentInfo {
    public static void main(String[] args) {
        String name = "Divya";
        int marks = 87;
        double cgpa = 8.6;
        char section = 'B';
        boolean passed = true;

        System.out.println(name + " | " + marks + " | " + cgpa + " | " + section + " | " + passed);
    }
}

Python works out the type from the value. This is called dynamic typing. You can check the type with type().

name = "Divya"
marks = 87
cgpa = 8.6
section = "B"
passed = True

print(name, marks, cgpa, section, passed, sep=" | ")
print(type(marks), type(cgpa), type(name), type(passed))

Output of the Java program:

Divya | 87 | 8.6 | B | true

Output of the Python program:

Divya | 87 | 8.6 | B | True
<class 'int'> <class 'float'> <class 'str'> <class 'bool'>
Point Java Python
Write the type? Yes: int marks = 87; No: marks = 87
Change the type later? No. marks = "high"; is a compile error Yes, but it is a bad habit
When are type errors found? Before running (compile time) While running
Semicolon at the end? Yes No

Common mistake: In Python, marks = 87 and later marks = "eighty" is allowed. Your code may then fail far away when it tries to add to marks. Keep one type per variable even when the language does not force you.

Type conversion

Sometimes you need to change a value from one type to another. The most common case is user input, which always arrives as text.

Java

public class ConvertDemo {
    public static void main(String[] args) {
        String text = "45";
        int number = Integer.parseInt(text);
        System.out.println(number + 5);

        double price = 99.9;
        int rounded = (int) price;
        System.out.println(rounded);

        System.out.println(7 / 2);
        System.out.println(7.0 / 2);
    }
}

Python

text = "45"
number = int(text)
print(number + 5)

price = 99.9
rounded = int(price)
print(rounded)

print(7 / 2)
print(7 // 2)

Output of the Java program:

50
99
3
3.5

Output of the Python program:

50
99
3.5
3

Notice two traps here:

  • Turning 99.9 into an int cuts off the decimal. It does not round up. Both languages give 99.
  • 7 / 2 is 3 in Java because both values are int. In Python 3, / always gives a decimal, and // does whole-number division.

If you skip conversion, you get surprises. In Python, "45" + "5" gives "455", because + joins two strings. In Java, "45" + 5 also gives "455".

Rules for naming variables

These rules apply to both Java and Python:

  • A name can have letters, digits and underscores. It cannot start with a digit. marks1 is fine. 1marks is not.
  • No spaces. Use totalMarks in Java (camelCase) and total_marks in Python (snake_case). These are the usual styles in each language.
  • Names are case-sensitive. Age and age are two different variables.
  • You cannot use keywords such as class, if or for as names.
  • Pick names that explain the value. studentCount is clear. x is not, except in short maths code.

Constants: values that should not change

Some values should never change, such as the maximum marks in an exam.

public class ConstantDemo {
    public static void main(String[] args) {
        final int MAX_MARKS = 100;
        int scored = 82;
        System.out.println("Percentage: " + (scored * 100.0 / MAX_MARKS));
    }
}
MAX_MARKS = 100
scored = 82
print("Percentage:", scored * 100 / MAX_MARKS)

Output (same for both):

Percentage: 82.0

In Java, final stops anyone from changing MAX_MARKS. Python has no such keyword for simple variables. Writing the name in capitals is a convention that tells other programmers to leave it alone.

Practice tasks

Try these in the language you are learning:

  1. Store your name, age, CGPA and whether you have a laptop in four variables. Print them on one line.
  2. Store the price of a book as text, such as "350". Convert it to a number and add 18% GST. You can check your answer with the GST calculator.
  3. Store marks for three subjects. Print the total and the average with two decimal places.
  4. Predict the output of 10 / 4 and 10 // 4 in Python, and 10 / 4 in Java. Then run them.

What to learn next

Variables hold one value. Next, learn to repeat work with loops, then store many values together in an array. If you are learning Java, the Java variables lesson goes deeper into memory and scope. If you have not chosen a language yet, read Java vs Python for freshers.

Frequently Asked Questions

What is a variable in simple words?
It is a labelled box that holds a value. You use the label to read or change the value later in the program.
What are the basic data types?
Most languages have whole numbers (int), decimal numbers (float or double), text (string), single characters (char in Java and C) and true or false values (boolean).
Why does Python not need data types?
Python does have data types. It works them out from the value when the program runs, so you do not write them. This is called dynamic typing. Java checks types before the program runs, which is called static typing.
What is the difference between a variable and a constant?
A variable's value can change. A constant should not change after it is set. Java uses the final keyword for constants. Python has no true constants, so programmers write the name in capitals, such as MAX_MARKS, to show it should not change.
Why does 7 / 2 give 3 in Java?
Both numbers are int, so Java does integer division and drops the decimal part. Write 7.0 / 2 to get 3.5. In Python 3, 7 / 2 gives 3.5 and 7 // 2 gives 3.

Want to Build Your Career in Java Full Stack with AI?

Join CodeBegun and train with working industry engineers — View the Java Full Stack curriculum

Apply for Demo Class →
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 4 December 2026 LinkedIn
Chat with us