In Java, we can cast a variable from one type to another.
So far, you learn to create variables that store five types of values int.
Long double.
Car and street.
In this lesson, we're going to use typecasting to convert a value from one type to another.
The first thing I'll need you to do is create a new class by yourself, so inside the section to project
create a new file name type conversion Java and make sure the type conversion class has a main method.
OK, the first thing we'll address is how we can cast a value from double to.
In other words, you can convert a double value to an integer.
Imagine we have a double variable called decimal and it stores the value four point three.
We also have an invariable integer, and right now it equals nothing, so we want to copy the first
value into integer, but remember that into variables cannot store decimals.
They can only store whole numbers.
So before you can store this value into an integer variable, you need to cast a double value to it.
And we can do that by specifying two things, telling Java the type that we're casting to.
In this case, we want to cast some value to end and then specify the value that you want to copy in
this case, it's the decimal.
So what job is going to do is convert the double Valetta into by cutting off the decimal, resulting
in a whole number, and now the invalid integer stores the previous value as a whole?
No.
I thought of a fun example where your friend Percy, let's call him, needs help calculating his salary.
So what we're going to do is make a double variable, double salary.
And let's assume you calculated the salary to be five thousand eight hundred and thirty three dollars
point thirty three cents.
And for the sake of it, let's add a bunch of more threes because we will assume the calculator spat
out that number.
OK, we need to print this family so that person can see it, system dot, dot, print nine.
And will say Pearse's.
Salary is.
In dollars.
Whatever the salary value is.
Go ahead and compile a code.
All right, now, looking at this message, accuracy is a really important in the thousands of dollars,
do we really care about cents, much less the other decimal points?
In this case, it would make more sense to express the salary as a whole number and not care about what
the calculator gave us.
So let's make another variable of type it into a rounded salary.
And we're going to cast the first Valetta at.
Int salary.
And what that's going to return is a whole no version of the salary value.
All right, recompiling our code.
And printing it and this looks a lot cleaner.
In the real world, data comes in many forms, and your job is to present this data to the user in a
clean and practical way.
In this case, the salary value came in as a double.
It had many useless decimal points.
So you cast the double value as an integer.
And printing this family looks much better.
In this lesson, you learn to cast values from one type to another, the salary value came in as a double.
It had many useless decimal point.
So you cast the double value at printing this value.
Look more presentable and converting between types is very common.
So for now, I encourage you to test your knowledge by doing the quiz and trying out the next workbook.