|NO.Z.00018|——————————|^^ 笔试 ^^|——|Java&核心类库.V03|——|Java.v03|string类.v03|笔试考点|

一、string笔试考点
package com.yanqi.task12;

public class StringExamTest {

    public static void main(String[] args) {

        // 1.请问下面的代码会创建几个对象?分别存放在什么地方?
        //String str1 = "hello";  // 1个对象  存放在常量池中
        //String str1 = new String("helo"); // 2个对象  1个在常量池中,1个在堆区

        // 2.常量池和堆区对象的比较
        String str1 = "hello";  // 常量池
        String str2 = "hello";  // 常量池
        String str3 = new String("hello"); // 堆区
        String str4 = new String("hello"); // 堆区

        System.out.println(str1 == str2);       // 比较地址  true
        System.out.println(str1.equals(str2));  // 比较内容  true
        System.out.println(str3 == str4);       // 比较地址  false
        System.out.println(str3.equals(str4));  // 比较内容  true
        System.out.println(str2 == str4);       // 比较地址  false
        System.out.println(str2.equals(str4));  // 比较内容 true

        System.out.println("------------------------------------------------------------");
        // 3.常量有优化机制,变量没有
        String str5 = "abcd";
        String str6 = "ab" + "cd";  // 常量优化机制  "abcd"
        System.out.println(str5 == str6); // 比较地址  true

        String str7 = "ab";
        String str8 = str7 + "cd"; // 没有常量优化
        System.out.println(str5 == str8); // 比较地址 false


    }
}
二、编译打印
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=51638:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath E:\NO.Z.10000——javaproject\NO.H.00001.javase\javase\out\production\javase com.yanqi.task12.StringExamTest
true
true
false
true
false
true
------------------------------------------------------------
true
false

Process finished with exit code 0

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on 2022-04-04 14:44  yanqi_vip  阅读(6)  评论(0)    收藏  举报

导航