The previous Java Basics series discussed the core concepts of Java, especially the foundation of object-oriented. In Java Advanced, I will supplement the basics of Java and move to the application level.
Most programming languages can handle strings. A string is an ordered collection of characters, such as "Hello World!". In Java, strings are stored as String class objects. Calling methods of string objects can implement string-related operations.
String class is included in java.lang package. This package will be automatically imported when Java starts, so it can be used as a built-in class. We don't need to explicitly use import to introduce the String class.
Create string
We previously used classes to create objects. It should be noted that the new keyword is not required to create a String class object. for example:
Copy the code code as follows:
public class Test
{
public static void main(String[] args)
{
String s = "Hello World!";
System.out.println(s);
}
}
In fact, when you write a "Hello World" expression, the object is already created in memory. If you use new String("Hello World!"), a string object will be created repeatedly.
An Object
The String class is the only class that does not require the new keyword to create objects. Need to pay attention when using it.
String operations
You can use + to concatenate strings, for example:
Copy the code code as follows:
"abc" + s
String operations are mostly implemented through the corresponding methods of strings, such as the following methods:
Copy the code code as follows:
Method effect
s.length() returns the length of s string
s.charAt(2) returns the character with subscript 2 in the s string
s.substring(0, 4) returns the substring with subscripts 0 to 4 in the s string.
s.indexOf("Hello") returns the index of the substring "Hello"
s.startsWith(" ") determines whether s starts with a space
s.endsWith("oo") determines whether s ends with "oo"
s.equals("Good World!") determines whether s is equal to "Good World!"
== can only determine whether the string is saved in the same location. You need to use equals() to determine whether the contents of the strings are the same.
s.compareTo("Hello Nerd!") compares the s string with the order of "Hello Nerd!" in the dictionary,
Returns an integer. If <0, it means s is before "Hello Nerd!";
If >0, it means that s is after "Hello Nerd!";
If ==0, it means that s is equal to "Hello Nerd!".
s.trim() removes the space string before and after s and returns a new string
s.toUpperCase() converts s to uppercase letters and returns a new string
s.toLowerCase() converts s to lowercase and returns a new string
s.replace("World", "Universe") replaces "World" with "Universe" and returns a new string
immutable object
String class objects are immutable objects. Programmers cannot modify existing immutable objects. We can also create immutable objects ourselves, as long as the interface does not provide methods for modifying data.
However, String class objects do have functions for editing strings, such as replace(). These editing functions are implemented by creating a new object rather than modifying the original object. for example:
Copy the code code as follows:
s = s.replace("World", "Universe");
The call to s.replace() on the right creates a new string "Hello Universe!" and returns (a reference to) that object. By assignment, the reference s will point to the new string. If there are no other references to the original string "Hello World!", the original string object will be garbage collected.
immutable objects
Java API
Java provides many powerful packages. An important aspect of Java learning is understanding these packages and the APIs (Application Programming Interface) contained in them. The String class is defined in java.lang.String. You can query the following Oracle website to find the official documentation of this class:
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html
This document contains the most comprehensive introduction to the String class.
In fact, there is a wealth of content in the API documentation, which you can get an overview at the link below:
http://docs.oracle.com/javase/6/docs/api/