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 named price that will hold a decimal number.
  • double price = 99.99; – declares and initialises it with the value 99.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.

TypeSize (bits)Typical Use
byte8Small whole numbers, like a score out of 100
short16Larger integers that still fit in a few bytes
int32Default whole‑number type, e.g., age, count
long64Very large integers, such as population figures
float32Decimal numbers with limited precision, like sensor data
double64High‑precision decimal numbers, e.g., money calculations
char16Single characters, like ‘A’ or ‘9’
boolean1 (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 int will cause a compile‑time error. Use double or 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 (scoreScore).

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 int for most whole numbers, double for 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

  1. Write a statement to declare a variable named height that can store a decimal number.
    Answer: double height; or double height = 0.0;
  2. 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.0 because a / b does integer division first (result 2) then is promoted to double.
  3. Explain the difference between float and double.
    Answer: Both store decimal numbers, but float uses 32 bits and is less precise, while double uses 64 bits and offers higher precision – it’s the default for decimal literals.
  4. Declare a boolean variable called isEligible that is true when a student’s score is at least 40.
    Answer: boolean isEligible = score >= 40; (assuming score is already defined).
  5. 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.
#ICSE#Class 10#Java#Data Types#Variables