wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Majji - Paper 1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Majji - Paper 1" Watch "Majji - Paper 1" New topic
Author

Majji - Paper 1

Usha Vydyanathan
Greenhorn

Joined: Jan 03, 2001
Posts: 17
Hi,
I thought if a statement is unreachable Java always gives error.
Following code compiles fine eventhough "System.out.println("After stop method");" is not reachable because thread will stop before that.

public class Q1 extends Thread
{
public void run()
{
System.out.println("Before start method");
this.stop();
System.out.println("After stop method");
}

public static void main(String[] args)
{
Q1 a = new Q1();
a.start();
}
}
Can any one explain? Thanks in advance.
regards,
Usha
shailesh sonavadekar
Ranch Hand

Joined: Oct 12, 2000
Posts: 1874
Usha , stop is deprecated method in java1.2. How can you compile this code ? i think with javac -deprecation Q1.java also will not compile.
Can you please check the code?
Shailesh.
Usha Vydyanathan
Greenhorn

Joined: Jan 03, 2001
Posts: 17
Hi shailesh,
I was getting warning during compilation. After that I was
able to the program.
Usha
Stevie Kaligis
Ranch Hand

Joined: Feb 04, 2001
Posts: 400
Shailesh...,
of course it will compile, check this from sun :
A deprecated API is an API that we recommend you no longer use, due to improvements in Java. While deprecated APIs are currently still implemented, they may be removed in future implementations, and we recommend using other APIs.
Usha...
by calling stop(), system.exit(0), etc...doesn't meant that the code after are NOT reachable, it means the rest of the code WILL NOT be execute.

stevie
sandeep bagati
Ranch Hand

Joined: Feb 22, 2001
Posts: 62
Originally posted by Usha Vydyanathan:
Hi,
I thought if a statement is unreachable Java always gives error.
Following code compiles fine eventhough "System.out.println("After stop method");" is not reachable because thread will stop before that.

public class Q1 extends Thread
{
public void run()
{
System.out.println("Before start method");
this.stop();
System.out.println("After stop method");
}

public static void main(String[] args)
{
Q1 a = new Q1();
a.start();
}
}
Can any one explain? Thanks in advance.
regards,
Usha

It is a good question .Yah if u are not using threads u get definitely a compile-time Objection to the code for java doesn't allow redundant code but in case of a thread when u say stop the thread is nomore in running state and hence the code after that doesnt matter.
A little change in yr program can help u in understanding better
public class Q111 extends Thread
{
public void run()
{
System.out.println("Before start method");
this.stop();
System.out.println("After stop method");
}
public static void main(String[] args)
{
Q111 a = new Q111();
a.start();

Thread.currentThread().stop();//(1)
System.out.println("After stop in main");(2)
}
Now after line marked (1) the code doesnt execute and doesnt
give compile-time error also.Now if u remove (1)(2) will get executed and After stop in main printed.Here it is not unreachable code rather u can say that the code after the stop is not consudered at all while in case of an unreachable statement like
while(false){
System.out.println("After stop in main");
}
thread that is executing this statement is running while it is programming error because of which a redundant code is found in the running thread and as i said java takes it as a compile-time
error.
Hope that i am right.
sandeep

James Du
Ranch Hand

Joined: Mar 23, 2001
Posts: 186
Hi all,
In the flow analysis carried out by complier to make sure all statements are reachable, the values of expressions are not taken into account! pls. look at the following quote from jls

An empty block that is not a switch block can complete normally iff it is reachable. A nonempty block that is not a switch block can complete normally iff the last statement in it can complete normally. The first statement in a nonempty block that is not a switch block is reachable iff the block is reachable. Every other statement S in a nonempty block that is not a switch block is reachable iff the statement preceding S can complete normally.
An expression statement can complete normally iff it is reachable

In java language, method invocation is considered "Expreseion", so it can always complete normally provided it is reachable! It follows that the statement following a "reachable" method invocation is always considered reachable!
In other words, the value of the method invocation(maybe nothing, in the case the return type is of type void) and the side affect brought in by the call is not taken in account at all! The java designer peremptorily desided not to check that.
pls. consider the following 3 classes

The only statement objected by complier is System.out.println("end") in class, if you comment it out, it will be complied cleanly;
If i am wrong pls. correct me.
Regards,
James
[This message has been edited by James Du (edited April 25, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Majji - Paper 1
 
Similar Threads
Synchronisation did not work.
stop ()
Threads
Thread
deprecated method stop()