commit a8d54ad54e493f63e23c92e717c6f33a3e017552 Author: Ramon Caballero Date: Tue Jul 23 15:33:39 2024 +0100 Example on how to use assertions in Java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d8aa9ac --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +# Unwanted Eclipse IDE settings files: +/.classpath +/.project +/.settings/ + +# Unwanted build generated folders: +/bin/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..575655a --- /dev/null +++ b/README.md @@ -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` diff --git a/src/Assertions.java b/src/Assertions.java new file mode 100644 index 0000000..bac4195 --- /dev/null +++ b/src/Assertions.java @@ -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."; + } +}