What does the “assert” keyword do? [duplicate]
This question already has an answer here: What does the Java assert keyword do, and when should it be used? 18 answers What does assert do? For example in the function: private static int charAt(String s, int d) { assert d >= 0 && d <= s.length(); if (d == s.length()) return -1; return s.charAt(d); } If you launch your program with -enableassertions (or -ea for short) then this statement assert cond; is equivalent to if (!cond) throw new AssertionError(); If you launch your program without this option, the assert statement will have no effect. For example, assert d >= 0 && d <= s.length(); ,