How many Java Strings Created?

前端 未结 2 1869
清酒与你
清酒与你 2020-12-11 10:05
public static void main(String [] args){ 
    String s = \"java\"; //line 1
    s.concat(\" SE 6\"); //line 2
    s.toLowerCase();  //line 3
    System.out.print(s);         


        
2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-11 10:14

    3 Java Strings are created.

    1. "java"  -> Goes into String constants pool // will be added if no already present
    2.  " SE 6" --> Goes into String constants pool?
    3. java SE 6 --> Goes on heap (call to concat)// Note : You are not re-assinging the value returned from concat() So s will still be "java"
    ** toLowerCase() \\ does nothing in your case since "java" is already present. toLowerCase retruns the same "java" object ( as there is no modification required to turn it into lowercase)
    

提交回复
热议问题