• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

short circuit operators

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai
can any one explain the question

1. public class Test {
2. public static void main(String [] args) {
3. System.out.println(args.length > 4 &&
4. args[4].equals("-d"));
5. }
6. }
f the program is invoked using the command line:
java Test One Two Three -d
What is the result?
A. true
B. false
C. Compilation fails.
D. An exception is thrown at runtime.

by
velan vel
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Velan,

The output will be 'B' - false

Regards,
Loga
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually the output is true.

The first part of the comparison (System.out.println(args.length > 4) returns true as we have 5 values in args
1) Test
2) One
3) Two
4) Three
5) -d

for the second comparison (args[4].equals("-d")), the value is true, since args @ 4th place (counting from 0) is "-d". Although "-d" is fifth but arrays start counting from 0. Hence the && operator will return as both the conditions are "True".
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Velan,
The result is false
Explaination:
As u are use conditional operator && and the first expression args.length > 4 is evaluated to false,so it does not evaluate the second expression args[4].equals("-d").
If the first expression evaluates to true by changing to args.length==4 this evaluates to true and the second expression is evaluated which results in ArrayIndexOutOfBoundsException.

cheers,
Santosh
 
santosh kothapalli
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Abhinav Gogna:
Actually the output is true.

The first part of the comparison (System.out.println(args.length > 4) returns true as we have 5 values in args
1) Test
2) One
3) Two
4) Three
5) -d

for the second comparison (args[4].equals("-d")), the value is true, since args @ 4th place (counting from 0) is "-d". Although "-d" is fifth but arrays start counting from 0. Hence the && operator will return as both the conditions are "True".



Abhinav,
Test is the name of the program and not the Argument.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the answer is false.
&& is shortcircuit operator.once the first condition is evaluated as fasle
it will not check the second condition.
args>4 is false because only commandline parameters are passed
 
They weren't very bright, but they were very, very big. Ad contrast:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic