Example on how to use assertions in Java

This commit is contained in:
Ramon Caballero 2024-07-23 15:33:39 +01:00
commit a8d54ad54e
3 changed files with 39 additions and 0 deletions

7
.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
# Unwanted Eclipse IDE settings files:
/.classpath
/.project
/.settings/
# Unwanted build generated folders:
/bin/

17
README.md Normal file
View File

@ -0,0 +1,17 @@
# Assertions
Example on how to use assertions in Java
## Compile
`javac Assertions.java`
## Run
Assertions need to be enabled with `-ea` or `-enableassertions`
`java -ea Assertions`
or
`java -enableassertions Assertions`

15
src/Assertions.java Normal file
View File

@ -0,0 +1,15 @@
public class Assertions
{
public static void main(String[] args)
{
int a = 25;
int b = 5 * 5;
assert a == b : "'a' and 'b' are not the same.";
String abc = "abc";
String def = "def";
assert abc.equals(def) : "'abc' and 'def' are not the same.";
}
}