This topic has been archived. It cannot be replied.
-
工作学习 / 学科技术讨论 / JAVA 再来一题String result = foo() + arg;
result += boo();
System.out.println("result = " + result);
假设foo()和 boo()不产生Object,arg 是String,以上代码执行完以后,假设GC 马上触发,JVM 会GC 几个Object?
-sxffff(lookingforjob);
2014-1-9
{201}
(#8547318@0)
-
3个? arg 1个。result 2个
-sallay(sallay);
2014-1-9
(#8547373@0)
-
3. but is arg, "result=" and the ("result="+result) . String is immutable object in java. and varaible result is just a ref to arg not a object.
-wxlin2k(wxlin2k);
2014-1-9
(#8547800@0)
-
再想想
-sxffff(lookingforjob);
2014-1-9
(#8547801@0)
-
bad questiob again. since jvm handle String diffently from other object. if this is part of larger program. String is not garanteed to be GCed. your context of the variable scope is unclear in this question.
-wxlin2k(wxlin2k);
2014-1-9
(#8547818@0)
-
同意,不过string pool本来就是考点之一。题目改一下:假如不考虑String Pool,foo(),boo()只有在返回时分别产生一个String Object,方法内部产生的不算,以上代码执行完以后,假设GC 马上触发,JVM 会GC 几个Object?
-sxffff(lookingforjob);
2014-1-10
(#8548003@0)
-
补充一下:方法内部产生的不算(这里方法指foo() and boo() only)
-sxffff(lookingforjob);
2014-1-10
(#8548008@0)
-
much better now, 5 is correct and your own question is some what still unclear what you are trying ask. here is the answers and steps:
-wxlin2k(wxlin2k);
2014-1-10
{695}
(#8549234@0)
-
题目不清楚,很不好意思,不过太清楚了就很容易了。你可以提问,你认为清楚了,再回答。
-sxffff(lookingforjob);
2014-1-10
(#8549446@0)
-
already replied with steps and pointed out your problem.you are not doing an actual interview and should be not be requesting questions. from your internet friends.
if you are intent to ask a question with clear answers then the question itself should be clear.
not meant to be sound offending to anyone. just want to have a better communication when chatting on line.
-wxlin2k(wxlin2k);
2014-1-10
{316}
(#8549479@0)
-
不动Java,凑热闹,按wxlin2k的"String is immutable object in java."数就是,有string操作就加1个,比如"aaa"+"bbb"是三个。
-tjhong(以后再说);
2014-1-10
(#8549495@0)
-
考虑String Pool啥地,跟值有关系,数对数错跟程序质量一点关系也没。
-tjhong(以后再说);
2014-1-10
(#8549507@0)
-
yup
-wxlin2k(wxlin2k);
2014-1-10
(#8549516@0)
-
yup
-wxlin2k(wxlin2k);
2014-1-10
(#8549511@0)
-
你这也叫做Java的题目。Java developers should not be concerned with this. It's the Java language designer's, or to be precise, the JVM's and its implementor's.4, including the literal string, if all objects mentioned in the subject are to be GC'ed.
-mingyangli(Life is a journey);
2014-1-10
{90}
(#8548198@0)
-
4个不对,Java developer should know what the given JVM is doing, in this case, let's just talk about the HotSpot.
-sxffff(lookingforjob);
2014-1-10
(#8548294@0)
-
为什么是4个?
-sallay(sallay);
2014-1-10
(#8548449@0)
-
GC should be transparent to app developer, if not, it is crappy language.
-llun(随心);
2014-1-13
(#8553161@0)
-
> 6
-wincity(红卫兵);
2014-1-10
(#8548845@0)
-
正确,能解晰一下吗?
-sxffff(lookingforjob);
2014-1-10
(#8549206@0)
-
you are wrong. >6 is the object instances created. not GCed.
-wxlin2k(wxlin2k);
2014-1-10
(#8549253@0)
-
几多钱?
-wincity(红卫兵);
2014-1-10
(#8549306@0)
-
厉害 快揭晓答案吧
-sallay(sallay);
2014-1-10
(#8549376@0)
-
考 stringbuffer呢吧
-aaronding(流浪的八毛儿);
2014-1-10
(#8549467@0)
-
正确。正确。其实我是在DZone上看到:
5 Coding Hacks to Reduce GC Overhead
想跟大家分享一下。
-sxffff(lookingforjob);
2014-1-10
(#8549513@0)
-
that is why I said yourself already misunderstood your own question. the link is about object created. not GCed. GC will check for reference.
-wxlin2k(wxlin2k);
2014-1-10
(#8549526@0)
-
当然要改一下了,但StringBuilder object 是会被GC的。
-sxffff(lookingforjob);
2014-1-10
(#8549541@0)
-
please refer to my answers before. the object that is refered by variable result and arg will not be GCed. since the instances are not orphaned.
-wxlin2k(wxlin2k);
2014-1-10
(#8549551@0)
-
好象StringBuffer object 已经不会被String object 引用了吧。
-sxffff(lookingforjob);
2014-1-10
(#8549579@0)
-
think again
-wxlin2k(wxlin2k);
2014-1-10
(#8549588@0)
-
the StringBuilder object will be GCed.
-sxffff(lookingforjob);
2014-1-10
{247}
(#8549620@0)
-
楼主是对的,你错的不小。
-wincity(红卫兵);
2014-1-10
(#8549671@0)
-
think again.
-wxlin2k(wxlin2k);
2014-1-10
(#8549701@0)
-
你的概念嫉妒模糊,所以说你犯大错误了
-wincity(红卫兵);
2014-1-10
(#8549713@0)
-
说的太对了!
-wxlin2k(wxlin2k);
2014-1-11
(#8550111@0)
-
StringBuilder最后的toString()返回的还是一个String对象 这个会被result这个变量所指。arg是另一个指向String的指针。因为这段代码的余下部分如果不是函数结束的话 result和arg就还在当前堆栈里。他们所指向的对象还不具备回收条件
-wxlin2k(wxlin2k);
2014-1-11
(#8550136@0)
-
要程序员自己用StringBuilder去做合拼优化是java的缺陷,不是特点。不要纠结在这上面。
-wxlin2k(wxlin2k);
2014-1-11
{658}
(#8550170@0)
-
已经明确这是hotspot,这个java已经自动帮你做了, 程序员不需要自己手动做. 手动做的原因是为了提高performance,那会跟好的Architecture有冲突, 这方面JAVA 已经比C/C++好了很多.
-sxffff(lookingforjob);
2014-1-11
(#8550211@0)
-
这篇文章说的是stringbuilder,在1.5之前用的是stringbuffer
-aaronding(流浪的八毛儿);
2014-1-10
(#8549531@0)
-
两个 class 是一样的,StringBuilder 不 synchronize,其他几乎完全一样。
-wincity(红卫兵);
2014-1-10
(#8549668@0)
-
要是这是这道题的原意,那题目就出错了。
-geekcode(文心雕码);
2014-1-10
(#8549859@0)
-
Why
-sxffff(lookingforjob);
2014-1-10
(#8549889@0)
-
几多钱? -wincity(红卫兵); 15:59 (#8549306@0)
-geekcode(文心雕码);
2014-1-10
(#8549960@0)
-
多多都给
-sxffff(lookingforjob);
2014-1-10
(#8549994@0)
-
:D 。。。八毛儿和wxlin2k的16:52贴已经解释了。不过,wxlin2k后来给的原因不对。》“假设foo()和 boo()不产生Object”。
产不产生Object不重要,重要的是1.有没有产生不被引用的Object,2. 有没有de-reference Object。比如,下面的foo()
String foo() {
// arr is a member variable of type Object[]
arr = null;
......
}
你说会GC 几个Object?
------------
另外,原文也很不严谨,没有考虑到编译器可能的优化,那些没用的StringBuffer和StringBuilder不一定会真的生成。原文建议的solution更是误人子弟。1.可读性比局部优化更重要。2.StringBuilder.append(...)这样的“优化”更是笑话。
-geekcode(文心雕码);
2014-1-10
{510}
(#8550045@0)
-
码哥有点抬杠了,这个其实是一道好题。如果你要一道严谨的有标准答案的题,这是一道坏题。但是如果你要一道简单却能测出面试人深浅的题,这是一道绝妙的题。码哥你这个回帖其实也间接地也证明了这是一道好题,你说的优化,dereferencing,可读性,等等,一听就知道是大拿。
-wincity(红卫兵);
2014-1-10
(#8550079@0)
-
你说抬杠就抬杠吧。来这儿不就是为了抬杠吗? :)
-geekcode(文心雕码);
2014-1-10
{29}
(#8550092@0)
-
您是大那,说的太对了!
-wxlin2k(wxlin2k);
2014-1-11
(#8550117@0)
-
码哥知识的渊博和对编程的深刻认识,和你诸多的概念错误,从两个极端证明了这道题的绝妙。不明确的题也有好处,它能测出面试人的品质,比如你做不做合理的假设,你愿不愿意沟通以及沟通能力,你是不是喜欢钻牛角尖,等等等等。有没有人说过你喜欢钻牛角尖?
-wincity(红卫兵);
2014-1-11
(#8550164@0)
-
不合理的是你们各位大哥啊,把缺陷当技术难点和重点。
-wxlin2k(wxlin2k);
2014-1-11
(#8550171@0)
-
你这位同学思路也模糊啊
-wincity(红卫兵);
2014-1-11
{996}
(#8550225@0)
-
码哥假设arg会被某个函数null就是因为原文的variable scope不清晰。跟我认为是本地变量没有冲突。大家看的角度不一样。但很佩服你居然从我的回复判断我的职业取向。真服了!
-wxlin2k(wxlin2k);
2014-1-11
(#8550261@0)
-
同学概念是一头雾水啊。null 一个 arg 是笑话,码哥没有说 null arg。说 foo() 和 boo() 也要被 GC 简直就是莫名其妙。谈堆栈也是概念不清的症状。从钻牛角尖看编程的取向不是算命,是经验之谈。
-wincity(红卫兵);
2014-1-11
(#8550314@0)
-
其实面试时检测 candidate 是不是喜欢钻牛角尖比问技术问题更重要。建议你以后面试不要钻牛角尖,很容易因此被刷掉的。
-wincity(红卫兵);
2014-1-11
(#8550319@0)
-
已经明白你为什么说我概念模糊。赶紧写的答案的确表达不清。我犯的是楼主同样的错误,foo() 和boo()不是对象,但楼主说它返回对象,那么就有两个non-refrenecing-object.。所以这两个会GC。至于堆栈,本地变量的result(或arg)在当前函数结束前当然在stack不在heap.就酱紫
-wxlin2k(wxlin2k);
2014-1-11
(#8550346@0)
-
你这个同学最大的优点就是固执。这是码哥的原话:
// arr is a member variable of type Object[]
arr = null;
另外你看这几个 foo() 和 boo():
int foo() { return 3; }
String foo() { return Thread().currentThread().getName(); }
String boo() { return System.getenv("PATH"); }
String boo() { return "ddd"; }
Object boo() { return Locale.ENGLISH; }
再另外 Java 里所有 new 出来的 objects 都在 heap。
-wincity(红卫兵);
2014-1-11
{388}
(#8550372@0)
-
没有说对象在stack,指向对象的是啥?兵哥您的优点是搞浑水 :)
-wxlin2k(wxlin2k);
2014-1-11
(#8550377@0)
-
看来你没有看码哥的内容 :)
-wxlin2k(wxlin2k);
2014-1-11
(#8550349@0)
-
要不要顺便算下命?呵呵。
-wxlin2k(wxlin2k);
2014-1-11
(#8550264@0)
-
要更正的一句是, 我是完全认同码哥的回复,只是我说原题的重点搞错了,码哥的回答最合理。因为java language specifation没有要求字符串合拼一定要编译成StringBuilder,我们不能认为SB一定有
-wxlin2k(wxlin2k);
2014-1-11
(#8550174@0)
-
其实JVM 是有规定使用StringBuilder的, 并不是我想出来的.
-sxffff(lookingforjob);
2014-1-11
(#8550207@0)
-
10个8个字符串操作根本不值得考虑性能,只有在大量字符操作时才值得StringBuilder 。when to use StringBuilder in java
-tjhong(以后再说);
2014-1-11
(#8550214@0)
-
JLS 出处,请注意原文用了"may", 不是”must".所以只是现在的java compiler太懒没有做好优化,但不是“有规定要求”。
-wxlin2k(wxlin2k);
2014-1-11
(#8550237@0)
-
已经明确是hotspot
-sxffff(lookingforjob);
2014-1-11
(#8550249@0)
-
这点可以认同。但请看看你自己原来的题目哪个地方有这个假设或预设条件。JVM是通称,不能预设是hotspot,,而且这个转换是在comipler不是JVM
-wxlin2k(wxlin2k);
2014-1-11
(#8550256@0)
-
回复下面的,咱都对,我说编译器没做好优化。你说程序员应该自己动手优化,一体两面
-wxlin2k(wxlin2k);
2014-1-11
(#8550567@0)
-
What complie .java to .class? that is not jvm that is javac complier. think before you replie me
-wxlin2k(wxlin2k);
2014-1-11
(#8550577@0)
-
can be JIT
-sxffff(lookingforjob);
2014-1-11
(#8550581@0)
-
wow full stop.无言了。投降,认输了!
-wxlin2k(wxlin2k);
2014-1-11
(#8550598@0)
-
4个不对,Java developer should know what the given JVM is doing, in this case, let's just talk about the HotSpot. -sxffff(lookingforjob); 1.10 11:11 (#8548294@0) reply more
-sxffff(lookingforjob);
2014-1-11
{530}
(#8550568@0)
-
Java Programming Guidelines: This section covers issues related to Java coding and performance. The guidelines outlined are not specific to GlassFish Server, but are general rules that are useful in many situations.
-sxffff(lookingforjob);
2014-1-11
{826}
(#8550545@0)
-
此题无解foo() bar()不产生object,但返回的object的toString里面产生不产生object呢?没说。这个string + string jvm用stringbuffer来解决,stringbuffer里面还是char[],题目中的的string长度不说,stringbuffer在append的时候可能需要重新创建更大的char[],太多未知条件了,所以说无解,题目很烂。
-aaronding(流浪的八毛儿);
2014-1-10
{272}
(#8549505@0)
-
所以 > 6 是最佳答案
-wincity(红卫兵);
2014-1-10
(#8549655@0)
-
其实 > 9 才是最佳答案
-wincity(红卫兵);
2014-1-10
(#8549681@0)
-
又想了一下, > 12 更好
-wincity(红卫兵);
2014-1-10
(#8549684@0)
-
此题无解 -aaronding(流浪的八毛儿); 16:48 (272 bytes. #8549505@0)。。。。:D
-geekcode(文心雕码);
2014-1-10
(#8549863@0)
-
Does it make sense to know details about GC? I've been a Java guy for over 15 years, but never ran into a situation related to it.
-verano(Summer);
2014-1-10
(#8549734@0)
-
上面那些是钻牛角尖用的。知道适当的时候用dispose() 就可以了。
-tjhong(以后再说);
2014-1-11
(#8550209@0)
-
每次被问到类似的题目,我就想起孔乙己故事里的回字有几种写法。
-vicky2005(孩子她爹);
2014-1-11
(#8550384@0)
-
严重同意
-wxlin2k(wxlin2k);
2014-1-11
(#8550446@0)