Saurabh Vyas

Ranch Hand
+ Follow
since Sep 02, 2003
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 Saurabh Vyas

Hi Guys,

Try this example written by me.

I guess it solves your problem of Running 3 Thread T1, T2, T3 along with main Thread with a condition that T1 should finish first, T2 should finish Second and T3 should finish 3rd and main should finish last. It makes use of join() to accomplish the task.



Cheers !!
Saurabh
Congratulations Raphael
13 years ago


As per K&B book, When you use <? super ... > syntax, you are telling the compiler that you can accept the type on the right-hand side of super or any of its supertypes,and this is the key part.

Now this means any of the below would be true i.e. compile successfully:


If we don't use <? super ..> syntax than following will not compile :


Now coming to the question as to why following code don't compile :


So as per my understanding, This is because, when it comes to adding an object to List with syntax <? super ...> compiler will consider it as follows :
This List


becomes



Thus you can only add an Object of Type String and nothing else. Not even anything of its Super type.
This is explained nicely in K&B Book.
Here is the extract for you.



Now as per book above line means :



By saying <? extends Instrument>, we're saying, "I can be assigned a collection that is a subtype of List and typed for <Instrument> or anything that extends Instrument. And oh yes, I SWEAR that I will not ADD anything into the collection."

Hi Ganesh,
Congratulations.

Can you please help us know as to how & from where did you prepare the topics specific to Java 1.6 ( Because K&B book for 1.6 is not yet available everywhere )

It would be nice of you if you can show us (those preparing for 1.6) some pointer / suggestions to prepare the topics specific to scjp 1.6

Thanks
Saurabh
13 years ago
John / anyone else in Forum.

Can you please show some pointers of online tutorials from where we can learn & prepare these newly added topics of SCJP 1.6


Thanks
Saurabh
Thanks Naveen

That was really informative.
Sorry Sid, The error is related to Casting rather than assignment to final variable.

Moreover I am not changing the content of a final variable in any of the above program
[ July 24, 2008: Message edited by: Saurabh Vyas ]
Can someone please explain as to why Program 1 shown below compiles & runs BUT Program 2 doesn't ?



Program 1 :
----------

public class SCJP{

public static void main(String[] args) {
final int x = 10;
short y = x ;

System.out.print(x);
System.out.println(y);
}
}





Program 2 :
-----------

public class SCJP{

public static void main(String[] args) {
final int x;
x = 10;
short y = x ;

System.out.print(x);
System.out.println(y);
}
}

To know how many object are actually created on HEAP, what i do is, I look out for the keyword "new". What ever no. of times the keyword "new" appears in the code, that is the actual number of objects that get created on Heap.

Thus in your example, since there are only 3 new keywords so there is no question of 4 objects getting created or coming to existence.
Java is pass by value even for objects.

Thus you won't be able to swap objects referred by reference variables class1 & class2 using a method call swap().

You can probably do the following to swap two objects :


MyClass class1 = new MyClass(10);
Myclass class2 = new MyClass(20);

Myclass class3 = class1;

class1 = class2;
class2 = class3;

In Case of Jaspreet's Example, it is clear that int is narrower than long so it got preference by the compiler.

But in Case of Phalguni's Example, How can we conclude that GFC203 was narrower than GFC202 ??

Is it that since GFC203 is a child class to GFC202 and thus it was being considered as a more specific class (& thus more narrower) as compared to it Generic Parent class, and thus got preference of selection by the Compiler !!!

Can anyone please Explain the behavior in detail.

Thanks
Saurabh
Thank You Benz and Mike and Prakash for your detailed explanation.
It was really helpful.
[ June 18, 2008: Message edited by: Saurabh Vyas ]
If answer to the above question is 3 i.e. only 3 objects namely i1, i2 & i3 are eligible for garbage collection. Than my question is what will happen to Object i inside each of these i1, i2 & i3 ???

Will they be also not Garbage collected along with i1,i2 & i3 ???
Scope of final variable declared inside the method is within the method only.
It can not be accessed outside the method.


class MyClass{

void myMethod(){
final int x = 5;
System.out.println("Value of x in myMethod = " + x);
}

void mySecondMethod(){
System.out.println("Value of x outside myMethod = " + x);
}
}



In the above example x won't be accessible in mySecondMethod()