CHAPTER 04
Beginner
Variables and Data Types
Updated: May 17, 2026
5 min read
# CHAPTER 4
Variables and Data Types
1. Introduction
Variables are the building blocks of any program. They are containers that store data in your computer's memory. Think of a variable as a labeled jar — the label is the variable name, and the contents inside is the value. In this chapter, we'll learn about Java's data types and how to use variables effectively.2. Learning Objectives
- Declare and initialize variables.
- Understand all 8 primitive data types.
- Differentiate between primitive and non-primitive types.
-
Use constants with the
finalkeyword.
- Perform type casting (widening and narrowing).
3. What is a Variable?
A variable is a named storage location in memory that holds a value.
java
4. Declaring Variables
Syntax:dataType variableName = value;
java
5. Primitive Data Types
Java has 8 primitive data types — this is a critical interview topic:| Type | Size | Range | Example |
|---|---|---|---|
byte | 1 byte | -128 to 127 | byte b = 100; |
short | 2 bytes | -32,768 to 32,767 | short s = 30000; |
int | 4 bytes | -2.1B to 2.1B | int i = 100000; |
long | 8 bytes | Very large | long l = 99999L; |
float | 4 bytes | 6-7 decimal digits | float f = 3.14f; |
double | 8 bytes | 15 decimal digits | double d = 3.14159; |
char | 2 bytes | Single character | char c = 'A'; |
boolean | 1 bit | true or false | boolean b = true; |
java
6. Non-Primitive Data Types
Non-primitive types are created by the programmer and refer to objects:-
String— A sequence of characters
-
Arrays— A collection of elements
-
Classes— User-defined blueprints
-
Interfaces— Contracts for classes
java
Key Difference: Primitive types store actual values. Non-primitive types store *references* (memory addresses) pointing to objects on the Heap.
7. Constants with final
A constant is a variable whose value cannot be changed after assignment.
java
8. Type Casting
Widening (Automatic): Smaller type → Larger type (safe, no data loss)
java
Narrowing (Manual): Larger type → Smaller type (risky, possible data loss)
java
9. var Keyword (Java 10+)
Java 10 introduced local variable type inference:
java
10. Common Mistakes
-
Using
floatwithout thefsuffix:float f = 3.14;causes an error. Usefloat f = 3.14f;.
-
Using
longwithout theLsuffix: Large numbers needlong x = 999999999L;.
-
Integer overflow:
int x = 2147483647 + 1;wraps around to a negative number!
-
Confusing
=and==:=is assignment,==is comparison.
11. Best Practices
-
Use
intfor whole numbers (most common).
-
Use
doublefor decimals (most common).
-
Use
booleanfor flags and conditions.
-
Use
finalfor values that should never change.
-
Name constants in
UPPERSNAKECASE.
12. Exercises
- 1. Declare variables to store your name, age, GPA, and enrollment status.
- 2. Create a program that demonstrates widening and narrowing type casting.
-
3.
Try to assign a new value to a
finalvariable. What error do you get?
13. MCQ Quiz with Answers
Question 1
How many primitive data types does Java have?
Question 2
Which data type would you use to store a person's age?
Question 3
What suffix is required for a float literal?
Question 4
What keyword makes a variable constant?
Question 5
What is narrowing type casting?
Question 6
What is the default value of an int instance variable?
Question 7
Which type stores true/false values?
Question 8
What is the size of a double in Java?
Question 9
Which is a non-primitive data type?
Question 10
What happens with int x = (int) 9.99;?
14. Interview Questions
- Q: What is the difference between primitive and non-primitive data types?
-
Q: What happens when an
intoverflows in Java?
- Q: Explain widening and narrowing type casting with examples.
-
Q: What is the difference between
floatanddouble?
15. FAQs
Q: Why does Java have so many integer types (byte, short, int, long)? A: Memory optimization. On embedded systems or when storing millions of values, usingbyte instead of int saves significant memory.
16. Summary
Variables store data in memory. Java has 8 primitive types covering integers, decimals, characters, and booleans. Non-primitive types reference objects. Constants usefinal. Type casting converts between types — widening is safe and automatic, narrowing is manual and risky.