131

I can't seem to be able to find a summary that distinguishes the difference between these three annotations.

Rick Hanlon II
  • 20,549
  • 7
  • 47
  • 53

3 Answers3

363

@NotNull: The CharSequence, Collection, Map or Array object is not null, but can be empty.
@NotEmpty: The CharSequence, Collection, Map or Array object is not null and size > 0.
@NotBlank: The string is not null and the trimmed length is greater than zero.

To help you understand, let's look into how these constraints are defined and carried out (I'm using version 4.1):

  1. The @NotNull constraint is defined as:

    @Constraint(validatedBy = {NotNullValidator.class})  
    

    This class has an isValid method defined as:

    public boolean isValid(Object object, ConstraintValidatorContext constraintValidatorContext) {
     return object != null;  
    }
    
  2. The @NotEmpty constraint is defined as:

    @NotNull  
    @Size(min = 1)    
    

    So this constraint uses the @NotNull constraint above, and @Size whose definition differs based on the object but should be self explanitory.

  3. Finally, the @NotBlank constraint is defined as:

    @NotNull  
    @Constraint(validatedBy = {NotBlankValidator.class})        
    

    So this constraint also uses the @NotNull constraint, but also constrains with the NotBlankValidator class. This class has an isValid method defined as:

    if ( charSequence == null ) {  //curious 
      return true;   
    }   
    return charSequence.toString().trim().length() > 0;  
    

    Interestingly, this method returns true if the string is null, but false if and only if the length of the trimmed string is 0. It's ok that it returns true if it's null because, as I mentioned, the @NotEmpty definition also requires @NotNull.

Here are a few examples:

  1. String name = null;
    @NotNull: false
    @NotEmpty: false
    @NotBlank: false

  2. String name = "";
    @NotNull: true
    @NotEmpty: false
    @NotBlank: false

  3. String name = " ";
    @NotNull: true
    @NotEmpty: true
    @NotBlank: false

  4. String name = "Great answer!";
    @NotNull: true
    @NotEmpty: true
    @NotBlank: true

Rick Hanlon II
  • 20,549
  • 7
  • 47
  • 53
  • 34
    I spent some time tracking this information down on my own, and I wanted to help others benefit from that effort. "To be crystal clear, it is not merely OK to ask and answer your own question, it is explicitly encouraged." http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/ – Rick Hanlon II Jun 16 '13 at 20:33
  • 4
    In my opinion, @NotBlank SHOULD accept null. There are some cases you would accept null for optional fields where blank is just invalid. – tbraun May 09 '16 at 15:18
  • 1
    I'd agree with that. If you want not null and not empty, you could use both. Since that's not the case, you could write your own validator and use that as you expect. – Rick Hanlon II May 09 '16 at 15:22
  • 1
    Just a note: @NotNull can be used with any Object, not just "CharSequence, Collection, Map or Array"; as indeed the code you inserted in point 1 shows. – Niccolò Jun 02 '16 at 12:29
  • @RickHanlonII Could u suggest that how is it getting validated in Hibernate Validator 5+. There doesn't seem to be any `ConstraintValidator` associated with these annotations [any longer](http://red.ht/2IiwAZw). – Steve Mar 11 '18 at 10:45
  • can someone help what validator annotation do we use for File type? – Anu Dec 22 '20 at 06:24
9

I liked the explanation in the below link: http://www.itprogrammingtutorials.com/2015/java/hibernate/hibernate-validator-diff-notblank-notempty/

@NotNull: Checks whether the value is not null, disregarding the content

@NotEmpty: Checks whether the value is not null nor empty. If it has just empty spaces, it will allow it as not empty.

@NotBlank: Checks whether the value is not null nor empty, trimming the value first. It means that, it won’t allow just empty spaces.

So, if you want to validate that a field is not null but also that it doesn’t has just empty spaces, but text, you should use @NotBlank.

Pratiksha
  • 91
  • 1
  • 2
6
  1. @NotNull: a constrained CharSequence, Collection, Map, or Array is valid as long as it’s not null, but it can be empty
  2. @NotEmpty: a constrained CharSequence, Collection, Map, or Array is valid as long as it’s not null and its size/length is greater than zero.
  3. @NotBlank: a constrained String is valid as long as it’s not null and the trimmed length is greater than zero.
Mukesh Kumar Gupta
  • 1,567
  • 20
  • 15