Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 5 of 10

Type Casting & the var Keyword

Type casting converts between types. Widening is automatic and safe; narrowing is explicit and may lose data. The var keyword (Java 10+) lets the compiler infer the type - reducing boilerplate while keeping full static typing.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Widening vs Narrowing

  • Widening (implicit): byte → short → int → long → float → double. Safe, no data loss.
  • Narrowing (explicit cast): double → int drops the decimal. int → byte may overflow.
  • String → int: Integer.parseInt("42"). Fails with NumberFormatException for invalid strings.
  • int → String: String.valueOf(42) or 42 + "" (less preferred).

Casting & var Examples

Casting & var Examples
public class CastingAndVar {

    public static void main(String[] args) {
        // Widening - automatic
        int count = 100;
        long bigCount = count;    // safe: int → long
        double d = count;         // safe: int → double → 100.0

        // Narrowing - explicit, risk of data loss
        double pi = 3.14159;
        int floor = (int) pi;     // 3 - decimal lost!
        System.out.println("Floor of pi: " + floor);

        // Overflow example (be careful!)
        int big = 130;
        byte tiny = (byte) big;   // 130 doesn't fit in byte (-128 to 127)
        System.out.println("130 as byte: " + tiny);  // -126 (overflow!)

        // var - type inference (Java 10+)
        // Compiler infers the type; it's still statically typed!
        var name = "Alice";           // inferred: String
        var score = 95;               // inferred: int
        var price = 9.99;             // inferred: double
        var books = new ArrayList<String>(); // inferred: ArrayList<String>

        // var improves readability for complex generics
        // Before var:
        // Map<String, List<Integer>> groupedScores = new HashMap<>();
        // After var:
        var groupedScores = new java.util.HashMap<String, java.util.List<Integer>>();

        // var CANNOT be used:
        // var x;             // ERROR - must have initializer
        // var x = null;      // ERROR - can't infer from null
        // as method parameter types or return types
    }
}

Quick Quiz

Tip

Tip

Practice Type Casting the var Keyword in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Type Casting & the var Keyword

Practice Task

Note

Practice Task - (1) Write a working example of Type Casting the var Keyword from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Type Casting the var Keyword is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.

Key Takeaways

  • Type casting converts between types.
  • Widening (implicit): byte → short → int → long → float → double. Safe, no data loss.
  • Narrowing (explicit cast): double → int drops the decimal. int → byte may overflow.
  • String → int: Integer.parseInt("42"). Fails with NumberFormatException for invalid strings.

Topics in This Module

0/10 done