Win a copy of Spring Boot in Practice this week in the Spring forum!

Bill Krieger

Ranch Hand
+ Follow
since Sep 27, 2001
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Bill Krieger

Originally posted by Justin Poggioli:
what is a poofter???

It means light in the loafers.
20 years ago
It's true that a cast at compile time will always compile when the object reference is not to a final class, but you don't have any casts in you code. Try the following:

With the cast, the code will compile and throw a run time exception.
I think the main problem here is the inappropriate use of inner classes. Your Employee class, Manager class, etc., should not be inner classes. As inner classes they require an instance of a Test object to be instantiated. There is no relationship between an employee and a 'test.'
20 years ago
You can have a class in a file where the file name is not the same as the class name, as you demonstrated. You cannot run its "main" method, however. The main method you want to invoke from the command line must be in a public class whose name matches the file name.

Originally posted by David Garland:
It compiles because you are allowed to attempt any cast, even if the cast definatly won't work at runtime (as in this case).

Actually, this isn't entirely true. Consider the following code.
1. Object o = new Object();
2. Runnable r = (Runnable) o;
3. Exception e = (Exception) r;
4. r = (Runnable) e;
5. String s = (String) r;
Clearly lines 2 to 5 will not work, since an Object is not an Exception or String, nor does Object implement Runnable. The point is, the compiler objects to line 5, because the cast can never work. It accepts lines 2 through four, because they might work, sometimes.
Line 2 would work if the reference o pointed to an Object that extends Object and implements Runnable. Such an object can always be refered to by an Object reference or a Runnable reference.
Line 3 would work if the reference r pointed to an object of a class that extends Exception and implements Runnable. Such an object can always be refered to by an Runnable reference or an Exception reference.
Line 4 would work if the reference e pointed to an object of a class that extends Exception and implements Runnable. Such an object can always be refered to by an Exception reference or a Runnable reference.
Line 5 would work if the reference r pointed to an object of a class that extends String and implements Runnable. Of course, no such class is possible. String can never be extended, and String does not implement Runnable. The compiler knows this, and it won't let this statement compile.
Any class that can be extended, can have subclasses that implement any interface. The compiler allows free casting between references to such classes, and references to interfaces. A reference to a 'final' class (e.g. String) cannot be cast to an interface, and vice versa, unless the class actually implements the interface.
When casting a reference to an object of one class to a different class, things are a considerably stricter. Any subclass of Exception can implement any interface, but it will never be a subclass of ArrayList. Therefore, the compiler will not allow casting an ArrayList reference to Exception, or vice versa. The object reference type, and the type it's being cast to, must exist in the same object hierarchy. For example, the compiler allows you to cast a reference of type Exception to InteruptedException. If the reference is actually an AWTException, then a runtime error occurs.
20 years ago
The variable 'new_letter' is a char primative. Primative values are not objects, they have no member variables or methods. Perhaps you meant to us the Character wrapper class? Perhaps...
new_letter = Character.toLowerCase( letters[(int)x] );
20 years ago

Originally posted by Vikrama Sanjeeva:
...What about Threads....

Thanks, everybody.
The thread questions were not too bad. You need to know the two ways of creating a thread (implementing Runnable and extending Thread). You need to know the difference between the run() and start() methods. You need to know that invoking run directly will cause things to happen, but not kick off a separate thread.
There were questions about synchronized code, very similar to those you see in mock exams. You know, can the order of the output be determined when you kick off multiple threads? When can updates be guaranteed to occur in pairs? There were several questions along those lines. The Mughal and Rasmussen book contained all I needed to know about threads, but I did spend some time studying that chapter until I was sure I had it down.
I think I got 5 or 6 questions on threads, so you do need to know them.
20 years ago
Like the subject says, I passed my Sun Certified Programmer For Java2 this morning, with 55 out of 59 questions correct.
To prepare, I used "A Programmer's Guide to Java Certification" by Mughal and Rasmussen. Actually, this was with a group of people from work, and we covered one chapter per week. On my first pass through the book, there was a lot that didn't stick. I made a second pass through the book on my own, and made detailed notes on things that weren't clear to me. That helped a lot. I made only one additional pass through my detailed notes. The value there was in writing them in the first place.
I also read this forum a lot. Following the discussions of mock exam questions, and the Java language in general, has been very helpful. Being on JavaRanch helped me a lot.
I took a lot of Mock exams. At least two per day for the last two weeks. Pretty much in order:
JavaRanch Rules Roundup 100% usually, by the end
Marcus Green 1 82%
Applied Reasoning 73%
John Hunt 86%
Marcus Green 2 83%
Brogden 80's to 90's
Brogden Hardest 89%
Khalid Mughal 86%
Boone 81%
RHE (CD borrowed from a friend) 84%
RHE Bonus 1 on CD 92%
MindQ (???)
Guoqiao Sun 1 75%
Guoqiao Sun 2 71%
Guoqiao Sun 3 83%
There were a few others I didn't keep track of.
On the mock exams, one third to half my errors were dumb mistakes where I knew better (with the greater percentages on the later exams). About half of what remained were things I did not know. The remainder of the mistakes were due to badly worded and/or erroneous questions. (In spite of what some mock exams say, I still believe you can not get a lock on a synchronized method, only on the underlying Object or class) Also, Guoqiao Sun has a couple of silly questions based on when two computed floating point numbers are compared for equality. I mean, who cares when two computed floating point numbers happen to accidentally be equal. Please don�t think I�m picking on Guoqiao Sun, his mock exams are really very good. Many of the mock exams have silly questions, some of which I missed.
On the real test, I think I eliminated my dumb errors. I finished one pass through the exam in 55 minutes. I spent maybe 15 minutes on the four hardest questions, and finished the remaining 55 in a half hour. I then had plenty of time to look over each of my responses a second time. I ended up changing a couple of answers on the second pass. I still left the exam with over a half hour of time remaining.
If anyone is still reading this, I'll just add that my background has been mostly C programming for seven years up until two and a half years ago. Then we started doing C++, where I made a conscious effort to learn object oriented programming. For the past year, I've been doing Java at a new job. I was fortunate enough to find an employer who would allow me to learn Java on the job, given my background in object oriented development and SQL. I've been playing at studying for the certification since May. I've been intensly working on it for the past four weeks. Now, I'm certified.
20 years ago

Originally posted by Jane Griscti:
The methods can only be called from synchronized code so 3 looks ok.

This is not what the question is asking. It's asking whether you can have a lock on a synchronized method. When you invoke a synchronized method, you obtain a lock on the object. That lock prevents other threads from executing any syncronized method or synchronized code block from that object. To claim that onecan obtain a lock on a synchronized method is false. The lock is on the object. I say 3 is not okay.
In the Marcus Green mock exam 3, question 27 reads:
Given the following class

What will happen when you attempt to compile and run it?
1) Compile time error
2) Output of "Hello Crowle"
3) Output of Hello Crowle followed by Borcetshire and Powick
4) No output
The answer is correctly given as 2. If this question is testing knowledge of the short circuit logical operators, then answer 3 is the wrong incorrect answer. It should read:
3) Output of Borcetshire and Powick followed by Hello Crowle
Someone with no knowledge of short circuit could still figure out that Hello Crowle will never precede Borcetshire and Powick, it could only follow them.
Is this being too picky, sugesting that a mock exam contains the wrong incorrect answer?

[This message has been edited by Bill Krieger (edited November 29, 2001).]
20 years ago
As the others have explained, if you exclude line IV, then no object of type fruit is ever instantiated. The problem, of course, is on line III. Every time you create an object of type Fruit, it creates an additional object of type Fruit at line III. The additional object creates yet another object of type Fruit at line III, which creates yet another object of type Fruit at line III, which creates yet another object of type Fruit at line III, and so on. Eventually, you run out of memory.
The more intersting case is when you include the static instance at line I. When you create the Fruit object in class Tomato, the first thing that happens is static initialization. At line I, a Fruit object is created during static initialization. Apparently, static initization is interupted while a single Fruit object is created and a reference to it is assigned to f. Then static initialization resumes and ultimately a second Fruit object is created and a reference to it is assigned to f in Tomato.main.
This is illustrated in the following code:

The output is:
i = 0
i = 5
In main, i = 3

This is strictly a matter of overloading. You haven't overridden any methods.
An object of class mamma has one eat method available:
void eat(mamma m)
An object of class cattle has two eat methods available:
void eat(mamma m) // inherited
void eat(cattle c)
An object of class horse has three eat methods available:
void eat(mamma m) // inherited
void eat(cattle c) // inherited
void eat(horse h)
When you invoke "", h is a reference to a mamma object so it invokes the method eat(mamma m).
Of course, none of this works at all since the System.out object has no pritln method. Change the pritln to println and it should work.
Question 21 asks:
How does the set collection deal with duplicate elements?
Based on the answer, it's clear this question is asking about the Set interface which extends the Collection interface. It seams to me that the word "Set" should be capitalized. Or somehow make it clear what is actually being asked about here.
Question 39 asks whether the following is true:
4) The set interface is designed for unique elements
This answer is given as being true, but I don't believe it should be. Not unless it's changed to:
4) The Set interface is designed for unique elements
The Set interface may be designed for unique elements, but there is no set interface in the standard Java libraries. I've seen mock exams that try to trip you up on these issues, e.g., you get a compile error because the code uses While instead of while. I naturally assumed that was the point of this question. That is, don't fall for the bogus "set" interface and go treating it like the "Set" interface.
20 years ago

Originally posted by Dave Vick:

You could convert the vecotr to an array and sort it using one of the sort method in the Arrays class. You'd have to create a comparator to do it but if your vector is fairly large this would probably be more efficient than writiing your own sort method. something like this:

Rather than convert it to an array, you could just use the Collections sort method. To modify your example:

Next, without changing the Vector to an array:

20 years ago
The problem is not that Object has no "add" method. As you correctly figured out, that isn't what's happening here. The problem is that Frame has no "add" method that accepts an argument of type Object. You can only "add" Component's and PopupMenu's. You can't "add" Object's. Even though it happens that the Object reference is actually pointing to a Component (Button), the compile will require an explicit cast to Component or to a sublcass of Component before it will accept the statement.