Java Learning String Class

Source: Internet
Author: User

String is a class type (non-primary type) in Java, a final class that cannot be inherited, and a string object is an immutable object. The declared string object should be assigned to the heap, and the declared variable name should hold a reference to the string object.

The run-time computed string gets a new string. tag= "Hel" +TAG;

This behavior is the particularity of Java's string for efficiency. The string is immutable, and the value cannot be changed after the string is allocated space and initial value. Once changed, the original object is discarded and the new space is allocated to the changed string. Frequent assignment of new values can have a significant impact on the efficiency of the program's operation.

using = = is not recommended to compare object values of non-primary types. Because it usually does not get the desired result, it can be said to be incorrect. Because the = = symbol compares the value of a variable, and the value of the variable is not an object, it is the first address of the object, so = = Cannot compare the object.

But string sometimes uses = = to get the right result, because Java, in order to improve the efficiency and utilization of this common type, can use reusable string constants in a pool, which is only possible to reuse.

1. String pool

When a name= "Hello" is defined, Java first looks for the string "Hello" in the string pool, and if not, establishes the string "Hello" object, and then the variable name points to the address.

It then defines a new string that, if determined at compile time, automatically points to an existing object in the string pool, but if it is computed at run time, it allocates new space to the object. The computed string does not know about any objects in the pool, but you can use the Intern method to point to objects in the string pool. Pointing to the same string object in the pool, using "= =" to compare is sure to return true. The hashcode of a string in the pool is unique.

Attentions: (1) The string object created with the new operation is not in the pool.

(2) The computed string is not in the pool.

(3) Only objects determined by the compilation period are placed directly into the pool or directed to the pool object.

(4) When you compare strings using the = = Action symbol, two strings are equal when the must and Hashcode values are equal.

(5) Intern only finds matching objects in the string pool, and automatically puts itself in the pool if it is not found.

2.equals and Hashcode

The following conventions are followed when overriding equals: reflexive, symmetric, transitive, consistent

The following conventions are observed when overriding hashcode: (1) Equal objects, hashcode must be equal, (2) unequal objects, hashcode not necessarily unequal, (3) hashcode are equal, not necessarily objects equal, and (4) hashcode are unequal, and objects must not be equal.

Java Learning String Class

Related Article

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.