码迷,mamicode.com
首页 > 其他好文 > 详细

Performance - Method passes constant String of length 1 to character overridden method

时间:2017-08-09 21:05:25      阅读:3036      评论:0      收藏:0      [点我收藏+]

标签:bsp   com   eth   his   hat   pil   ace   compiler   cte   

This method passes a constant literal String of length 1 as a parameter to a method, that exposes a similar method that takes a char. It is simpler and more expedient to handle one character, rather than a String.

Instead of making calls like: 
String myString = ... 
if (myString.indexOf("e") != -1) {
int i = myString.lastIndexOf("e");
System.out.println(myString + ":" + i); //the Java compiler will use a StringBuilder internally here [builder.append(":")]
...
return myString.replace("m","z");
}
Replace the single letter Strings with their char equivalents like so:
String myString = ... 
if (myString.indexOf(‘e‘) != -1) {
int i = myString.lastIndexOf(‘e‘);
System.out.println(myString + ‘:‘ + i); //the Java compiler will use a StringBuilder internally here [builder.append(‘:‘)]
...
return myString.replace(‘m‘,‘z‘);
}

Performance - Method passes constant String of length 1 to character overridden method

标签:bsp   com   eth   his   hat   pil   ace   compiler   cte   

原文地址:http://www.cnblogs.com/winner-0715/p/7327334.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!