Це відео не доступне.
Перепрошуємо.

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/

КОМЕНТАРІ • 41

  • @Kevin-uh4km
    @Kevin-uh4km Рік тому +1

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

  • @thore98
    @thore98 2 роки тому +3

    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 :)

  • @FictionsAndIllusions
    @FictionsAndIllusions 7 років тому +5

    This was better explained than my prof! Thank you.

  • @loladiazvarela8855
    @loladiazvarela8855 Рік тому

    thank you!

  • @delboytrotter77
    @delboytrotter77 3 роки тому +1

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

  • @nul2215
    @nul2215 5 років тому +1

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

  • @raymondjolly282
    @raymondjolly282 7 років тому +1

    Excellent explanation. Thanks

  • @kaidai1043
    @kaidai1043 7 років тому +2

    Great Video! This helps a lot!

  • @victrur
    @victrur Рік тому

    Excelent explanation!

  • @Ravikumar-gj6qw
    @Ravikumar-gj6qw 4 роки тому +1

    Most useful tutorial tq bro

  • @5b2c950
    @5b2c950 6 років тому +1

    Great video! Thank you so much!

  • @h3w45
    @h3w45 5 років тому +1

    you explained it very well, thanks.

  • @PlayaBurger
    @PlayaBurger 7 років тому +1

    awesome explanation! thanks

  • @rodion-ukr5938
    @rodion-ukr5938 7 років тому +2

    Wow! What a great video!

    • @deegeu
      @deegeu  7 років тому

      Thank you! Glad it was helpful for you.

  • @sriramchandrasekaran8501
    @sriramchandrasekaran8501 8 років тому +1

    Thank you so much :) very good video

  • @leonnguyen9654
    @leonnguyen9654 7 років тому +1

    thank you, sir

  • @Ravikumar-gj6qw
    @Ravikumar-gj6qw 5 років тому

    Good explanation bro

  • @robwm1
    @robwm1 8 років тому +1

    Great shirt!

    • @deegeu
      @deegeu  8 років тому

      Thanks! I'm starting to get quite the collection of these. :)

  • @vickdick8411
    @vickdick8411 6 років тому

    Nice!

  • @rajtilak029
    @rajtilak029 6 років тому

    just wow!

  • @sprokey
    @sprokey 7 років тому +2

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

    • @deegeu
      @deegeu  7 років тому +1

      Thanks. :)

  • @CPTBeggar
    @CPTBeggar 7 років тому

    This helped me so much

    • @theblindprogrammer
      @theblindprogrammer 2 роки тому

      What project are you building if you don't mind me asking.

  • @Kostas1601
    @Kostas1601 7 років тому

    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?

    • @theblindprogrammer
      @theblindprogrammer 2 роки тому

      // 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!

  • @camaleon465
    @camaleon465 6 років тому

    so in short performance vs versatility right?

  • @jameswhyte1340
    @jameswhyte1340 8 років тому +1

    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)

    • @deegeu
      @deegeu  8 років тому

      +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.

    • @jameswhyte1340
      @jameswhyte1340 8 років тому +1

      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?

    • @deegeu
      @deegeu  8 років тому +1

      +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.

  • @klarissaclairiton9010
    @klarissaclairiton9010 7 років тому +1

    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.

    • @deegeu
      @deegeu  7 років тому +1

      Actually the video was made after that change. Old habits die hard. :)

  • @contort69
    @contort69 10 місяців тому

    This is not the ideal way for me to learn this..this is like a lecture

  • @harshhwardhanrai3716
    @harshhwardhanrai3716 4 роки тому +1

    JOHNY SIN

  • @pleasethink4789
    @pleasethink4789 7 років тому

    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

    • @theblindprogrammer
      @theblindprogrammer 2 роки тому

      If you need any help with Java, I am here to help. Ask away!

  • @sunigkale8779
    @sunigkale8779 7 років тому

    Chicken on your shirt is very annoying to me