James Baud

Ranch Hand
+ Follow
since Jan 06, 2001
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 James Baud

It's baaaaack. Thanks everyone. You never know how important something is until it's gone !
22 years ago

Can anyone explain why this prints 1 if f() is private in PolyB, else if public it prints 2?
------------------
~James Baud
He who asks, is a fool for five minutes;
but, he who does not ask, remains a fool forever. (Chinese proverb)
[This message has been edited by James Baud (edited April 10, 2001).]
22 years ago
The navigation tree has disappeared from my forums summary page since couple of days ago. The page I am referring to is: http://www.javaranch.com/cgi-bin/ubb/Ultimate.cgi?action=intro&BypassCookie=true
The only way I could go to a forum right now is to do a search on a general word like "java", click on a particular forum> click on topic of search result> then click back to that forum where I wanted to go. Obviously, this is ridiculous but it's my only option for now.
Please help me restore my sanity, er... my forums summary page with the navigation tree. I'd appreciate your help.
------------------
~James Baud
He who asks, is a fool for five minutes;
but, he who does not ask, remains a fool forever. (Chinese proverb)
22 years ago
Manfred, I'm with you 100% that static methods cannot be overridden, thx - sorry for the inappropriate use of the term and the concept. The only satisfactory correction availabe therefore is to call getType() via an object reference.
------------------
~James Baud
He who asks, is a fool for five minutes;
but, he who does not ask, remains a fool forever. (Chinese proverb)
22 years ago

Originally posted by Zahid, Butt:
Hi ALL,
By the way, the question was from JQ+, under the
"Easy Questions" section of mock exams.
Thanks.


I'd be interested to know if the "is-like-a" concept is used by somebody else other than Bruce Eckel in TIJ. What is more perplexing is for a great mock exam like JQ+, to put this concept as part of their test. From what I understand, the concept of interface falls under the "is-a" relationship since Java does not support multiple inheritance, ie class JamesBondVehicle extends Car, Airplane, Boat. Inasmuch as an abstract class provides for a protocol for certain behavior for its subtypes through abstract methods, possibly, so does the interface. Observe closely that Bruce Eckel claims ownership of the "is-like-a" term in TIJ.
Try to remember that the concept of multiple interfaces may be just one form of inheritance - subtyping to combine behavior. Thus if we were to follow the logic of having "is-a-" name for the different forms of inheritance outside of "strict" inheritance we need to have names for the following:

    Specialization
    Specification
    Construction
    Extension
    Limitation
    Variance
    Combination - multiple inheritance

    ref. Object-Oriented Programming - Timothy Budd 2nd Ed. CH7 - Inheritance

    ------------------
    ~James Baud
    He who asks, is a fool for five minutes;
    but, he who does not ask, remains a fool forever. (Chinese proverb)
Ooops, sorry 'bout the slip. What I meant was, option C would have been correct if "both" overriding and overridden getType() methods were static. Or, another option would be to call getType() in main() through a OverType object reference, ie new OverType().getType(...). My point is, the program will not compile as it is, given option C.
Lam, try to compile the original code w/ option C and you'll see what I mean.
------------------
~James Baud
He who asks, is a fool for five minutes;
but, he who does not ask, remains a fool forever. (Chinese proverb)
22 years ago
I tried the Sun sample questions today at
WGS-PREX-J000: ePractice Sample Questions On item 6 the code goes like this:

Question: When inserted on line 10, which line will override the getType method and allow compilation to succeed?
Answer choices:
A o public Text getType(String a, int b, char c) {
B o public Text getType(int b, String a, char c) {
C o public String getType(String a, int b, char c) {
D o public String getType(int b, String a, char c) {
Sun's Answer:
Option C is correct because the method name, argument list, and return type are the same as the getType method defined in the IntType class. Overriding methods must be declared with the same
name, argument list, and return type as the overridden method.
Option A is incorrect because the return type is not the same as the getType method in the IntType class. Option B is incorrect because both the return type and argument list are different
from the getType method in the IntType class. Option D is incorrect because the argument list is different from the getType method in the IntType class.
My comment is this: Option C would have been correct if it had the static modifier for getType() method because it is being called from the static method main().
Am I missing something here? This test question is from Sun and I'm doomed if I get a question of similar context in the SCJP exam.

Also, is this the main Sun mock exams (10 questions)?
------------------
~James Baud
He who asks, is a fool for five minutes;
but, he who does not ask, remains a fool forever. (Chinese proverb)
22 years ago
Thanks again, Paul, Bill. I'm still not sure if the JQ+ comment here is accurate.
Question ID :952739433290
In which of the following cases a thread will definitely be alive but not be running?
Wrong answer: The thread calls yield()
JQ+ Comment: Note that, yield() does not neccessorily put the calling thread on hold.

------------------
~James Baud
He who asks, is a fool for five minutes;
but, he who does not ask, remains a fool forever. (Chinese proverb)
Thank you both for your responses.
I sense the contradiction to both statements to when a yield() is called and there are no ready-to-run Threads. My reading of Bill's response is that a state transition is guaranteed as a call to yield() returns immediately - even for the minutest amount. But, what Paul is saying is that a call to yield() may be ignored as yield() is not a blocking call and therefore state transition may not occur.
The broader question may also be - what are the guaranteed states and transitions? I once had a healthy debate w/ my Java instructor who insisted that under NORMAL execution a thread can move from non-runnable (waiting, sleeping or blocked) to dead state. My position was (according to my limited knowledge), the only 2 transitions to dead state is from running or ready-to-run states.

------------------
~James Baud
He who asks, is a fool for five minutes;
but, he who does not ask, remains a fool forever. (Chinese proverb)
Does a call to yield() guarantee a change of state from running to ready-to-run state regardless of whether there are other ready-to-run thread objects?
Another way of putting it is: is it safe to declare that yield() does not neccessarily put the calling thread on hold even for the minutest amount of time? - meaning no state transition occurs

------------------
~James Baud
He who asks, is a fool for five minutes;
but, he who does not ask, remains a fool forever. (Chinese proverb)
There's tons of explanation on unreachable statements in
JLS (�14.20) .
The most important thing to remember I guess is the special treatment that the compiler gives to the if-then statement. As the JLS states:


...the following statement results in a compile-time error:
while (false) { x=3; }
because the statement x=3; is not reachable; but the superficially similar case:
if (false) { x=3; }
does not result in a compile-time error. An optimizing compiler may realize that the statement x=3; will never be executed and may choose to omit the code for that statement from the generated class file, but the statement x=3; is not regarded as "unreachable" in the technical sense specified here.
The rationale for this differing treatment is to allow programmers to define "flag variables" such as:
static final boolean DEBUG = false;
and then write code such as:
if (DEBUG) { x=3; }
The idea is that it should be possible to change the value of DEBUG from false to true or from true to false and then compile the code correctly with no other changes to the program text.


------------------
~James Baud
He who asks, is a fool for five minutes;
but, he who does not ask, remains a fool forever. (Chinese proverb)
Ratul,
Your best bet would be to try out (compile & run) your code as you'll learn a lot this way.
Using your 2nd example, array[index] = index = 3;;
Operator [] has higher precedence than assignment =. Precedence lets you determine w/c one to apply first if you have operators of different precedence in an expression.
If two operators have the same precedence, associativity rules are used to determine w/c operator should be applied first. In your 2nd example again, we have two = operators that obviously have the same precedence so we need to apply the associativity rule for operator =, w/c is right to left.
Associativity rule:
Unary postfix and all binary operators associate from left to right, except for the assignment operator. All else associate from right to left.
------------------
~James Baud
He who asks, is a fool for five minutes;
but, he who does not ask, remains a fool forever. (Chinese proverb)
<pre>
OPERATOR PRECEDENCE
P-ost-fix Operators [] . (parameters) expression++ expresion--
P-efix Unary Operators ++expression --expression
O-bject creation and cast new (type)
M-ultiplication * / %
A-ddition + -
S-hift << >> >>>
R-elational Operators < <= > >= instanceof
E-quality Operators == !=
B-itwise/boolean AND &
-itwise/boolean XOR ^
-itwise/boolean OR |
L-ogical AND &&
-ogical OR | |
C-onditional Operator ?:
A-ssignment = += -= *= /= %= <<= >>= >>>= &= ^= !=
</pre>
Let me first explain the 2nd example.

According to our table above, operator [] has higher precedence over assignment, therefore it gets evaluated first as [0]. The expression becomes array[0]=index=3. The next evaluation is for the two = operators. This follows the right to left associativity rule for = operator so, index gets assigned a value of 3, and in turn array[0] gets assigned the new value of index w/c is 3.

In the above example, the postfix operation (i++) gets evaluated before assignment (=) in accordance with our precedence table. If you were to translate the expression, i = i++, in english it would say, "evaluate value of i and use it in the expression before incrementing". So, i is evaluated as 0, then using it in the expression with right to left associativity, i=0. The increment ++ is evaluated but assignment has already happened so this "side-effect" is not utilized. Hence the output prints 0.
I have devised an acronym to help me remember operator precedence:
P-entium P-Cs O-utnumber M-ainframes A-lso S-ame R-eason E-very B-ug L-ikely C-omes A-lways
Hope this helps.

[This message has been edited by James Baud (edited March 24, 2001).]
Objects in the String pool are never gc'd. So only 1 will be eligible for gc. There's lot of discussions on this in JavaRanch if you search for string pool.
My understanding is that this type of question will not appear in the exam, according to this post.

------------------
~James Baud
He who asks, is a fool for five minutes;
but, he who does not ask, remains a fool forever. (Chinese proverb)


I have read in order to declare an abstract class you must have at least one abstract method is this true?
Not true, its the other way around. If you declare an abstract method in a class, then that class must be declared abstract. An abstract class may have non-abstract methods.



Also , is null a keyord? I was taking a mock exam and it said it was not.
null, just like true and false is a reserved literal, not a keyword.



I understand bitwise operators, but I do not understand why this returns 12 instead of 14 System.out.println(010|4);
10 equals 1010
4 equals 0100
result 1110 = 14
can anybody please explain it in plain english, maybe toss in an example?

010 is octal representation of decimal 8 or binary 001 000. Therefore, 1000 | 0100 == 1100 (decimal 12).


------------------
~James Baud
He who asks, is a fool for five minutes;
but, he who does not ask, remains a fool forever. (Chinese proverb)