What is Assertion in java ?

Alok mishra

Alok mishra

@alok-mishra-E4tgN8 Oct 24, 2024
What is assertion in java and why it is used ?

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Anoop Kumar

    Anoop Kumar

    @anoop-kumar-GDGRCn Sep 11, 2013

    Assertions are generally part of Unit Testing.
    You are going to test something which are are you expecting.
    Like if you are expecting null then you can assert
    assert i==null : "throw this message"
    Assertions are bydefault disabled so adding assertions will not affect your run time. but when you need it.
    just enable is by sending  jvm parameter -ea.
    
    example :  java -ea <className>
    Good one but very less used. everyone is using IDE with debugger.
  • vikaskumar11233

    vikaskumar11233

    @vikaskumar11233-J10JeV Sep 12, 2013

    Assertion are used to verify the correctness of an invariant in the code. They should never be triggered in production code, and are indicative of a bug or misuse of a code path.