Java/J2EE and SOA (305 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (45 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (304 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

How To Implement Assertion In Java?

Published on Jul 31,2019 34 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

Often times, the need to verify the conditions present in our program arises. The assert keyword in Java allows the users to verify or test the assumptions made during the program. This article will introduce you to Assertion In Java.

Following pointers will be covered in this article,

So let us get started with this article

Declaring Assertion In Java

The assert statement is used alongside a Boolean expression and can be declared as follows:

assert expression;

Another way to declare the assertion is as follows:

assert expression1 : expression2;

Example


import java.util.Scanner;
public class Test
{
public static void main( String args[] )
{
int value = 18;
assert value >= 20 : " Eligible";
System.out.println("Value: "+value);
}
}

Output

Value: 18

The output after enabling assertions will be as follows:

Exception in thread “main” java.lang.AssertionError: Eligible

Moving on with this Assertion In Java Article,

Enable Assertions

It must be noted that, assertions are disabled by default.

The syntax for enabling the assertion statement is as follows:

java –ea Test

Another method for enabling assertions:

java –enableassertions Test

Moving on, let us see how to disable assertions,

Disable Assertions

The assertion statements can be disabled as follows:

java –da Test

Another method for enabling assertions:

java -disableassertions Test

Reasons For Using Assertions

There are various reasons as to why a user might want to use assertions:

  • Making sure that assumptions defined in the comments are right.
  • To ensure that the switch case is not reached.
  • To check the state of the object.

Moving on with this Assertion In Java Article

Where To Use Assertion And Not?

Where To Use Assertions?

  • Conditional cases and conditions at the beginning of a method.
  • Arguments to private methods.

Where Not To Use Assertions?

  • Checking arguments in the public methods that are provided by the user should not be done using assertions.
  • Assertions should not be used on command line arguments.
  • Replacing error messages should not be done using assertions.

Moving on to the final bit of this Assertion In Java Article

Sample Program For Assertion In Java


import java.util.Scanner;
public class Test
{
public static void main( String args[] )
{
Scanner scanner = new Scanner( System.in );
System.out.print("Enter the ID ");
int value = scanner.nextInt();
assert value>=15:" Invalid";
System.out.println("Value "+value);
}
}

Output

Enter the ID

Exception in thread “main” java.lang.AssertionError: Invalid

To make sure that the assumptions made during the program are correct,  assertions prove to be an important keyword.

Thus we have come to an end of this article on ‘Assertion In Java in Java’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this blog  and we will get back to you as soon as possible.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.