File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How does this " if " statement behave

 
Shailendra Guggali
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check the foll code :
class Test
{
public static void main(String[] args)
{
if (args[0].equals("open"))
if (args[1].equals("someone"))
System.out.println("Hello!" );
else System.out.println("Go away "+ args[1]);
}
}
What would the output be if you enter the command line
1. java Test
2. java Test closed
3. java Test open
4. java Test open someone
Pls try to explain why - ( just answers can be obtained by running on the machine,but i want to understand why)
 
Priya Kannan
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The Program can be interpreted as:

1) java Test------No arguments passed,trying to access args[0] & so ArrayIndexOutOf BoundsException thrown
2)java Test closed-----Fails at #1,control passes #6(No output)
3)java Test open----#1 condition succeeds,but since only one argument is passed, error at #2
4)java Test open someone----#1 condition succeeds,#2 condition succeeds,prints Hello!
I added the ubb code tags to make this clearer Carl
[This message has been edited by Carl Trusiak (edited February 04, 2001).]
 
Shailendra Guggali
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx priya
i got it
further at //6 you can add one more else for the outer if statement - if - else is like LIFO
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic