Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!

Rajiv Rai

Ranch Hand
+ Follow
since Jun 14, 2010
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
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 Rajiv Rai

What is the benefit of the following statement

List l = new ArrayList();


over the following statement



ArrayList l = new ArrayList();
7 years ago
I have been reading java interview questions and read the following about implementation inheritance.


"When using implementation inheritance, make sure that the sub-classes depend only
on the behavior of the superclass, not on the actual implementation."

Am confused about what the above statement means?
7 years ago
Thanks for the replies but I still have not got the answer to my second question

When we use synchronized block with "this" , i mean current object,
can any other thread enter any other synchronized method(method not block) for that instance.
7 years ago
I am reading the SCJP 5 book chapter on Threads. To quote from
the synchronization objective

"When you synchronize a method, the object used to invoke the method is the
object whose lock must be acquired. But when you synchronize a block of code, you
specify which object's lock you want to use as the lock, so you could, for example,
use some third-party object as the lock for this piece of code. That gives you the
ability to have more than one lock for code synchronization within a single object."


What does the above mean? I can't understand the part about the synhronized block
in the above paragraph

Also, is the following true

If there is a class which has a synchronized method and a synchronized block,
if a thread enters a synchronized block, no other thread can execute the synchronized
method on the same instance. Just like if a thread enters a synchronized method , no other thread
can execute any synchronized methods on the same instance


Am stuck on this part,
please help to understand

Thanks




7 years ago
I have been reading the "Networking and threads" chapter of the HeadFirst Java book.

A client makes a Socket connection to the server by the following code



and the server does the following



To make a Socket connection, the knowledge of the IP address and the port number is required.
I want to know how , when and where does the server get to know the IP address and port number
of the client to make a socket connection to the client?
According to the Threads chapter in the SCJP 5 book


"The main() method, that starts the whole ball rolling, runs in one thread, called
(surprisingly) the main thread. If you looked at the main call stack (and you can, any
time you get a stack trace from something that happens after main begins, but not
within another thread), you'd see that main() is the first method on the stack—
the method at the bottom."


So , all I want is just a stack trace which highlights the order of method calls
in the main thread which as in this code has no exception code at all.

How can this be achieved ?

Thanks
7 years ago


Guys , please suggest me how can I get the stack trace of the above method calls
in method3(),Just doing it as experimentation
7 years ago
@Henry

The query I meant to ask is why it is that we can't restart a thread ,
may be after it has finished with its job.
That way we won't have to create new Thread objects.
What was the logic behind the prevention of restarting a
thread?
7 years ago
There is no guarantee that Thread.yield method
will put current thread into runnable state.

Is the above true?
7 years ago
I read the following in the SCJP 5 book

"Once a thread has been started, it can never be started again"

What is the logic behind the above?




When i run the above it prints "Rajiv" once and
then throws IllegalThreadStateException.

7 years ago


Why does line 10 throw a compiler error
whereas line 15 does not?

As per me if line 10 throws an error due to the
static issue , its the same case for line 15


Thanks
7 years ago
Well its given in SCJP 5 book but i just checked an ebook of SCJP 6
and in the SCJP 6 book they have modified it

So may be it was an error in the SCJP5 book
7 years ago





According to SCJP book both the above code snippets are equivalent

Now due the error received above , can i assume that both code snippets
are not equivalent?
7 years ago


When executing this code it throws the following error

Exception in thread "main" java.lang.NullPointerException
at com.rajiv.ArrayTest2.main(ArrayTest2.java:14)


Can't figure out why it throws this error?
Please help

Thanks
7 years ago
The serializable class Box does not declare a static final serialVersionUID field of type long..

For which classes is the above warning messages shown?
Is it just for classes which implement Serializable?
7 years ago