Це відео не доступне.
Перепрошуємо.
What is Autoboxing and Unboxing in Java? - 045
Вставка
- Опубліковано 19 сер 2024
- 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.
The java.lang package also includes a wrapper class for each of these primitive data types. All of the number types are subclasses of the Number class.
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
▼
Like the video? Don't forget to subscribe! ➜ www.deegeu.com/...
▼
Keep up to date with the newsletter! ➜ www.deegeu.com/...
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.
WEBSITE : dj@deegeu.com
TWITTER : / deege
FACEBOOK: / deegeu.programming.tut...
GOOGLE+ : google.com/+Dee...
About Me: www.deegeu.com/...
Related Videos:
• What important Java Te...
Media credits: All images are owned by DJ Spiess unless listed below
Music
Smooth Sailing (with Guitar) by Audionautix is licensed under a Creative Commons Attribution license (creativecommon...)
Artist: audionautix.com/
4:44 Thank you! I could not understand Objects for the longest time. But such a simple explanation made it click right away!
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 :)
This was better explained than my prof! Thank you.
thank you!
I like the video. Makes me wish i was on holiday plus you have explained it well
Yes this guy knows his stuff.
good vid, explained it clearly. Keep up the good work!
Excellent explanation. Thanks
Great Video! This helps a lot!
Excelent explanation!
Most useful tutorial tq bro
Great video! Thank you so much!
you explained it very well, thanks.
awesome explanation! thanks
Wow! What a great video!
Thank you! Glad it was helpful for you.
Thank you so much :) very good video
thank you, sir
Good explanation bro
Great shirt!
Thanks! I'm starting to get quite the collection of these. :)
Nice!
just wow!
Your T-Shirt is distracting :-), moreover , thanks for explaining the concept, good job !
Thanks. :)
This helped me so much
What project are you building if you don't mind me asking.
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?
// Passing by reference, we will create a class called Building
public class Building {
private String personName;
public Building(String personName) {
this.personName = personName;
}
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
}
// Main class
public class example {
public static void main(String[] args) {
Building building = new Building("Mike");
// check if Mike lives in the building
// check the content of the building object memory
System.out.println("Does 'Mike' live in this building?" +
" " + (getPersonName(building, "mike") ? "
Yes, he lives here!" :
"No, he moved out!") ); // true / yes
// So, let's pass the object building to the method
// and give a different name, assume 'Mike' moved out the building
getPersonName(building, "Jerry");
// check if Mike lives in the building
// check the content of the building object memory
System.out.println("Does 'Mike' live in this building?" +
" " + (getPersonName(building, "mike") ? "
Yes, he lives here!" :
"No, he moved out!") ); // true / yes
}
public static boolean getPersonName(Building building, String name){
// let us change the address's resident
building = new Building("James");
/**
* if we run the program, and we are looking for 'Mike' as the name
* this method will return false, since we change what the address or
* referrence was point to.
*/
return building.getPersonName().equalsIgnoreCase(name);
}
}
// #1 when I don't add (
building = new Building("James");
)
Does 'Mike' live in this building?
Yes, he lives here!
Does 'Mike' live in this building?
Yes, he lives here!
// #2 when I instantiate the "James" name, when "James" moves in
Does 'Mike' live in this building? No, he moved out!
so in short performance vs versatility right?
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)
+James Whyte The third example. The first will unnecessarily create an Integer instance. The second will create in Integer instance that is scoped for the method only.
Deege U Thanks!
so no wrappers are needed for PDT's because they don't need to be deep copied because they PDT's which are immutable data? is that why?
And similar for Strings, since they are immutable objects, they cannot change so each copy object would get their own individual copy when running the copy constructor?
So with the exclusion of immutable objects the only thing we need to deep copy are immutable object references to other objects because a change in one of their references will persist to the others?
+James Whyte A PDT is just the value. For example take the number 42. A PDT would store 42 in a 4 byte memory location. To copy it, you just store 42 in a new location. A new class will have it's own memory location for a PDT. Every instance has it's own data. The PDT is mutable, because you can change the value to 43 at any time. Strings and Numbers are immutable, because once you create a class instance and set the value in the class, the value inside the instance cannot be changed. You can reuse an immutable reference when making a copy, because if the value changes you need to create a new instance. It won't affect other instances using the original reference. When you copy an instance, you're just copying the reference. A shallow copy will copy references in the source instance. A deep copy will create new instances, and copy the data from the source instance to the new instance.
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.
Actually the video was made after that change. Old habits die hard. :)
This is not the ideal way for me to learn this..this is like a lecture
JOHNY SIN
Could you provide the link to the "weird java number tricks" video you referred to at 5:04 in this video: ua-cam.com/video/VbMWqfEtbMo/v-deo.htmlm4s
If you need any help with Java, I am here to help. Ask away!
Chicken on your shirt is very annoying to me