On the understanding and application of string in Java programming

1、 "= =" and equals ()

Run the following code, how to interpret its output?

First S0 = = S1

During Java execution, a string heap will be maintained. For some string objects that can be shared, you will first find out whether the same string content (same characters) exists in the heap. If so, you will return directly without creating a new object.

The value in S0 is the value of the referenced S1. You have not created an object, so the comparison result is true.

Similarly, the value in S2 also refers to the value of S1, so the comparison result is also true

new String("Hello")==new String("Hello")

At the same time, two new objects are created in the heap. The contents of both objects are hello,

But just like basket a and basket B both contain an apple, can basket a contain an apple and basket B contain an apple?

Of course not. If the apples are the same, the apples can be equal to the apples, but the basket is different

In Java, only one copy of string constant ("hello") with the same content is saved to save memory, so S0, S1 and S2 actually refer to the same object.

When compiling S2 a sentence, the compiler will remove the "+" sign and directly connect the two strings to get a string ("hello"). This optimization is automatically completed by the java compiler.

When you directly use the new keyword to create a string object, although the values are the same (both "hello"), they are still two independent objects.

Use of "= =" in Java

1. Basic data type: content is compared; 2 reference data type: the object address is compared;

Look at the following code

analysis:

Assigning a value to a string variable means that two variables (S1, S2) now refer to the same string object "a"!

The content of the string object is read-only. Using "+" to modify the value of S1 variable actually obtains a new string object with the content of "ab", which is different from the original S1

The referenced object has nothing to do with "a", so S1 = = S2 returns false;

The "ab" string in the code is a constant, and the string it references is independent of the "ab" object referenced by S1.

String. The equals () method compares the contents of two strings.

2、 String, equals() method

String in Java Implementation code of equals() method:

The equals () method is a method in the root class object. The source code is as follows:

Note: instanceof is a binary operator (operator) of Java and PHP, and = =, >, < are the same kind of things. Because it is composed of letters, it is also a reserved keyword of Java. Its function is to judge whether the object on the left is the instance of the class on the right and return Boolean type data. It can be used to judge whether the instance of the child class in inheritance is the implementation of the parent class.

From the above code, we can know:

(1) Equals in the string class first compares addresses. If it is a reference to the same object, it can be seen that the objects are equal and return true. (2) if there is not the same object, the equals method continues to compare the characters in two string objects one by one. Only when they are completely equal, it returns true, otherwise it returns false.

3、 Sort out length(), charat(), getchars(), replace(), touppercase(), tolowercase(), trim() and tochararray() of string class

instructions

Length(): get the string length charat(): get the character at the specified position getchars(): get the substring from the specified position and copy it into the character array replace(): substring replacement touppercase(), tolowercase(): case conversion trim(): remove the leading and trailing spaces tochararray(): convert the string object into a character array

4、 The method of string class can be called continuously:

Please read the source code of the above methods of string class in JDK, imitate its programming mode, and write a mycounter class. Its methods also support the above "cascading" call feature. The call examples are:

MyCounter counter1=new MyCounter(1); MyCounter counter2=counter1. increase(100). decrease(2). increase(3);

summary

The above is all about the understanding and application of string in Java programming. I hope it will be helpful to you. Welcome to: detailed code of sharing function of circle of friends on wechat public platform implemented by Java, sharing of usage examples of BigDecimal in Java programming, detailed explanation of Dao mode and code examples of Java, etc. if there are any deficiencies, please leave a message to point out. Thank you for your support!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>