I am confused about Java (constantly updating ...)

Source: Internet
Author: User

I am confused about Java (constantly updating ...)

This article mainly lists some of the issues that are frequently used in Java and that are not very important to everyone.

Will be updated constantly. Welcome to stay tuned ......

If you think it is unreasonable, you are welcome to comments. How can you be impressed if there is no spark?

(1) is Java A value transfer? Or is it passed by reference?

This problem occurs because some people say they have started to write a blog, so how does he write a blog: Is Java A value transfer? Or is it passed by reference ?, Let me see.

I thought it was quite reasonable. I immediately replied to him, "That's good ". It is reasonable, but according to his statement, what is the transfer of values?

This morning, I went to work. It was a relatively early time. Baidu had this problem, so I saw another article: Java: pass by value or pass by reference for detailed explanation, there are various types of comments in it. I still don't get a unified answer.

Next I will see another article: only passing by value in Java, not passing by reference !, And the comments corresponding to this article. I have my own opinions.

Comparing these blogs and their comments, "in Java, only the basic types and strings defined in the following way are passed by value (that is, strings are defined using double quotation marks directly: string str = "xiaobao ge"), and the rest are passed by reference." There seems to be some truth.

Once an instance object of java. lang. String is created, its character sequence cannot be changed.

String also transmits the reference copy, but because String is final, it is equivalent to passing by value.

In fact, the difference between value-based and reference-based is that "whether to copy the object's memory during transfer ", in java, the basic type is because the storage area in JVM is different from that of a common object, so it will be copied before transmission. The copied value is passed, but the object will not be copied during transmission, directly pass the "reference value" to point to the memory area of the same object heap. (However, the difference between the value-based and reference-based statement is that the correctness of "whether to copy the object's memory during transfer" remains to be verified)

Later, I said that I should not draw a conclusion by comparing several blog articles. I am very scared ......

The answer is: Is java A value transfer or a reference transfer ?. I have a different understanding.

However, according to the description of Horstmann's "java core technology" (Chinese Version 8th P115-P117), there is no reference transfer in java, the original excerpt is as follows:
"Java programming language always uses value calls. That is to say, the method obtains a copy of all parameter values. In particular, the method cannot modify the content of any parameter variables passed to it. "

"Some programmers (or even the author of this book) Think That the java programming language uses reference calls for objects. In fact, this understanding is not correct ."

I am looking at the Java Virtual Machine. I have a deep understanding of the jvm memory distribution structure. I will write a blog to explain it in detail. We also recommend that you understand the jvm memory distribution structure.

If you have carefully read other blogs posted by me, let alone the concept of value transfer and reference transfer, at least you know what kind of call and what value you get. This is already quite good.

After so much learning, I realized that it is very important to write rigorous blogs to avoid mistakes. I am also deeply aware of my lack of understanding about jvm. In addition, the reading process of some of the above materials is still quite rewarding. At least it is more rewarding than giving me a conclusion that "the java programming language always uses value calls.

Updating .......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.