
Play Store Application link – https://play.google.com/store/apps/details?id=com.ideepro.java25hours
Learn with youtube video –
Comparison between typecasting and boxing/unboxing in Java:
Typecasting | Boxing/Unboxing | |
---|---|---|
Definition | Converting a value of one data type to another data type | Converting a primitive data type to an object and vice versa |
Syntax | (target_type) value | Object obj = value; or target_type value = (target_type) obj; |
Example | int x = (int) 3.14; | Integer y = new Integer(3); or int z = y.intValue(); |
Performance | Faster | Slower |
Use | When you need to convert a value of one data type to another data type and you are sure that the conversion will be successful | When you need to use a primitive data type as an object, for example, to store it in a collection |
💡Definitions
-Assigning a value of one type to a variable of another type is known as Type Casting.
💡Datatype hierarchy-
(Biggest datatype) double > float > long > int > char > short > byte (smallest datatype)


Comparison between auto typecasting and manual typecasting in Java:–
Auto Typecasting | Manual Typecasting | |
---|---|---|
Definition | Implicit conversion of a value from one data type to another | Explicit conversion of a value from one data type to another |
Syntax | No special syntax required | (target_type) value |
Example | int x = 3.14; | int x = (int) 3.14; |
Performance | Faster | Faster |
Use | When the target data type is large enough to hold the value being converted | When you need to explicitly control the type of the value being converted, or when the target data type is not large enough to hold the value being converted |
💡(i)Widening/Automatic type conversion/Up casting
-When value of small datatype is saved into big datatype.
i.e-
int i=10;
double d=i;
💡(ii)Narrowing/manual type conversion/down casting
-When value of big datatype is saved into small datatype.
i.e- double d=455.3d;
int i=(int)d;
Code samples are here
1-Program to perform upcasting and downcasting
package typecasting6; public class Typscting { public static void main(String[] args) { //upcasting int a1=20; //from int double b=a1; //to double System.out.println("Upcasting from int to double = "+b); //downcasting double x=10.0d; //from double int c=(int)x; //to int System.out.println("Downcasting from double to int = "+c); } }
💡Boxing/InBoxing and OutBoxing-
Comparison between boxing and unboxing in Java:-
Boxing | Unboxing | |
---|---|---|
Definition | Converting a primitive data type to an object | Converting an object to a primitive data type |
Syntax | Object obj = value; | target_type value = (target_type) obj; |
Example | Integer y = new Integer(3); | int z = y.intValue(); |
Performance | Slower | Slower |
Use | When you need to use a primitive data type as an object, for example, to store it in a collection | When you need to use the value of an object as a primitive data type |
Boxing
💡(i) Boxing/InBoxing
– Conversion of primitive into non primitive
i.e – int i=10;
Integer i1=i; // Integer is a class so i1 is a object(non primitive)
💡(ii) OutBoxing
– Conversion of non primitive into primitive
i.e Integer i=10;
int i1=i;

code samples are here
2-Program to perform boxing and outboxing
package boxingNunboxing7; public class BoxngUnboxing { public static void main(String[] args) { //boxing int x=10; //from primitive(x) Integer c=x; //to non primitive(b) System.out.println(c); //out boxing Integer a1=20; //from non primitive(a1) int b=a1; //to primitive(b) System.out.println(b); } }
Questions set —
- What is difference between automatic type conversion and manual type conversion?
- What is difference between inboxing and outboxing?
Boxing
Boxing and Inboxing are same.