VisualStudioTutor.com Java Tutorial All Lessons

Lesson 6 of 20

Variables and Data Types

Store text, whole numbers, decimal values, characters, and Boolean states in correctly typed Java variables.

Learning objectives

  • Declare and initialize variables.
  • Use the common primitive types int, double, char, and boolean.
  • Use String for text.
  • Understand type conversion and casting.
  • Declare constants with final.

1. Variables

A variable is a named storage location with a declared type. The type tells Java what kind of value the variable is allowed to hold.

Example: common data types

public class DataTypeDemo {
    public static void main(String[] args) {
        String studentName = "Mira";
        int age = 19;
        double cgpa = 3.72;
        char grade = 'A';
        boolean active = true;

        System.out.println(studentName);
        System.out.println(age);
        System.out.println(cgpa);
        System.out.println(grade);
        System.out.println(active);
    }
}

String stores text, int stores whole numbers, double stores decimal values, char stores one character, and boolean stores either true or false.

2. Declaration, assignment, and initialization

int score;      // declaration
score = 85;     // assignment

double price = 29.90; // declaration + initialization

A local variable must be assigned a value before you read it.

3. Type conversion and casting

Java can widen some numeric values automatically, such as int to double. Narrowing conversions require an explicit cast because information may be lost.

Example: widening and narrowing

public class ConversionDemo {
    public static void main(String[] args) {
        int quantity = 7;
        double quantityAsDouble = quantity;   // automatic widening

        double price = 19.95;
        int wholePart = (int) price;          // explicit narrowing

        System.out.println(quantityAsDouble);
        System.out.println(wholePart);
    }
}

4. Constants with final

Use final when a value should not change after initialization.

final double TAX_RATE = 0.08;
double subtotal = 120.00;
double tax = subtotal * TAX_RATE;

Mini project: product calculation

public class ProductSummary {
    public static void main(String[] args) {
        String product = "Wireless Mouse";
        double unitPrice = 59.90;
        int quantity = 2;
        final double TAX_RATE = 0.08;

        double subtotal = unitPrice * quantity;
        double tax = subtotal * TAX_RATE;
        double total = subtotal + tax;

        System.out.println("Product: " + product);
        System.out.println("Quantity: " + quantity);
        System.out.printf("Subtotal: RM %.2f%n", subtotal);
        System.out.printf("Tax: RM %.2f%n", tax);
        System.out.printf("Total: RM %.2f%n", total);
    }
}

Common mistakes

ProblemWhy it happensHow to fix it
Using single quotes for a StringSingle quotes represent a char.Use "Java" for a String and 'J' for a char.
Assigning a decimal to intAn int cannot directly store a decimal value.Use double or cast intentionally.
Changing a final variableConstants cannot be reassigned.Create a normal variable if the value must change.

Summary

Variables give programs memory. Choosing the correct type makes your code clearer and prevents invalid assignments. In Lesson 7, you will combine variables with keyboard input so users can supply data while the program is running.