what is the advantage of string object as compared to string literal

后端 未结 6 1983
你的背包
你的背包 2021-01-02 16:59

i want to know where to use string object(in which scenario in my java code). ok i understood the diff btwn string literal and string object, but i want to know that since

6条回答
  •  温柔的废话
    2021-01-02 17:35

    literal strings are objects created in a String Pool and if they have the same value, they are referencing to the same object.

    System.out.println("abc"=="abc"); // the output is true
    

    Meanwhile, string object are real objects in memory and if they have the same value, there's no guarantee that they are referencing to the same object.

    String a = new String("abc");
    String b = new String("abc");
    System.out.println(a==b); // the output is false
    

提交回复
热议问题