Error Revision: SCJP Certification Spectay Analysis 2 Question

zhaozj2021-02-08  244

40:

String s = "hello";

String t = "hello";

CHAR C [] = {'h', 'e', ​​'l', 'L', 'o'};

Which returnes?

A. S.Equals (T);

B. T. Equals (C);

C. s == T;

D. T. Equals (New String ("Hello");

E. T == C.

(ACD)

Title: What are you returning to true.

This is discussed in the discussion of the equals () method and == operator in the previous item 10. == The operator is compared to whether the operand number is the same object, and the String's equals () method is compared to whether the contents of the two String objects are the same, and the parameters are like a String object, it is possible to return TRUE Other objects return false. It is to be pointed out because S and T are not created by NEW, they point to the same string in the memory pool, so its address is actually the same (this can be obtained from the result of referring to a simple test program, limited to The space does not list the test code and the anti-compilation analysis, so the answer C is also correct.

(The article originally leaked the answer C, now add and modify the analysis), the following is the test code and anti-compilation analysis:

Public Class Tests

{

Public static void main (string [] args)

{

String s = "hello";

String t = "hello";

IF (S. Equals (T)) {

System.out.println ("Hello World!");

}

}

}

Reflexible key extract:

Method void main (java.lang.string [])

0 LDC # 2

2 ASTORE_1

3 LDC # 2

5 ASTORE_2

6 ALOAD_1

From two of the marks 0 and 3, it can be seen that the memory address of S and T is the same, and the program operation results also prove this conclusion.

转载请注明原文地址:https://www.9cbs.com/read-1965.html

New Post(0)