File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubts??????? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubts???????" Watch "doubts???????" New topic
Author

doubts???????

Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
Hi,
please would anyone clear my doubt.
1)
assume x=0
public class testclass
{
public void test(int x)
{
loop:for(int i=1;i<5;i++)
{
for(int j=1;j<5;j++)
{
System.out.println(i);
if(x==0)
{continue loop;
}
System.out.println(j);//line 1
}
the output given is 1234.
but i think it should be compile time error as the statement
on line 1 is never reached.
2)
when an exception is caught in the catch clause and after the finally is executed do the remaining statements execute.
example
public testclass
{
public static void main(String args[])
{
int k=0;
try
{
int j=5/k;
}
catch(ArithmeticException e)
{System.out.println("1");
}
catch (RuntimeException e)
{System.out.println("2");
]
finally
{
System.out.println("3");
}
System.out.println("4");
}
}
Will the output be 134 or 13

nss
Anu Kris
Greenhorn

Joined: Oct 01, 2001
Posts: 13
1.for the first doubt,i think the coding is incomplete and the answer 1234 gets printed which is the value of i.
2.after the exception is caught ,finally will be executed,even if the exception was not caught finally will be executed and the normal flow of the program will continue.So the result is 134
Pratibha Enjeti
Greenhorn

Joined: Jun 14, 2001
Posts: 20
Hello! Neha,
Your first question, your sample code does look incomplete,
try the program below which does the same thing, which gives you the output : 1 2 3 4, not a compile time error.
Reason: x is always =0, when i=1 , the condition matches and "continue loop" is executed, continue statement always breaks out of the present iteration and control reaches back to the label statement pointed by Continue. Hence,increments to the next iteration.
Here, "Continue loop" breaks out of the inner loop j , never reaching the print statement and increments the value of i=2.
Prints the value and "continues" the "loop" or the label statement.
When i =4 it follows the same procedure and i is incremented to 5. Now the outer loop has reached the limit to the program exits out, never reching Print j statement.
-Its ok for code to not do anything sometimes as long as the syntax is ok. Generally you would not code that way.
Here's the code:
public class testclass
{
static int x =0;

public static void main(String args[])
{
int j;
loop:for(int i=1;i<5;i++){

for(j=1;j<5;j++) {
System.out.println(i);
if(x==0){
continue loop;
}
System.out.println(j);//line 1
}
}
}
}

}

2. After finally statement in your eg. o/p will be 134. Program flow does continue after the finally statement. JLS has some good titbits on Exception handling , which you can refer to !
I hope my explanations help you.
Pratibha.
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
thanx anu and pratiba for clearing my doubts.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubts???????