Seventh Day of Java learning

Source: Internet
Author: User

Today is 2016, May 10, Java study Seventh Day!

One, P104 String class

Two different ways to instantiate objects

1. The string class object is instantiated in the form of direct assignment strings;

such as: String str= "Hello",

2, the String class is constructed to instantiate an object of the string class, the string class is constructed as: public string (String str)

such as: String str =new string ("string")

Comparison of strings

What is the difference between "= =" and "Equals ()" In the String class?

1, = =: The comparison is two string memory address of the value is equal, is a numerical comparison;

2. Equals (): Compares the contents of a two string, which is a content comparison.

Note: The string constant itself is an anonymous object of a string class. What is an anonymous object? As already mentioned, an anonymous object is an anonymous object that refers to the heap memory space that no stack memory points to.

Here is also a question about compiler optimizations, such as String s= "AA" + "BB" + "CC" In our view is occupied 3 heap memory space, but after the compiler optimization, it becomes a string s= "AABBCC", which will save space, This is also the magic of the high-level Java language.

The difference between the two instantiation methods of the String class

Direct assignment: String str= "Hello";

If string str1= "Hello";

String str2= "Hello";

String str3= "Hello";

So STR1==STR2==STR3;

The same is true for strings in direct assignment operations: Direct assignment puts the contents of the string into the pool for use by other string objects that continue to use the direct assignment, and if the newly declared string content is no longer in the pool, it will open a new one and continue into the pool for the next use. That is, the above 3 string class, str1 str2 str3 points to a heap of memory, this heap memory is "Hello".

Constructor method Assignment: String Str=new string ("Hello")

Because each string is an anonymous object of the string class, it first opens up a space in the heap memory to hold the string "Hello", and then uses the keyword new to open up another heap of memory space, but the real use of the keyword new Open heap memory, The heap memory space of the previously defined string constants will not have any stack memory pointing, it will become garbage, waiting to be reclaimed by GC. So when you use a constructor method to assign a value, you actually create an instantiated object of two string classes.

By comparison, the instantiation of a string object is always done in the form of direct assignment in all future developments.

Second, the common method of P112 string class.

This is mainly through the Java Doc document to find all the methods defined in the String class, and learn.

Test1:

Code:

Package com.cqvie.test;

public class Test1 {

public static void Main (string[] args) {
String s= "How is You";
int T=s.length ();
System.out.println (t);

}

}

Output:

11

This is a string class that evaluates the length of the string s by means of the public int length ().

Test2:

Code:

Package com.cqvie.test;

public class Test2 {

public static void Main (string[] args) {
String t= "The weather is fine today";
int Y=t.lastindexof ("weather");
System.out.println (y);

}
}

Output:

2

This is a public int lastIndexOf (string str), which represents the position of the string from the back forward and cannot find the return-1.

TEST3:

Code:

Package com.cqvie.test;
Finds the position of a string backward from the specified location
Public abstract class Test3 {

public static void Main (string[] args) {
TODO auto-generated Method Stub
String t= "Yesterday the day After Tomorrow";
int S=t.indexof ("Day", 0);
int R=t.indexof ("Day", s+1);
System.out.println (s);
System.out.println (R);
}
}

Output:

1
3

The method name is: public int indexOf (string str,int fromIndex) locates the string position backwards from the specified location and cannot find the return =-1.

TEST4:

Code:

Package com.cqvie.test;

public class Test4 {

public static void Main (string[] args) {
TODO auto-generated Method Stub
String s= "The affairs of the Family matter everything cares about";
int n=5;
int t=-1;
for (int i=0;i<n;i++) {
T=s.indexof ("The Thing", t+1);
}
System.out.println (t);
}
}

Output:

8

This method is the same as the previous method, which is to find the position of the string backwards from the specified location. The question is not the same, find the position of nth string in S, such as the subject, find the fifth "thing" in the position of S in 8.

It is important to note that the initial value of T should be-1, so that it is searched from the first character.

Seventh Day of Java learning

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.