Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!

Clay Chow

Ranch Hand
+ Follow
since Nov 09, 2008
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Clay Chow

Ruben Soto wrote:\


In this case, the compiler is preventing you from declaring an A<K> reference where K is out of bounds. You could argue that the compiler could actually let this code be legal, and check on the bounds when an instance of A is instantiated, but this is not the actual problem in this case. The actual problem is that now k is a reference of A which might be out of bounds, and you might be able to add to an actual A instance (which will be within bounds) passed as a parameter to the method an element of type K, which will break the type safety of the collection (since K is out of bounds.) This couldn't happen when the parameter had a wildcard, since you can't add elements through generic references with wildcards.

Let me know if you see any flaw in my reasoning, Ankit. And again, thanks for the great explanation.



I think I am following. I agree with why/why not the return type generics compile/do not compile. But have a question with the parameter ones.


So you are saying:

(1) public abstract <K> A<? extends Number> useMe(A< ? super K> k);

(2) public abstract <K> A<K> useMe(A<K> k);

(3) public abstract <K> A<? super Number> useMe(A< ? extends K> k);


One question:
For (3), the parameter generic does compile because you are unable to add to the parameter collection. (2) would not compile because one would be able to add to the collection. However, (1) compiles, and you can still add to the parameter collection (because a collection reference with generic super, allows additions, sometimes though). So maybe this still enforces the class generic declaration.

Also, in this example, 'A' is not a collection, but just another Object that doesn't do much. However, if A was a collection (say it did extend ArrayList), you would still be able to add to the parameter collection in (1).


Another question:
It was said that the parameter generics in (1) and (3) are valid because the type parameter <K extends Number> would be checked when the 'A' is instantiated.
Could this not also apply to the parameter generic in (2) ? Since the A<K> would be instantiated (and therefore be valid) before it is passed into the method, it stands to reason that K must therefore follow <K extends Number> and would therefore be valid to add elements of type K into that collection.
hey,

Thanks.
I did do some searches, based on the code statements.
I guess it doesn't search through that.
Source: Examlab for SCJp 1.6 - Diagnostic Exam - Question 57




For the above code, I do not quite fully understand why the below two method declarations compile.

public abstract <K> A<? extends Number> useMe(A<?super K>);

public abstract <K> A<? super Number> useMe(A<?extends K>);



Is it because in <? super K> and <? extends K>, the 'K' refers to the 'K' in the CLASS declaration ?
If so, then A<?super K> may still fail the class declaration evaluation of <K extends Number>.
Is that possibility just ignored ?


x = x++ + x--;

i brake it down as.
x = (x++) + (x--); x = 45
x = (45) + (x--); x = 46
x = (45) + (46); x = 45;
x = 91

post-increment means you use the current value of the variable first, then the variable value changes. I think you get that.
But in long statements, the statement is evaluated from left to right; rather than evaluate all the variables first, then the post-increments and pre -increments. Therefore, the variable value, x, is changing even as this statement is completing.

x = x++ + --x; = 90

\
hope that helps
Hi,

Would it ever be possible to see a generic method declartion that looks like K<K>. I have tried and not been able to get one to compile. For example:



Also, I do not fully understand why this will not compile.

13 years ago
Definitely do not.
Did not make one and

Made sure there are none in the directory and program is not linked to any package.

From the java API i see there's another Object interface, org.omg.CORBA
Interface Object.

13 years ago

For the below code, I get the following errors:


inconvertible types
found: Object
required: Fruit
if (o instanceof Fruit)
Fruit a (Fruit) a


I am using a java/javac version 1.6. Any idea why this is not working for my compiler ? I assume it should work because all classes inherit from Object. I thought it was referring to the wrong Object class, so I changed the method to public boolean add(java.lang.Object o), which seems to work.



13 years ago
Hi,
I am studying for my SCJP and wrote this little bit of code to figure out how polymorphic calls work.




Which outputs:

fruit
Apple
fruit
Apple
fruit
fruit


Expected:
Apple
Apple
Apple
Apple
fruit
fruit

Question:

why does line A1 not retrieve the instance value ? Even though at compile time, it reads the reference value, at runtime it should read the instance value.

13 years ago
Thanks for your help.

I will look up the Integer Cache topic.

Did you agree with my assessments in the opening post ?

Also, when a class is instantiated, does it also create all the parent objects as well (i guess when it calls the parent constructors. Therefore, in the code below, there would be two object(an 'Over' and an 'Under') eligible for GC after the 'a=null' line.



[ December 15, 2008: Message edited by: Clay Chow ]
13 years ago
So when a class inherits a method, would it be right to think of it as a copy of the method is placed in the inheriting class ?

That's how I used to think of it, but after experimenting w/ the code below, I see it more that the parent classes are created as well.

The code below outputs 'one'. While I would expect 'two'. I would expect the "return (this) name" line to point to the 'Over' class when referenced from the Over reference.



13 years ago
Thanks for the advice, but that question is less about "is that object gc eligible?" and more about "was there a new object created?".

I still am confused on the situation above (any help would be great).

Also, another example I thought of would be below. Am I right in thinking there are two Integer objects created (one for 6 and one for 7) ? and would that mean that after the "y++" line, there is one object that is garbage collector eligible (i.e. new Integer(6)) or would it not be eligible because they are like immutable (like Strings are).

thanks!

[CODE}
Integer y = new Integer(6);
y++;
[/CODE]
13 years ago

Originally posted by Clay Chow:
So another question on objects:

Since an array (whether it be an array of primitives or objects) is an object itself, then a 2D array would create (number of elements in first dimension plus one) objects ?

For example, in the below code.

Line 1 creates 4 objects.
Line 2 creates 1 object (?)

After line 4, one object is eligible for gc (the original array at x[0]).
However, after line 5, there is still only 1 object eligible for gc (since there was not an array at x2[0] originally).

Thanks in advance for your help!

13 years ago
I know that method-inner classes do not have any access modifiers (like other local variables). But I see that the members of the inner class can have access modifiers. However, it appears these are ignored.

For example, the code below produces "A\nB". When I would expect there would be a compiler error on line 16.

Also, another question, how would i then be able to use the object returned by "getClassy" (i.e. call "doB()" from Object a) ?






[ December 14, 2008: Message edited by: Clay Chow ]
[ December 14, 2008: Message edited by: Clay Chow ]
13 years ago
The first line in each constructor is either a call to a super constructor or a call to another constructor of the same class. If this is not written explicitly by the programmer, the compiler implicitly will put a call to "super()". Which what the compiler does to your B() constructor.

However, super() in this case is A(), which does not exist.

To solve,
just put a A() constructor in class A.

Hope that solves it.
13 years ago
So another question on objects:

Since an array (whether it be an array of primitives or objects) is an object itself, then a 2D array would create (number of elements in first dimension plus one) objects ?

For example, in the below code.

Line 1 creates 4 objects.
Line 2 creates 1 object (?)

After line 4, one object is eligible for gc (the original array at x[0]).
However, after line 5, there is still only 1 object eligible for gc (since there was not an array at x2[0] originally).

Thanks in advance for your help!