0
public class Main {
    public static void main(String args[]) {
        sumArray(8);
    }

    public static int sumArray(int nums){
       assert nums == 5;
       return nums;
    }
}

I was just wondering, when I pass a number that is not equal to 5, why doesn't the compiler throw any errors, since I am asserting that nums is equal to 5?

Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47
E-SetPrimeEpsilon
  • 113
  • 1
  • 3
  • 9

1 Answers1

1

If the program is running with assertions enabled, then the condition is checked at runtime. If the condition is false, the Java runtime system throws an AssertionError.

For backward compatibility, the JVM disables assertion validation by default. They must be explicitly enabled using either the -enableassertions command line argument, or its shorthand -ea

Reference: https://www.baeldung.com/java-assert

Dennis
  • 3,962
  • 7
  • 26
  • 44