diff --git a/Java/Autoboxing/Autoboxing and methods/Autoboxing_and_its_methods.ipynb b/Java/Autoboxing/Autoboxing and methods/Autoboxing_and_its_methods.ipynb new file mode 100644 index 0000000000..2b6adca83e --- /dev/null +++ b/Java/Autoboxing/Autoboxing and methods/Autoboxing_and_its_methods.ipynb @@ -0,0 +1,162 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Autoboxing and its methods.ipynb", + "provenance": [], + "collapsed_sections": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "HPjAzvleM6kJ" + }, + "source": [ + "#Autoboxing\n", + "\n", + "Since Java 5, we have a feature called autoboxing. With this feature, we need not be concerned that the class will return a wrapper class or the primitive data type as Java does the conversion for you.\n", + "\n", + "The Java compiler automatically does the conversion of the primitive class into its corresponding wrapper class. For example converting an int to an Integer, boolean to Boolean and so on.\n", + "\n", + "Let us understand this with an example.\n", + "\n", + "\n", + "\n", + "```\n", + "List amount=new ArrayList<>();\n", + "amount.add(1001.23);\n", + "```\n", + "\n", + "This will not show error as double is autoboxed to its wrapper class Double automatically.\n", + "\n", + "You could still write that the long way by writing something like `amount.add(new Double(70))` and pass the wrapper object.\n", + "\n", + "The Java compiler applies autoboxing when a primitive value is:\n", + "\n", + "* Passed as a parameter to a method that expects an object of the corresponding wrapper class.\n", + "\n", + "* Assigned to a variable of the corresponding wrapper class.\n", + "\n", + "Here's a full program example to demonstrate autoboxing.\n", + "\n", + "\n", + "\n", + "```\n", + "class Example{ \n", + " public static void main(String args[]){ \n", + " int a=100; \n", + " Integer a1=new Integer(a);\n", + " \n", + " Integer a2=3;//Boxing \n", + " \n", + " System.out.println(a1+\" \"+a2); \n", + " } \n", + "} \n", + "```\n", + "Output:\n", + "\n", + "\n", + "```\n", + "100 3\n", + "```\n", + "\n", + "#Unboxing\n", + "\n", + "Unboxing is the exact opposite of autoboxing. Unboxing enables the conversion of wrapper class object into its corresponding primitive.\n", + "\n", + "Let us consider the amount example again.\n", + "\n", + "```\n", + "List amount=new ArrayList<>();\n", + "amount.add(1001.23);\n", + "double unboxed=amount.get(0);\n", + "```\n", + "\n", + "Here the second line autoboxes double into Double as shown before. But now the third line in this code gets the Double and unboxes it into its primitive.\n", + "\n", + "The Java compiler applies unboxing when an object of a wrapper class is:\n", + "\n", + "* Passed as a parameter to a method that expects a value of the corresponding primitive type.\n", + "\n", + "* Assigned to a variable of the corresponding primitive type.\n", + "\n", + "Here's a full program example to demonstrate unboxing.\n", + "\n", + "```\n", + "class Example{ \n", + " public static void main(String args[]){ \n", + " Integer i=new Integer(100); \n", + " int a=i; \n", + " \n", + " System.out.println(a); \n", + " } \n", + "}\n", + "```\n", + "\n", + "Output:\n", + "\n", + "```\n", + "100\n", + "```\n", + "\n", + "#Special cases\n", + "\n", + "###Unboxing a null\n", + "\n", + "\n", + "\n", + "```\n", + "List amount=new ArrayList<>();\n", + "amount.add(null);\n", + "double unboxed=amount.get(0);\n", + "```\n", + "\n", + "In this code, in the second line autoboxing occurs and null is added to the list. In the third line, when we try to access null, it gives us Null Pointer Exception as we are accessing null.\n", + "\n", + "###Being careful while autoboxing integer\n", + "\n", + "Let us take this code for example.\n", + "\n", + "\n", + "```\n", + "List amount = new ArrayList<>();\n", + "amount.add(1);\n", + "amount.add(2);\n", + "amount.remove(1);\n", + "System.out.println(amount);\n", + "```\n", + "\n", + "It actually outputs 1. After adding the two values, the List contains [1, 2]. We then request the element with index 1 be removed. That’s right: index 1. Because there’s already a remove()\n", + "method that takes an int parameter, Java calls that method rather than autoboxing. If you want to remove the 2, you can write numbers.remove(new Integer(2)) to force wrapper class use.\n", + "\n", + "


\n", + "sources used: The book \"OCA study guide\" by Jeanne Boyarsky and Scott Selikoff\n", + "https://www.javatpoint.com/autoboxing-and-unboxing\n", + "https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ] + } + ] +} \ No newline at end of file