国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
到底創(chuàng)建了幾個(gè)對(duì)象? " >到底創(chuàng)建了幾個(gè)對(duì)象?
st1和st2是否相等? " >st1和st2是否相等?
判斷一下st2和st3是否相等 " >判斷一下st2和st3是否相等
Home Java JavaInterview questions 5 String interview questions, less than 10% of people can answer them all correctly! (with answer)

5 String interview questions, less than 10% of people can answer them all correctly! (with answer)

Aug 23, 2023 pm 02:49 PM
java interview questions

This article will take a look at 5 interview questions about the Java String category. These five questions, I myself asked during the interview process I have personally experienced several questions, and this article will help you understand why the answers to these questions are what they are.

Are st1 and st2 equal?

public class Demo2_String { 
  public static void main(String[] args) {
    String st1 = "abc";
    String st2 = "abc";
    System.out.println(st1 == st2);
    System.out.println(st1.equals(st2));
  }
}

Output result:

  • First line: true
  • Second Line: true

Analysis

First look at the first print statement. In Java, the symbol == is Comparison operator, it can determine whether the basic data type and reference data type are equal. If it is a basic data type, == compares whether the values ??are equal. If it is a reference data type, ==The comparison is whether the memory addresses of the two objects are equal.

Strings do not belong to the 8 basic data types. String objects belong to reference data types. In the above, "abc" is assigned to two string objects st1 and st2 at the same time, pointing to the same address. , so the == comparison output result in the first print statement is true Then we look at the comparison of equals in the second print statement. We know that equals is a method of the parent class Object, and this equals method is rewritten in the String class.

在JDK API 1.6文檔中找到String類下的equals方法,點(diǎn)擊進(jìn)去可以看到這么一句話“將此字符串與指定的對(duì)象比較。當(dāng)且僅當(dāng)該參數(shù)不為null,并且是與此對(duì)象表示相同字符序列的String 對(duì)象時(shí),結(jié)果才為 true?!?注意這個(gè)相同字符序列,在后面介紹的比較兩個(gè)數(shù)組,列表,字典是否相等,都是這個(gè)邏輯去寫(xiě)代碼實(shí)現(xiàn)。

由于st1和st2的值都是“abc”,兩者指向同一個(gè)對(duì)象,當(dāng)前字符序列相同,所以第二行打印結(jié)果也為true。下面我們來(lái)畫(huà)一個(gè)內(nèi)存圖來(lái)表示上面的代碼,看起來(lái)更加有說(shuō)服力。

5 String interview questions, less than 10% of people can answer them all correctly! (with answer)

內(nèi)存過(guò)程大致如下:

1)運(yùn)行先編譯,然后當(dāng)前類Demo2_String.class文件加載進(jìn)入內(nèi)存的方法區(qū)

2)第二步,main方法壓入棧內(nèi)存

3)常量池創(chuàng)建一個(gè)“abc”對(duì)象,產(chǎn)生一個(gè)內(nèi)存地址

4)然后把“abc”內(nèi)存地址賦值給main方法里的成員變量st1,這個(gè)時(shí)候st1根據(jù)內(nèi)存地址,指向了常量池中的“abc”。

5)前面一篇提到,常量池有這個(gè)特點(diǎn),如果發(fā)現(xiàn)已經(jīng)存在,就不在創(chuàng)建重復(fù)的對(duì)象

6)運(yùn)行到代碼 Stringst2 =”abc”, 由于常量池存在“abc”,所以不會(huì)再創(chuàng)建,直接把“abc”內(nèi)存地址賦值給了st2

7)最后st1和st2都指向了內(nèi)存中同一個(gè)地址,所以兩者是完全相同的。

到底創(chuàng)建了幾個(gè)對(duì)象?

String st1 = new String(“abc”);

答案是:在內(nèi)存中創(chuàng)建兩個(gè)對(duì)象,一個(gè)在堆內(nèi)存,一個(gè)在常量池,堆內(nèi)存對(duì)象是常量池對(duì)象的一個(gè)拷貝副本。

分析

我們下面直接來(lái)一個(gè)內(nèi)存圖。

5 String interview questions, less than 10% of people can answer them all correctly! (with answer)

當(dāng)我們看到了new這個(gè)關(guān)鍵字,就要想到,new出來(lái)的對(duì)象都是存儲(chǔ)在堆內(nèi)存。然后我們來(lái)解釋堆中對(duì)象為什么是常量池的對(duì)象的拷貝副本。

“abc”屬于字符串,字符串屬于常量,所以應(yīng)該在常量池中創(chuàng)建,所以第一個(gè)創(chuàng)建的對(duì)象就是在常量池里的“abc”。

第二個(gè)對(duì)象在堆內(nèi)存為啥是一個(gè)拷貝的副本呢,這個(gè)就需要在JDK API 1.6找到String(String original)這個(gè)構(gòu)造方法的注釋:初始化一個(gè)新創(chuàng)建的 String 對(duì)象,使其表示一個(gè)與參數(shù)相同的字符序列;換句話說(shuō),新創(chuàng)建的字符串是該參數(shù)字符串的副本。所以,答案就出來(lái)了,兩個(gè)對(duì)象。

st1和st2是否相等?

package string;
public class Demo2_String {
   public static void main(String[] args) {
     String st1 = new String("abc");
     String st2 = "abc";
     System.out.println(st1 == st2);
     System.out.println(st1.equals(st2));
   }
}

答案:false 和 true 由于有前面兩道題目?jī)?nèi)存分析的經(jīng)驗(yàn)和理論,所以,我能快速得出上面的答案。

==比較的st1和st2對(duì)象的內(nèi)存地址,由于st1指向的是堆內(nèi)存的地址,st2看到“abc”已經(jīng)在常量池存在,就不會(huì)再新建,所以st2指向了常量池的內(nèi)存地址,所以==判斷結(jié)果輸出false,兩者不相等。

第二個(gè)equals比較,比較是兩個(gè)字符串序列是否相等,由于就一個(gè)“abc”,所以完全相等。

內(nèi)存圖如下

5 String interview questions, less than 10% of people can answer them all correctly! (with answer)

st1和st2是否相等?

public class Demo2_String { 
   public static void main(String[] args) {
     String st1 = "a" + "b" + "c";
     String st2 = "abc";
     System.out.println(st1 == st2);
     System.out.println(st1.equals(st2));
   }
}

答案是:true 和 true 分析:“a”,”b”,”c”三個(gè)本來(lái)就是字符串常量,進(jìn)行+符號(hào)拼接之后變成了“abc”,“abc”本身就是字符串常量(Java中有常量?jī)?yōu)化機(jī)制),所以常量池立馬會(huì)創(chuàng)建一個(gè)“abc”的字符串常量對(duì)象,在進(jìn)行st2=”abc”,這個(gè)時(shí)候,常量池存在“abc”,所以不再創(chuàng)建。所以,不管比較內(nèi)存地址還是比較字符串序列,都相等。

判斷一下st2和st3是否相等

public class Demo2_String {
 
   public static void main(String[] args) {
     String st1 = "ab";
     String st2 = "abc";
     String st3 = st1 + "c";
     System.out.println(st2 == st3);
     System.out.println(st2.equals(st3));
   }
}

答案:

  • false
  • true

Analysis

The first answer above is false, the second is true, and the second is true. We can understand it easily, because one of the comparisons is "abc" and the other is the spliced ??" abc", so equals comparison, this outputs true, which we understand easily.

So why the first judgment is false, we are confused. Similarly, below we use API comments and memory diagrams to explain why this is not equal.

First of all, open the introduction of String in JDK API 1.6 and find the sentence in the picture below.

5 String interview questions, less than 10% of people can answer them all correctly! (with answer)

The key point is the sentence in the red circle. We know that any data and string are subjected to the plus sign ( ) operation, and the final result is a spliced ??new string. What exactly did the number operation do? Recommended to read.

The above comments explain that the principle of this splicing is that the StringBuilder or StringBuffer class and the append method inside implement splicing, and then call toString() to convert the spliced ??object into a string object, and finally convert the resulting string object The address is assigned to the variable. Based on this understanding, let's draw a memory diagram for analysis.

5 String interview questions, less than 10% of people can answer them all correctly! (with answer)

About memory process

1) The constant pool creates the "ab" object and assigns it to st1, so st1 points to "ab"

2) The constant pool creates the "abc" object and assigns it to st2, so st2 points to "abc"

3) Due to the splicing method used here, the third step is to use the append of the StringBuffer class Method, "abc" is obtained. At this time, memory 0x0011 represents a StringBuffer object. Note that it is not a String object.

4) Call the toString method of Object to replace the StringBuffer object with a String object.

5) Assign the String object (0x0022) to st3

So, the == judgment result of st3 and st2 is not equal because the memory addresses of the two objects are different.

Summary

This interview question is completely required to master some annotations and principles in the JDK API, as well as memory graph analysis , to get the correct result. I admit that drawing the memory diagram helped me understand why the answer is like this.

The above is the detailed content of 5 String interview questions, less than 10% of people can answer them all correctly! (with answer). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276
Interviewer: Spring Aop common annotations and execution sequence Interviewer: Spring Aop common annotations and execution sequence Aug 15, 2023 pm 04:32 PM

You must know Spring, so let’s talk about the order of all notifications of Aop. How does Spring Boot or Spring Boot 2 affect the execution order of aop? Tell us about the pitfalls you encountered in AOP?

Interview with a certain group: If you encounter OOM online, how should you troubleshoot it? How to solve? What options? Interview with a certain group: If you encounter OOM online, how should you troubleshoot it? How to solve? What options? Aug 23, 2023 pm 02:34 PM

OOM means that there is a vulnerability in the program, which may be caused by the code or JVM parameter configuration. This article talks to readers about how to troubleshoot when a Java process triggers OOM.

Ele.me's written test questions seem simple, but it stumps a lot of people Ele.me's written test questions seem simple, but it stumps a lot of people Aug 24, 2023 pm 03:29 PM

Don’t underestimate the written examination questions of many companies. There are pitfalls and you can fall into them accidentally. When you encounter this kind of written test question about cycles, I suggest you think calmly and take it step by step.

Last week, I had an interview with XX Insurance and it was cool! ! ! Last week, I had an interview with XX Insurance and it was cool! ! ! Aug 25, 2023 pm 03:44 PM

Last week, a friend in the group went for an interview with Ping An Insurance. The result was a bit regretful, which is quite a pity, but I hope you don’t get discouraged. As you said, basically all the questions encountered in the interview can be solved by memorizing the interview questions. It’s solved, so please work hard!

5 String interview questions, less than 10% of people can answer them all correctly! (with answer) 5 String interview questions, less than 10% of people can answer them all correctly! (with answer) Aug 23, 2023 pm 02:49 PM

?This article will take a look at 5 interview questions about the Java String class. I have personally experienced several of these five questions during the interview process. This article will help you understand why the answers to these questions are like this.

Novices can also compete with BAT interviewers: CAS Novices can also compete with BAT interviewers: CAS Aug 24, 2023 pm 03:09 PM

The extra chapter of the Java concurrent programming series, C A S (Compare and swap), is still in an easy-to-understand style with pictures and texts, allowing readers to have a crazy conversation with the interviewer.

A question asked in almost all Java interviews: talk about the difference between ArrayList and LinkedList A question asked in almost all Java interviews: talk about the difference between ArrayList and LinkedList Jul 26, 2023 pm 03:11 PM

The data structure of Java is the focus of the interview. Anyone who has participated in a Java interview must have some experience. When interviewers ask such questions, they often want to check whether you have studied the underlying structures of commonly used data types in Java, rather than simply staying at the level of "knowing how to use".

Interviewer: Tell me about the class loading process (10 diagrams) Interviewer: Tell me about the class loading process (10 diagrams) Aug 23, 2023 pm 03:05 PM

When we want to use a class, we need to load the class into memory through ClassLoader.

See all articles