What is the difference in using a primitive and wrapper data type and what is the need for a wrapper data type?

I searched it all over the Internet, but all the answers were only the difference. I know the difference, but I do not understand the difference in their applications.

For example, suppose we have to take two floating values, if we use double, we can easily compare using a==b , whereas if we use Double, we will need to use a.equals(b) .

+5
source share
3 answers

This is more than behind the scenes. One reason is how the collection API is developed in Java ...

Keep in mind that you simply can’t do anything:

 List<int> myList 

in java. For this you need a wrapper

 List<Integer> myList 

because Collections work with objects, not primitives.

Wrappers, on the other hand, are objects that offer developers an excellent group of methods / constants that make certain operations “faster and easier”, such as:

 int x = 17; 

Now, thanks to the wrapper, you can do:

 String pars= Integer.toBinaryString(x); pars= Integer.toHexString(x); Integer.MAX_VALUE; Integer.highestOneBit(x); 

Without this, you will get pain in the neck, since the primitive x does not really help you do this.

+3
source

You can find it on the blog.

1. First

Double is a reference type, so you can use it as a template argument

Example:

 public class Tmp<T> { public Tmp() { } } 

If you want to create a class like This.

Then you need to pass the type of the link, creating an in object. for instance

 new Tmp<Integer>() 

You will get an error if you create an object like:

 new Tmp<int>() 

2. Second

It is only because of the Wrapper classes that typical data type programming can be performed.

For example, the following method takes any number ( Byte, Integer, Double, Short, Float, Long, BigDecimal, BigInteger, AtomicInteger, AtomicLong ) and returns Integer adding these numbers.

 public Integer add(Number a, Number b){ return a.intValue() + b.intValue(); } 

3. Thirdly

In an earlier version, Java does not support AutoBoxing and AutoUnboxing. So, if you use this version of Java, you can easily distinguish both.

For example, if you are using Java 1.4 or earlier, then:

 Integer a = 1; // Auto Boxing(Not Works) Integer a2 = new Integer(2); // Boxing (It Works) 

4. Fourth

Storage of both is also different. Primitive types are stored in Stack , and reference types are stored in Heap

5. Fifth

You can use the functionality of this class, such as parsing Integer, Double, etc. strings, and use the same conditions.

Here are the functions and conventions of the Integer class

enter image description here

6. Sixth

You can serialize Integer while this is not possible with int

7. Seventh

You can pass Integer as an RMI method, but this is not possible with int

Note. Both Integer and int can be part of another object in the RMI argument; in fact, inside the Integer class, they store the value in int .

8. The eighth

The int variable is mutable (this does not apply to final int ), and the Integer is immutable. It will create a new object when we change the value.

+6
source

When you use Collections , you should use objects, not primitives.

 List<double> list = new ArrayList<double>; // not allowed List<Double> list = new ArrayList<Double>; // allowed Map<double, String> map = new HashMap<double, String>(); // not allowed Map<Double, String> map = new HashMap<Double, String>(); // allowed 

Or, if you want your Double to be null. Useful for example with Hibernate and DTO or POJO

 private int number; // default: 0 private Integer number; // default: null 

Double allows you to quickly do

 double.intValue(); double.toString(); 
+2
source

Source: https://habr.com/ru/post/1266350/


All Articles