Reputation: 11
I recently started to learn Groovy and found not a natural behavior.
class BasicRouter {
int method_(first){
return 1;
}
int method_(int first){
return 2;
}
int method_(short second){
return 3;
}
int method_(Integer first){
return 4;
}
}
br = new BasicRouter()
int x = 1
assert br.method_(x) == 4
assert br.method_((int)29) == 2
Why in the first case we are passing a variable of type int, we get 4 and not 2?
I expect that method int method_(int)
will be called.
Thanks.
Upvotes: 1
Views: 429
Reputation: 76
The answer is here - http://docs.groovy-lang.org/latest/html/documentation/core-differences-java.html#_primitives_and_wrappers
Groovy uses objects for everything
This changes if you use CompileStatic
@groovy.transform.CompileStatic
def fInt(int x) {new BasicRouter().method_(x)}
assert fInt(1) == 2
Upvotes: 4