String class in Java

Source: Internet
Author: User

1. Variables defined by the string class are not allowed to become

Eg:String s0= "Kvill";

string S1 = new String ("Kvill");

Why design to be immutable:

1.1 The need for a string constant pool

1.2 allows string object caching Hashcode

Hash codes for string objects in Java are used frequently, such as in containers such as HashMap.

String invariance guarantees the uniqueness of the hash code, so you can cache it with confidence. This is also a performance optimization tool, meaning that you do not have to calculate a new hash code every time. The following code is in the definition of the string class:

private int  hash; / /used to cache hashcode   

1.3 security
String is used by many Java classes (libraries) as parameters, such as the network connection address URL, file path, and the reflection mechanism required by the string parameter, if the string is not fixed, it will cause a variety of security risks.

2,String.intern ()

a method of extending a constant pool at run time.

When a string instance str calls the Intern () method, Java looks for a constant pool with the same Unicode string constant, if any, returns its reference, if not, adds a string of Unicode equal to STR in the constant pool and returns its reference

String class in Java

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.