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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How does this " if " statement behave" Watch "How does this " if " statement behave" New topic
Author

How does this " if " statement behave

Shailendra Guggali
Ranch Hand

Joined: Feb 01, 2001
Posts: 86
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

Joined: Jan 19, 2001
Posts: 72
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

Joined: Feb 01, 2001
Posts: 86
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
 
subject: How does this " if " statement behave
 
Similar Threads
operators Q
Command Line Invocation
It should throw a runtime error..
More basic concepts
if statement question from JQ+