It really comes from my unfamiliarity with PHP. I've written the function in java and re-implemented it in PHP as I didn't expect to have java on the server. It was a pleasant surprise to find it!
The thing is, to a certain extent I know how java works. I can say how much memory my data structures will take up, roughly what time-complexity etc etc. I don't know PHP well enough to be able to compare my two implementations. The fact is that one's a programming language and one's a scripting language and each have their strengths and weaknesses. I happen to believe that tight loops and recursion are slightly better in java.
I suppose the only answer is test it and see! I agree, starting and stopping a jvm every query is expensive. But perhaps so is PHP in its own way? I've also worked with Rails and found it to be great for all kinds of exciting things. But chewing arrays...
FIY support said this about executing a java program from php with exec():
" As long as it's not persistent and it doesn't eat up too much of the
server's CPU/RAM then it's cool. "
Thanks for your input!