For every winner, there are dozens of losers. Chances are you're one of them.
SCJP6 - 93% SCWCD5 - 97%
For every winner, there are dozens of losers. Chances are you're one of them.
The reason I'm asking this is that I don't find this intuitive and in case of the exam i would have said compilation fails.
Horia Constantin wrote:
Any links regarding this matter? @Harsh Pensi Where did you find that information?
SCJP6 - 93% SCWCD5 - 97%
Harsh Pensi wrote:
Horia Constantin wrote:
Any links regarding this matter? @Harsh Pensi Where did you find that information?
Read it in the book for scjp6 by Kathy Sierra and Bert Bates.
For every winner, there are dozens of losers. Chances are you're one of them.
SCJP6 - 93% SCWCD5 - 97%
Harsh Pensi wrote:Hi Horia, its on pg. 246.
It's just two wrapper objects that happen to have the same value. Because they
have the same int value, the equals() method considers them to be "meaningfully
equivalent", and therefore returns true. How about this one:
Integer i3 = 10;
Integer i4 = 10;
if(i3 == i4) System.out.println("same object");
if(i3.equals(i4)) System.out.println("meaningfully equal");
This example produces the output:
same object
meaningfully equal
Yikes! The equals() method seems to be working, but what happened with ==
and != ? Why is != telling us that i1 and i2 are different objects, when == is saying
that i3 and i4 are the same object? In order to save memory, two instances of the
following wrapper objects (created through boxing), will always be == when their
primitive values are the same:
n Boolean
n Byte
n Character from \u0000 to \u007f (7f is 127 in decimal)
n Short and Integer from -128 to 127
Note: When == is used to compare a primitive to a wrapper, the wrapper will be
unwrapped and the comparison will be primitive to primitive.
Where Boxing Can Be used
As we discussed earlier, it's very common to use wrappers in conjunction with
collections. Any time you want your collection to hold objects and primitives, you'll
want to use wrappers to make those primitives collection-compatible. The general
rule is that boxing and unboxing work wherever you can normally use a primitive or
a wrapped object. The following code demonstrates some legal ways to use boxing:
class UseBoxing {
public static void main(String [] args) {
UseBoxing yu = new UseBoxing();
yu.go(5);
}
boolean go(Integer i) { // boxes the int it was passed
Boolean ifSo = true; // boxes the literal
Short s = 300; // boxes the primitive
For every winner, there are dozens of losers. Chances are you're one of them.
SCJP6 - 93% SCWCD5 - 97%
Harsh Pensi wrote:Haha i dont believe this.
Did you read the page???
For every winner, there are dozens of losers. Chances are you're one of them.
primitive values are the same:
n Boolean
n Byte
n Character from \u0000 to \u007f (7f is 127 in decimal)
n Short and Integer from -128 to 127
Note: When == is used to compare a primitive to a wrapper, the wrapper will be
unwrapped and the comparison will be primitive to primitive.
Where Boxing Can Be used
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
SCJP6 - 93% SCWCD5 - 97%