Type conversion and casting
22 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)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
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); }}450.0 450 123456792
What this code does
inttodoubleandinttolongare widening, so no cast is needed. Adoubleprints with a decimal point, so 450 becomes450.0.inttofloatis also automatic, but afloatkeeps only about 6–7 significant digits.- So 123456789 is stored as 123456792: widening is allowed, yet it lost precision.
(int) approxshows 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.
Example 2 · Narrowing: the cast cuts, it never rounds
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); }}Rupees: 99 Attendance as int: 74 Minus: -9
What this code does
(int) priceis narrowing: a value goes into a smaller type. The cast cuts off.99, so you get 99, not 100.(int) 74.9gives 74 and(int) -9.99gives -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.
- (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
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); }}91 91.4 91.4 3
What this code does
total / subjectsuses twointvalues, so the answer is theint91.7 / 2gives 3 the same way: the.5is dropped.- Casting
totaltodouble, or writing5.0, makes one side a decimal, so the answer is 91.4.
Example 4 · Put the cast in the right place
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); }}Wrong: 91.0 Right: 91.4
What this code does
- In
wrong, the brackets run first:457 / 5gives theint91, then the cast makes it91.0. - In
right,totalbecomes adoublebefore 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