Compile and run time analysis of String class

Source: Internet
Author: User

add a string to a string - What are the special places when strings are stitched together?

at compile time and run time this What is the character of a string for 2 periods?

1, String s = "S1";

String S1 = "S" +1;

System.out.println (S=S1);

Run Result: true

Compile period, you can determine s 1 -- because s 1 is also a constant. At compile time, s 1 Span style= "font-family: ' Times New Roman '" >1 .

so, when output, s 1 object is not created, directly pointing to the s refers to this object.


2,String s = "S1";

int SS = 1;

String S1 = "S" +SS;

System.out.println (S=S1);

Run Result: false

S and SS The value can be determined at compile time, but s 1 The value cannot be determined.

for s 1 this statement, ss -- The variable must not be determined at run time until its value is run to ss 1 The value is not determined until after the run time.

Conclusion: Can determine its value at compile time, "= "; The compilation period cannot be determined by its value, not" = ".

3,String c = "C1";

final int cc = 1;

String C1 = "C" +CC;

System.out.println (C=C1);

Run Result: true

at compile time,c,cc,c1 CC final cc this variable is 1 to replace it.

4, String d = "D1";

Final int dd = GETDD ();

String D1 = "D" +DD;

System.out.println (D=D1);

public static int GETDD () {

return 1;

}

Run Result:false

"=" is compared to "memory address" for an object.

AlthoughDD is final before theexecution of this sentence, the method can have a return value, that is, the runtime to determine its value, so the compilation period is not determined by the DD value of the.







Compile and run time analysis of String class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.