When are Lines created or destroyed constant?

Consider the following code

public static void method(String[] srgs){  
try{  

}catch(){  
System.out.println("Hello World" + "one");}  
catch(..){  
System.out.println("Hello World" + "two");}
catch(..){  
System.out.println(getString());}
}
  • When are these lines created? I assume that Lines will be created when an Exception occurs at runtime. A string is created at runtime and displayed. My peer tells me that since they are constant Strings, they will be created as soon as the class loads. It is right?

  • When are Strings dumpsters collected? Do they collect garbage at all? Assuming the same method can be called many times during the life cycle of a program, does it make sense to just cache them?

+5
source share
1 answer
  • String , , JVM . , +, , (.. , "Hello Worldone" "Hello Worldtwo"). ยง 3.10.5 JLS.

  • String - , , - , . String ( ), ( ).

    , , String- .

+6

All Articles