tags:

views:

117

answers:

3

Please give me some details with at least one example.

+1  A: 

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
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 expression1assert 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
A: 

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
You have to run it with flags: -ea btw
OscarRyz
@OscarRyz - yes, correct - I missed that - I've updated my answer
Noel M
A: 

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
in c #addSystem.diagnostics packagenamespace 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
-1 @Vijay Do you realize C# and Java are different programming languages don't you?
OscarRyz
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 itu realsied it now?
Vijay Bhaskar Semwal
it is not good to publice your answer you start criticized of some one
Vijay Bhaskar Semwal
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
@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