Matthew Brown wrote:No, intValue() isn't called. In the first example they are actually referencing the same object.
Matthew Brown wrote:The run-time keeps a cache of Integer objects within a certain range and will re-use those when it can. This is safe because Integer objects are immutable.
Roel De Nijs wrote:
Matthew Brown wrote:No, intValue() isn't called. In the first example they are actually referencing the same object.
That's not true at all! In the 1st example an Integer (variable i) and an int (variable j) are compared. When == is used to compare a primitive to a wrapper, the wrapper will be unwrapped and the comparison will be primitive to primitive. That's why true is printed!
Prathima gaitonde wrote:Point A:
1> Integer i=10; will be created in cache. whose address say add1.As Integer cache range from value -128 to 127.
2> Integer j=10; will get add1 from the cache.
3> Integer k=129; is never created in cache it will be create in heap whose address say add2.
4> Integer y=new Integer(10); same as k say its address add3.
Therefore, above only i==j is true rest are false.
Prathima gaitonde wrote:Point B:
When wrapper is compared with primitive, No matter whether they are of same type or not the wrapper class call method intValue() before comparison
so..
int x=10;
x==y;//true
x==i;//true
Prathima gaitonde wrote:1> Like Integer, is there cache mechanism for char,byte,short,long,float,double?
Prathima gaitonde wrote:2> Is cache mechanism same as String pooling?
Prathima gaitonde wrote:3> Why cant we compare object of different class with ==? Why cant it be Short.intValue==Integer.intValue()?
Prathima gaitonde wrote:4> On line no. 19, is it Short.doubleValue() called?
Prathima gaitonde wrote:5>Finally I dont get the behaviour of Number!!! Why is it compile time error while comparing with primitives? Why is it true only with Integer comparison?
Prathima gaitonde wrote:1> Double d=10;
Here 10 is an int primitive, d is an object hence, primitive should be promoted to object, so...
d=Integer.valueOf(10);//compiler try to promote.
d being Double and sibling of Integer says to the compiler, Hello I am not Integer, I am a double. Hence compile time error.
Prathima gaitonde wrote:2> Number is an abstract class, and is a super class of wrappers, so...
Number n=10 or 10.0 or 10.0f or 10L all are fine as Integer, Long, Double, Float, Short, Byte, Character all are subclass/Children of Number.
Catch mechanism is same with the number, If its assigned a value which is in catch then address of that object will be return.
Hence, from original example:
Number myNumber1 = 10;
Integer myInteger1 = 10;
Integer myInteger2 = 10;
Prathima gaitonde wrote:3> When a primitive is compared with wrapper, its respective method is called, ex: for Integer with any primitive, Integer.intValue() and then 2 primitives are compared,
But Number, is there a equal primitive value called num? Oh!!!, no compiler shouts, so
Number n==int i ;//Number.numValue() no such method exists hence compile time error.
Prathima gaitonde wrote:4>Double d==Integer i
No Cat is indeed different from Dog (Might be they are sharing the name, I don't care), I am not a fool to compare them says the compiler!!!
Prathima gaitonde wrote:5>Nice to know tip....
Integer i=new Integer(10);
Short s=new Short(10);
i.equals(s)//false
Prathima gaitonde wrote:Chat appart answer for point 5 is equals method in all the wrapper class(and String) first checks for the same instance then, the value.
Prathima gaitonde wrote:6>Integer i=new Integer(10);
Integer j=10;
Why is it not, i==j, be i.intValue()==j.intValue()?
If this is what compiler does than, How can I compare the address of i and j for equality?
Prathima gaitonde wrote:7>Still not 100% clear point is,
Short s=10;
s==10.0// No compilation issue as s calls shortValue() i.e, s.shortValue()==10.0
Here right hand operand is primitive, remains primitive, and its double. No question of it getting changed.
Left hand operand is an object, which now has to be converted as primitive, and it will allways call its respective wrapper method as explained it point 3. so s.shortValue() is called,
Doubt :
1>((double) s.shortValue()) Is this a casting? How does this work, though I do understand that, short s=10, double d=10.0, s==d are equal but don't know much about, compiled code, i.e how does it work internally!!!. First int is assigned to short, second short is compared with double
Prathima gaitonde wrote:Double d=10.0;
Number n=10.0;
d==n// No compilation issue as d is n. prints false as, Double are never cached, hence d and n are separate objects.
Prathima gaitonde wrote:Where as
short s=10;//A primitive value short
Number n=s;// Object 10 catched into short. Its address say add1
Short myShort=10;//gets add1
myShort==n//true
Prathima gaitonde wrote:"The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive)."
I think this is the reason for, behavior of the above code
Prathima gaitonde wrote:The Out put of the above code is ?,?,? in my computer.
Is it depends on the compiler? I mean to say Does out put of this program, differ from computer to computer?
The reason I am asking this is, If such questions were asked in the exam, is the out put unpredictable?
Roel De Nijs wrote:
Prathima gaitonde wrote:1> Double d=10;
Here 10 is an int primitive, d is an object hence, primitive should be promoted to object, so...
d=Integer.valueOf(10);//compiler try to promote.
d being Double and sibling of Integer says to the compiler, Hello I am not Integer, I am a double. Hence compile time error.
Correct!
Prathima gaitonde wrote:After so many days, I got this doubt, why Short s=150; is not a compiler error?
s=Integer.valueOf(150);// Short=Integer!!! As valueOf() method returns Integer object.
How can an Integer object fit into Short?
Sorry if I am missing something which is already there in the previous discussion.
Roel De Nijs wrote:
As you know the compiler is a very smart cookie.
A little pop quiz: what happens if you use a value outside the range of a short? Like:
Prathima gaitonde wrote:A little pop quiz: what happens if you use a value outside the range of a short? Like:
Roel De Nijs wrote:
Now you can earn some bonus points:
1/ how can you make this code compile? (You can't (of course) change the type of variable ss and the value must be boxed, so you can't use constructors/methods of Short)
2/ what's the output of System.out.println(ss);?
Prathima gaitonde wrote:Even now I cant figure out why is casting(short) works fine, and not (Short). If you can explain me this, that would be great.
Roel De Nijs wrote:
Now the last tricky statement. This statement prints indeed false. The explanation is very similar to line17. It compiles successfully, because Short IS-A Number. And we know (from explanation line17) that myNumber1 refers to the same cached Integer instance as myInteger1. So myNumber1 can never refer to the same (cached) Short instance as myShort1, hence false is printed.
Sharmili Rameshbabu wrote:Why is it. is it because Short myShort1 = 10; is decompiled as Short myShort1 = Short.valueOf(10) ? then also should it not refer to the same literal in the cache?
Another nice to know: if you would compare a Short and an Integer both having the same value (e.g. 10), false will be returned. So myInteger1.equals(myShort1) will always return false. Reason: the equals method of a wrapper class will only return true if and only if the argument is not null, has the same type and contains the same value as this object.
Sergej Smoljanov wrote:equals from Integer
Sharmili Rameshbabu wrote:According to the abovelogic, should not we get a compile time error if we are trying something like this:
Because compiler knows b1 is not of type Integer?
Sharmili Rameshbabu wrote:I did check the equals method of Integer, but my doubt is, compiler knows the reference type of b1, and Byte does not have a relationship with Integer, So should it not be telling us, this will never return true?
Sharmili Rameshbabu wrote:Can you please tell what is happening here..
I was expecting this to print false but it prints true
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Sharmili Rameshbabu wrote:Can you please tell what is happening here..
I was expecting this to print false but it prints true
Roel wrote: In this post is explained why you get true (instead of false). You are comparing a primitive and a wrapper using the == operator, so the wrapper will be unwrapped/unboxed and thus you compare 2 ints, both having exactly the same value (8).
Sharmili Rameshbabu wrote:So the references are different and hence i thought i==j should print false.
Sharmili Rameshbabu wrote:
I completely agree to this, but i was misguided by the fact that when we use a new keyword, the integer cache is not used. So
creates a new object in heap and
is seen as Integer j=Integer.intValue(8); and uses the cache. So the references are different and hence i thought i==j should print false.
To be frank, am reading this complete thread for the third time and am able to convince myself for true as well as for false!
Puspender Tanwar wrote:even though 10 is also in range of Short and Short IS-A Number, then why at //line3, Number is not using Short.valueOf(10);
Puspender Tanwar wrote:if
will the compiler use ??
nick woodward wrote:is all of this in the oca 7 scope?
pretty sure mala's and the k&b book didn't go into anywhere near this detail. i didn't even know there was an equivalent to the string pool.
nick woodward wrote:wow.
is all of this in the oca 7 scope?
pretty sure mala's and the k&b book didn't go into anywhere near this detail. i didn't even know there was an equivalent to the string pool.
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|