Differences between primary types and wrapes
Hi,
I am developing an two applications that change information over RMI, i can use primitive types, like long, boolean, int or wrapes of primary types like Long, Boolean, Integer
Beacuse both types are serializable, but i dont nkow what type must i use, somebody can explain me what is the best from the point of view of the performance and serialization?
thanks
[388 byte] By [
kerulea] at [2007-11-27 5:05:29]

> The primitives, usually, unless you have a reason to use a wrapper.
I would have said "...unless you absolutely have to." Autoboxing encourages the illusion that primitives and wrapper objects are interchangeable, and that's a dangerous illusion. Consider this example: import java.util.*;
public class Test
{
public static void main(String... args) throws Exception
{
List<Integer> intList = Arrays.asList(1, 2, 300, 400);
int a = 1;
Integer b = 2;
int c = 300;
Integer d = 400;
System.out.printf("%3d == %3d: %b%n", a, intList.get(0),
a == intList.get(0));
System.out.printf("%3d == %3d: %b%n", b, intList.get(1),
b == intList.get(1));
System.out.printf("%3d == %3d: %b%n", c, intList.get(2),
c == intList.get(2));
System.out.printf("%3d == %3d: %b%n", d, intList.get(3),
d == intList.get(3));
}
}
Output: 1 ==1: true
2 ==2: true
300 == 300: true
400 == 400: false In some cases, == is comparing two ints; in others, two objects. If you use primitives and wrappers interchangeably without appreciating the distinction, you'll never know which kind of comparison is being performed. To minimize the risk of this kind of bug, you should always declare primitive values as primitives, not as wrapper types.