Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,300 --> 00:00:03,550
In Java, we can cast a variable from one type to another.
2
00:00:04,140 --> 00:00:08,430
So far, you learn to create variables that store five types of values int.
3
00:00:09,470 --> 00:00:11,810
Long double.
4
00:00:12,730 --> 00:00:14,290
Car and street.
5
00:00:16,180 --> 00:00:20,680
In this lesson, we're going to use typecasting to convert a value from one type to another.
6
00:00:22,710 --> 00:00:28,050
The first thing I'll need you to do is create a new class by yourself, so inside the section to project
7
00:00:28,470 --> 00:00:34,950
create a new file name type conversion Java and make sure the type conversion class has a main method.
8
00:00:41,680 --> 00:00:45,460
OK, the first thing we'll address is how we can cast a value from double to.
9
00:00:46,630 --> 00:00:49,870
In other words, you can convert a double value to an integer.
10
00:00:50,890 --> 00:00:55,240
Imagine we have a double variable called decimal and it stores the value four point three.
11
00:00:56,390 --> 00:01:02,150
We also have an invariable integer, and right now it equals nothing, so we want to copy the first
12
00:01:02,150 --> 00:01:06,650
value into integer, but remember that into variables cannot store decimals.
13
00:01:06,930 --> 00:01:08,790
They can only store whole numbers.
14
00:01:09,800 --> 00:01:15,200
So before you can store this value into an integer variable, you need to cast a double value to it.
15
00:01:15,740 --> 00:01:21,740
And we can do that by specifying two things, telling Java the type that we're casting to.
16
00:01:23,120 --> 00:01:29,930
In this case, we want to cast some value to end and then specify the value that you want to copy in
17
00:01:29,930 --> 00:01:31,250
this case, it's the decimal.
18
00:01:35,980 --> 00:01:41,620
So what job is going to do is convert the double Valetta into by cutting off the decimal, resulting
19
00:01:41,620 --> 00:01:46,690
in a whole number, and now the invalid integer stores the previous value as a whole?
20
00:01:46,690 --> 00:01:47,170
No.
21
00:01:51,390 --> 00:01:56,200
I thought of a fun example where your friend Percy, let's call him, needs help calculating his salary.
22
00:01:57,060 --> 00:02:01,080
So what we're going to do is make a double variable, double salary.
23
00:02:01,770 --> 00:02:07,440
And let's assume you calculated the salary to be five thousand eight hundred and thirty three dollars
24
00:02:07,800 --> 00:02:09,120
point thirty three cents.
25
00:02:09,900 --> 00:02:14,310
And for the sake of it, let's add a bunch of more threes because we will assume the calculator spat
26
00:02:14,310 --> 00:02:15,030
out that number.
27
00:02:16,000 --> 00:02:22,600
OK, we need to print this family so that person can see it, system dot, dot, print nine.
28
00:02:26,280 --> 00:02:27,960
And will say Pearse's.
29
00:02:30,000 --> 00:02:31,590
Salary is.
30
00:02:33,970 --> 00:02:35,290
In dollars.
31
00:02:37,300 --> 00:02:39,340
Whatever the salary value is.
32
00:02:40,490 --> 00:02:42,260
Go ahead and compile a code.
33
00:02:56,110 --> 00:03:01,690
All right, now, looking at this message, accuracy is a really important in the thousands of dollars,
34
00:03:01,700 --> 00:03:05,740
do we really care about cents, much less the other decimal points?
35
00:03:06,340 --> 00:03:11,470
In this case, it would make more sense to express the salary as a whole number and not care about what
36
00:03:11,470 --> 00:03:12,650
the calculator gave us.
37
00:03:13,780 --> 00:03:18,010
So let's make another variable of type it into a rounded salary.
38
00:03:23,060 --> 00:03:25,450
And we're going to cast the first Valetta at.
39
00:03:29,070 --> 00:03:30,390
Int salary.
40
00:03:32,850 --> 00:03:36,570
And what that's going to return is a whole no version of the salary value.
41
00:03:41,670 --> 00:03:43,620
All right, recompiling our code.
42
00:03:50,280 --> 00:03:53,160
And printing it and this looks a lot cleaner.
43
00:03:54,070 --> 00:03:58,510
In the real world, data comes in many forms, and your job is to present this data to the user in a
44
00:03:58,510 --> 00:04:00,290
clean and practical way.
45
00:04:01,270 --> 00:04:04,230
In this case, the salary value came in as a double.
46
00:04:04,600 --> 00:04:06,750
It had many useless decimal points.
47
00:04:07,180 --> 00:04:10,030
So you cast the double value as an integer.
48
00:04:11,500 --> 00:04:13,630
And printing this family looks much better.
49
00:04:19,279 --> 00:04:25,270
In this lesson, you learn to cast values from one type to another, the salary value came in as a double.
50
00:04:25,760 --> 00:04:27,770
It had many useless decimal point.
51
00:04:27,800 --> 00:04:31,670
So you cast the double value at printing this value.
52
00:04:31,670 --> 00:04:36,450
Look more presentable and converting between types is very common.
53
00:04:36,950 --> 00:04:42,350
So for now, I encourage you to test your knowledge by doing the quiz and trying out the next workbook.
5258
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.