Search code examples
rrjava

why use the operator `$` is very time consuming than use `.jcall` expression


I knew two type of expression to call java interface in R.for example there is a java function definition public void add(int a,int b){} in class Test


library(rJava)
.jinit()
.jaddClassPath(dir( "lib", full.names=T ))

Test = J('...Test')
test = new(Test)
a = as.integer(3)
b = as.integer(4)
.jcall(test,'V','add',a,b) #first type of expression
test$add(a,b) #second type of expression

I tested tow kind of expressions, the first type is much more efficient than the second.I want to know the details of reason. thanks so much.


Solution

  • According to rjava site:

    This interface uses Java reflection API to find the correct method so it is much slower and may not be right (works for simple examples but may not for more complex ones).

    So it is slower because of Reflection.