简体   繁体   中英

Reference or value when stored in primitive types?

I'm new with java, have been mostly programming in C/C++ in the past.

I've been practicing on lint code.com lately, and I came across some oddities in the interpretation of the same piece of code between my IDE(Android Studio) and the online judge system of lintcode.com.

So here's the code:

public class Solution{
    public int kthSmallest(int[][] matrix, int k) {
        int a = matrix[0][0];
        matrix[0][0] = 100;
        return a;
    }
}

Note that the input matrix is {{1,5,7},{3,7,8},{4,8,9}} and k is 4, in Android Studio I get 1 as the return value, on lintcode.com I get 100...

Since 'a' is a primitive type, I was expecting that it should only copy the value of matrix[0][0], but it seems to be storing it's reference instead, which behaviour is correct? :S

which behaviour is correct?

The value being stored is correct.

Primitives vs References

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM