Use of the String class in Java

Source: Internet
Author: User
Tags first string memory usage
String Class

1. Initialization of String objects

Because string objects are particularly common, Java provides a simplified special syntax when initializing a string object, in the following format:

String s = "abc";

s = "Java language";

In fact, according to the object-oriented standard syntax, the format should be:

string s = new String ("abc");

s = new String ("Java language");

Just according to object-oriented standard syntax, there is a big waste in memory usage. For example, string s = new String ("abc"); Two string objects are actually created, one is an "ABC" object, stored in a constant space, and one is a space that uses the new keyword as the object s application.

The parameters of other construction methods can be referred to the API documentation for the string class.

2, common operation of strings

A, Charat method

The effect of this method is to get the specified character in the string by index value (the index value of the first character in the string is 0, the second character is 1, and so on). For example:

String s = "abc";

char C = s.chatat (1);

The value of the variable c is ' B '.

B, CompareTo method

The purpose of this method is to compare the size of the two strings, which is to compare the character encoding of each character in turn. First, compare the first character of the two string, returns a value greater than 0 if the character encoding of the first string is greater than the character encoding of the second string, and returns less than 0 if less than, or 0 if the character encoding in the two string is identical.

For example:

String s = "abc";

String S1 = "Abd";

int value = S.compareto (S1);

Value is a value that is less than 0, that is, 1.

There is also a similar method comparetoignorecase in the string class, which ignores the case of characters and compares the rules with CompareTo. For example:

String s = "aBc";

String S1 = "ABC";

int value = S. Comparetoignorecase (S1);

Then value is 0, or two strings equal.

C, concat method

The purpose of this method is to concatenate strings, to concatenate two strings to form a new string. For example:

String s = "abc";

String S1 = "Def";

String s2 = s.concat (S1);

The value of the new string S2 generated after the connection is "abcdef", while the value of the string s and S1 does not change. If you need to connect multiple strings, you can use the following methods:

String s = "abc";

String S1 = "Def";

String s2 = "1234";

String s3 = S.concat (S1). Concat (S2);

The resulting new string S3 the value "abcdef1234".

In fact, in actual use, the syntax provides a simpler form, is to use "+" for string concatenation. For example:

String s = "abc" + "1234";

The value of the string s is "abc1234", which makes writing easier and more intuitive.

and use "+" to connect, not only to connect strings, but also to connect to other types. However, when a connection is required, at least one content that participates in the connection is a string type. and the "+" matching order is left-to-right, if the contents of the connection between the two are basic numeric type, then the addition operation, if the participation of the content of the connection is a string to follow the string to connect.

For example:

int a = 10;

String s = "123" + A + 5;

Then after the connection the value of the string s is "123105", the calculated procedure is to first concatenate the value of the string "123" and the variable A, generate the string "12310", and then use the string to connect with the number 5 to generate the final result.

And the following code:

int a = 10;

String s = a + 5 + "123";

Then the value of the string s is "15123" after the connection, and the calculation process is to first compute a and 5, since all are numeric, the addition or the numeric value of 15, and then use the numeric value 15 and the string "123" to connect to obtain the final result.

and the following connection code is wrong:

int a = 12;

String s = a + 5 + ' s ';

Because there is no string that participates in the connection, the computed result is a numeric value that cannot be assigned a numeric value to the string s when assigned.

D, EndsWith method

The purpose of this method is to determine whether a string ends with a string, and returns True if it ends with the corresponding string.

For example:

String s = "Student.doc";

Boolean B = S.endswith ("Doc");

The value of the variable B is true.

E, Equals method

The purpose of this method is to determine whether the contents of two string objects are the same. Returns true if the same, otherwise returns false. For example:

String s = "abc";

string S1 = new String ("abc");

Boolean B = S.equals (S1);

The use of "= =" compares the two objects in memory stored in the same address. For example, in the above code, if you judge:

Boolean b = (s = = S1);

The value of variable B is false, because the address of the S object is the address of "ABC", and S1 uses the new keyword to apply for additional memory, so the memory address is not the same as the address of "ABC" of S, so the value obtained is false.

There is a similar method equalsignorecase in the string class, which is used to ignore case comparison of two strings for the same content. For example:

String s = "abc";

String S1 = "ABC";

Boolean B = S. Equalsignorecase (S1);

The value of the variable B is true. </

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.