CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy Java · 7 min · free

Type Conversion in Java: Implicit and Explicit Casting

7 min4 KwickClipsFull text belowFree
Next lesson →Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

Widening happens automatically, narrowing needs a cast and may lose data, and type promotion decides the result type inside an expression.

Follows the syllabus of: ISC Class 11 Computer Science (868), ISC Class 12 Computer Science (868)

On screen in this lesson

Two kinds of conversion

Type conversion: changing a value from one type to another
Implicit (widening): Java does it automatically, safely
Explicit (narrowing): you write a cast, data may be lost

The widening order

byte to short
short to int (char also widens to int)
int to long
long to float
float to double

Narrowing: you must cast

Cast syntax: (type) value, like (int) 99.99
Without a cast: error, possible lossy conversion
double to int cuts off the decimal, it does not round
A value too big for the type wraps around

Casts and their results

CastResultWhy
(int) 99.9999decimal cut off
(int) -7.8-7cut toward zero
(byte) 30044wraps: 300 - 256
(int) 'A'65Unicode value
(char) 66Bcode to char

Type promotion in expressions

byte, short and char become int in arithmetic
If either value is double, the result is double
Else if either is float, the result is float
Else if either is long, the result is long

Compound assignment casts for you

b = b + 5; with byte b: compile error
b += 5; with byte b: compiles
+= hides a cast: b = (byte)(b + 5)
So b += 200 can silently wrap around

Quick answers

Does an int cast round the value?

No. (int) 99.99 gives 99, because the decimal is cut off, not rounded.

Why does byte b = x + y fail for two bytes?

Both bytes are promoted to int in the expression, so the result is an int.

KwickClips from this lesson

Short clips, one idea each. Good for revision the night before.

The full lesson, in text

Hello students, welcome to Kwickprep. Ninety nine point nine nine rupees stored in an int becomes ninety nine. Where did the paise go? Today we learn how Java converts one data type into another. We cover widening, narrowing, type promotion in expressions, and the output questions exams ask on casting.

First, the new term. Type conversion means changing a value from one data type to another. Implicit conversion is done by Java on its own, and it happens when a smaller type goes into a bigger type. This is called widening. Explicit conversion is written by you, using a cast, when a bigger type goes into a smaller one. This is called narrowing, and some data may be lost.

Widening always follows this order, from small to big. A byte can widen to short. A char has no sign, so it does not widen to short, but it widens to int and beyond. An int widens to long. A long widens to float, because float covers a far bigger range, even though it may round very long numbers. And a float widens to double, the biggest of all.

Here is widening in action. Runs is an int with the value two hundred fifty. Storing it in a long would need no cast, because a long can hold every int. Storing it in a double also needs no cast. Notice that the double prints as two fifty point zero, because a double always shows a decimal part.

Narrowing goes the other way, and Java will not do it on its own. You write the target type in brackets before the value, like int in brackets, ninety nine point nine nine. This is called a cast. Without the cast, the compiler says, incompatible types, possible lossy conversion. When a double becomes an int, the decimal part is simply cut off. It is never rounded. When a value is too big for the new type, it wraps around and gives a strange number.

Here, price is ninety nine point nine nine, and rs, short for rupees, is the int cast of price. The point nine nine is cut off, so rs is ninety nine. Next, three hundred is cast to a byte, which can only go up to one hundred twenty seven. A byte has two hundred fifty six possible values, so three hundred wraps around to three hundred minus two fifty six. That is forty four.

Learn these five casts, because they appear again and again. Int of ninety nine point nine nine is ninety nine. Int of minus seven point eight is minus seven, not minus eight, because the cut is toward zero. Byte of three hundred is forty four, as we just saw. Int of char A gives its Unicode value, sixty five. And char of sixty six gives the letter B.

Type promotion is a quiet, automatic widening that happens inside an expression. First, byte, short and char are always raised to int before any arithmetic. Then, if either value is a double, the whole result becomes double. Otherwise, if either value is a float, the result becomes float. Otherwise, if either value is a long, the result is long, and if none of these, it stays int.

Here, x and y are both bytes. Many students expect x plus y to be a byte too. But both are promoted to int, so writing byte s equals x plus y gives a lossy conversion error. We must store it in an int. The variable s is thirty. Thirty by four is seven, because both are int. Thirty by four point zero is seven point five, because the double promotes the whole division.

Here is a favourite viva question. If b is a byte, b equals b plus five gives an error, because b plus five is an int. But b plus equals five compiles without any problem. That is because plus equals hides a cast, so it really means b equals byte of b plus five. The danger is that a large value, like plus equals two hundred, wraps around without any warning.

Now three output questions, like the ones in board papers. Pause and predict each line before I explain. The char c starts as A, and plus equals two moves it to C, with the hidden cast. So the first line prints C. In c plus one, the char is promoted to int, so it prints sixty eight. In the last line, we cast sixty eight back to char, so it prints D.

Look carefully at the brackets here, because they change everything. In the first line, the cast works on p alone, so seven point zero is divided by two. That gives three point five. In the second line, the brackets make p by q happen first, as int division. That gives three. Only then is three cast to double, so it prints three point zero.

One more set. In the first line, the cast happens before the addition, so three point nine nine becomes three. Three plus zero point five is three point five. In the second line, ten by four is two, because both are int. Then two times two point zero is four point zero. In the last line, both chars are promoted to int. Small a is ninety seven and small b is ninety eight, so it prints one ninety five.

Let us revise what we learned today. Widening goes from a small type to a big type, and Java does it automatically. Narrowing goes from big to small, needs a cast, and may lose data. An int cast cuts off decimals, and values out of range wrap around. In expressions, byte, short and char become int. And the biggest type in an expression decides the type of the result. Now try the three output questions again with your own numbers.

Courses that teach this

CourseUnit
ISC Class 11 Computer Science (868)Object-Oriented Programming with Java
ISC Class 12 Computer Science (868)Primitive Values, Wrapper Classes, Types and Casting
Programming All levels JavaJava Language Basics

Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.

Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →