Java Learning Notes — Telusko Core Java | Video 05
After learning how to set up Java and write my first Java program, I reached one of the most fundamental topics in programming:
Variables and data.
At first, variables seemed simple.
I saw code like:
int num = 3;
and thought:
"Okay,
numcontains 3. That's all."
But when I started looking at the statement more carefully, several questions came up:
- What exactly is a variable?
- Why do I need to tell Java the type?
- What does
intactually mean? - Why is text stored using
String? - What does
=really do? - Why are there both
print()andprintln()? - Can I perform calculations directly using variables?
- Why would I store a calculation in another variable instead of printing it directly?
These questions helped me understand that variables are not just something to memorize.
They are one of the basic building blocks of programming.
This article contains what I understood from Video 05 — Variables, Data & the int Type, along with additional explanations and examples that helped me make the concepts clearer.
What This Article Covers
In this article, I will answer these eight questions:
- What is a variable?
- Why does Java require a variable type?
- What does
intmean? - What is
Stringused for? - What does
=do? - What is the difference between
print()andprintln()? - Can variables be used in arithmetic expressions?
- Why store an intermediate result in another variable?
I will also cover:
- Data and processing
- Database vs variable
- Strong typing
- Semicolons
- Blocks
- Recompiling after changes
- Common beginner mistakes
1. What is a variable?
What I understood first
A variable is one of those programming concepts that sounds complicated until it is explained with a simple example.
A useful beginner-friendly way to imagine a variable is:
A named storage location that holds a value while the program is running.
For example:
int num = 3;
Here, num is a variable.
I can think of it as a named box:
```text id="7e8f6c"
num
┌────────┐
│ 3 │
└────────┘
The name `num` allows me to refer to that stored value later.
For example:
```java
int num = 3;
System.out.println(num);
The output is:
3
Instead of repeatedly writing the value 3, I can use the variable name num.
Why are variables useful?
Imagine I am building a student application.
I might need to work with:
- Student name
- Age
- Marks
- Balance
- Number of subjects
Instead of putting values directly everywhere, I can give them meaningful names.
For example:
String name = "Poushmita";
int age = 22;
int marks = 85;
Now the program has named pieces of data that I can use later.
Variables can change
A variable can generally hold a different value later, as long as the new value is compatible with its declared type.
For example:
int score = 10;
score = 20;
System.out.println(score);
The output is:
20
The variable score initially held 10, and later it was assigned 20.
This is one reason it is called a variable.
Variable vs database
This distinction was also useful for me.
A database is generally used for persistent storage.
A variable is temporary working storage used while a program is executing.
A simple comparison:
| Concept | Purpose |
|---|---|
| Database | Persistent storage |
| Variable | Temporary working storage during program execution |
For example, an application might retrieve a user's name from a database and temporarily store it in a variable while processing the request.
```text id="9h3yik"
Database
↓
Stored data
↓
Application
↓
Variable
↓
Processing
### 🧠 Easy to Remember
> **Variable = named storage for a value during program execution**
Think:
> **Name → Value**
For example:
```text
num → 3
🎯 Short Interview Answer
A variable is a named storage location used to hold a value during program execution. It allows a program to store, access, and modify data.
2. Why does Java require a variable type?
What I understood first
This was one of the things that made Java look stricter than some other languages.
For example, in Java I write:
int num = 10;
I don't simply write:
num = 10;
I have to specify:
int
Why?
Because Java is a strongly typed language.
What does strongly typed mean here?
When I declare a variable, I specify the type of data that the variable is intended to hold.
For example:
int age = 22;
Here:
int
↓
Type
age
↓
Variable name
22
↓
Value
The type tells Java what kind of value the variable is designed to store.
Another example:
String name = "Poushmita";
Here:
String
↓
Type
name
↓
Variable name
"Poushmita"
↓
Value
Why is this useful?
Suppose I have:
int age = 22;
Java knows that age is intended to hold an integer value.
If I try to assign incompatible data to it, Java can detect the problem.
For example:
int age = "twenty two";
This is not valid because "twenty two" is text, not an integer.
This type checking helps catch many mistakes before the program runs.
Types also communicate meaning
The type doesn't only help the compiler.
It also helps me as a developer understand the code.
Compare:
int age = 22;
with:
String age = "22";
Both may display 22, but they represent different kinds of data.
The first is a number.
The second is text.
That difference becomes very important when performing calculations.
🧠 Easy to Remember
Java wants to know what kind of data a variable is supposed to hold.
Think:
Type → Variable → Value
🎯 Short Interview Answer
Java is strongly typed, so variables are declared with a type that specifies the kind of data they are intended to store. This provides type checking and helps make code more predictable and maintainable.
3. What does int mean?
What I understood first
int is one of the first data types I encountered in Java.
It represents an integer, meaning a whole-number value without a fractional part.
Examples include:
int num = 5;
int score = 10;
int balance = -5;
So int can represent both positive and negative integer values within its defined range.
What is an integer?
An integer is a whole number.
Examples:
-10
-5
0
1
5
100
These are integers.
But:
6.5
3.14
10.75
are not integers because they contain fractional parts.
So this is not appropriate:
int price = 6.5;
because 6.5 is not an integer.
Java has other types for values that require fractional parts, which I will explore in later lessons.
Breaking down int num = 5
Let's look at:
int num = 5;
It contains several pieces:
```text id="h5lq0m"
int
↓
Data type
num
↓
Variable name
=
↓
Assignment operator
5
↓
Value
;
↓
End of statement
Understanding this small statement is actually very important because the same pattern appears throughout Java:
```text
type variableName = value;
More examples
int age = 22;
int marks = 85;
int temperature = -5;
int count = 100;
All of these store integer values.
🧠 Easy to Remember
int= integer = whole-number values
Think:
int → -5, 0, 10, 100
but not:
int → 6.5
🎯 Short Interview Answer
intis a Java primitive data type used to store integer values, including positive and negative whole numbers.
4. What is String used for?
What I understood first
When I wanted to store text, I couldn't use int.
For example:
String name = "Poushmita";
Here, String is used to represent text.
What is a String?
A String represents a sequence of characters.
Examples:
String name = "Poushmita";
String city = "Kolkata";
String message = "Hello World";
The text is written inside double quotes.
Why can't I use int for text?
Because int is designed for integer values.
This is valid:
int age = 22;
This is also valid:
String age = "22";
But they are not the same thing.
In the first case:
22 → number
In the second:
"22" → text
That difference becomes important when I perform operations on the values.
For example:
int a = 10;
int b = 20;
System.out.println(a + b);
produces:
30
But string concatenation behaves differently:
String a = "10";
String b = "20";
System.out.println(a + b);
produces:
1020
This is a useful example of why the type of a variable matters.
🧠 Easy to Remember
String→ text
Examples:
"Hello"
"Java"
"Poushmita"
🎯 Short Interview Answer
Stringis used to represent text, which is a sequence of characters. String values are commonly written inside double quotes.
5. What does = do?
What I understood first
The symbol:
=
looks like a mathematical equal sign.
But in a Java statement such as:
int num = 3;
it does something slightly different.
In Java, = is the assignment operator.
It assigns the value or result of the expression on the right to the variable on the left.
Think right → left
For:
int num = 3;
I can think:
3
↓
num
or:
Put the value
3intonum.
So:
```text id="uq3jdu"
Right-hand side
↓
assigned to
↓
Left-hand side
### Example
```java
int num = 3;
means:
Create an integer variable called
numand assign3to it.
Then:
num = 10;
means:
Assign
10to the existing variablenum.
Assignment can use expressions
The right side doesn't have to be a single value.
For example:
int num1 = 3;
int num2 = 5;
int result = num1 + num2;
The expression:
num1 + num2
is evaluated, and its result is assigned to result.
Conceptually:
num1 + num2
↓
8
↓
result
Assignment vs comparison
This distinction is extremely important.
In Java:
=
means:
Assignment
It does not mean "check whether two values are equal."
Comparison for equality uses:
==
For example:
if (num == 10) {
...
}
I don't need to go deeply into == yet, but remembering the difference now will prevent confusion later.
🧠 Easy to Remember
=→ assignment
Think:
Right side → left side
🎯 Short Interview Answer
The
=operator is the assignment operator in Java. It assigns the value of the expression on the right-hand side to the variable on the left-hand side.
6. What is the difference between print() and println()?
What I understood first
Both methods can display output.
For example:
System.out.print(5);
and:
System.out.println(5);
Both display:
5
So what's the difference?
The difference is what happens after the output is printed.
print()
print() displays the value and keeps the cursor on the same line.
For example:
System.out.print(5);
System.out.print(15);
Output:
515
Both values appear on the same line.
println()
println() displays the value and then moves to the next line.
For example:
System.out.println(5);
System.out.println(15);
Output:
5
15
So:
print()
↓
Print and stay on same line
println()
↓
Print and move to next line
A simple example
System.out.print("Hello ");
System.out.print("Java");
Output:
Hello Java
But:
System.out.println("Hello");
System.out.println("Java");
Output:
Hello
Java
Why is this useful?
When creating console applications, controlling how output appears can be important.
For example, if I want a sentence to be constructed on one line, I can use print().
If I want each piece of output on its own line, I can use println().
🧠 Easy to Remember
print()→ same line
println()→ new line
The ln in println is a helpful reminder:
ln → line
🎯 Short Interview Answer
print()displays output without moving to the next line, whileprintln()displays output and then moves the cursor to a new line.
7. Can variables be used in arithmetic expressions?
What I understood first
Yes.
This is one of the main reasons variables are useful.
Instead of writing values directly into every calculation, I can store them in variables and then use those variables in expressions.
For example:
int num1 = 3;
int num2 = 5;
System.out.println(num1 + num2);
The output is:
8
What actually happens?
The expression:
num1 + num2
uses the values currently stored in those variables.
Conceptually:
num1 → 3
num2 → 5
3 + 5
↓
8
Then System.out.println() prints the result.
Other arithmetic operations
Variables can also be used with operators such as:
+
-
*
/
%
For example:
int a = 10;
int b = 3;
System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);
System.out.println(a % b);
The exact behavior of operations such as integer division and remainder becomes more important as I learn Java's numeric types.
Why use variables instead of values directly?
Compare:
System.out.println(3 + 5);
with:
int num1 = 3;
int num2 = 5;
System.out.println(num1 + num2);
The second version is more meaningful when the values represent real data.
For example:
int price = 500;
int discount = 100;
System.out.println(price - discount);
Now the code communicates what the numbers mean.
🧠 Easy to Remember
Variables can participate in expressions just like values can.
Think:
Variable → stored value → expression → result
🎯 Short Interview Answer
Yes. Variables can be used in arithmetic expressions. Java uses the values currently stored in the variables when evaluating the expression.
8. Why store an intermediate result in another variable?
What I understood first
Suppose I have:
int num1 = 3;
int num2 = 5;
System.out.println(num1 + num2);
This works perfectly.
But I can also write:
int num1 = 3;
int num2 = 5;
int result = num1 + num2;
System.out.println(result);
At first, I wondered:
Why create another variable when I can print the calculation directly?
The answer is that storing the result gives the value a name.
Direct calculation
System.out.println(num1 + num2);
The calculation happens and the result is immediately printed.
Store the result
int result = num1 + num2;
System.out.println(result);
Now the calculation and output are separate.
Conceptually:
Input values
↓
Calculation
↓
Result variable
↓
Output
Why is this useful?
In a small example, it may not seem necessary.
But imagine a larger program where I need to use the result several times.
For example:
int price = 500;
int discount = 100;
int finalPrice = price - discount;
Now finalPrice can be used in multiple places:
System.out.println(finalPrice);
or:
// use finalPrice in another calculation
The variable also makes the code easier to understand.
Compare:
System.out.println(price - discount);
with:
int finalPrice = price - discount;
System.out.println(finalPrice);
The second version tells me what the result actually represents.
Intermediate results in larger programs
Real applications often perform multiple steps:
Input
↓
Process
↓
Intermediate result
↓
More processing
↓
Final result
Variables make it possible to store those intermediate values.
This becomes extremely important as programs become more complex.
🧠 Easy to Remember
Store a result in a variable when you need to reuse it, name it, or make the calculation easier to understand.
Think:
Input → Calculation → Result variable → Output
🎯 Short Interview Answer
An intermediate result can be stored in another variable so that it can be reused, given a meaningful name, or used in later calculations. It can also improve code readability.
💾 Why Applications Work With Data
What I understood from the lesson
Almost every useful application works with data.
For example:
- A banking application works with account information and transactions.
- An e-commerce application works with products and orders.
- A student application works with names, marks, and attendance.
- A social-media application works with users, posts, comments, and messages.
A simplified view is:
```text id="7h5f3w"
Input
↓
Processing
↓
Storage
Data may come from:
* Users
* Files
* APIs
* Databases
* Sensors
* Other applications
The program processes that data and produces some result.
### Persistent vs temporary data
This is where the database vs variable distinction becomes useful.
A database is generally used for **persistent storage**.
A variable is generally used as **temporary working storage during program execution**.
For example:
```text id="s4ppx4"
Database
↓
Retrieve user balance
↓
Variable
↓
Perform calculation
↓
Updated value
↓
Database
So variables and databases solve different problems.
✏️ Java Statements and Semicolons
What I understood first
I noticed that many Java statements end with:
;
For example:
int num = 3;
The semicolon marks the end of a Java statement.
Example
int num = 3;
System.out.println(num);
Both are statements, and both end with semicolons.
What about curly braces?
Curly braces define blocks of code.
For example:
{
int num = 3;
System.out.println(num);
}
The braces themselves are not simply "another kind of semicolon."
They define a block containing statements.
This distinction becomes much more important when I learn:
- Classes
- Methods
ifelse- Loops
🧠 Easy to Remember
;→ ends a statement
{ }→ defines a block
🔄 Compile Again After Changing Code
What I understood from this lesson
This is an easy mistake to make when starting out.
Suppose I write:
System.out.println("Hello");
Then compile:
javac Hello.java
and run:
java Hello
I get:
Hello
Now I change the source code:
System.out.println("Hello Java");
If I immediately run the old compiled class without compiling the changed source again, I may still see the previous output.
That's because the .class file hasn't been updated.
The correct workflow
After changing the source:
```text id="k4t8ve"
Edit
↓
Save
↓
Compile
↓
Run
For example:
```bash
javac Hello.java
java Hello
🧠 Easy to Remember
Changed
.java→ compile again before running
Think:
Edit → Compile → Run
⚠️ Common Beginner Mistakes
Declaring a variable without a type
Incorrect:
num = 3;
At this stage, Java expects a declaration such as:
int num = 3;
Trying to store a decimal in int
Incorrect:
int price = 6.5;
6.5 is not an integer.
Forgetting the semicolon
Incorrect:
int num = 3
Correct:
int num = 3;
Confusing = with comparison
Remember:
= → assignment
== → equality comparison
Forgetting to recompile
If I modify:
Hello.java
I should compile again before running the updated program.
Using print() when I need separate lines
This:
System.out.print(5);
System.out.print(15);
produces:
515
while:
System.out.println(5);
System.out.println(15);
produces:
5
15
🧠 One-Minute Revision Sheet
If I need to revise this lesson quickly, this is what I want to remember:
Variable
↓
Named storage for a value
Java
↓
Strongly typed
String
↓
Text
int
↓
Integer / whole number
=
↓
Assignment
;
↓
End of statement
print()
↓
Same line
println()
↓
New line
Variables
↓
Can be used in expressions
Intermediate result
↓
Store calculation in another variable
After editing
↓
Compile again
↓
Run
And the basic variable pattern is:
type variableName = value;
For example:
int num = 3;
🎯 8 Interview Questions — Super Short Answers
What is a variable?
A variable is a named storage location used to hold a value during program execution.
Why does Java require a variable type?
Java is strongly typed, so a variable declaration specifies the kind of data the variable is intended to store.
What does int mean?
intis a primitive data type used to store integer or whole-number values.
What is String used for?
Stringis used to represent text or a sequence of characters.
What does = do?
=is the assignment operator. It assigns the value of the right-hand expression to the variable on the left.
What is the difference between print() and println()?
print()stays on the same line, whileprintln()moves to a new line after printing.
Can variables be used in arithmetic expressions?
Yes. Java uses the values currently stored in variables when evaluating arithmetic expressions.
Why store an intermediate result?
To give the result a meaningful name, reuse it later, or make the program easier to understand.
What I Learned From This Lesson
This lesson changed how I looked at a simple statement like:
int num = 3;
Before, I saw it as just one line of Java code.
Now I can break it down:
int
↓
What kind of data?
num
↓
What is the variable called?
=
↓
Assign the value
3
↓
What value should it hold?
;
↓
Statement is complete
That small line contains several important programming concepts.
I also started seeing the relationship between data and processing more clearly:
Input
↓
Variables
↓
Processing
↓
Intermediate Results
↓
Output
And that is essentially what many programs do at a basic level.
A Note for Other Beginners
If variables feel confusing at first, don't try to memorize every data type immediately.
Start with the basic idea:
A variable is a named place where a program can temporarily store data.
Then learn the pattern:
type variableName = value;
For example:
int age = 22;
String name = "Poushmita";
Once this pattern becomes familiar, other data types and more complex expressions become much easier to understand.
Also remember that:
=
means assignment.
It does not mean comparison.
And whenever you change your Java source code:
Edit → Save → Compile → Run
That small workflow will save a lot of confusion when you're starting.
📚 Related Java Learning Resources
This article is part of my Java Learning Notes — Telusko Core Java series.
If you are following the series from the beginning:
👉 Video 01 — Java Introduction: 8 Questions I Had as a Beginner and Their Easy Answers
👉 Video 02 — Setting Up Java: 6 Questions I Had About JDK, VS Code, LTS and PATH
👉 Video 03 — Writing Your First Java Code: 5 Questions I Had as a Beginner
If you have also covered the JVM/JRE/JDK lesson, add that article here as well:
🔗 My Java Learning Notes
I am maintaining the complete structured notes and resources in my GitHub repository.
👉 Java Learning Notes — Telusko Core Java
The GitHub repository contains my concise learning notes and resources, while these DEV.to articles go deeper into the questions and concepts that I found confusing during my learning.
Final Takeaway
For me, this lesson made the idea of data inside a program much clearer.
The simple mental model I want to remember is:
Data
↓
Variable
↓
Processing
↓
Result
↓
Output
And the basic Java syntax:
int num = 3;
can be understood as:
int
→ type
num
→ variable name
=
→ assignment
3
→ value
;
→ end of statement
The most important things I want to remember from this lesson are:
Variable → named storage
String→ text
int→ integer
=→ assignment
print()→ same line
println()→ new lineEdit → Compile → Run
These are small concepts, but they form the foundation for much more advanced Java programming.
And now that I understand how Java stores and processes basic data, the next step is to learn more about the different data types Java provides and how they behave.
This is part of my ongoing Java learning journey. I'm documenting what I learn, the questions I get stuck on, and the explanations that finally make the concepts click for me — hopefully they can help another beginner too.
Top comments (0)