In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to analyze the String pit in Android SDK. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
The pit of String in Android SDK
Recently, I have been looking up a SDK-related bug in the project. We pass a string to the java layer through the C # code, and then parse the corresponding json data in the java layer. An exception "System.err: org.json.JSONException: End of input at character 0 of" is thrown when using JSONObject.
Public void Sample (String json)
{
JSONObject jsonObj
Try
{
JsonObj = new JSONObject (json)
}
Catch (JSONException e)
{
QLog.Log (e.toString ())
Return
}
QLog.Log (json.toString ())
}
Finally, we located it to the code above.
JsonObj = new JSONObject (json)
Now that there is a problem, we make a security judgment to skip execution when passing in a null or an empty string "". So you have the following code.
If (json = = null | | json = = "")
{
QLog.Log ("json is null or empty.")
Return
}
But we ran the code and found that the line of code had not been executed. Still get to the place of Json parsing and throw an exception. We typed log in the C # layer and found that the value passed in was the empty string "". However, the java layer determines that the empty string "" is not executed. Therefore, we decisively searched the judgment interface of the java layer string. Finally let the truth come to light.
The following is a comparison of the java layer string.
The string S1 does not point to any memory space in the heap and returns true
The string S1 contains the same content as S2 and the memory addresses of S1 and S2 are the same, returning true
The string s1.equals (S2), S1 has the same content as S2, and returns true
The string s1.isEmpty (), whose length of S1 is 0, returns true (String S1 = ""; s1.isEmpty () is true)
The C # layer determines whether two strings are equal, in fact, most of them directly use = = to determine whether the values are equal. If you want to determine whether the memory address is equal, you need to call ReferenceEquals to determine.
String.ReferenceEquals (S1 and S2)
In other words, C # determines whether the value is equal by = = and whether the memory address is equal by ReferenceEquals.
But java determines whether the value is equal by equal and whether the memory address is equal by =.
Usually we are used to writing C #, here we take it for granted the habit of string processing of C #, we need to pay attention to it!
The above is how to analyze the pit of String in Android SDK. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.