What is Autoboxing and Unboxing in Java? - 045

preview_player
Показать описание
Let's start with the number classes, and then we'll talk about how Java uses autoboxing and unboxing to convert these classes to and from Java primitives.

In earlier videos we looked at the primitive data types for numbers. They were the bytes, shorts, ints, longs, floats, and doubles.

Java converts between numbers and primitives automatically using something called autoboxing and unboxing.

Autoboxing is when Java automatically converts a number primitive to an instance of the corresponding number class. The simplest example of this is when you declare a number class instance.

Unboxing happens in the other direction. This is when we have the number class, but Java is expecting the primitive. In this case, Java "unboxes" the primitive from the wrapper class.

#java #programming #tutorial





Concepts: Java autoboxing and unboxing, Java Number classes, Java converting types

Social Links: Don't hesitate to contact me if you have any further questions.

Related Videos:

Media credits: All images are owned by DJ Spiess unless listed below

Music
Рекомендации по теме
Комментарии
Автор

4:44 Thank you! I could not understand Objects for the longest time. But such a simple explanation made it click right away!

Kevin-uhkm
Автор

Thanks for the tour of the Java Maze. Thanks to the good explanation, I can now find my way around. Keep up such a good job. Greetings from Germany :)

thore
Автор

This was better explained than my prof! Thank you.

FictionsAndIllusions
Автор

I like the video. Makes me wish i was on holiday plus you have explained it well

delboytrotter
Автор

good vid, explained it clearly. Keep up the good work!

nul
Автор

I have a question regarding autoboxing and pass by reference/value. Okay, say we have a primitive wrapper class reference, and we pass it to a method in Java (that obviously expects an input value of that wrapper object type, say Integer). The moment we do that, will Java unbox the value and pass it by value and NOT by reference? Or is there never a reference created at all in the case of these wrapper classes?

Kostas
Автор

What if we had to make a deep copy of an instance variable gunAmmo, so within the copy constructor how would we deal with that instance variable public int gunAmmo?
would it be, for example:
public Gunner(Gunner other){
this.gunAmmo = new Integer(other.gunAmmo);

or

public Gunner(Gunner other){
Integer gunAmmo = new Integer(other.gunAmmo)

or just

public Gunner(Gunner other){
this.gunAmmo = (other.gunAmmo)

jameswhyte
Автор

so in short performance vs versatility right?

camaleon
Автор

This video is old. The DIAMOND declaration in your ArrayList has been updated since then. You now only have to declare an empty diamond after the new keyword.

klarissaclairiton
Автор

Your T-Shirt is distracting :-), moreover, thanks for explaining the concept, good job !

sprokey
Автор

Chicken on your shirt is very annoying to me

sunigkale