Before...
x is: 10
y is: 10
After re-assigning x
x is: 20
y is: 20
Before...
x is: 10
y is: 10
After re-assigning x
x is: 20
y is: 10
All things are lawful, but not all things are profitable.
J Solomon wrote:[I'm assuming that when you declare y and assign it to x, you are actually copying the actual value that lives at location aliased by x. Is this a pass by value as opposed to pass by reference?
Can someone please explain this one to me?
Sunni srivastav wrote:I can understand that you are trying to correlate Java with C/C++.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Before....
e.name is: something
s.name is: something
After....
e.name is: CHANGED
s.name is: CHANGED
J Solomon wrote:What about the following example..........
All things are lawful, but not all things are profitable.
Knute Snortum wrote:Is the reason y is still 10 because of auto-unboxing?
All things are lawful, but not all things are profitable.
I wrote:In the case of autoboxing of an int, the Integer that results from autoboxing is always a new instance.
Junilu Lacar wrote:Technically, autoboxed int values from
-127-128 to 127 will always be the same pre-existing(?) Integer instances (I guess they're intern'ed by the JVM or something like that).
Don't ever abuse code with this knowledge though.
luck, db
There are no new questions, but there may be new answers.
Darryl Burke wrote:
Junilu Lacar wrote:Technically, autoboxed int values from
-127-128 to 127 will always be the same pre-existing(?) Integer instances (I guess they're intern'ed by the JVM or something like that).
No, they are cached in the private nested class Integer.IntegerCache. See Integer.java around lines 580 - 606 (Java SE 6).
It wouldn't be abuse. This behavior is defined in the JLS: http://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.7-300
It is possible to increase that range. The JVM says it would be better to cache every boxed Integer:Junilu Lacar wrote: . . . Technically, autoboxed int values from
-127-128 to 127 will always be the same pre-existing(?) Integer instances . . .
The same link that Darryl quoted wrote:Ideally, boxing a primitive value would always yield an identical reference. In practice, this may not be feasible using existing implementation techniques.
Campbell Ritchie wrote:The JVM says it would be better to cache every boxed Integer
Those cherries would go best on cherry cheesecake. Don't put those cherries on this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|