1
  • Integer x = 5;
  • Integer x = Integer.valueOf(5);

Is there any scenario where I would want to use the 2nd one specifically or is it redundant altogether and shouldn't not bother about it?

Amit
  • 19
  • 3

2 Answers2

1

Both statements are equivalent.

The statement Integer x = 5 would be compiled to

Integer x = Integer.valueOf(5);

The compiler will do that for you behind the scene, so the only difference is the number of character in source file.

Sleiman Jneidi
  • 22,907
  • 14
  • 56
  • 77
1

After Java 5 (because of autoboxing / unboxing) there is no difference except the first one is shorter.

Areca
  • 1,292
  • 4
  • 11
  • 21