Java assert Keyword8 Apr 2025 | 3 min read The assert keyword in Java is used for debugging purposes. It is primarily used to test assumptions in the code by throwing an AssertionError if an expression evaluates to false. Assertions are typically used during development and testing but are disabled by default at runtime. To enable assertions, you need to use the -ea (enable assertions) JVM option when running the program. Syntax:Single Expression If the condition evaluates to false, an AssertionError is thrown. Expression with an Error Message If the condition is false, an AssertionError is thrown with a message. Program 1: Basic AssertionExampleCompile and RunOutput: Number is: 5 After enabling assertions Output: Exception in thread "main" java.lang.AssertionError: Number is not greater than 10 Explanation The program d demonstrates the use of the assert keyword in Java to validate assumptions during runtime; the program initializes num as 5 and uses an assertion assert num > 10 : "Number is not greater than 10", which checks if num is greater than 10; since the condition is false, an AssertionError with the message "Number is not greater than 10" is thrown when executed with assertions enabled (-ea), otherwise, assertions are ignored, and the program prints "Number is: 5". Program 2: Using Assertions in a MethodExampleCompile and RunOutput: 16 9 After enabling assertions Output: 16 Exception in thread "main" java.lang.AssertionError: Number should be non-negative at AssertionExample2.square(AssertionExample2.java:3) at AssertionExample2.main(AssertionExample2.java:7) Explanation The program demonstrates the use of the assert keyword in Java to validate method inputs by ensuring that the given number is non-negative before calculating its square. When assertions are disabled (default mode), the program executes normally and prints 16 and 9, even though square(-3) violates the assertion condition, however, when assertions are enabled using -ea, the second method call fails, throwing an AssertionError with the message "Number should be non-negative", terminating execution. Program 3: Assertions in LoopsExampleCompile and RunOutput: Even number: 1 Even number: 2 Even number: 3 Even number: 4 Even number: 5 After enabling assertions Output: Exception in thread "main" java.lang.AssertionError: Odd number encountered: 1 Explanation The program demonstrates the use of the assert keyword in Java to validate conditions during execution; in this program, an assertion checks whether each number in the loop is even (i % 2 == 0), and if the condition fails (i.e., i is odd), an AssertionError with the message "Odd number encountered: " is thrown; when run without enabling assertions (-ea not used), the assertion is ignored, and all numbers (1 to 5) are printed, but when assertions are enabled (java -ea AssertionExample3), the program terminates at i = 1 with an AssertionError. Important Points to Remember
Next TopicCounter variable in Java |
A symmetric tree also referred to as a mirror tree is a binary tree, where left and right subtree are inversions of one another. The concept is more important in computer science particularly when learning about trees and recursion. A symmetric binary tree means that for every...
5 min read
There is equality between two double buffers if, and only if the elements are of the same type, there are an equal number of remaining elements, and the two sequences of elements are point-by-point equivalent when taken to be considered irrespective of where they started. The...
4 min read
Cloning is a fundamental concept in Java that allows developers to create a duplicate copy of an object. This process is essential for various scenarios, such as preserving the state of an object, creating backups, or implementing certain design patterns. However, Java provides two distinct types...
7 min read
In the realm of software development, solving array-based problems efficiently is crucial, especially in technical interviews and competitive programming. One such problem is finding the smallest positive number missing from an unsorted array. This problem tests a programmer's ability to manipulate and traverse arrays, as...
6 min read
An input array is given to us. That input array is the preorder traversal of a Binary Search Tree (BST). The task is to detect and print the leaf nodes of the Binary Search Tree. A leaf node is a node of a tree that has...
9 min read
An integer 𝑔 presents a primitive root of prime number 𝑛 modulo n because it produces all numbers between 1 and 𝑛−1 when subjected to modular arithmetic. The power of g modulo n under the circumstance of prime numbers n allows every integer value from 1...
5 min read
It is that part of Computer Science that deals with image manipulation and analysis in the digital domain. Image processing, due to the increasing use of multimedia, has become an integral part of tasks like image enhancement, text extraction, artistic effects, etc. In this section, we...
9 min read
In the ever-evolving world of software development, various architectural paradigms and design patterns have emerged to meet the diverse needs of modern applications. One such architectural style is the monolithic architecture, which has been a longstanding and reliable approach for building software systems. In this section,...
5 min read
Niven numbers are named after Ivan Niven, a Canadian mathematician who introduced them in a paper in 1977. However, they were first studied by the Indian mathematician D. R. Kaprekar in the 1950s. In this section, we will learn what is niven number with example and...
5 min read
Java extends keywords enable classes to inherit properties and behaviours from superclasses. It establishes an inheritance relationship between two classes (subclass and superclass). A subclass inherits all non-private characteristics and procedures from its superclass, which serves as both parent and base class. Syntax: class Subclass extends Superclass...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India