• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assertion Q

 
Sai Charan
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain the output of this code. Thanks in advance.

class ATest
{
public static int y = 0;
public static void main(String[] args)
{
int t = 2;
assert t <4 : foo(7);
assert t > 1: bar(8);
assert t > 1: foobar(10);
System.out.println("done");
}

public static int bar(int x) {
System.out.println("bar");
return y = x;

}
public static int foo(int x) {
System.out.println("foo");
return y = x;

}
public static boolean foobar(int x) {
System.out.println("foobar");
return false;

}
}

Running the code with assertions enabled as below giving output?
java -ea ATest
done
[ May 26, 2006: Message edited by: Sai Charan ]
 
warren li
Ranch Hand
Posts: 128
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
all of the following:
assert t <4 : foo(7);
assert t > 1: bar(8);
assert t > 1: foobar(10);

passes the assertion test since t is 2, so
these 3 methods never get executed.

so the next line "done" is immediately reached.
 
Amirr Rafique
Ranch Hand
Posts: 324
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the assert statement part after : is executed when assert got fialed. As in your case all three assert get paased due to which none of three method are called.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic