Casting a primitive vs Creating a primitive object

I'm not used to throwing a primitive data type into an object. I saw some code:

public static int CompareAges(Person p1, Person p2) { Integer age1 = p1.getAge(); return age1.compareTo(p2.getAge()); } 

The implementation of age1 seemed extraneous, so I tried to write code as:

 public static int CompareAges(Person p1, Person p2) { return p1.getAge().compareTo(p2.getAge()); } 

But this caused a compiler error because p1.getAge() is a primitive int data type, not an Integer , which is an object.

Intuitively, I did:

 public static int CompareAges(Person p1, Person p2) { return ((Integer) p1.getAge()).compareTo(p2.getAge()); } 

and it worked!

Question: What did I miss? Since when did we start casting primitives as value types ?

+5
source share
3 answers

What is going on inside:

 1. Integer age1 = p1.getAge(); Integer != int Integer = Integer.valueOf(int) Integer age1 = Integer.valueOf(p1.getAge()); 2. p1.getAge().compareTo(p2.getAge()); int.compareTo(int) ^^^ // it just a primitive type, as a result - the compile error 3. ((Integer) p1.getAge()).compareTo(p2.getAge()) Integer.compareTo(int) Integer.compareTo(Integer.valueOf(int)) ((Integer) p1.getAge()).compareTo(Integer.valueOf(p2.getAge())) 4. (Integer) p1.getAge() ---> Integer.valueOf(p1.getAge()) // why so? look at callOfCode answer 

But, in my opinion, ((Integer) p1.getAge()).compareTo(p2.getAge()) looks ugly.

I would replace it with

 p1.getAge() > p2.getAge() ? 1 : (p1.getAge() < p2.getAge() ? -1 : 0) 

or

 Integer.compare(p1.getAge(), p2.getAge()) // java 7+ 

I would not want to do a casting in this case.


You can find more about "autoboxing / unboxing" here .

+6
source

Each primitive has its own type of boxing.

enter image description here

Boxing can be implicit (Autoboxing) or explicit, as you did in your code.

p2.getAge() is an example of AutoBoxing or Implicit Boxing. The compareTo method accepts an object. Since this is a primitive, Java automatically converts it into a correspondent box object (Integer). You made an explicit box with cast (Integer) p1.getAge()

+3
source

The difference between new Integer(primitive); and Integer integer = (Integer)primitive; runs only at the bytecode level. In the first case, a new object of the Integer class is created in memory. In the second case, Integer.valueOf(primitive) is called internal. This is a static method that checks whether a primitive falls in the range from -128 to 127, and if it falls, it returns a value from the integer cache and a new object is not created. If it does not fall into this range, a new Integer object is created. It is used to increase efficiency. But such efficiency is low today.

 int z = 1; Integer a = (Integer)z; Integer b = (Integer)z; //Prints true, since Integer object is retrieved from cache (range of -128 to 127) System.out.println(a == b); int w = 999; Integer c = (Integer)w; Integer d = (Integer)w; //Prints false, since 999 is not in cache, new Integer objects are instantiated and they points to different places in memory System.out.println(c == d); 
+3
source

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


All Articles