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 |
Java, a versatile and widely-used programming language, is renowned for its platform independence, thanks to its architecture-neutral nature. The term architecture neutrality refers to Java's ability to run on any device or platform without requiring modifications to its code. This unique feature has played a...
4 min read
Given an integer array (arr) and an integer target, we need to find the value closest to the target that can be obtained by applying the bitwise AND operation on a non-empty subarray of arr. The task is to return the minimum absolute difference between the...
8 min read
To print output to the console in Java, use the System.out.println() function. For logging or auditing purposes, you could, nevertheless, choose to reroute this output to a file in some circumstances. The PrintStream class can be used to do this. In this section, we will...
3 min read
Java Primitive Data Types Primitive data types in Java are the building blocks of data manipulation. They are the most basic data types available in the Java language. Java is a statically-typed programming language, which means that all variables must be declared before they are used. Primitive data...
5 min read
Java is a widely used programming language that is popular for its versatility, ease of use, and robustness. However, one of the most common issues encountered by Java developers is the Null Pointer Exception. In this article, we'll take a closer look at what this exception...
4 min read
In sorting of Collections in Java 8, Lambda Expression and Collections interface place an important role. There are various ways through which we can sort a list using Java 8 Lambda Expression. But Collections interface itself provides some sorting methods through which we can easily sort...
7 min read
The java.util.function package, which was first released with Java 8, includes the DoubleConsumer Interface, which is used to do functional programming in Java. It is an example of a function that accepts a single double-valued argument but outputs nothing. In order to define its accept()...
4 min read
Binary numbers play a vital role computer science. It shows information using only the numbers 0 and 1. Determining whether a binary number is divisible by 3 or not is an understanding of modular arithmetic, numbers and multipliers is required. A binary number can be analyzed to...
5 min read
In Java, stack trace is nothing but location of the exceptions. In other words, we can say that stack trace hunt (trace) for the line where exception may raise. In this section, we will discuss Java stack trace in detail. What is stack trace? In Java, the...
2 min read
Both Set and List are commonly used collection classes in Java that offer different functionalities. There are situations where you might need to convert a Set to a List to perform specific operations or leverage the features and methods offered by the List interface. In this...
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