6

Possible Duplicate:
What does assert do?

Please give me some details with at least one example.

Community
  • 1
  • 1
Vijay Bhaskar Semwal
  • 1,525
  • 3
  • 12
  • 8

3 Answers3

9

Try this:

public class AssertionTest {

  public static void main(String args[]) {
     boolean assertTest = true;
     assert assertTest;
     assertTest = false;
     assert assertTest;
  }
}

If you compile and run this, you should have an idea of how the assertion statement works.

Update:
As correctly pointed out in the comments, after compilation, you run this as java -ea AssertionTest - the -ea flag enables assertions.

Noel M
  • 15,812
  • 8
  • 39
  • 47
3

You use the assert keyword to verify if something you believe about your code is true.

The assertion in not a substitute for code validations, because it can be disabled at runtime ( it is disabled by default ) So, if the assertion is disabled and you use it to control your logic, you'll have undesired results.

For instance:

class SomeClass {
    public void someMethod( String input ) {
         // do something with the input... 
         String result = processs( input );
         assert result.startWith("OK");
         // continue with your code.. 
         return result;
     }
    ....
 }

In that code, the program does something with the input. Your assumption is, that the result string starts with "OK" always. You put the assertion, to make sure that happens, but you don't put any logic around that ( you don't do anything if that doesn't happens )

When you're testing your code, with assertion enabled, if you notice your result doesn't start with "OK", then the program will stop it's execution.

To enable/disble assertions, you have to pass the flag -ea to the java

See : http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html for more information.

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • in c # add System.diagnostics package namespace ConsoleApplication16 { class Program { static void Main(string[] args) { int value = 7; Debug.Assert(value >8, "value must be less then 5"); Debug.WriteIf(value ==7, "value is 7"); } } } – Vijay Bhaskar Semwal Aug 26 '10 at 06:57
  • i was just giving the difference so u can realised how different both language with out confused it will make clear or better understanding or command over it u realsied it now? – Vijay Bhaskar Semwal Aug 27 '10 at 11:44
  • it is not good to publice your answer you start criticized of some one – Vijay Bhaskar Semwal Aug 27 '10 at 11:46
  • Thanks for the explanation. But it would be better if you can give me an example for my understanding.what is wrong if i m taking it with c# refrence – Vijay Bhaskar Semwal Aug 27 '10 at 11:53
  • @Vijay This is very hard to explain. **1st** There's nothing wrong with the C# comparison. You didn't add any comment, so I and didn't knew if you was confusing them or not. **2nd** If you ask about one thing in one language, it doesn't make any sense to bring a different one **without any comment**. **3rd** Different languages apply the same features differently. For instance, I don't know C# ( and you're assuming I do ) It looks to me they have a class `Debug` with a method `Assert`, but ***Why did you expect me to do with that pasted code*** It's very confusing. – OscarRyz Aug 27 '10 at 17:45
1

An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program. For example, if you write a method that calculates the speed of a particle, you might assert that the calculated speed is less than the speed of light.

Each assertion contains a boolean expression that you believe will be true when the assertion executes. If it is not true, the system will throw an error. By verifying that the boolean expression is indeed true, the assertion confirms your assumptions about the behavior of your program, increasing your confidence that the program is free of errors.

Check out links below for more details and examples -

http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html

http://www.roseindia.net/javacertification/scjp5/assertionsexample.shtml

Sachin Shanbhag
  • 54,530
  • 11
  • 89
  • 103
  • An assertion checks a boolean-typed expression that must be true during program runtime execution. The assertion facility can be enabled or disable at runtime. Assertion statements have two forms assert expression1 assert expression1 : expression2; first is simple form of assertionnd second form takes another expression. In both of the form boolean expression represents condition that must be evaluate to true runtime. example vijay(int a) { if(a>=0) { System.out.println("vijay"); } else { error } } vijay(float b) { assert (b>0) : not gretarer ; // do } – Vijay Bhaskar Semwal Aug 26 '10 at 06:40