Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!

raghu babu

Ranch Hand
+ Follow
since Jul 05, 2005
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 raghu babu

And the rule in java is, an overriding method cannot throw newer exceptions, other than subclasses of those in the overridden method or the same exceptions.

That's true but it is valid for only checked exceptions. ArithmeticException
is unchecked exception. Pl. provide your compiled code if you believe that
'd' is resulting in compile time error.
What happens when you call a Thread's run method directly (as opposed to calling start)?

This means you are not spawning a Thread and run method is executed
normally as any other method would.
In class C,

new Thread(new B(),"T1").start();

starts a new thread named T1, and this start invokes run method of
class B.

class B's run method is :
new A().run(); <== call class A's run method
<== and class A's run method prints the
<== name of current executing thread
<== and currently executing thread is T1

new Thread(new A(),"T2").run();
<== This statement is creating a new Thread
<== and calling run method of Thread directly
<== Note : run method called in this statement
<== none of the run methods coded here, but
<== the one coded in java.lang.Thread and
<== which calls the run method of Runnable
<== passed in the constructor and this is
<== A's run method. ( This statement calls
<== run method directly so no new thread
<== is spawned in statement. ) So, A's run
<== method still prints T1 as that is the only
<== thread still executing, so T1 is printed
<== again
new Thread(new A(),"T3").start();
<== This spawns a new thread named T3 and by now
<== it should be clear why T3 is printed

Hope this clarifies the confusion.
This code doesn't even compile as a[5] within try block is not a statement.

Pl. provide correct code to clear your confusion.
Here is the explanation for this :

If m1 is a private member of the class T where the invocation expression occurs, then the implementation in class T is selected at run-time regardless of the run-time type of the object.

This is excerpt is from Dan Chisholm's site.

Here m1 is method1()
T is Parent class.

Based on this explanation, at compile time, method2 in parent class calls, priavate method method1. As the above statement clearly explains,
parent's method1 is what called at runtime.

Hope this helps.

on the other hand following declaration results in a compile time error :

char e = \u0031 ; //observe, no tick marks
sleep method should be between a try-catch block as it throws InterruptedException. But, the options given doesn't show that
sleep is coded between try-catch block.

Let's walkthru the code to make things clear how method resolution works
in this overload scenario.

On Line 1, at compile time, compiler looks at line 1, and interprets that
you are trying to call eating method of class A (as 'a' is reference type A) by passing an argument of class type B.

But class A has eating method which accepts arguments of class A. But from the inheritance structure, B extends A, that means wherever you can pass of type A, a type B class can be passed. So, for line 1, compiler chooses to use eating method from class A and that is the method invoked even at runtime (which version of overload method to be called, is decided at compile time, unlike override methods).

Hope this is clear, if not more confusing, what you already know.

I have to disagree with these statements.
still works but now the method invoked is that exists in
class A, as 'b' is of type B and it doesn't have eat method
that accepts class type A as argument, but only of type B.
But class B inherits its method eating(typeA) method from
class A.

Also, b.eating((B)a) won't work at runtime, as runtime type of 'a' is
of class type A and not of class type B, this results a runtime exception.

Hope this helps.
Smitha, it depends whether you observered the call to start() in an instance method of the class which extends a thread or in the main method.
I'm sure code you are referring is inside an instance method and this should make clear what marc is trying to explain.

It is always makes things easy, if you can post the code.
Try passing a value of "10" instead of "127" to the constructor and see the result (surprisingly, you get true).

This is due to the weird code, that exists in Integer.toString(int i) method. I'm hoping someone can explain the logic why Sun developed such an anamoly for this method.
I'm having a hardtime to understand the code you mentioned and the pertinent answers you are expecting. Only if you could repost your code with your doubs, would be of any help.
charAt(int i) function's return type is char.

The result of this function is not a compile time constant,
so it results in compile error.

in your example,
byte b1 = 'a'; // fine

try this,
char a = 'a';
byte b1 = a //you will get a compiler error

in case of
float f2 = "a".charAt(0);

result of right hand side in the above assignment is of type char
and gets promoted to int and then to float, which is not a problem.
implicit narrowing rules are applicable only to integer type not to floating point numerals. Hope this helps.
>>Addressing the element s1[ 1 ] produces a null value.

s1 is declared as a String of Arrays, but the declaration haven't
yet provided how much space to be allocated, i.e., it is not declared
something like
String[] s1 = new String[10];

>>Addressing the element s2[ 1 ] produces an empty String.
This doesn't produce empty String, but outputs null, as element
of s2 is a String and String gets a default null reference value.