由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 问一个Java题
相关主题
TIJ上写错了?Java大侠们:Hashtable help please!
string pool vs class constant pool问题,在线等expression in unicode
Java能帮我做这个project吗?实实在在受不了了,还是来这里求教!
help!!显示email中文的问题
什么应用需要几百个节点的java呢一道 JAVA Stack vs Heap 题
怎麼得到字符串中的raw bytes?Java练习题 3
Question about displaying Chinesejava 的内存分布?
why doesn't replaceAll work?问个set和literal String的问题
相关话题的讨论汇总
话题: string话题: abhay话题: deol话题: java话题: new
进入Java版参与讨论
1 (共1页)
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(...)就足够了。

相关主题
怎麼得到字符串中的raw bytes?Java大侠们:Hashtable help please!
Question about displaying Chineseexpression in unicode
why doesn't replaceAll work?实实在在受不了了,还是来这里求教!
进入Java版参与讨论
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存在哪?

1 (共1页)
进入Java版参与讨论
相关主题
问个set和literal String的问题什么应用需要几百个节点的java呢
Core Java那两本砖头书太浅了怎麼得到字符串中的raw bytes?
问个xml的问题Question about displaying Chinese
Database Pooling 的问题why doesn't replaceAll work?
TIJ上写错了?Java大侠们:Hashtable help please!
string pool vs class constant pool问题,在线等expression in unicode
Java能帮我做这个project吗?实实在在受不了了,还是来这里求教!
help!!显示email中文的问题
相关话题的讨论汇总
话题: string话题: abhay话题: deol话题: java话题: new