This topic has been archived. It cannot be replied.
-
工作学习 / IT技术讨论 / 请教JAVA问题。在一class中定义了 Long amnt = new Long(1200); 然后我要把值改成 1500, 用 amnt=1500 是不行了,那要用什么呢?谢谢。
-ccl(cl);
2002-10-2
(#777685@0)
-
It's immutable, like String. try amnt=new Long(1500);
-yuanzidan(原子弹);
2002-10-2
(#777717@0)
-
Thanks. Will this cause any memory leak? The concern is that the second new allocate memory one more time while the old memory allocated for 1200 is not released yet and is becoming an orphan?
-ccl(cl);
2002-10-2
(#777741@0)
-
Well, the garbage collector will take care of it sooner or later. From my memory, this kind of in immutable objects will not cause any machine memory problems, they are in heap, and they are reusable.For example: you can create thousands of new Long(1500) objects in you code, they consume only one spot in heap.
-yuanzidan(原子弹);
2002-10-2
{112}
(#777765@0)
-
I am convinced by your garbage collector theory although your example does not seem to support this theory. Thanks a lot.
-ccl(cl);
2002-10-2
(#777777@0)
-
Sorry for confusion. My point is that garbage collector may use different algorithm for mutable object and immutable object. They are all in memory but they are in different location.
-yuanzidan(原子弹);
2002-10-2
(#777820@0)
-
Oh, your example does not conflict with your theory either. Thanks.
-ccl(cl);
2002-10-2
(#777784@0)
-
amnt was declared as long, you can't just assign an number to it.
-jeffrey815(Smartiecat);
2002-10-2
(#777737@0)
-
Thanks, yuanzidan gave an solution and it worked, my concern is memory leak.
-ccl(cl);
2002-10-2
(#777747@0)
-
Does Java even have memory leak?
-dennis2(Dennis);
2002-10-2
(#777749@0)
-
I have no idea but so doubt. If amnt is assigned a new piece of memory, where is the old memory? collected back automatically? Should we use some release mechanism before the second new? Any idea?
-ccl(cl);
2002-10-2
(#777756@0)
-
I thought one of the advantage Java has is garbage collection, so you don't have to worry about memory leak. No?
-dennis2(Dennis);
2002-10-2
(#777772@0)
-
I am convinced. Thanks.
-ccl(cl);
2002-10-2
(#777779@0)
-
Java internally has its own garbage collector that will collect all unreference memory and make them available again.
-jeffrey815(Smartiecat);
2002-10-2
(#777789@0)
-
While thinking about my question, you can go to 枫下调查 answer another question ---- 如果中国京剧院来你的城市演京剧,票价如下,您会去看吗 (单选). Thanks.
-ccl(cl);
2002-10-2
(#777768@0)