Java assert KeywordLast Updated : 17 Mar 2025 The Java assert keyword is used to test your assumptions about the program. While executing assertion, it is believed to be true. If it fails, JVM will throw an error named AssertionError. It is mainly used for testing purpose. Advantage of AssertionIt provides an effective way to detect and correct programming errors. Syntax of using AssertionThere are two ways to use assertion. First way is: and second way is: Simple Example of Assert Keyword in Java
Output: Enter ur age 11
Exception in thread "main" java.lang.AssertionError: Not valid
Where not to use Assertion:There are some situations where assertion should be avoid to use. They are:
Next TopicFor-each-loop-in-java |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India