💻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;

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?

8 comments

  1. Thanks for the various tips provided on this weblog. I have seen that many insurance firms offer prospects generous reductions if they favor to insure a few cars together. A significant quantity of households possess several vehicles these days, specially those with more mature teenage kids still living at home, along with the savings for policies might soon begin. So it is a good idea to look for a great deal.

  2. Appreciating the persistence you put into your site and in depth information you present. It’s good to come across a blog every once in a while that isn’t the same old rehashed information. Wonderful read! I’ve bookmarked your site and I’m including your RSS feeds to my Google account.

  3. I enjoy what you guys are usually up too. This kind of clever work and reporting! Keep up the amazing works guys I’ve included you guys to blogroll.

  4. Hi, i feel that i saw you visited my blog so i came to return the favor?I am attempting to to find things to enhance my website!I assume its adequate to make use of some of your ideas!!

Leave a Reply

Your email address will not be published. Required fields are marked *