Tsallaka zuwa abun ciki
Justice Tech School

Type Casting

Widening conversions are automatic. Narrowing needs a cast and may lose data.

Example
public class Main {
  public static void main(String[] args) {
    int i = 9;
    double d = i;
    int n = (int) 9.99;
    char c = (char) 66;
    System.out.println(d + " " + n + " " + c);
  }
}

Result

9.0 9 B

Exercise

Cast to int.

int n = () 9.9;

Quiz Test

What is (int) 9.99?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.