preface
Only a bald head can be strong.
Welcome to our GitHub repository Star: github.com/ZhongFuChen…
Note a stupid operation while writing code. String immutability
First, explain the background
I have a system that provides an RPC interface to send SMS messages. External calls to my interface need to pass in parameters such as mobile phone number. I am responsible for parsing these parameters, doing some business processing, and then calling the interface of SMS channel provider to send SMS messages.
Whenever I call the interface of the SMS channel provider, I will store the record of this send (into MySQL). Similarly, the SMS channel will return the send or failed receipt to me, and I will also store it (into MySQL).
That day, someone came to find me, said a mobile phone number can not receive SMS, the user did not shield SMS (overdue, shutdown) and so on some operations, but can not receive SMS.
So I went to check. First of all, I went to the DB to find out whether there was a corresponding sending record. I found that this record existed and there was no abnormality in the DB.
- So, this excludes the case where the operation is intercepted in mid-stream (since it is already in the library, it must have called the SMS carrier’s interface).
Later, I retrieved the log to see what information the Result object returned by calling the SMS operator was, and then I asked the SMS operator why this problem might occur. The reply from the other side is: “If part of your phone number has this problem, is your phone number not trim?”
So, I went to get the log, found that the phone number behind really with a space (tied to the heart, has not been able to see before). Fixing this problem is extremely easy, I just trim the phone number in the entry.
Write code
I support sending the same message to multiple mobile phone numbers, so I use HashSet to receive the mobile phone numbers. To trim the phone number, I wrote the following code:
// Description: The Task has a key attribute, which is of type HashSet
if(task.getKey() ! =null && task.getKey().size() > 0) {
for(String s : task.getKey()) { s.trim(); }}Copy the code
The code is very simple, I did two steps:
- Check whether the value is null. If the value is not null, the mobile phone number collection is traversed
- Trim each phone number
Is there a problem with the code above? There has to be a problem. I wouldn’t write a problem.
If you call trim() on line 11 and then print it on line 12, you’ll still have Spaces.
2.1 Why does this illusion exist?
In fact, when we first learn Java, we will certainly learn the String class. It is also clear that strings are immutable, but there is always a feeling that we have changed the String object. Why?
I think the first point is this: we tend to operate on mutable objects, and when some property of an object changes, we assume it has changed. For example:
HashSet<Student> students = getStudent();
for (Student s1 : students) {
s1.setName("Java3y");
}
Copy the code
We assume that all Student names in the HashSet have been changed to Java3y, which they are.
I think the second point is this: when we operate on strings, we pass the result of the operation directly, which looks like modifying the original object. For example, something like this:
/ / to heavy
String phone = "137888888888";
sendPhone(phone.trim());
// Convert to uppercase and print
System.out.println(phone.toUpperCase());
/ /... , etc.
Copy the code
2.2 how to change
Now that you know the problem, strings are immutable, so operating on a String that “looks” like changing the original String actually generates a new String.
Back to my problem, it’s easy to solve, just set the trim phone number to HashSet
// Description: The Task has a key attribute, which is of type HashSet
HashSet<String> hs = new HashSet();
if(task.getKey() ! =null && task.getKey().size() > 0) {
for (String s : task.getKey()) {
hs.add(s.trim());
}
}
task.setKey(hs);
Copy the code
The last
This B wrote an article explaining how he “rationalized” his Bug writing. Shame on him.
Happy to export dry Java technology public number: Java3y. The public account has more than 200 original technical articles, massive video resources, beautiful brain map, attention can be obtained!
Think my article is good, give it a thumbs up!