Java local variable final

Source: Internet
Author: User

Local variables and formal parameters are final.

To open another thread B in a thread A, if thread B wants to use thread A's local variables, then a's local variable needs to be defined as final. Reason: Local variables are shared within threads, and each thread cannot access local variables of other threads, but the appeal is contrary to this principle, so why not add final? The reason is that with final, when the B thread is created, the final tag variable is passed as a parameter of thread B's constructor method to B, which solves this problem, which is a clever approach, which can be seen through the class file anti-compilation.

In addition, Java's string variable is special, and the values of the variables he defines are all stored in the constant pool, whether final or not. The following results are all true. And if the equality points to the same address.

Local variables of the base type also point to the same address if the values are the same. Only the back and the front do not want to wait for the time to point to the new address, which is why the basic local variables do not affect each other for reasons.

public class Stringtest {private static string S1 = "123"; static final string s2 = "123";p ublic static void Main (string[] args) {string s3 = "123"; final String s4 = "123"; System.out.println (S1 = = s2); System.out.println (s3 = = S4); System.out.println (S1 = = S3);}}

  

Java local variable final

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.