Is Java “pass-by-reference” or “pass-by-value” ?

Java (9 Part Series)

1 What is reflection in Java and why is it useful ?
2 Is Java “pass-by-reference” or “pass-by-value” ?
5 more parts…
3 How NullPointerException can be avoided in Java
4 What are the differences between a HashMap and a hash table in Java ?
5 Why is char[] preferred over String for passwords in Java ?
6 When to use LinkedList over ArrayList in Java ?
7 How do I convert a String to an int in Java ?
8 What are sealed classes in Java ?
9 10 Java Frameworks & Libraries

Java is a “pass-by-value” language, which means that when an argument is passed to a method, the value of the argument is copied and passed to the method, rather than a reference to the original object. This means that any changes made to the argument within the method have no effect on the original object outside of the method.

In contrast, a “pass-by-reference” language would pass a reference to the original object, rather than a copy of the value, so any changes made to the object within the method would be reflected in the original object outside of the method.

The “pass-by-value” behavior of Java can be counterintuitive at times, especially for programmers who are accustomed to languages that use “pass-by-reference.” However, it is a fundamental aspect of the language and is important to understand in order to write correct and effective Java code.

Java (9 Part Series)

1 What is reflection in Java and why is it useful ?
2 Is Java “pass-by-reference” or “pass-by-value” ?
5 more parts…
3 How NullPointerException can be avoided in Java
4 What are the differences between a HashMap and a hash table in Java ?
5 Why is char[] preferred over String for passwords in Java ?
6 When to use LinkedList over ArrayList in Java ?
7 How do I convert a String to an int in Java ?
8 What are sealed classes in Java ?
9 10 Java Frameworks & Libraries

原文链接:Is Java “pass-by-reference” or “pass-by-value” ?

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容