0

I found the following code in transform method of class PerspectiveTransform in JAI library:

public void transform(double[] srcPts, int srcOff,
              double[] dstPts, int dstOff,
              int numPts) {

        if ( srcPts == null ) {
            throw new IllegalArgumentException(JaiI18N.getString("Generic0"));
        }

        if (dstPts == null) {
            dstPts = new double[numPts * 2 + dstOff];
        }

        while (numPts-- > 0) {
            double x = srcPts[srcOff++];
            double y = srcPts[srcOff++];
            double w = m20 * x + m21 * y + m22;

            if (w == 0) {
                dstPts[dstOff++] = x;
                dstPts[dstOff++] = y;
            } else {
                dstPts[dstOff++] = (m00 * x + m01 * y + m02) / w;
                dstPts[dstOff++] = (m10 * x + m11 * y + m12) / w;
            }
        }
    }

Looks like if dstPts is null, then new array is created. But this array won't return outside, isn't it?

1
  • Yep, you are right, java pass by value. Commented Nov 14, 2013 at 18:56

3 Answers 3

3

Just a note: Java is always pass by value. The little line at the bottom is that when you pass an object as a parameter, its reference is the value that is actually passed.

For example, C# has the ref keyword, which allows the passing of a reference. In Java, you have to wrap your object in a sort of container and work with the container, so you can switch the underlying element freely.

As you say, the new array won't be accessible from the outside unless it is returned. I understand that was done to prevent the following while from throwing a NPE, but the processing result is lost as the method is actually coded.

Sign up to request clarification or add additional context in comments.

9 Comments

Java is always pass by value. Exactly. No ifs or buts.
"is that when you pass an object as a parameter" The thing is, you cannot "pass an object as a parameter" in Java. Objects are not values in Java. The only types in Java are primitive types and reference types. Therefore, each parameter must be of a primitive type or reference type. There is no "object type".
@newacct "The little line at the bottom is that when you pass an object as a parameter, its reference is the value that is actually passed." I don't know if you were clarifying or correcting me, but your quote seems out of context.
@Gamb: The fact that you are saying "pass an object" is itself wrong because you cannot pass something that is not a value.
@newacct Again, you're missing the context. The line serves as a clarification, not an affirmation.
|
2

You're right, if that is the actual code. I imagine people aren't passing nulls as parameters when they're using the method, but even so that's a bug.

Comments

0

Arrays are Objects, and their reference is passed by value.

3 Comments

You gave a right answer but put the most important part last.
Yes. But it's a copy of the original reference. So you can't do dstPts = new double[numPts * 2 + dstOff]; in a method and expect it to have any effect outside. So you're giving a mostly correct but misleading answer.
Objects cannot be passed, period.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.