编写高质量代码:改善Java程序的151个建议(51~60)

建议五十一:不要主动进行垃圾回收

建议五十二:推荐使用String直接量赋值

不建议使用String str = new String("a")来赋值,推荐使用String str = "a"

建议五十三:注意方法中传递的参数要求

建议五十四:正确使用String,StringBuffer,StringBuilder

String类是不可改变的量,String str = "a";str = "b";只是改变了str的指向,"a"仍然存在内存中。

public class Client {
    public static void main(String[] args) {
        String str = "a";
        String str1 = "a";
        System.out.println(str == str1);
    }
}

StringBuffer是一个可变字符序列,它的值是可以改变的

StringBuffer sb = new StringBuffer("a");
sb.append("b");

StringBuffer和StringBuilder基本相同,StringBuffer线程安全但性能较差

(1)使用String类的场景

在字符串不经常变换的场景中使用String类,例如常量的声明,少量的变量运算等。

(2)使用StringBuffer的场景

在频繁的进行字符串运算(如拼接,替换,删除等),并且运行在多线程环境中,例如xml解析,http参数解析和封装

(3)使用StringBuilder的场景

在频繁的进行字符串运算(如拼接,替换,删除等),并且运行在单线程环境中,如SQL语句的封装,JSOn封装。

建议五十五:注意字符串的位置

public class Client {
    public static void main(String[] args) {
        String str1 = 1 + 2 + "apples";
        String str2 = "apples" + 1 + 2;
        System.out.println("str1: " + str1 + " str2: " + str2);
    }
}


在“+”表达式中,String字符串拥有最高优先级。

建议五十六:自由选择字符串的拼接方法

对一个字符串拼接有三种方法:加号,concat方法,StringBuffer(或StringBuilder)的append方法

public class Client {
    public static void main(String[] args) {
        String str1 = "a";
        String str2 = "m";
        str1 = str1 + "b";
        str2 = str2.concat("n");

        StringBuffer str3 = new StringBuffer("x");
        str3.append("y");

        System.out.println("str1: " + str1 + " str2: " + str2 + " str3: " + str3);
    }
}

append方法最快,concat方法次之,加号最慢。

但大多数情况下使用加号,符合人类阅读,在系统性能临界时才考虑concat和append方法。

建议五十七:推荐在复杂字符串操作中使用正则表达式

例子:统计一篇文章中英文单词的数量

public class Client {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String str = scanner.nextLine();
//            使用split方法分割后统计
            int wordsCount = str.split(" ").length;
            System.out.println(str + " 单词数:" + wordsCount);
        }
    }
}

出现多种异常情况,如果使用循环来处理,则会使程序复杂性大大提高,所以使用正则表达式。

public class Client {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String str = scanner.nextLine();
//            正则表达式对象
            Pattern pattern = Pattern.compile("\\b\\w+\\b");
//            生成匹配器
            Matcher matcher = pattern.matcher(str);
//            记录单词数量
            int wordsCount = 0;
//            遍历查找匹配,统计单词数量
            while (matcher.find()) {
                wordsCount++;
            }
            System.out.println(str + " 单词数:" + wordsCount);
        }
    }
}

正则表达式在字符串的查找,替换,剪切,复制,删除等方面有非凡的作用,特别是对大量文本字符的处理(如需要读取大量的LOG日志)

建议五十八:强烈建议用UTF编码

建议五十九:对字符串排序持一种宽容的心态

public class Client {
    public static void main(String[] args) throws Exception {
        String[] strs = {"张三(Z)", "李四(L)", "王五(W)" };
//        定义一个中文排序器
        Comparator comparator = Collator.getInstance(Locale.CHINA);
//        升序排列
        Arrays.sort(strs, comparator);
        int i = 0;
        for (String str : strs) {
            System.out.println((++i) + "、 " + str);
        }
    }
}

对于汉子排序,使用Collator类

建议六十:性能考虑,数组是首选

性能要求较高的场景中使用数组代替集合。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值