SEARCH BY

All

  • All
  • Java8
  • Spring

@NotNull vs @NotEmpty vs @NotBlank

Post a Comment


@NotNull 

@NotNull is a variable level annotation used to specifies that that the variable must not be null
String name=null;  --> returns false
name=""; --> returns true
name=" "-> return true
is applicable for char, string, collections and arrays

@NotEmpty 

@NotEmpty is a variable level annotation used to specify that value must not be null and not be empty that means size must be greater than zero
is applicable for char, string, collections and arrays

@NotBlank

@NotBlank is variable level annotation applicable only for String where variable not to be null and length must be more  than zero.


@NotNull
@NotEmpty
@NotBlank
Char
Yes
Yes
Yes
String
Yes
Yes
Yes
Collections
Yes
Yes
No
Arrays
Yes
Yes
No
Size > 0
May be empty
Yes
Yes
Empty
allowed
Not allowed
Not allowed

differnces between null and emtpy

String name=null; -> this is null
String name=""; -> not null but empty

jaya
I love software designing, coding, writing and teaching...

Share this article

Related Posts

Post a Comment

Place your code in <em> </em> tags to send in comments | Do not add hyper links in commnet

Close

Subscribe our newsletter for the latest articles directly into your email inbox