l**********9 发帖数: 537 | 1 String a="abhay"
String b="deol"
System.out.println(a+b);
======================
String a=new String("abhay");
String b=new String ("deol");
System.out.println(a+b);
========================
Which is more efficient? why?
有2种看法,参看职业杯
http://www.careercup.com/question?id=69271
谢谢了 |
g*****g 发帖数: 34805 | 2 In my career I've never found a case that you need
to new a string. enough said.
【在 l**********9 的大作中提到】 : String a="abhay" : String b="deol" : System.out.println(a+b); : ====================== : String a=new String("abhay"); : String b=new String ("deol"); : System.out.println(a+b); : ======================== : Which is more efficient? why? : 有2种看法,参看职业杯
|
u****s 发帖数: 2186 | 3 I did,忘了什么地方了,either RMI or JNI, 不加new String就是不work
【在 g*****g 的大作中提到】 : In my career I've never found a case that you need : to new a string. enough said.
|
l**********9 发帖数: 537 | 4 我的理解是:
如果是第一次创建a和b,第一种也需要在String Pool创建对象,然后将a指向String
Pool里的对象呀, 这和第二种在heap创建对象再指向a效率差不多吧, 另外,因为
第一种先需要查找String pool里面是否存在"abhay"字符串,所以还需要多的开销,是
不是第一种就效率低些呢?
不知道String pool里面到底存的是不是也算对象。
这个纯属面试题,实际工作中不会用第二种情况。谢谢了
【在 g*****g 的大作中提到】 : In my career I've never found a case that you need : to new a string. enough said.
|
h*****0 发帖数: 4889 | 5 第二种也得在String Pool存对象啊,要不然那个String literal程序记在哪?
这个问题没啥意义吧。只要知道String.equals(..), String.intern(...)就足够了。
【在 l**********9 的大作中提到】 : 我的理解是: : 如果是第一次创建a和b,第一种也需要在String Pool创建对象,然后将a指向String : Pool里的对象呀, 这和第二种在heap创建对象再指向a效率差不多吧, 另外,因为 : 第一种先需要查找String pool里面是否存在"abhay"字符串,所以还需要多的开销,是 : 不是第一种就效率低些呢? : 不知道String pool里面到底存的是不是也算对象。 : 这个纯属面试题,实际工作中不会用第二种情况。谢谢了
|
m******t 发帖数: 2416 | 6
Sometimes you have to read a file into a char[] or byte[],
and then construct a String with it.
There, that's the case that will complete your career. ;-)
【在 g*****g 的大作中提到】 : In my career I've never found a case that you need : to new a string. enough said.
|
g*****g 发帖数: 34805 | 7 For boilerplate work like this, you are supposed to use
a nicely packaged API that handles this. There are too
many corner cases with encoding and you are better off
not handling it yourself.
Talk about standing on the shoulder of giants, :-)
【在 m******t 的大作中提到】 : : Sometimes you have to read a file into a char[] or byte[], : and then construct a String with it. : There, that's the case that will complete your career. ;-)
|
h*****0 发帖数: 4889 | 8 new String(bytes, utf-8), that would be the best way to eliminate any corner
cases.
【在 g*****g 的大作中提到】 : For boilerplate work like this, you are supposed to use : a nicely packaged API that handles this. There are too : many corner cases with encoding and you are better off : not handling it yourself. : Talk about standing on the shoulder of giants, :-)
|
g*****g 发帖数: 34805 | 9 Wait, I did this, when I had to handle some encoding issues
on emails, it's been a while. :-)
corner
【在 h*****0 的大作中提到】 : new String(bytes, utf-8), that would be the best way to eliminate any corner : cases.
|
l**********9 发帖数: 537 | 10 这个太对了,忘记new还需要在pool里面存一份了,所以第二种一共要创建4个对象,第
一种快了,谢谢了
【在 h*****0 的大作中提到】 : 第二种也得在String Pool存对象啊,要不然那个String literal程序记在哪? : 这个问题没啥意义吧。只要知道String.equals(..), String.intern(...)就足够了。
|
|
|
m******t 发帖数: 2416 | 11 I don't know what shoulder I need to stand on to convert
a char[] to a string (which doesn't even involve any encoding
issues)), hey, but it's always good to know there are shoulders.
;-)
【在 g*****g 的大作中提到】 : For boilerplate work like this, you are supposed to use : a nicely packaged API that handles this. There are too : many corner cases with encoding and you are better off : not handling it yourself. : Talk about standing on the shoulder of giants, :-)
|
m****r 发帖数: 6639 | 12 in fact, we use that quite a bit.
corner
【在 h*****0 的大作中提到】 : new String(bytes, utf-8), that would be the best way to eliminate any corner : cases.
|
g**e 发帖数: 6127 | 13 new 为啥要在pool里保存?
了。
【在 l**********9 的大作中提到】 : 这个太对了,忘记new还需要在pool里面存一份了,所以第二种一共要创建4个对象,第 : 一种快了,谢谢了
|
h*****0 发帖数: 4889 | 14 new String("abcdefghijklmnopqrstuvwxyz");
如果你认为是动态时开在堆里的,那那个a-z的string literal存在哪?
【在 g**e 的大作中提到】 : new 为啥要在pool里保存? : : 了。
|
g**e 发帖数: 6127 | 15 高。谢谢
【在 h*****0 的大作中提到】 : new String("abcdefghijklmnopqrstuvwxyz"); : 如果你认为是动态时开在堆里的,那那个a-z的string literal存在哪?
|