+ 1
What is the diff between int and Integer in java ?
where we use int and where the other one
3 Antworten
+ 3
int is what is call in other languages (and also in Java) a primitive type. What this means is that is a memory container (what we call a variable) predefined in the language that can store a specific type of value.
Integer is the Class version of int, and as a class it can have member methods and properties defined in it. there ia also a class version for other primitives (i.e. Double class equivalent to the double data type).
You would mostly want to use int instead of Integer unless its mandatory to use the object/class type. One example of this is when you need to type cast the elements in an ArrayList. The type cast needs the Integer cause int will just not work. There are also some interesting methods in the Integer class to manipulate intengers that you should be aware of their existance in cause you need one in the future.
Have a nice coding.
0
int is a primitive type. Variables of type int store the actual binary value for the integer you want to represent.
Integer is a class, no different from any other in the Java language. Variables of type Integer store references to Integer objects, just as with any other reference (object) type.
0
"int" is a primitive data type. While "Integer" is a wrapper class of "int" that could be used to modify the "int" and contains some info for the "int".
This wrapper class also exist to other primitive data type like "float" with "Float", and "boolean" with "Boolean". You can search the internet for more info of this wrapper class.