This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How does this " if " statement behave

 
Shailendra Guggali
Ranch Hand
Posts: 86
  • 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
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic