Forum Stats

  • 3,722,529 Users
  • 2,244,331 Discussions


Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Autoboxing int Integer

2934488 Member Posts: 8


I am preparing for the exam.

I need help to understand why in this code the int method is chosen, I have tested only with Integer and that one is also executed:

public class Teststuff {

            public static void doSum(Integer x, Integer y){

          System.out.println("Integer sum is "+(x+y));



      public static void doSum(int x, int y){

          System.out.println("int sum is "+(x+y));




    public static void main(String[] args) {

        doSum(10, 20);



Thank you!


  • jwenting
    jwenting Member Posts: 4,863
    edited January 2020

    Just try to reason which method would be the closest match, and the answer becomes quite obvious.

    There is of course a potential for ambiguity, and that's if you try to call for example doSum(10, Integer.valueOf(20));

    I've not tried what would happen in that case, I'd not be surprised if it'd lead to a compiler error.

Sign In or Register to comment.