local-host 2008-09-02 20:14 采纳率: 100%
浏览 817

Java 是"参考传递"还是"按值传递"?

I always thought Java was pass-by-reference.

However, I've seen a couple of blog posts (for example, this blog) that claim that it isn't.

I don't think I understand the distinction they're making.

What is the explanation?

转载于:https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value

  • 写回答

30条回答 默认 最新

  • 乱世@小熊 2012-09-14 18:22
    关注

    A few corrections to some posts.

    C does NOT support pass by reference. It is ALWAYS pass by value. C++ does support pass by reference, but is not the default and is quite dangerous.

    It doesn't matter what the value is in Java: primitive or address(roughly) of object, it is ALWAYS passed by value.

    If a Java object "behaves" like it is being passed by reference, that is a property of mutability and has absolutely nothing to do with passing mechanisms.

    I am not sure why this is so confusing, perhaps because so many Java "programmers" are not formally trained, and thus do not understand what is really going on in memory?

    评论

报告相同问题?