One of my Arrays examples is Java Arrays. One of my Arrays examples is Java Arrays. Stop gossiping and return to the truth. Let’s Talk Android!
Guys, we mentioned the String type in the introduction to basic types in Java in the previous chapter. Classes were not introduced at that time, so we will cover it in more detail today.
1. Initialization of String
A String is what we normally call a String, and it’s one of the most common classes in Java. Since it is a class, you can use it to instantiate objects. Next, we’ll show you how to instantiate a String object in code.
String str1 = "Java"; / / way
String str2 = new String("Java"); 2 / / way
Copy the code
Either way, you can define a variable of type string and assign it a value. But there are differences:
-
The first method is to associate str1 with a variable that is already in the heap memory. Before this, the variable in the heap memory has no specific name, so we call it an anonymous object. Anonymous objects can be associated with the variable STR1, and they can also be associated with other string variables.Copy the code
-
The second method is to allocate a new memory space in the heap for STR2, and associate the memory space with str2.Copy the code
To see the difference between the two approaches, here’s another code:
String str3 = "Java";
String str4 = new String("Java");
Copy the code
Comparing the above code, we can say that STR1 and STR3 are associated with the same memory space, and str2 and STR4 are associated with different memory space. Is it really so? There are guest officer questions, don’t worry, we will later through the specific code to prove. In addition to the differences, the two methods have something in common, that is, they are both strings. The variable is just a name, and the real value is in memory. We can manipulate the string in memory by manipulating the variable name. Ok, without further ado, let’s do some code to illustrate:
public class StringEx {
public static void main(String args[])
{
String str1 = "Java";
String str2 = new String("Java");
String str3 = "Java";
String str4 = new String("Java");
if(str1 == str3)
System.out.println("Str1 and Str3 use the same memory address");
else
System.out.println("Str1 and Str3 don't use the same memory address");
if(str2 == str4)
System.out.println("Str2 and Str4 use the same memory address");
else
System.out.println("Str2 and Str4 don't use the same memory address"); }}Copy the code
In the above code we use the operator “==” to determine whether the string variables use the same memory space, the following program results, please refer to:
Str1 and Str3 use the same memory address
Str2 and Str4 don't use the same memory address
Copy the code
From the results of the program, you can see the difference between the two types of string instantiation. This is the best validation of our analysis above. In addition, the “==” operator is used to determine whether two variables are equal, but it can only be used for Java variables of basic types, such as int or double. For String variables, it can only determine whether the memory address of the variables is the same, not whether the values of the variables are equal.
2. String method
Now that we’re done with String initialization, let’s look at the methods that the String class gives us to easily manipulate String variables.
public int length(a) // Returns the length of the string
public boolean equals(Object arg0) // Determine whether the contents of two strings are equal. Note the difference with the operator "=="
public boolean equalsIgnoreCase(Object arg0) // Compares two strings for equality, regardless of case
public int indexOf(String arg0) // Find the position of a string, return the position of the string if found, otherwise return -1. Note that the string position starts at 0
public String substring(int arg0) // Intercepts a string from ar0 to the end of the string
public String[] split(String arg0) // Delimits the original string based on the contents of string ar0, stores the delimited string into an array of strings, and returns the array
Copy the code
The String class provides many methods that we won’t list. There are only a few common methods listed here. For the rest you can refer to the JDK documentation. We have listed the prototype functions of common methods and their functions, and we will show you how to use these functions through concrete examples:
public class StringEx {
public static void main(String args[])
{
String str1 = "Java";
String str2 = new String("Java");
String str3 = "Java";
String str4 = new String("Java");
String str5 = "www.google.com";
String strArray[] ;
strArray = str5.split("\ \.");
System.out.println("The length of str1 is: "+str1.length());
System.out.println("str1 equals str2: "+str1.equals(str2));
System.out.println("str1 compare with str5: "+str1.equalsIgnoreCase(str5));
System.out.println("is google in str1? "+str1.indexOf("google"));
System.out.println("is google in str5? "+str5.indexOf("google"));
System.out.println("str5 substring from 4: "+str5.substring(4));
System.out.println("str5 split by '.' :");
for(int i=0; i<strArray.length; ++i) System.out.println(strArray[i]); }}Copy the code
There are two points in the above code that I need to highlight:
-
The position of the string starts at zero, which is the same as the subscript value of the array;Copy the code
-
When delimiting strings, if special strings are used to delimit them, they need to be escaped, and escaped with two "\". Such as "in the code above. That's the escape operation.Copy the code
The following is the running results of the program, please refer to the code:
The length of str1 is: 4
str1 equals str2: true
str1 compare with str5: false
is google in str1? -1
is google in str5? 4
str5 substring from 4: google.com
str5 split by '. ' :
www
google
com
Copy the code
Java: String, String, String, String, String, String