Home > Back-end >  String
String

Time:09-23

String s1="a"; String s2="b"; String s3=s1 + s2; A total of create a few objects? S3 "ab" exists in the heap? Constant pool will have a "ab"? Four objects created in total?

CodePudding user response:

Constant pool "ab", only "a" and "b"
Create and you here refers to when creating? If refers to the time to create the code is executed, the only s3 (strictly speaking also created the StringBuilder), because the "a" and "b" in the class loading has been created and put into the constant pool, code just let the object of the reference variable constant pool, will not create objects,
These you can decompiling, look at pseudo code instructions
Javap -c - v yourclass. Class