Lesson 22 - Boxing, wrapper classes, packages and more
In the previous lesson, The equals() and clone() methods in Java, we went over the equals() and clone() object methods. Welcome you all to the last tutorial of the basics of the object-oriented programming in Java. We'll finish the remaining pieces of the theory and then we'll meet again in the form applications course.
Boxing, unboxing, and nullable types
We already know the differences between primitive and reference types. Sometimes, it's useful to work with primitive types as we would with reference types. In these cases, we use boxing. We box a primitive type into the general ancestor of all objects - the Object type. Boxing in action would look something like this:
int a = 10; Object boxedInt = a;
Then, we would be able to work with the variable boxedInt as with a reference type, e.g. to store it into some collection for generic types only. Alternatively, it could contain the null value. An ordinary int would be just copied without any dependencies.
Similarly, we can unbox variables. To do so, all you have to do is
...End of the preview...
ict.social is a large database made up of manuals and tutorials, whose main goal is to provide high-quality IT education to everyone. We started out in the Czech republic, where we display roughly a million articles per month and receive plenty of gratitude from our users. Thanks to our successful establishment, we are now bringing these articles to the rest of the world.
Although we are trying to keep our content free of charge, maintaining the site is a huge effort for everyone involved. Therefore, some content (exercises and more advanced material) costs network points. Don't worry, they're really cheap :)
Requested article covers this content:
In the final lesson, we'll learn about boxing and unboxing, wrapper classes for Java primitive types, packages, nested classes and more.
Buying this article gives you unlimited access to it forever. You will learn some more and help us keep giving our site maintenance which helps you and others get better futures. It's a win-win.
You gain points by supporting our network. This is done by sending a helpful amount of money to support the site, or by creating content for the network.
You can get points immediately using:
|Credit card||SMS||Wire transfer|