Remember once foolish experience --String immutability

Foreword

Only the head can become strong.

Text has been included to my GitHub repository, welcome Star: https://github.com/ZhongFuCheng3y/3y

Once recording when writing code that stupid operation, this involved knowledge: String immutability

One, change the background

I have here a system that provides a RPC interface to send text messages. I need to call external interface of the incoming phone number, and so on parameters and I am responsible for resolving these parameters, do some business deal and then call interface to send SMS text messages channel partners.

Whenever SMS channel interface End call, I will send this document incoming (stored in MySQL), likewise return receipt sent SMS channel Chamber of Commerce or failure to me, I will put in storage (stores in MySQL).

That day, someone to find me, say a phone number can not receive text messages, and the user does not shield SMS (arrears, shutdown), and so some operations that receive text messages.

So I went to troubleshoot it, first of all I'm going to find inside DB has not sent a corresponding record, this record is found to exist, but does not come out anything unusual in the DB.

  • So, which ruled out this operation in the middle of the case being intercepted (as it has been put in storage, and would certainly call through SMS operator interface)

Later on Qulao logs to see what information Result object calls SMS operator returns, and then ask the operator about the message may appear what causes this problem yes. There reply was: " If it is part of the phone number appears this situation, your phone number is not no trim ah? "

So, I Qulao log and found the back of the phone number with a real space (cut to the heart, and have failed to see before). To deal with this problem becomes extremely simple, as long as the trim on my mobile phone number at the entrance inside just fine.

Second, write code

I am here to support the same SMS to multiple phone numbers , cell phone number so I here use the HashSet to receive. The phone number for trim I wrote the following code:

// 说明:Task对象 有个 key属性,这个key属性的类型是HashSet

if (task.getKey() != null && task.getKey().size() > 0) {
    for (String s : task.getKey()) {
        s.trim();
    }
}

The code is simple, I do it in two steps:

  • Determine whether null, null values ​​are not traverse the collection of phone numbers
  • Be trim for each phone number

The above code is the problem? There must be a problem, ah, no problem I wrote what.

The following write a small Demo, we will find: Calling on the 11 lines of code trim()after the method, and then output in line 12, or the situation will be spaces.

The sample code

2.1 Why is this illusion?

In fact, when we begin to learn Java, certainly you will learn the String class. When learning is also clear String is immutable , but always have a feeling we String object to the change, and why?

Immutable String

I think the first point is this: Our operations are often variable objects, some of the properties of the object changed, we thought to have changed. For example the following code:

HashSet<Student> students = getStudent();
for (Student s1 : students) {
    s1.setName("Java3y");
}

Executed, we think that changed Java3y in the name of Student HashSet inside, and in fact is true.

I think the second point is this: we usually operate String object is a direct result of the operation pass in the past, it looks like the same as modifying the original object. Similar code such as the following:

// 去重
String phone = "   137888888888    ";
sendPhone(phone.trim());

// 转成大写后输出
System.out.println(phone.toUpperCase());

// ... 等等

Returns a new String object

How to change 2.2

Now the problem has been known, String objects are immutable, and String objects to operate, "seemingly" the original String object is changed, in fact, generated a new String object.

I return to the problem, solved, the trimbetter to set the phone number on the list HashSet

// 说明:Task对象 有个 key属性,这个key属性的类型是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);

At last

The B wrote an article to explain how he is "reasonable" wrote Bug, really shameless.

Willing output of dry cargo of Java technology public number: Java3y . A public number more than 200 original articles technical articles, massive video resources, beautiful mind map, attention can get!

Forwarded to the circle of friends is the biggest support for me!

I think the article is well written, points praise !

Guess you like

Origin www.cnblogs.com/Java3y/p/11229748.html