1 00:00:00,210 --> 00:00:02,400 Every primitive type has a wrapper class. 2 00:00:05,390 --> 00:00:09,770 Java is object oriented, that means there is a class for every primitive type. 3 00:00:19,070 --> 00:00:21,470 In this lesson, you'll learn about wrapper classes. 4 00:00:24,160 --> 00:00:28,030 You can find the code for this lesson by following this path in the resources. 5 00:00:38,040 --> 00:00:40,530 Let's get one question out of the way, what is a rapper? 6 00:00:41,070 --> 00:00:44,850 A rapper is an immutable class that wraps around a primitive type. 7 00:00:47,200 --> 00:00:54,730 Here's an example, integer integer is an immutable class that wraps around the type because integer 8 00:00:54,730 --> 00:00:59,530 is an immutable class, then variables of that type story, a reference to an object. 9 00:01:00,790 --> 00:01:07,840 They can be now, they can call methods, but they cannot change, which means they are immune to the 10 00:01:07,840 --> 00:01:08,650 reference trap. 11 00:01:14,880 --> 00:01:19,890 So inside Rupert Java, there are a bunch of primitive variables if you view the runtime. 12 00:01:26,330 --> 00:01:31,820 You'll notice that a primitive is just a value and nothing more, and because it's primitive, the variable 13 00:01:31,820 --> 00:01:34,580 cannot be null and it cannot call any methods. 14 00:01:41,440 --> 00:01:43,840 And so we can set up a variable of type integer. 15 00:01:48,310 --> 00:01:54,070 Equal to a new object of the integer class and then pass the integer value into the constructor. 16 00:02:00,850 --> 00:02:06,370 And now the variable stores a reference to an integer object, the integer object contains a field that 17 00:02:06,370 --> 00:02:07,840 matches the value we pastan. 18 00:02:12,220 --> 00:02:15,850 And because it's an object, the variable can call many methods like. 19 00:02:17,630 --> 00:02:21,660 Percent to string int value, double value and so much more. 20 00:02:25,080 --> 00:02:27,810 And because it's an object, it can also be No. 21 00:02:29,080 --> 00:02:31,740 But there's a shorter way to define a proper object. 22 00:02:32,070 --> 00:02:37,890 The syntax where you directly create a new object of the integer class with the value inside the constructor 23 00:02:37,890 --> 00:02:38,800 is deprecated. 24 00:02:39,210 --> 00:02:44,430 You can just assign the value directly in Java is going to create an object and update the field behind 25 00:02:44,430 --> 00:02:45,120 the scenes. 26 00:02:45,630 --> 00:02:47,580 You can confirm this by running the debugger. 27 00:02:51,600 --> 00:02:57,210 And like before, the variable stores, a reference to an integer object and the integer object contains 28 00:02:57,210 --> 00:02:59,610 a field that matches the value of Pastan. 29 00:03:05,340 --> 00:03:11,820 And here are some more upper classes, double long and character like integer, they are immutable class 30 00:03:11,820 --> 00:03:19,920 types, variables of each type story reference to an object they can be know they can call methods they 31 00:03:19,920 --> 00:03:21,000 cannot change. 32 00:03:21,180 --> 00:03:27,300 You cannot update the status of an immutable object, which means they're immune to the reference trap. 33 00:03:29,770 --> 00:03:35,910 So inside, Rupert Jarvis said the three variables equal to a new object of the respective wrapper class, 34 00:03:36,550 --> 00:03:38,200 first, we're going to do with the Longway. 35 00:04:04,010 --> 00:04:05,330 All the rerun, the debugger. 36 00:04:10,480 --> 00:04:11,650 Step through each line. 37 00:04:13,820 --> 00:04:19,610 And notice that every object contains a field that matches the value that we bastin because they're 38 00:04:19,610 --> 00:04:23,000 all objects, each one can be null and can call methods. 39 00:04:26,940 --> 00:04:32,460 And so in general, the syntax where you create a new object of the wrapper class is deprecated, you 40 00:04:32,460 --> 00:04:37,290 can just assign the value directly and Jeff is going to create an object and update every field behind 41 00:04:37,290 --> 00:04:37,980 the scenes. 42 00:04:46,000 --> 00:04:51,910 Let's talk about the reference trap rappers are immutable class types, so they are immune to the trap. 43 00:04:54,800 --> 00:05:00,080 A rapper such as Integer doesn't have any suitors, so it's impossible to update the field. 44 00:05:01,340 --> 00:05:04,010 Let's set another integer variable equal to the first. 45 00:05:13,350 --> 00:05:19,330 Launched the debugger, and it follows that when you set an integer variable equal to another, it copies 46 00:05:19,330 --> 00:05:24,140 of the reference inside and now both variables are going to point to the same integer object. 47 00:05:24,690 --> 00:05:26,490 Should I worry about the reference trap? 48 00:05:26,790 --> 00:05:32,460 No, it's impossible to update the state of the object when you assign the variable annuity Energia. 49 00:05:40,650 --> 00:05:46,080 When you assign the variable a new integer, it's going to equal a brand new object of the integer class 50 00:05:46,080 --> 00:05:49,950 with the value 10, the same applies to the other rappers. 51 00:05:53,570 --> 00:06:00,110 OK, so you might be asking one to use primitive one to use proper use, primitive 90 percent of the 52 00:06:00,110 --> 00:06:06,560 time, primitive takes up less space and they're faster, only use a wrapper when you need the variable 53 00:06:06,560 --> 00:06:10,340 to be null or when you need to call a method from the variable. 54 00:06:15,610 --> 00:06:17,830 In this lesson, you learned about wrapper classes. 55 00:06:19,290 --> 00:06:25,560 A wrapper class wraps around a primitive type javas object oriented, so there's a class for every primitive 56 00:06:25,560 --> 00:06:25,950 type. 57 00:06:33,190 --> 00:06:39,460 Use a primitive type 90 percent of the time, because they take up less space and they're faster, only 58 00:06:39,460 --> 00:06:44,830 use a wrapper when you absolutely need to, when you need the variable to be known or if you need to 59 00:06:44,830 --> 00:06:46,390 call a method from the variable.