Java字符串声明 [英] Java String declaration

查看:123
本文介绍了Java字符串声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

String str = new String(SOME) String str =SOME
这些声明是否会产生性能变化。

What is the difference between String str = new String("SOME") and String str="SOME" Does these declarations gives performance variation.

推荐答案

String str = new String("SOME")

总是在堆上创建一个新对象

always create a new object on the heap

String str="SOME" 

使用字符串池

试试这个小例子:

        String s1 = new String("hello");
        String s2 = "hello";
        String s3 = "hello";

        System.err.println(s1 == s2);
        System.err.println(s2 == s3);

为避免在堆上创建不必要的对象,请使用第二种形式。

To avoid creating unnecesary objects on the heap use the second form.

这篇关于Java字符串声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆