3

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

  1. If abc this string constant does not exist, then create two objects, namely abc this string constant, and new String this instance object.
  2. 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.

file

Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Please indicate the source for Mic带你学架构 !
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!

跟着Mic学架构
810 声望1.1k 粉丝

《Spring Cloud Alibaba 微服务原理与实战》、《Java并发编程深度理解及实战》作者。 咕泡教育联合创始人,12年开发架构经验,对分布式微服务、高并发领域有非常丰富的实战经验。