• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Majji - Paper 1

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi shailesh,
I was getting warning during compilation. After that I was
able to the program.
Usha
 
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic