Assert is for private arguments only

Most validity checks in a program are checks on parameters passed to non-private methods. The assert keyword is not meant for these types of validations. (Some may find this surprising; here is the Oracle guideline.)

Assertions can be disabled. Since checks on parameters to non-private methods implement the requirements demanded of the caller, turning off such checks at runtime would mean that part of the contract is no longer being enforced.

Conversely, checks on arguments to private methods can indeed use assert. These checks are made to verify assumptions about internal implementation details, and not to check that the caller has followed the requirements of a non-private method's contract.

In addition, if an AssertionError can be thrown by a method, then this fact should not appear in the method's javadoc comment. The specification of a method's behaviour should never mention assertions, since assertions can be disabled.

Example

public final class SpaceShuttle {

  /**
  * The non-nullity of aName is part of the contract of the
  * constructor.
  *
  * @param aName is non-null.
  */
  public SpaceShuttle(String aName) {
    //WRONG way:
    assert aName != null : "Name must not be null";
    //RIGHT way:
    if (aName == null) {
      throw new IllegalArgumentException("Name must not be null.");
    }
    fName = aName;
  }

  public String getName() {
    return fName;
  }

  // PRIVATE 
  private final String fName;
}
 

See Also :
Validate method arguments
Design by Contract