Difference between java ' assert 'and ' if () {} else exit;'

you could, assert is specifically designed to assert some part of code,

assert will throw AssertionError if it fails to assert


Also See

  • assert reference

A bit of google maybe ?

" The main thing you should keep in mind is that the if-else statement should be used for program flow control and the assert keyword should only be used for testing purposes. You should never use asserts to actually perform any operation required for your application to work properly. According to Sun's official Java documentation: "Each assertion contains a boolean expression that you believe will be true when the assertion executes." "

Read more: http://wiki.answers.com/Q/What_is_the_difference_between_assert_keyword_and_if_keyword_in_java#ixzz1v2GGfAhq


I can just ignore the assertion

class A{

public static void main(String[] args) {
    assert false;
  System.out.println("hi");
}
}

This code will print hi by default

$ java -cp . A
hi
$ java -ea -cp . A
Exception in thread "main" java.lang.AssertionError
    at A.main(A.java:6)

Tags:

Java

Assert