What does the assert keyword mean in java?

I saw somewhere in the GWT code, it was something like this

assert display instanceof Widget : "display must extend Widget"; 
+4
source share
5 answers

The assert keyword, as the name suggests, makes a statement about the code. It is used to indicate what is held all the time - or at least it should be true!

The assert keyword is followed by a boolean value ( true or false ) or an expression that must be evaluated at run time that returns a boolean value.

 assert true; assert 1 == 1; 

If for any reason the boolean expression evaluates to false, an AssertionError is thrown.

 // this will throw an AssertionError: int x = 1; assert x == 2; 

When you use it, you make a clear expression about the state of your program at a given point, which makes it easier for the reader to read through your code.

There is a programming paradigm called a contract program in which code fragments contain statements about the preconditions that must be met correctly for them to be executed properly, and afterwords that are guaranteed to be sure after they are executed. You can use the assert keyword to implement this.

For example, if you write a method that calculates the square root of a number, it will only work for numbers that are greater than or equal to zero, and the result is guaranteed to satisfy the same conditions:

 public double sqrt(final double x) { assert x >= 0 : "Cannot calculate the square root of a negative number!" double result = ...; assert result >= 0 : "Something went wrong when calculating the square root!" return result; } 

The most interesting aspect of the statements is that you can ask the compiler to remove them from the bytecode (using the -disableassertion argument) so that you don't get any execution penalty during production execution. For this exact reason, it is fundamental that the expression to be evaluated does not cause side effects , i.e. The expression should look like a pure mathematical function. Otherwise, the behavior of your program may change if the compiler removes your statements.

Finally, if statements are compiled into bytecode, they can be read using software that will automatically generate tests that try to break your code. It may be helpful to find errors earlier!

+11
source

assert keyword was introduced in 1.4 (follow this link for a full description). This is a summary to throw an exception at runtime if the condition is not met.

Think of it as

 assert condition : message 

as

 if ( ! condition ) { throw new AssertionError ( message ) ; } 

The idea is to give developers an easy way to help users (in your case, GWT API users) detect common errors / traps

When it was introduced, the assert became a reserved word and caused several compilation problems when the old code was recompiled for I.4. Especially for JUnit test suites, where the very commonly used assert() method was used. JUnit responded by replacing assert with assertTrue()

+3
source

This means that if the display is not an object of type Widget, you will get an AssertionError with the text string following the statement. Statements are useful for debugging.

0
source
Keyword

assert is used to simplify custom exceptions. Be that as it may, to define a custom exception, we must create our own exception class, first defining the condition that causes the exception, then we must throw it in our program. but from java 1.5 onwards we have a keyword like assert, where only we have to write assert (condition), if condtion is true, it executes another part of the program, otherwise, if it is false, it creates an object of class AssertionError, and we have to handle this. therefore there is no need to define our userdefind error.

0
source

The following text (emphasis mine) clearly explains the various forms of statements:


A statement of a statement has two forms.

The first, simpler form:

assert Expression1;

where expression 1 is a boolean expression. when the system executes the statement, it evaluates the expression 1, and if it false generates an AssertionError without a detailed message.

The second form of approval:

assert Expression1: Expression2; (Your example falls out here)

where: Expression1 is a boolean expression. Expression2 is an expression that matters. (This cannot be a call to a method that is declared void.) Use this version of the assert statement to provide a detail message for an AssertionError. The system passes the value of Expression2 to the corresponding AssertionError constructor, which uses the string representation of the value as a detailed error message.

Also, for more information, see the following oracle link: http://docs.oracle.com/javase/7/docs/technotes/guides/language/assert.html

0
source

Source: https://habr.com/ru/post/1342584/


All Articles