Java Language Asserting

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Syntax

  • assert expression1;
  • assert expression1 : expression2;

Parameters

ParameterDetails
expression1The assertion statement throws an AssertionError if this expression evaluates to false.
expression2Optional. When used, AssertionErrors thrown by the assert statement have this message.

Remarks

By default, assertions are disabled at runtime.

To enable assertions, you must run java with -ea flag.

java -ea com.example.AssertionExample

Assertions are statements that will throw an error if their expression evaluates to false. Assertions should only be used to test code; they should never be used in production.



Got any Java Language Question?