Dave Terrian

Greenhorn
+ Follow
since Jun 25, 2000
Merit badge: grant badges
For More
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 Dave Terrian

I believe this is in the JLS Here.

If doing a cast of the R ordinary class to the T ordinary class type, ie: (T)R, then R must be either the same class as T or a subclass of T, or a run-time exception is thrown.


In your example, (Subclass)B, consider:

As Jose said, at runtime it makes no sense to try to refer to your object as a String. o (R) is not a subclass of String (T).
In other words, you can refer to an object using a more general reference in the class hierarchy but not a more specialized one.
Something like that
This might be what you're referring to:
http://cseng.aw.com/book/0,3828,0201596148,00.html
Click on the Sample Chapters link on the left.
- Dave
We can "say, loosely, that the static keyword always marks a 'top-level' construct (variable, method, or class), which is never subject to an enclosing instance."
"This shows why an inner class cannot declare a static member, because the entire body of the inner class is in the scope of one or more enclosing instances."
See here
- Dave
This might be useful:
Runtime Exceptions--The Controversy, part of the Essential Java Classes trail of the Java Tutorial

[This message has been edited by Dave Terrian (edited April 27, 2001).]
Oh I'm not a thread expert by any means.
In my simplistic view of the world sleep will pause a thread (move it from a running to a blocking state) until a specified time has elapsed.
join will also pause a thread but it is used to block as you wait for another thread to end (move to a dead state). There's also a combo variation that will pause while you wait on another thread to die but after a maximum duration, the thread moves back to a runnable state.
In other words, sleep is used if you just want to wait for a while and join is used if you started another thread to do some processing and you don't want to continue until that thread is done.
You might use sleep if you want to show a splash screen when your application starts up. You might use join if you started a printjob thread and one that spins an hourglass cursor and you want the main process thread to wait until the printjob finishes to signal the hourglass to stop.
There's also a yield method that puts your thread back into the ready state - essentially giving up the current running state and signaling to the scheduler that another ready-thread has the chance to jump in (if there is one).
Corrections or additions are welcome,
- Dave
How about:
try {thread.join();} catch (InterruptedException e) {}
I was a little surprised by the ending " + + k". Is there any significance to " + + " or is it just taken as "+0+" by the parser? I notice you can also do this and get the same result:
int i = ++k + k++ + + + + + + + + + + + + + k;
Thanks,
- Dave

Why would you give us code with 13 errors in it? :P
You said: It will print out "a.i=10"(not a.i=10!)
Umm, it prints out 0 when I run my fixed up code. The thread is sleeping while the main process thread prints out the value. If you made the main process sleep 2 seconds before printing the value of i, you'd likely see the result of 10. Anyone agree?
class A implements Runnable{
  public int i=0;
  public void run() {
    try {
      Thread.currentThread().sleep(1000);
    }
    catch (Exception e) {}
    i = 10;
  }
}
public class B{
  public static void main(String args[]){
    A a = new A();
    Thread thread = new Thread(a);
    thread.start();
    System.out.println("a.i="+a.i);
  }
}
- Dave
Compiler errors out? Worked fine for me
Ok, you're all confusing me. C says that it's illegal. Because you can do this (TestAbstract t) then C is not a correct answer. The answer's A and B, right?

[This message has been edited by Dave Terrian (edited April 23, 2001).]
Wow, I was a little skeptical that it calls the super's constructor before initializing instance variables but you're right. By changing the code to this:

We see that x remains 0 until after the super is called.
Two questions:
Is this something you knew by experimentation or would it be found in the JLS or in a certification book? I've never seen the construct of an instance initialization block like that in my studies.
Is this really something you find in the exam?!
- Dave
The answer can be found in the JLS. Also see example 6.6.7.
Basically I think because i is a protected member of A, and B extends A,
B can access i if:

  • It does so without going through another reference (i=i*2)
  • Or it does so using a B reference.

  • In your example, B is accessing i through an A reference (a.i=a.i*2) which isn't allowed.
    - Dave
Curious,
I just read in Bill Brogden's Exam Prep (p.53) that the only way to detect NaN is with the Float.isNaN or Double.isNaN method.
I also read in The JavaRanch SCJP FAQ that:
>x!=x is true if and only if x is NaN
(Don't kill me Bill, but) couldn't one conclude that actually you could, however silly it would be, detect NaN by doing all of the following:
if (Float.isNaN(x))
if (x!=x)
and even if ((""+x).equals("NaN"))
- Dave
You were correct in saying that BorderLayout honors the preferred height for north and south positioned components.
Preferred size is determined by Component.getPreferredSize(). I believe by default an AWT component gets this information from its peer but of course you can override it and thereby set any dimension you want. With Swing components, you can change the preferred size by simply using JComponent.setPreferredSize().
Here's a snippet of the source code to BorderLayout's layoutContainer method:

So you can see that it really does use the preferred size for north and south positions. Also, here's some code that demonstrates setting the size of a northern positioned AWT button:

(My first time answering someone's question on here! Let's hope I'm right... )
- Dave

[This message has been edited by Dave Terrian (edited April 21, 2001).]
Here I go again - digging out the certification books, reviewing JavaRanch posts and showing off to coworkers about my understanding of shadowing and shift operators. I've done this a couple times now spanning a couple years. I usually get through chapter 6 and a couple practice exams before the idea of certification gets brushed under the rug (and the knowledge of operator presedence becomes a fleeting memory). This time I don't want to go unfulfilled (darnit!). So as I'm getting back into things, does anyone have a motivational speech they'd like to share? Suggestions for pacing myself, getting over the fear of not passing or ways of finding time to study and sticking with it?