Why Java Data Types and Variables Matter
Imagine you have a toolbox. Each drawer holds a specific kind of tool – a screwdriver, a hammer, a wrench. In Java, variables are the drawers and data types are the tools that tell the computer what kind of thing to store.
💡 In Simple Words: A variable is a named box where you keep a value, and a data type tells the box what kind of value it can hold – like numbers, letters, or true/false.
What Is a Variable?
A variable is simply a name that points to a memory location. When you write int age = 15;, you are creating a box called age that can store an integer (a whole number). The computer now knows two things: the name of the box and the kind of thing that can go inside.
How to Declare a Variable
Declaration follows the pattern dataType variableName;. You can also give it a starting value (called initialisation) in the same line:
double price;– declares a variable namedpricethat will hold a decimal number.double price = 99.99;– declares and initialises it with the value99.99.
Java’s Built‑In Data Types
Java splits its data types into two families: primitive (the simplest, built‑in types) and reference (objects like Strings). For a beginner, the primitive types are the ones you’ll see most in ICSE exams.
| Type | Size (bits) | Typical Use |
|---|---|---|
| byte | 8 | Small whole numbers, like a score out of 100 |
| short | 16 | Larger integers that still fit in a few bytes |
| int | 32 | Default whole‑number type, e.g., age, count |
| long | 64 | Very large integers, such as population figures |
| float | 32 | Decimal numbers with limited precision, like sensor data |
| double | 64 | High‑precision decimal numbers, e.g., money calculations |
| char | 16 | Single characters, like ‘A’ or ‘9’ |
| boolean | 1 (conceptual) | True or false values, useful for decisions |
Quick Peek at Reference Types
The most common reference type you’ll meet early on is String. Unlike primitives, a String holds a sequence of characters – think of it as a word or sentence.
Declaring and Using Variables – Step by Step
Let’s walk through a tiny program that asks for a student’s marks and tells whether they passed.
public class Result {
public static void main(String[] args) {
int marks = 78; // 1. Declare an int variable and give it a value
boolean passed = marks >= 35; // 2. Use a boolean to store the result of a comparison
System.out.println("Marks: " + marks);
System.out.println("Passed? " + passed);
}
}
Notice how the boolean variable passed stores the outcome of the expression marks >= 35. This tiny trick saves you from writing the same comparison again later.
Common Mistakes to Avoid
- Mismatched types: Trying to put a decimal number into an
intwill cause a compile‑time error. Usedoubleor cast the value. - Uninitialised variables: Java forces you to give a variable a value before you read it. Forgetting to initialise leads to a "variable might not have been initialized" error.
- Naming rules: Variable names can’t start with a number, can’t contain spaces, and are case‑sensitive (
score≠Score).
Bullet Summary – What to Remember
- Variables are named storage boxes; data types define what fits inside.
- Primitive types: byte, short, int, long, float, double, char, boolean.
- Use
intfor most whole numbers,doublefor decimals. - Always initialise a variable before using it.
- Follow Java’s naming conventions: start with a letter, use camelCase (e.g.,
totalScore).
📝 Likely Exam Questions
- Write a statement to declare a variable named
heightthat can store a decimal number.
Answer:double height;ordouble height = 0.0; - What will be the output of the following code?
int a = 5; int b = 2; double c = a / b; System.out.println(c);
Answer:2.0becausea / bdoes integer division first (result 2) then is promoted to double. - Explain the difference between
floatanddouble.
Answer: Both store decimal numbers, butfloatuses 32 bits and is less precise, whiledoubleuses 64 bits and offers higher precision – it’s the default for decimal literals. - Declare a boolean variable called
isEligiblethat is true when a student’s score is at least 40.
Answer:boolean isEligible = score >= 40;(assumingscoreis already defined). - Why must a variable be initialised before it is used?
Answer: Because Java needs a definite value to work with; using an uninitialised variable could lead to unpredictable results, so the compiler forces you to assign a value first.