File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assertions clarification

 
Srinivasa Kadiyala
Ranch Hand
Posts: 237
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pl go through the following code:

<<<<< code starts here >>>>>>>>>>>>>>


public class TestAssertions{


public static void main(String args[]){

int x=2;
assert (x==2): new TestAssertions().go(x);
System.out.print(x);

}

public int go(int x){
System.out.print("in go()...");
x=x*2;
System.out.print(x);
return x;

}

}


<<<<<<<<<<<<<<< code ends here >>>>>>>>>>>>>>>>

compiled:
javac -source 1.4 TestAssetions.java >>> Success

Run:

java -ea TestAssertions >>> Success

output:
2

Confused with the output. I am expecting the following..

in go()... 4 4

why the out put is different?

Thanks in advance
 
patrick J. d'cousta
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sir,
Assertions are enabled but the condition
x==2 will be true so the output is 2.
put assert(x>2) and run with assertions enabled
 
Srinivasa Kadiyala
Ranch Hand
Posts: 237
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Patrick

Thanks for poiting the logic.

Hereunder is the result:

C:\temp>javac -source 1.4 TestAssertions.java

C:\temp>java -ea TestAssertions
in go()...4Exception in thread "main" java.lang.AssertionError: 4
at TestAssertions.main(TestAssertions.java:7)

Thanks....
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic