Gil Steidle

Ranch Hand
+ Follow
since Nov 19, 2010
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 Gil Steidle

Question from Exam Lab's Java Programmer 6.0

How many objects are eligible for garbage collection after c.aob=null?
The correct answer is 2. I can only find but 1.
The A() object that was created by c=new A() is the one that I found as eligible for gc.

Thanks,
Gil


Paul, thank you for your thorough explanation. I appreciate it.
Here's the semi-colon that is missing ;
Sorry, but I can't seen to wrap my brain around this concept yet. In the code example below is my confusion.

How should I approach solving this puzzle of being able to use an Apple or Macintosh or Red Delicous or maybe an Orange Fruit list?

This means that it will only allow you to add objects that IS-A Apple, IS-A Macintosh, IS-A GrannySmith, and IS-A anything else that IS-A Apple.



Does this mean...

This means that it will only allow you to add objects that IS-A Apple, AND IS-A Macintosh, AND IS-A GrannySmith, AND IS-A anything else that IS-A Apple?

Given the current code no class meets that criteria, is that how you got to...

The only possible item that can be added, because it can be assigned to all of those reference types, is null.



If I'm following you then I would need to generalize the method's add to accept any Type that is allowed. I changed the method to this



Unsuccessful. Help.
Ok, I think the light bulb is getting a little brighter. Thanks for responses. I tried flipping the logic and used "extends" in place of "super" to get the classes Apple or lower. This doesn't work. I thought it would. What am I still missing?

New code and results...




C:\JAVA\SCJP\StudyGuide Exam>javac Macintosh.java
Macintosh.java:17: cannot find symbol
symbol : method add(Apple)
location: interface java.util.List<capture#28 of ? extends Apple>
list.add(new Apple()); // NO, now it does not work with extends
^
1 error

C:\JAVA\SCJP\StudyGuide Exam>
This abbreviated code is taken from a question in the SCJP 6 Study guide. My confusion is over what the <? super> is doing. From reading the book I (mis)understand that anything "higher" in the class heirachy should be allowed??? So, if we have class tree of: Fruit, Apple, Macintosh why can't I stick my Fruit into my List<? super Apple> list? Just the opposite is happening, I can put a Macintosh into the list even though the Macintosh is "lower" on the tree (no pun intended). I thought that List <? extends Apple> would allow this behavior? Code and output are below.


Thanks for your help with this,
Gil





Results:

C:\JAVA\SCJP\StudyGuide Exam>javac Macintosh.java
Macintosh.java:16: cannot find symbol
symbol : method add(Fruit)
location: interface java.util.List<capture#972 of ? super Apple>
list.add(new Fruit()); // No
^
1 error

C:\JAVA\SCJP\StudyGuide Exam>
Chris, thanks for the link it helped sink this concept in a little more. Am I correct in saying that even without a notify (or notifyAll) that a lock will be released when a synchronized method or code block finishes? I'll run some tests to find out. But can someone confirm or deny?
So, in other words a notify() will not release the lock until the encapsulating synchronized code block is finished?


The revised code is above and the results are below. Any thoughts?


C:\JAVA\SCJP\ch9>java ThreadA
Waiting for Thread b to complete.
0
1
3
6
10
15
21
***Notified***
28
36
45
Waiting no more.
Total: 45
I should have included this originally, sorry. This is the output I am seeing.

C:\JAVA\SCJP\ch9>java ThreadA
0
1
3
6
10
15
21
***Notified***
28
36
45
Waiting for Thread b to complete...
Total: 45

But I am expecting something like:

C:\JAVA\SCJP\ch9>java ThreadA
0
1
3
6
10
15
21
***Notified***
Waiting for Thread b to complete...
Total: 45
28
36
45


Greetings,

I am not understanding what the notify() method is doing. In the example, I am expecting the the notify() to wake up ThreadA and let it resume and print the total (which is 21 at the time of notification). Instead, ThreadA always prints 45.

Thanks for your help.




Thanks for the link. It explained my scenario perfectly. I'm frustrated with the authors for not mentioning this non trivial jls spec but including it on a question. At least I don't remember reading about it before. Anyway, I'll just keep plugging along through the practice tests.

Thanks!
10 years ago
I missed this question in the scjp 6 study guide and am hoping for clarification. Chapter 4 question 6:


I thought the instance code block would execute before the member variable "index" was defined and so not compile, which is wrong. This does compile and that tells me that the member vars get defined prior to the instance code block being executed.

Is there a complete listing somewhere of the sequence of events that occur (as far as scjp 6 is concerned) when you start a java program and when you instantiate a class? For example: Class vars are defined, main method, local method vars defined...

Thanks,
Gil
10 years ago