What is packaging and unpacking (boxing/unboxing)?

What is packaging and unpacking (boxing/unboxing) and why is it necessary?

Would be happy with examples.

Author: Anton Sorokin, 2016-10-28

3 answers

Working with objects inevitably entails memory and performance overhead. To avoid this, we use variables of primitive types. These are essentially simple variables, like in C or C++. byte takes up 1 byte of memory, int and float take up 4 bytes each, long and double take up 8 bytes each, and so on. Unlike operations with objects, operations with variables of primitive types do not require allocation/deallocation of memory and are performed quickly - they are in most cases eventually They are compiled into simple processor instructions, which allows Java programs to often run at a speed comparable to programs written in simple compiled (directly into machine code) languages (such as C, C++).

The disadvantage with them is that you can't do with them what you can do with all objects - they don't have methods. You can't, for example, write

int a = 5;
ArrayList list = new ArrayList(); 
String s = a.toString(); // Ошибка
list.add(a))             // Можно, но произойдет автоупаковка 
                         // и в коллекцию будет помещен Integer

You can't put them in collections and so on.

To get around this the disadvantage is that for all primitive types, there are corresponding wrapper classes, whose objects can store values of primitive types, but have all the properties of normal objects:

Integer a = 5;
ArrayList list = new ArrayList(); 
String s = a.toString();  // OK
list.add(a))              // OK

Creating a wrapper object from a primitive type variable is called boxing, and getting a primitive type value from a wrapper object is called unboxing. Shell objects can be assigned values of primitive types, and variables of primitive types can be assigned values shell variables, and if necessary, shell objects are automatically created with the corresponding values (auto-packaging) , or vice versa, primitive values are extracted from the shells (auto-extracting):

int a = 5;
Integer b = 10;
a = b;             // OK, атораспаковка
b = a * 123;       // OK, автоупаковка

In cases where objects are required by the context (assignment, method call with passing parameters), and we use values of primitive types (variables or expressions of type 2 * 3), auto-packaging always occurs.

All wrapper objects - immutable types, i.e. when we assign a new value to them, a new one is actually created to replace the old object.

 11
Author: m. vokhm, 2018-12-13 04:41:24

In versions below JDK 1.5, it was not easy to convert primitive data types such as int, char, float, double to their shell classes Integer, Character, Float, Double. Starting with JDK 5, this function, converting primitive types to equivalent objects, is implemented automatically. This property is known as Autoboxing. The reverse process, respectively – is Unboxing, i.e., the process of converting objects to their corresponding primitive types.

Example the code for auto-packaging and unpacking is shown below:

Auto-packaging

 1 Integer integer = 9;

Unpacking

1 int in = 0;
2 in = new Integer(9);

When is auto-packaging and unpacking used?

Auto-packaging is used by the Java compiler under the following conditions:

  • When a primitive type value is passed to a method as a method parameter that expects an object of the corresponding wrapper class.
  • When a primitive type value is assigned to a variable of the corresponding class shells.

Source link

 8
Author: Константин, 2016-10-28 13:43:07

To understand the meaning of packaging-unpacking, you need to understand how the executable environment and your program works at the processor level, as well as understand that the term OOP is just an add-on to classical structural programming. The intermediate bytecode during program execution is converted by the executable environment into commands of a specific microprocessor. There are 3 main ways to store, process data, and interact with the program: processor-register, processor-stack, and processor-memory. Register commands are the shortest and fastest, stack commands are the shortest, but they are executed at a higher number of processor cycles, and memory commands are the longest and slowest. In most cases, simple data types are stored and processed in the processor registers and on the stack, while classes and more complex data types are stored in the so - called" heap " - dynamic memory, which is controlled by the executable environment. The mechanism of the heap is quite complex and working with a heap of software the time is always longer. The stack-registers bundle is most efficient in cyclic algorithms, where each iteration has long complex calculations, while a cyclic code fragment with register addressing is executed hundreds or even tens of thousands of times faster than if the data were stored in the heap:) but storing data in memory simplifies the program and reduces its size, and also allows you to use all the charms of OOP. Packaging-unpacking was invented in order to avoid loss software performance-if speed is critical - decompress your data into a simple compact format, if speed is not critical-pack it into an object type and simplify the program. In addition to speed, a situation with significant resource costs is possible - for example, you need to create an array of a million single-byte elements: which is better to use - a primitive single-byte type or a packed one with a size of several tens of bytes?

 -1
Author: Андрей aka OberoN, 2017-02-24 20:33:56