💻Step 10:Type Casting, Boxing/InBoxing and OutBoxing -6th hour +code

Learn with youtube video – 

Comparison between typecasting and boxing/unboxing in Java:

TypecastingBoxing/Unboxing
DefinitionConverting a value of one data type to another data typeConverting a primitive data type to an object and vice versa
Syntax(target_type) valueObject obj = value; or target_type value = (target_type) obj;
Exampleint x = (int) 3.14;Integer y = new Integer(3); or int z = y.intValue();
PerformanceFasterSlower
UseWhen you need to convert a value of one data type to another data type and you are sure that the conversion will be successfulWhen 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)

primitives
type-casting-different-forms

Comparison between auto typecasting and manual typecasting in Java:

Auto TypecastingManual Typecasting
DefinitionImplicit conversion of a value from one data type to anotherExplicit conversion of a value from one data type to another
SyntaxNo special syntax required(target_type) value
Exampleint x = 3.14;int x = (int) 3.14;
PerformanceFasterFaster
UseWhen the target data type is large enough to hold the value being convertedWhen 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);
}
}

Run this Program ▶️

💡Boxing/InBoxing and OutBoxing-

Comparison between boxing and unboxing in Java:-

BoxingUnboxing
DefinitionConverting a primitive data type to an objectConverting an object to a primitive data type
SyntaxObject obj = value;target_type value = (target_type) obj;
ExampleInteger y = new Integer(3);int z = y.intValue();
PerformanceSlowerSlower
UseWhen you need to use a primitive data type as an object, for example, to store it in a collectionWhen 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;

autoboxing-and-unboxing-in-java

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);
}
}

Run this Program ▶️

Questions set —

  • What is difference between automatic type conversion and manual type conversion?
  • What is difference between inboxing and outboxing?

2 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.