I have a Java class with two overloaded methods: one takes an int and the other takes an Integer. I expected the code to throw a StackOverflowError because of infinite recursion, but instead, the method accepting a primitive int is called. Here is my code:
public class Teste {
public void process(int value) {
System.out.println("Processing primitive int: " + value);
}
public void process(Integer value) {
process(value);
}
public static void main(String[] args) {
Teste teste = new Teste();
teste.process(Integer.valueOf(20));
}
}
When I run this code, it outputs Processing primitive int: 20, which suggests that the method process(int value) is being called. Why is this happening instead of the code getting stuck in a recursive loop?
From my understanding, the method process(Integer value) should call itself recursively because I am passing an Integer object. How does Java resolve this method call to avoid infinite recursion?
Any insights into why the primitive method is chosen would be greatly appreciated!