Getting started with JavaSE 28: String class of common Java classes (below)

Source: Internet
Author: User

Getting started with JavaSE 28: String class of common Java classes (below)
Immutability of seven strings

After a String object is created, it cannot be modified. It cannot be changed. The so-called modification actually creates a new object and points to a different memory space. For example

As shown in:

Running result:

In combination with the above Code, you must know about strings:

1) A String object is declared through String s1 = ""; s1 stores the reference of the String object, and the reference relationship in the memory is as follows:

As shown in:

Then, use s1 = "Welcome to:" + s1; changed string s1. The essence is to create a new String object, and variable s1 points to the new character.

String object, as shown in:

2) Once a string is created in memory, the string cannot be changed. If you need a variable string, we can use

StringBuffer class or StringBuilder class.

3) each time a new string is added, a new object is generated. Even if the content of the two strings is the same, it is "false" when "=" is used for comparison, as shown in

If you only need to compare whether the content is the same, you should use the "equals ()" method. Both s3 and s4 use the new object created in the heap memory and have their own addresses,

And = is the address, so it is false.

Instance:

Public class Test {public static void main (String [] args) {String s1 = "imooc"; String s2 = "imooc"; // defines the String s3, save the content String s3 = "I love" + s1 after splicing "I love" and s1; // compare the s1 and s2 strings as constant strings. // imooc, when it appears multiple times, it will be optimized by the compiler, and only one object is created. out. println ("is the memory address of s1 and s2 the same? "+ (S1 = s2); // compare the s1 and s3 System. out. println strings (" is the s1 and s3 memory addresses the same? "+ (S1 = s3); String s4 =" I love "+ s1; // compare strings s4 and s3 // s1 are variables, s4 only knows the specific value at runtime, so s3 and s4 are different objects. out. println ("is the s3 and s4 memory address the same? "+ (S4 = s3 ));}}

 

Compile the running result:

Explanation:

S1 stores the string in the constant pool, so its address is given by the constant pool. S2 is also the address given by the constant pool, and the address is always a string.

The address of "imooc", so the address of s1 and s2 is the same. However, s3 is a constant with a variable, so it exists in the heap memory. This address

The heap memory is new to s3. Similarly, the s4 address is also provided by heap memory. Therefore, s3 and s4 have different memory addresses.

Common Methods of the eight String type

The String class provides many methods to process strings, such as obtaining String lengths, intercepting strings, and converting strings into uppercase letters.

Or lower case, string segmentation, etc.

Common method 1 of the String class:

 

Use the code to familiarize yourself with the usage of the method:

 

 

Public class Test {public static void main (String [] args) {String s1 = "sun java"; String s2 = "sun Java"; System. out. println (s1.charAt (1); // u System. out. println (s2.length (); // 8 System. out. println (s1.indexOf ("java"); // 4 System. out. println (s1.indexOf ("Java"); //-1 System. out. println (s1.equals (s2); // false System. out. println (s1.20.signorecase (s2); // true String s = "I'm a programmer, I'm learning java"; String str = s. replace ('I', 'you'); System. out. println (str );}}
Running result:

 

Common method 2 of the String type:

Instance code:

 

public class Test{    public static void main(String[] args){        String s = "Welcome to Java World!";        String s1 = " sun java ";         System.out.println(s.startsWith("Welcome"));//true        System.out.println(s.endsWith("World"));//false        String sL = s.toLowerCase();        String sU = s.toUpperCase();        System.out.println(sL);        System.out.println(sU);        String subs = s.substring(11);        System.out.println(subs);        String sp = s1.trim();        System.out.println(sp);    }}  

 

Running result:

 

 

Note:

1) The index of characters in string s2 starts from 0 and ranges from str. length () to 1.

2) When indexOf is used for character or string search, if a match is returned, the location index is returned. If no match is returned,-1 is returned.

3) when using substring (beginIndex, endIndex) for string truncation, including the beginIndex position, excluding the endIndex position

Character.

Common method 3 for the String type:

Static overload method

Public static String valueOf (...) can be converted to a String of the basic type.

For example:

 

public static String valueOf(double d)public static String valueOf(int i)

 

The public String [] split (String regex) method can split a String according to the specified delimiter and return the split String array.

Instance code:

 

Public class Test {public static void main (String [] args) {int j = 1234567; String sNumber = String. valueOf (j); System. out. println ("j is" + sNumber. length () + "digits"); String s = "Mary, F, 1976"; String [] split = s. split (","); for (int I = 0; I
 
  

 

Running result:

Similarities and differences between "=" and equals () in the String class

1) =: determines whether the first address of two strings in the memory is the same, that is, whether it is the same string object.

2) equals (): Compares whether the content stored in two string objects is consistent.

The usage of these two methods has been used in many places. Here we will not detail the similarities and differences between them.

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.