General Programming

Rule 5: Prefer primitive types to boxed primitives


Differences between primitives and boxed primitives –
1.only value is associated to primitive where as identity and value is associated with boxed primitive i.e 2 boxed primitive having same value will not have same identity
2.primitive have fully functional value where-as the non-primitives have one nonfuntional value – null , in addition to functional value.
3.primitives are more time and space efficient than boxed primitives.

Applying the == operator to boxed primitives is almost always wrong it will not show equaltiy of value .wayaround unbox in a local varialbe before comparing.

when you mix primitives and boxed primitives in an operation, the boxed primitive is auto-unboxed. If a null object reference is auto-unboxed, you get a NullPointerException

public class Unbelievable {
static Integer i;
public static void main(String[] args) {
if (i == 42)
System.out.println(“Unbelievable”);
}
}

Published by

Unknown's avatar

sevanand yadav

software engineer working as web developer having specialization in spring MVC with mysql,hibernate

Leave a comment