Week 17 — What are the differences between int and Integer in Java

Question of the Week #17
What are the differences between int and Integer in Java
25 Replies
Eric McIntyre
Eric McIntyre2y ago
int is a primitive type representing a whole number while Integer is a wrapper class with methods
Submission from seacousin#5520
Eric McIntyre
Eric McIntyre2y ago
Integer is the wrapper class for the primitive type int. What this basically means is Integer allows for more customizing. (On mobile ill turn into code later) int x = 5 vs Integer x = new Integer(parameters); its treated like a class where you need to make a new instance, vs how int just lets you store the variables value quickly
Submission from Mochatitan#5944
Eric McIntyre
Eric McIntyre2y ago
int is to make the new variable while integer is to add properties to the variables ie: int example = Integer.valueof(String)
Submission from Lieutenant Piplup#9658
Eric McIntyre
Eric McIntyre2y ago
The int keyword is used to define a variable of type integer. Integer is a class.
int myNumber = 3;

List<Integer> myList = new ArrayList<>();
int myNumber = 3;

List<Integer> myList = new ArrayList<>();
Submission from MinecraftMan1013#7242
Eric McIntyre
Eric McIntyre2y ago
int is a primitive datatype. Integer a complex one used as template parameter.
Submission from CuteTwink#4348
Eric McIntyre
Eric McIntyre2y ago
int is a basic type like double or bool Integer is an Object like a String so you need to use that for stuff like generic types int example:
int x = 20;
int x = 20;
Integer example:
ArrayList<Integer> list = new ArrayList<>();
ArrayList<Integer> list = new ArrayList<>();
Eric McIntyre
Eric McIntyre2y ago
also since Integer is an object it has methods that you can use like
toString();
toString();
Submission from BecauseISaid#0830
Eric McIntyre
Eric McIntyre2y ago
int is a primitive Datatype while Integer is a Wrapper Class adding functionality in way of methods like .parseInt(String) or on account of being a Object class being useable in ArrayLists or Generics.
Submission from Ignitrum#0176
Eric McIntyre
Eric McIntyre2y ago
int
int
is only the type of the variable. So if you want to parse a String to an integer, you need the
Integer
Integer
class to parse it with
Integer.parseInt("34")
Integer.parseInt("34")
to convert it to an
int
int
variable
Submission from Giotsche#5027
Eric McIntyre
Eric McIntyre2y ago
int is a primary data type in Java whereas Integer is an object which encapsulates an int value
Submission from imraklr#1712
Eric McIntyre
Eric McIntyre2y ago
int is a primitive data type. value of int variable could be change. Integer is a class, could be create an object Integer Integer wrapped int and provide methods and static methods to operation for object examles int numb = 2; Integer number = 4; we could -> Integer numb1 = Integer.parseInt(numb); we couldn't -> int number1 = int.parseInt(number);
Eric McIntyre
Eric McIntyre2y ago
Sometimes JVM use to optimalization int instead Integer because could be quickly
Submission from Piotr Łyszkowski#0456
Eric McIntyre
Eric McIntyre2y ago
int is a primitive type and Integer is a primitive wrapper class. A variable in Java can be either a primitive or a reference. Primitives represent a specified value. This can be a boolean (1 bit), a byte (8 bits), a short (16 bits), an int (32 bits), or a long (64 bits). If you would like to count rational numbers, you have a float (32 bits), or a double (64 bits). References represent information regarding that variable. That information includes other primitives and references. One of Java's biggest design flaws is the fact that they used too many references and not enough primitives. Some features that the Integer wrapper class allows that int does not allow is casting to other primitives, such as float, double, long, and short. Integer also contains static methods which can be used to cast a string into an int, like here:
Integer.parseInt("12345"); // returns 12345 as an int
Integer.parseInt("12345"); // returns 12345 as an int
You would think, why can't Java just add it into int? Since Java was designed for enterprises, one of their core philosophies is to be conservative. In the world of software development, conservative means "slow to change". You might initially think that "slow to change" is contrary to one of the objectives of new technology, until you realize that another objective of new technology is efficiency. If your team is maintaining a large codebase, it would be an efficient choice to use Java which sees version updates every 6 months, which mostly has minimal feature changes, if any, and some library changes. This is unlike Node.JS which has millions of libraries, each of which have drastic codebase changes every 18 months which require you to refactor your code which is usually dependent on most of these libraries.
Submission from amicharski#7113
Eric McIntyre
Eric McIntyre2y ago
int is a primitive type that cannot be null and Integer is the object nullable representation. Prefer int for optimization. And use Integer for null
Submission from Héphaïsto#8899
Eric McIntyre
Eric McIntyre2y ago
In Java, an int is a primitive type. Integer however, is a wrapper class for the int and, because Integer is a class, it allows you to use methods its methods. Because of that, Integer is a more powerful tool than a simple int, depending on the need.
Submission from Maruvert#2038
Eric McIntyre
Eric McIntyre2y ago
int is a primitive, while Integer is a class that wraps around the primitive int. The reason for Integer existing is because of generic types, since you can't use a primitive as generic type. This makes things like List<Integer> possible, where instead of int directly, you use Integer. The java compiler automatically unwraps Integers into ints if needed, and vice versa. Meaning that this:
List<Integer> integers = new ArrayList();
integers.add(123);
int math = integers.get(0) + 321;
List<Integer> integers = new ArrayList();
integers.add(123);
int math = integers.get(0) + 321;
is valid java code. There are other wrapper classes as well, Long for long, Short for short, Byte for byte, Character for char, Double for double and Float for float. All of them except for Character (including Integer) extend Number. All wrapper classes have utility functionality. An example on Integer:
int maxSigned = Integer.MAX_VALUE; // -> 0x7fffffff
int minSigned = Integer.MIN_VALUE; // -> 0x7fffffff + 1
Integer.toString(255, 16); // toString(i, radix) -> "ff"
Integer.toString(minSigned); // "-2147483648"
Integer.toUnsignedString(minSigned); // "2147483648"
int hexFF = Integer.parseInt("FF", 16); // parseInt(s, radix) -> 255
int maxSigned = Integer.MAX_VALUE; // -> 0x7fffffff
int minSigned = Integer.MIN_VALUE; // -> 0x7fffffff + 1
Integer.toString(255, 16); // toString(i, radix) -> "ff"
Integer.toString(minSigned); // "-2147483648"
Integer.toUnsignedString(minSigned); // "2147483648"
int hexFF = Integer.parseInt("FF", 16); // parseInt(s, radix) -> 255
Generally, all wrapper classes (except for Character) can convert between themselves: Integer.valueOf(10).longValue() is equivalent to (long) 10, for example. All of them implement Comparable, which means that you are able to compare between 2 numbers of the same type by just calling (for example) Integer.compare(10, 11). This will (by definition) return the direction the second number is from the first number, which means -1 if the second number is smaller than the first number, 0 if they're equal, and 1 if the second number is bigger than the first number. There are other utility methods and constants in the wrapper classes, if I'd elaborate on those here the message would get too long.
Submission from 0x150#9699
Eric McIntyre
Eric McIntyre2y ago
The type int is a primitive type, implying that is it stored directly on the stack as opposed to objects which are stored on the heap. The class Integer on the other hand is a wrapper class for the primitive type int. This is useful in several cases, for example when you need to use it as a type parameter, since type parameters do not allow for primitive types. The Integer class has a constructor however due to auto-boxing, the following code is valid:
Integer x = 4;
int a = 2;
Integer b = a;
Integer x = 4;
int a = 2;
Integer b = a;
Eric McIntyre
Eric McIntyre2y ago
Also, the Integer class has the Integer#parseInt method, which parses a string for an int and returns it. There are 2 versions of this method, one lets you specify the base, while the other one is base 10 by default.
Submission from 未来#9204
Eric McIntyre
Eric McIntyre2y ago
int is a primitive type in java, and is hardcoded into the JVM. Integer is a wrapper class which represents ints as objects. This is very useful in various scenarios, one of the most common being its use in generics. Generics do not allow the use of primitives, and as such, the wrapper classes (including Integer) are useful if one would like to use Integers with generics, be it in collections or any other usages. Wrapper classes can also be inferred using autoboxing. So, for example, if we have an ArrayList<Integer>, we can add raw int types using the add(1) method. The compiler uses autoboxing to convert this call to add(Integer.valueOf(1)). This makes using wrapper classes much simpler. Another difference is as follows: while it is possible to use the statement int.class, it is not possible to explore the .class file, yet with Integer, since it is an object semantic and not a class semantic, we can do so.
Submission from Luqmaan#0205
Eric McIntyre
Eric McIntyre2y ago
int is an primitive data type and Integer is an primitive wrapper datatype we cant use any method there but on wrapper classes like Integer we can make an object from it so we can only store in primitive data types their specified data value example:
int integer = 0;
int integer = 0;
also in wrapper data types we can do autoboxing and unboxing in it note: autoboxing is basic data operations to the object & unboxing is the operation of converting objects to primary data (primitive data type like int, float, boolean, double,..)
int integer = 10;
Integer wrapperInteger = Integer.valueOf(integer); // int converted to Integer
Integer autoboxing = integer;

Integer anotherIntegerButWrapper = new Integer(11);
int primitiveInteger = anotherIntegerButWrapper.intValue(); // Integer converted to int
int unboxing = anotherIntegerButWrapper;
int integer = 10;
Integer wrapperInteger = Integer.valueOf(integer); // int converted to Integer
Integer autoboxing = integer;

Integer anotherIntegerButWrapper = new Integer(11);
int primitiveInteger = anotherIntegerButWrapper.intValue(); // Integer converted to int
int unboxing = anotherIntegerButWrapper;
so the diffrent is int is an primitive data type that saves specified datas and Integer is an wrapper Classes (primitive wrapper classes) and we can do things in there like autoboxing/unboxing
Submission from shelon_#4695
Eric McIntyre
Eric McIntyre2y ago
In Java, an int is a primitive data type that represents a whole number between -2147483648 and 2147483647. Because it is primitive, it has no methods or constructors, the only thing it can do is hold a value. On the other hand, Integer is the reference type of int. It stills holds a value between the same limits, as well as being able to be null. Integer has other features that its primitive counterpart does not. Integer is treated like a class (cause it is), and has methods you can run both statically and non statically, like Integer#parseInt for example. Additionally, you can instantiate this class if you want, though that's been deprecated since Java 9. One final note that is in generic types situations likes defining an ArrayList, Integer must be used in place of int, a confusing concept for rookie programmers.
// throws an error
int hi = new int(2);
// also throws an error
int hello = null;

// works (though not recomended)
Integer howdy = new Integer(5);

// has methods!
Integer.toBinaryString(hi);
// no methods :(
int.toBinaryString(hi);
// throws an error
int hi = new int(2);
// also throws an error
int hello = null;

// works (though not recomended)
Integer howdy = new Integer(5);

// has methods!
Integer.toBinaryString(hi);
// no methods :(
int.toBinaryString(hi);
⭐ Submission from Microsoft Edge#9986
Eric McIntyre
Eric McIntyre2y ago
int is a primitive datatype, Integer is a wrapper class. The point of Integer is to be used when it's not possible to use primitive data types, like with Collections for example. The following code will give a compile time error.
ArrayList <int> primitiveList = new ArrayList<>();
ArrayList <int> primitiveList = new ArrayList<>();
While the following code will compile successfully
ArrayList <Integer> wrapperList = new ArrayList<>();
ArrayList <Integer> wrapperList = new ArrayList<>();
Integers, like other objects, should be compared with .equals(), while ints can be compared with '==' Comparing Integers with '==' can sometimes lead to unexpected behaviours, because some JVMs will cache values between -127 and 128. Which means the following code will print "true"
Integer int1 = new Integer(5);
Integer int2 = new Integer(5);
System.out.println(int1==int2);
Integer int1 = new Integer(5);
Integer int2 = new Integer(5);
System.out.println(int1==int2);
Because both int1 and int2 are actually references to the same object. While this will print false
Integer int1 = new Integer(500);
Integer int2 = new Integer(500);
System.out.println(int1==int2);
Integer int1 = new Integer(500);
Integer int2 = new Integer(500);
System.out.println(int1==int2);
Eric McIntyre
Eric McIntyre2y ago
It's possible to assign an int to an Integer and vice versa, this is called auto-boxing. However it may cause performance overheads if used too much.
⭐ Submission from DragonHunter#8609
Eric McIntyre
Eric McIntyre2y ago
In Java, int is a primitive type which holds a 32bit signed integer value.
int i = 1337;
System.out.println(i);//1337
i++
System.out.println(i);//1338
//i=null;//compile-time-error
int i = 1337;
System.out.println(i);//1337
i++
System.out.println(i);//1338
//i=null;//compile-time-error
In contrast, Integer is an immutable class wrapping such an int. Since Integer is a class (extending Object), variables of its type can also be null;
Integer i=null;
System.out.println(i);//null
i=Integer.valueOf(1337);
System.out.println(i);//1337
int intRepr = i.intValue();
System.out.println(i);//1337
Integer i=null;
System.out.println(i);//null
i=Integer.valueOf(1337);
System.out.println(i);//1337
int intRepr = i.intValue();
System.out.println(i);//1337
In fields, ints are 0 by default while Integer fields are null by default. Java has a concept called autoboxing which automatically converts between primitive variables and their wrapper classes:
int i = 1337;
Integer j = i;//j is assigned to Integer.valueOf(i)
int k = j;//Java also automatically converts back Integer to int
int i = 1337;
Integer j = i;//j is assigned to Integer.valueOf(i)
int k = j;//Java also automatically converts back Integer to int
Eric McIntyre
Eric McIntyre2y ago
Since primitives can currently not be used in generics, Integer needs to be used instead of int in type arguments.
//List<int> intList=new ArrayList<>();//compile-time error
List<Integer> intList=new ArrayList<>();
intList.add(13);//autoboxing converts from the int 13 to an instance of the wrapper class
intList.add(37);
System.out.println(intList);//[13, 37]
//List<int> intList=new ArrayList<>();//compile-time error
List<Integer> intList=new ArrayList<>();
intList.add(13);//autoboxing converts from the int 13 to an instance of the wrapper class
intList.add(37);
System.out.println(intList);//[13, 37]
Submission from dan1st#7327
Want results from more Discord servers?
Add your server