‫Welcome back.
‫In this video, we'll talk about typecasting in Java.
‫Typecasting is the process of converting a value from one data type to another.
‫Sometimes we need to convert a value to a different data type to perform certain operations or assignments.
‫Java supports two types of typecasting implicit, which is the automatic type casting and the explicit,
‫which is the manual type casting.
‫Let's start with the implicit type casting.
‫Implicit type casting happens automatically when the conversion is safe and doesn't result in any loss
‫of data.
‫It occurs when a value of a smaller data type is assigned to a variable of a larger data type, for
‫example.
‫Let's create a variable called num int equal to ten.
‫I need.
‫Do cast this number.
‫Into a double.
‫So for example, num double equal to num.
‫And I'm creating a variable called num double.
‫And I'm assigning its value to the num end so the num double will take the value.
‫Ten.
‫So in this example, the integer value ten is implicitly cast to double value 10.00.
‫Okay, so it becomes now 10.0 because it's a double and double.
‫It contains the decimal point since double can handle larger decimal values.
‫It's safe to convert an integer to a double without any data loss.
‫So in this example it's automatically converted from integer to double because there is no data loss.
‫Now let's talk about the explicit type casting.
‫Explicit type casting requires the developer to manually convert a value from one data type to another.
‫It is needed when we want to convert a value from a larger data type to a smaller data type, or when
‫there is a possibility of data loss.
‫For example.
‫Let me create here a double num or pi equal to 3.40.
‫I need to convert this pi to an integer so I'll create an int num pi equal to.
‫Pie.
‫Okay, There is an error here because I need to provide an integer and not double.
‫So there is an error.
‫The solution for this problem is by casting manually to end.
‫So I'm converting the value here, casting it to an integer and storing it inside a variable which is
‫of type int called numpy.
‫So I'm converting this to an integer.
‫Where is the data loss?
‫Yes, there is a data loss where the double is 3.14 when converting it to an int.
‫The the point, the decimal point and the decimal part of this pi will be removed.
‫So this numpy will store the value three without any decimal point or any decimal part.
‫Okay, so this result in in a data loss.
‫So for that it's.
‫Explicit type casting and it's done manually.
‫Always pay attention when making explicit type casting for the data lost during the conversion.
‫Sometimes we need to convert values between different data types to perform operations.
‫Let's move to this important topic.
‫Type casting between different data types.
‫I have an int num and equal to five for example.
‫And or in my into my int.
‫It's good because we have created before.
‫And I have a character here called my char equal to.
‫H.
‫Okay, This is how we create characters in Java.
‫We learn it before.
‫Now I need to add five to this character A how to do it.
‫I start by num int or my int the variable plus a.
‫Okay, this is how we add number to a character.
‫But wait, you told me that it is a character.
‫I need to make an addition between the character and the numbers.
‫Yes.
‫In this example we add the integer value five to the Ascii value of the character A, which is 65.
‫In the Ascii table, the value of A is 65.
‫After adding them together, we explicitly cast the result to a character type.
‫So I cast it to car and here the result will be a value of 70, which refers to capital F.
‫So let me show you system out dot print and I need to print my character.
‫If we run the program from here run main method, it will give us.
‫It will give us.
‫Letter F, which is 70in the Ascii table.
‫Typecasting in Java is a powerful feature that allows us to work with different data types and perform
‫a wide variety of tasks in our programs.
‫However, it's essential to use its.
‫Carefully and be aware of potential data loss or possession issues when performing explicit type casting.