November 28, 2007

Java Puzzle-1

What will be the output of the code snippet given below and why?

public static void main(String[] args) {
if (method()¦ method())
System.out.println("stmt1");
}
static boolean method() {
System.out.println("stmt2");
return true;
}


Send me the answer in your comment.

5 comments:

  1. this line :

    if (method() method())

    isnt correct so an error is outputted...

    if it was correct like:

    if (method() == method())

    the output is stmt1

    ReplyDelete
  2. Nothing will output.

    ReplyDelete
  3. I pipe symbol will not validate that code snippet either...

    Is this a trick question or can you program in Java?

    ReplyDelete
  4. "The Ghost In Your Machine"-This snippet will definitely work. Try this out in your own IDE.

    ReplyDelete
  5. I think you're just asking what the difference is between | and ||, for which I implore you to look at the Java Language Specification, which covers it explicitly.

    This isn't a tricky question, it's very basic Java.

    ReplyDelete