Skip to lesson
MentorNxt home

Java Programming

Variables & Data Types

Type conversion and casting

A value can move from one type to another. Java does it by itself when nothing can be lost, like int to double. Going from a bigger type to a smaller one needs a cast.

Small to big: Java does it for you

Your marks are an int, 450, but a report needs them as a double. Widening: Java changes a value to a bigger type by itself, because the bigger type has room for it.

Example 1 · Widening: Java does it for you

Widening.javaJava
public class Widening {    public static void main(String[] args) {        int marks = 450;        double marksAsDouble = marks;        long bigMarks = marks;        int bigCount = 123456789;        float approx = bigCount;        System.out.println(marksAsDouble);        System.out.println(bigMarks);        System.out.println((int) approx);    }}
Output
450.0
450
123456792

What this code does

  • int to double and int to long are widening, so no cast is needed. A double prints with a decimal point, so 450 becomes 450.0.
  • int to float is also automatic, but a float keeps only about 6–7 significant digits.
  • So 123456789 is stored as 123456792: widening is allowed, yet it lost precision. (int) approx shows the stored value as a whole number.

Values widen in this order: byte → short → int → long → float → double.

Big to small: you must cast

A phone costs 99.99 and you want the whole rupees in an int, but int rupees = price; does not compile: possible lossy conversion from double to int. A cast tells Java you are sure: the new type in brackets before the value.

Syntax
(type) value
Picture 1 / 2
An int value of 450 goes into a bigger double by itself and becomes 450.0.
Picture 1 / 2

Example 2 · Narrowing: the cast cuts, it never rounds

Narrowing.javaJava
public class Narrowing {    public static void main(String[] args) {        double price = 99.99;        int rupees = (int) price;        double attendance = 74.9;        System.out.println("Rupees: " + rupees);        System.out.println("Attendance as int: " + (int) attendance);        System.out.println("Minus: " + (int) -9.99);    }}
Output
Rupees: 99
Attendance as int: 74
Minus: -9

What this code does

  • (int) price is narrowing: a value goes into a smaller type. The cast cuts off .99, so you get 99, not 100.
  • (int) 74.9 gives 74 and (int) -9.99 gives -9: the cast cuts toward zero; it never rounds.

A cast does not check the range. If the value does not fit, you get a wrong number and no error. Try n = 3000000000 in the lab.

Experiment

Cast lab

Change d (try 99.99, -9.99, 74.5) and n (try 3000000000). See what each cast keeps.

double d = 99.99;
long n = 3000000000L;
System.out.println((int) d);
System.out.println((long) d);
System.out.println((int) n);
(int) d
99
(long) d
99
(int) n
-1294967296

The division surprise

Your total is 457 marks in 5 subjects, so total / 5 should be 91.4, but Java prints 91. Integer division: when both numbers of / are int, the answer is an int and the decimal part is dropped.

Example 3 · The division surprise

Average.javaJava
public class Average {    public static void main(String[] args) {        int total = 457;        int subjects = 5;        System.out.println(total / subjects);        System.out.println((double) total / subjects);        System.out.println(total / 5.0);        System.out.println(7 / 2);    }}
Output
91
91.4
91.4
3

What this code does

  • total / subjects uses two int values, so the answer is the int 91. 7 / 2 gives 3 the same way: the .5 is dropped.
  • Casting total to double, or writing 5.0, makes one side a decimal, so the answer is 91.4.

Example 4 · Put the cast in the right place

CastPlace.javaJava
public class CastPlace {    public static void main(String[] args) {        int total = 457;        int subjects = 5;        double wrong = (double) (total / subjects); // too late: already 91        double right = (double) total / subjects;        System.out.println("Wrong: " + wrong);        System.out.println("Right: " + right);    }}
Output
Wrong: 91.0
Right: 91.4

What this code does

  • In wrong, the brackets run first: 457 / 5 gives the int 91, then the cast makes it 91.0.
  • In right, total becomes a double before the division, so the decimals stay.

Arithmetic operators (opens in a new tab) covers / and the other maths symbols.

Next step

Check your understanding

10 quick questions