Nearby lessons

13 of 125

Java - Type Casting

📌 What You Will Learn
  • The small building blocks of Java: tokens
  • All 8 primitive data types with their ranges
  • Type casting — implicit and explicit
  • Control statements: if, switch, loops
  • Arrays — single, double and jagged
  • Variable length arguments (var-args)

Type Casting is a core concept of the Java language. This lesson explains Type Casting and Type Casting — The Complete Rules with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Type Casting

Type casting means converting a value from one type to another type. There are two kinds:

  • Implicit (automatic / widening) — small type to big type. Java does it by itself. Safe, no data loss. Example: int to long, long to float.
  • Explicit (manual / narrowing) — big type to small type. We must write the target type in brackets. Risk of data loss. Example: double to int.

Widening order (safe direction, automatic):

Example01
Loading editor…

Type Casting

Trainer's Note: Memory trick: in widening, no data is lost — so Java allows it freely. In narrowing, data may be lost — so Java forces you to write the cast bracket, which is like signing your name: 'I know what I am doing'.
Example02
Loading editor…

Type Casting

In simple words: Widening is automatic and safe; narrowing needs brackets and may lose data. Going from a small type to a big type like int to long is free, but going back means writing (int) and accepting the risk.
Example03
Loading editor…

Type Casting — The Complete Rules

The Implicit Casting Chart (with char)

Implicit (automatic) conversion flows only from lower to higher types, following this chart:

So these are automatic (no error): byte -> int, char -> int, long -> float. But these are errors without a cast: int -> byte, byte -> char, char -> short.

Trainer's Note: The surprising one: `long` (8 bytes) to `float` (4 bytes) is allowed automatically! Reason: float can hold larger magnitude numbers than long due to its internal arrangement (the classic material uses a classroom-bench analogy). But float to long is NOT automatic — it needs an explicit cast.

Expression Promotion — X + Y = Z

When two values are added in an expression, the result type is decided by these rules:

Example04
Loading editor…

Type Casting — The Complete Rules

Example05
Loading editor…

Type Casting — The Complete Rules

Also remember: assigning a literal like byte b = 128; is an error, because 128 is out of byte range and is treated as an int.

Explicit Casting — The Cast Operator ( )

Explicit (narrowing) casting forces a higher type into a lower type. Data may be lost. Format: P a = (Q) b;

Example06
Loading editor…

Type Casting — The Complete Rules

Explicit casting also allows conversions that implicit casting forbids: char to byte/short and back are possible only with a cast.

Example07
Loading editor…

Type Casting — The Complete Rules

Chained Casting

Example08
Loading editor…

Type Casting — The Complete Rules

Trainer's Note: Golden rule of thumb: widening = automatic and safe; narrowing = needs brackets and may lose data. And one placement trap: (byte)(b1 + b2) casts the sum (correct), but (byte)b1 + b2 casts only b1 and then the result is still int (error).
Example09
Loading editor…
📝 Key Takeaways
  • Tokens are the smallest pieces of a program: identifiers, literals, keywords, operators, separators.
  • Java has 8 primitive types: byte, short, int, long, float, double, char, boolean.
  • Widening casting is automatic and safe; narrowing casting needs brackets and may lose data.
  • if / switch decide which path runs; for / while / do-while repeat code; break and continue control loops.
  • Arrays hold many same-type values; index starts at 0; size is fixed; for-each loop reads them simply.
  • Var-args (int... x) lets a method take any number of arguments.

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.