2

I have seen many times in code that people use int or Integer to declare variable in beans. I know int is datatype and Integer is wrapper class. My question is, in which condition int or Integer should be used and is there any advantage of either of them over another?

  • 3
    Does this answer your question? [What is the difference between an int and an Integer in Java and C#?](https://stackoverflow.com/questions/564/what-is-the-difference-between-an-int-and-an-integer-in-java-and-c). Specifically, take a look at [this answer](https://stackoverflow.com/a/608/7151494) from there. – Fureeish Jul 18 '20 at 19:28
  • 1
    I think we should always use `int` by default, a primitive type has less cost than on object. Use an `Integer` object when you need the functionality of an object. e.g. function argument by reference or when you are returning an int and you want to have a case where the result can be null. – Jeiraon Jul 18 '20 at 19:40
  • 1
    Always search Stack Overflow thoroughly before posting. – Basil Bourque Jul 18 '20 at 20:21

4 Answers4

1

My question is, in which condition int or Integer should be used and is there any advantage of either of them over another?

Well, you should use the reference type Integer whenever you have to. Integer is nothing more than a boxed int. An Object with a single field containing the specified int value.

Consider this example

public class Ex {
    int field1;
    Integer field2;

    public Ex(){}
}

In this case field1 will be initialized with the value 0 while field2 will be initialized with null. Depending on what the fields represent, both approaches might be advantageous. If field1 represents some kind of UUID, would you want it to be initialized with a zero value?

I wouldn't worry too much about the performance implications of Autoboxing. You can still optimize after you get your code running.

For more information take a look at the documentation

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Integer.html

Tmrd993
  • 41
  • 2
0

You always use int, pretty much.

Integer should rarely be used; it is an intermediate type that the compiler takes care of for you. The one place where Integer is likely to appear is in generics, as int is simply not legal there. Here is an example:

List<Integer> indices = new ArrayList<Integer>();
int v = 10;
indices.add(v);

The above works: It compiles with no errors and does what you think it would (it adds '10' to a list of integer values).

Note that v is of type int, not Integer. That's the correct usage; you could write Integer here and the code works as well, but it wouldn't be particularly idiomatic java. Integer has no advantages over int, only disadvantages; the only time you'd use it, is if int is straight up illegal. Which is why I wrote List<Integer> and not List<int> as the latter is not legal java code (yet - give it a few versions and it may well be legal then, see Project Valhalla).

Note also that the compiler is silently converting your v here; if you look at the compiled code it is as if javac compiled indices.add(Integer.valueOf(v)) here. But that's fine, let the compiler do its thing. As a rule what the compiler emits and what hotspot optimizes are aligned; trust that what javac emits will be relatively efficient given the situation.

rzwitserloot
  • 44,252
  • 4
  • 27
  • 37
0

int is a primitive type, a value type for number literals.

  • it is used whenever and wherever you just want to do some basic arithmetical operation;
  • it is a value type, so it's stored in the Stack area of the memory, hence operations on it are much faster;
  • whenever it's needed, compiler implicitly and automatically casts back and forth (a.k.a Boxing and Unboxing) from int to Integer and vice versa;

Integer is a Class, which is a reference type and you instantiate an Object of that type.

  • you create an object of that class, which means, that you also have some methods and operations on that object;
  • any time you do some arithmetic operation on the instance of Integer, under the hood, it's still implemented by int primitives, and it's just wrapped into box/container;
  • it is a reference type / object, which is very important, as you can Serialize or Deserialize it;
  • it also has some very useful utility factory methods, like Integer.valueOf(..) for example, to parse the String as an integer;
  • it can be well used into declarations of the generic types and it, as a class, supports the hierarchy as well. For instance, it extends Number, and you can make use of this;
  • it is stored in the Heap area of the memory.
Giorgi Tsiklauri
  • 6,699
  • 7
  • 29
  • 54
-1

int is a primitive and Integer is an object . From an memory footprint point of view , primitive consume less memory than object and also primitives are immutable (since they use pass by value ) . Here is a good article on when to use what : https://www.baeldung.com/java-primitives-vs-objects

Ankur Srivastava
  • 720
  • 9
  • 10
  • 1
    Well, `Integer` objects are also immutable. – MC Emperor Jul 18 '20 at 19:53
  • 1
    Pass by value does not imply immutability. Java is **always** pass by value. You don't create a new `int` and keep the previous one in the memory until GC collects it if you assign some different value to it. – Fureeish Jul 18 '20 at 19:57