An impressive interview question: String parameter passing

Source: Internet
Author: User

Today, I went to a well-known company in Beijing for an interview. I did some interview questions for the company, and then I had a simple interview.

During the interview, the interviewer asked me to explain how to answer one of the questions. The code is roughly as follows:

 

                  String str = "s"                                                   old = old + "-change"       }

 

 

The actual output result should be "s", while the dish mistakenly thinks "s-change" and tells the interviewer a bunch of truth.

Afterwards, I felt that I was not good at learning, and I was ashamed of myself ....

Why is the result of s? After reading the materials, although the original String exists in the form of objects, it is still considered as the basic data type, just like Integer and Double, although it is a basic type of packaging class, however, it is still considered as the basic data type.

Since it is considered as a basic data type, no matter how it is operated in the method, it will not affect the outside world.

In fact, from another perspective, we can also find that this is an error.

First, we all know that strings cannot be changed. You can simply understand them without further discussion.

When we use a string as a parameter to pass in a method, the value is passed as a pointer, which points to the real string of the heap area, so we can read this string in the method, but we can only read it. When we try to do the following:

Str = str. replace ("a", "B ");

The following str stores the original pointer, which can indeed read the original string and then perform the replacement operation. However, during the replacement operation, it is absolutely impossible to modify the original string, because the string is immutable, a new string can only be generated in the heap area, and then the address (pointer) of the new string is assigned to str.

At this time, str no longer saves the original pointer, so the read content has changed, but it does not mean that the content pointed to by the original pointer has changed.

In addition, you can add another knowledge point to the dish.

The difference between Integer I = 1; and Integer I = new Integer (1.

Integer I = 1; 1 is directly found from the constant pool, and then the address is assigned to I, which makes full use of the constant pool to save memory. Note that in addition to strings, the constant pool of other types has a range, and the out-range is invalid.

Integer I = new Integer (1); in this way, the number of writes is the number of objects created in the memory. Each object stores a number 1, therefore, this is a waste of memory and is not recommended.

The constant pool is saved in the heap.

 

The level of minor food is limited. I'm sorry for any mistakes!

 

 

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.