What does the Java assert keyword do, and when should it be used?

前端 未结 19 1486
眼角桃花
眼角桃花 2020-11-22 15:58

What are some real life examples to understand the key role of assertions?

相关标签:
19条回答
  • 2020-11-22 16:09

    To recap (and this is true of many languages not just Java):

    "assert" is primarily used as a debugging aid by software developers during the debugging process. Assert-messages should never appear. Many languages provide a compile-time option that will cause all "asserts" to be ignored, for use in generating "production" code.

    "exceptions" are a handy way to handle all kinds of error conditions, whether or not they represent logic errors, because, if you run into an error-condition such that you cannot continue, you can simply "throw them up into the air," from wherever you are, expecting someone else out there to be ready to "catch" them. Control is transferred in one step, straight from the code that threw the exception, straight to the catcher's mitt. (And the catcher can see the complete backtrace of calls that had taken place.)

    Furthermore, callers of that subroutine don't have to check to see if the subroutine succeeded: "if we're here now, it must have succeeded, because otherwise it would have thrown an exception and we wouldn't be here now!" This simple strategy makes code-design and debugging much, much easier.

    Exceptions conveniently allow fatal-error conditions to be what they are: "exceptions to the rule." And, for them to be handled by a code-path that is also "an exception to the rule ... "fly ball!"

    0 讨论(0)
  • 2020-11-22 16:10

    In addition to all the great answers provided here, the official Java SE 7 programming guide has a pretty concise manual on using assert; with several spot-on examples of when it's a good (and, importantly, bad) idea to use assertions, and how it's different from throwing exceptions.

    Link

    0 讨论(0)
  • 2020-11-22 16:10

    Assertion are basically used to debug the application or it is used in replacement of exception handling for some application to check the validity of an application.

    Assertion works at run time. A simple example, that can explain the whole concept very simply, is herein - What does the assert keyword do in Java? (WikiAnswers).

    0 讨论(0)
  • 2020-11-22 16:11

    A real world example, from a Stack-class (from Assertion in Java Articles)

    public int pop() {
       // precondition
       assert !isEmpty() : "Stack is empty";
       return stack[--num];
    }
    
    0 讨论(0)
  • 2020-11-22 16:16

    Here's the most common use case. Suppose you're switching on an enum value:

    switch (fruit) {
      case apple:
        // do something
        break;
      case pear:
        // do something
        break;
      case banana:
        // do something
        break;
    }
    

    As long as you handle every case, you're fine. But someday, somebody will add fig to your enum and forget to add it to your switch statement. This produces a bug that may get tricky to catch, because the effects won't be felt until after you've left the switch statement. But if you write your switch like this, you can catch it immediately:

    switch (fruit) {
      case apple:
        // do something
        break;
      case pear:
        // do something
        break;
      case banana:
        // do something
        break;
      default:
        assert false : "Missing enum value: " + fruit;
    }
    
    0 讨论(0)
  • 2020-11-22 16:19

    Assertions are used to check post-conditions and "should never fail" pre-conditions. Correct code should never fail an assertion; when they trigger, they should indicate a bug (hopefully at a place that is close to where the actual locus of the problem is).

    An example of an assertion might be to check that a particular group of methods is called in the right order (e.g., that hasNext() is called before next() in an Iterator).

    0 讨论(0)
提交回复
热议问题