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
trueandfalsein lower case. Python writesTrueandFalsewith a capital letter. - In Java,
charuses single quotes ('A') andStringuses double quotes ("A"). Python accepts either quote for text. - A Java
intholds values from about minus 2.1 billion to plus 2.1 billion. Uselongfor bigger numbers. A Pythonintcan 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 = 87and latermarks = "eighty"is allowed. Your code may then fail far away when it tries to add tomarks. 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.9into anintcuts off the decimal. It does not round up. Both languages give99. 7 / 2is3in Java because both values areint. 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.
marks1is fine.1marksis not. - No spaces. Use
totalMarksin Java (camelCase) andtotal_marksin Python (snake_case). These are the usual styles in each language. - Names are case-sensitive.
Ageandageare two different variables. - You cannot use keywords such as
class,iforforas names. - Pick names that explain the value.
studentCountis clear.xis 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:
- Store your name, age, CGPA and whether you have a laptop in four variables. Print them on one line.
- 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. - Store marks for three subjects. Print the total and the average with two decimal places.
- Predict the output of
10 / 4and10 // 4in Python, and10 / 4in 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?
What are the basic data types?
Why does Python not need data types?
What is the difference between a variable and a constant?
Why does 7 / 2 give 3 in Java?
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

