For example why can you do:
int n = 9;
But not:
Integer n = 9;
And you can do:
Integer.parseInt("1");
But not:
int.parseInt("1");
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
intis a primitive type. Variables of typeintstore the actual binary value for the integer you want to represent.int.parseInt("1")doesn’t make sense becauseintis not a class and therefore doesn’t have any methods.Integeris a class, no different from any other in the Java language. Variables of typeIntegerstore references toIntegerobjects, just as with any other reference (object) type.Integer.parseInt("1")is a call to the static methodparseIntfrom classInteger(note that this method actually returns anintand not anInteger).To be more specific,
Integeris a class with a single field of typeint. This class is used where you need anintto be treated like any other object, such as in generic types or situations where you need nullability.Note that every primitive type in Java has an equivalent wrapper class:
bytehasByteshorthasShortinthasIntegerlonghasLongbooleanhasBooleancharhasCharacterfloathasFloatdoublehasDoubleWrapper classes inherit from Object class, and primitive don’t. So it can be used in collections with Object reference or with Generics.
Since java 5 we have autoboxing, and the conversion between primitive and wrapper class is done automatically. Beware, however, as this can introduce subtle bugs and performance problems; being explicit about conversions never hurts.