The regression of X on Y and the regression of Y on X are different procedures. Your Excel code is treating A1:A50 as the criterion (Y axis) and B1:B50 as the predictor (X axis). Your Java code is treating stockReturn as X and marketReturn as Y. Is this what you intended?

In other words, these should produce the same answer if stockReturn is in B1:B50 and marketReturn is in A1:A50. If that's not true, you will need to reverse one of them.