What is the meaning of using the wildcard notation? I read that it means nothing can be added. Then what is the point of creating a list if nothing can be added?
Also what is the difference between <? extends Integer> and <?> in the reference?
Maduranga Liyanage wrote:
2) Please look at the following code (taken from a practice test from the ranch):
The explanation says:
lthree and lfour are two seperate objects. if the lines 1 and 2 were lthree = 2 and lfour = 2 the result would have been true. This is when the objects are created in the pool. When the references i and eye in the pool are compared 2==2 results in true and 2000==2000 is false since it exceeds 127
First I dont get why 2000==2000 is false and the relationship with the 127.
If the value p being boxed is true, false, a byte, a char in the range \u0000 to
\u007f, or an int or short number between -128 and 127, then let r1 and r2 be
the results of any two boxing conversions of p. It is always the case that r1 ==
r2.
Maduranga Liyanage wrote:
Secondly, the '==' operator compares the addresses of the objects. So prints 'false' in the println. But when compared inside the 'method' why does it return true?
Does the method autounbox the Integer obects?
If so can't I use any Interger class methods on the objects passed in to the 'method'?
All code in my posts, unless a source is explicitly mentioned, is my own.
Maduranga Liyanage wrote:Thank you very much for the replies.
I still got some confusion over this.
1) So what the objective means is, if I box an int in the range -128~127 twice, the resulting objects will be the same?
for example;
Integer i1 = new Integer(127);
Integer i2 = new Integer(127);
Integer i3 = new Integer(125);
Then, i1==i2, but i2 != i3, correct?
So i1 and i2 actually point to the same object? Is this because Integer objects are immutable, like String objects?
Maduranga Liyanage wrote:
2) I still dont understand the concept of declaring a List in this manner. Maybe it doesnt make sense, but I found it in a practice exam, and would like to know its meaning.
a. List <? extends Integer> intList = new ArrayList<Integer>;
b. List <?> intList = new ArrayList<Integer>;
The explanation said that nothing can be added to the List created this way. Then whay does Java allow to instantiate a List in this manner, if nothing is allowed to be added?
Thank you very much.
All code in my posts, unless a source is explicitly mentioned, is my own.
Maduranga Liyanage wrote:Thank you very much Ruben.
I understood the Integer problem.
But still not 100% on the Generics. It is after all a very annoying topic.
I don't understand how I can 'access the elements' if I cannot put anything into the list in the first place?
And initializing with <?> means I can add anything or nothing? If I can add anything, then what is the meaning of the <Integer> in the right hand side?
Thank you very much.
All code in my posts, unless a source is explicitly mentioned, is my own.
Maduranga Liyanage wrote:Thank you soo much Ruben. That was great. Actually it also cleared some other confusions I had with Generics.
Thank you again.
Cheers.
All code in my posts, unless a source is explicitly mentioned, is my own.
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
|