My understanding of java String and source code analysis, javastring

Source: Internet
Author: User

My understanding of java String and source code analysis, javastring

Abstract: Original Source: http://www.cnblogs.com/Alandre/ sediment tile pulp carpenter hope to reprint, retain abstract, thank you!

Get up every day and tell yourself that your goal is "technology + English and Life"! -Sand, brick, and tile pulp carpenter

1. char is a String

Ii. String recognition and source code analysis

1. char is a String

This is the second time I went back to chew on the Basic java book. I think that I am ignorant. If you have a good education, you can comment on your private message. My views are as follows:
Why char? I want to sell a token first. In java, char is unicode encoded and occupies 16 bits (2 bytes ). From ansi encoding (1 byte) to unicode encoding (2 bytes ). The Unicode is used in Java Because Java's Applet (web page) runs and Unicode contains the widest range of features, such as Chinese, English, Spanish, German, and French. Therefore, char is one of the basic types of java and is easy to use.

Note: in java, a unit of code that the char type describes with UTF-16 encoding. Ii. String recognition and source code analysis

Here I am going to answer the question: why is String closely related to char. Because the java String is a char value sequence (actually a final char array. This is proven in the source code:

/** The value is used for character storage. */    private final char value[];
To prove the above, you can write a piece of interesting code:
public class Test{    \u0070\u0075\u0062\u006C\u0069\u0063 static void main(String\u005B\u005D args)    {        System.out.println("this is not PI: \u03C0");    }}

After running, you can see the following output:

this is not PI: π

 

But some people should not misunderstand, such as java's

String name = "Jeff Li";

Think it is like char name [] = "Jeff Li" in C language; this cognition is wrong, others are more like char * pointer:

char *name = "Jeff Li";

 

The most common is the most frequently used boolean equals () analysis. The source code is as follows:

public boolean equals(Object anObject) {        if (this == anObject) {            return true;        }        if (anObject instanceof String) {            String anotherString = (String) anObject;            int n = value.length;            if (n == anotherString.value.length) {                char v1[] = value;                char v2[] = anotherString.value;                int i = 0;                while (n-- != 0) {                    if (v1[i] != v2[i])                            return false;                    i++;                }                return true;            }        }        return false;    }

From the source code: Example (A. equals (B ))
First: if (this = anObject)

// This indicates A. In fact, it is used to determine whether the B object and the referenced variable of A indicate the addresses of the two variables stored in the heap (that is, the stack reference content is naturally the same ).

Second: if (anObject instanceof String)

// In javaInstanceofAn operator is used to indicate at runtime whether an object is an instance of a specific class. This sentence is strongly converted to String anotherString = (String) anObject below;
Third: At the beginning, I was confused about the source code: I think the String type anotherString gets the char value sequence (char []), which is back to the previous content.

anotherString.value

I guess it means the above. If you can say something about Daniel, please. Because when I want to implement a simple StringCopy, this will never work and I don't know why.

Fourth: the rest is char [] direct loop comparison.

 

Some common split (regular) formats are not described here.

 

There is also a good code sharing: From org. apache. commons. lang

public static boolean isEmpty(String str) {        return str == null || str.length() == 0;    }
Iii. Thank you for your knowledge sources and summary.

String is a frequently used thing. Let's look at your own opinions.

1. char is a String

Ii. String recognition and source code analysis

References:

Http://elmer08.blog.163.com/blog/static/71204254201011121382438/

Http://blog.csdn.net/sunzhenhua0608/article/details/7628663

Http://blog.csdn.net/f562867448/article/details/8679550


Java source code String class problems

1. count is the length of the input parameter (that is, original). You can see the comment in src:
/** The count is the number of characters in the String .*/
So at the beginning, it was not 0, but original. length ()

2. count is a private member, which means that it can only be used within the class. Therefore, the internal function of other String classes can be directly accessed within the String class.

Java source code: I want to know the definition of String and Set. Where can I find the source code?

Find your JDK installation cookbook with a src.zip (same as the bin folder). decompress the package to obtain the source file.
Set. java is in the decompressed Java/lang/util folder.

String. java is in the decompressed Java/lang folder.

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.