A fan who has worked for 6 years told me,
Interviews are getting more and more difficult recently. Basically, they ask about the underlying principles of technology, and some even ask about operating system level knowledge.
I said that there are many excellent programmers graduated from various first-tier factories, and the demand for programmers is also decreasing due to the bad market environment.
If the technical foundation is not good, it will indeed be difficult to find a job.
The question shared today is: "How many objects does new String("abc") create?
On this question, look at the answers of ordinary people and experts.
Ordinary people:
"How many objects does new String("abc") create?
I think there are two objects one is the new Sting itself and the other is the string "abc"
Expert:
OK, interviewer.
First of all, there is a new
keyword in this code. This keyword is a string object instantiated in the heap memory according to the loaded system class String when the program is running.
Then, in the construction method of this String, an "abc" string is passed, because the string member variable in String is final modified, so it is a string constant.
Next, the JVM will take the literal "abc" into the string constant pool to try to get its corresponding String object reference. If it can't get it, it will create a "abc" String object in the heap memory.
And save the reference to the string constant pool.
If there is a definition of the literal "abc" in the future, because the reference of the literal "abc" already exists in the string constant pool, it is only necessary to obtain the corresponding reference from the constant pool, and there is no need to create it again.
So, for this question, I think the answer is
- If
abc
this string constant does not exist, then create two objects, namelyabc
this string constant, andnew String
this instance object. - If
abc
this string constant exists, only one object will be created
Summarize
As you can see from the expert's answer, you must have a deep enough understanding of the runtime memory division in the JVM and the JVM constant pool.
Now technical interviews are also biased towards systematic inspections, and are no longer point-based questions.
Friends who like it remember to like and favorite.
If you have any work and study questions, you can send me a private message at any time.
Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Please indicate the source forMic带你学架构
!
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。