Is it Ok to pass int to a method which is taking Integer as parameter. Here is the code
public class PassingInt
{
public static void main(String args[])
{
int a = -1;
passIntToInteger(a);//Is this Ok?
}
private static void passIntToInteger(Integer a)
{
System.out.println(a);
}
}
passIntToInteger(new Integer(a));valueOfwith the wrappers instead ofnew.valueOfuses a cache for some "common" values, i.e. -127 to +127 internally - that means no object creation needs to happen for these values. One general piece of advice to the OP is to generally avoid using auto-boxing and wrapped types when possible because it hides things that are happening and can bite back: You can get aNullPointerExceptionwhen unboxing anIntegerwrapper type that isnullfor example. Theequalsmethod also has some quirks when working with longs if I remember correctly.