Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does request.getCookies() return null when no cookies have been sent?

Tags:

java

servlets

According to the Javadoc, HttpServletRequest.getCookies() "Returns an array containing all of the Cookie objects the client sent with this request.", and it returns null if no cookies were sent.

Is there a specific reason for this behavior other than returning an empty array, which seems a little more intuitive to me and avoids needing to check for null before iterating on the array to look for a specific cookie?

like image 917
Raibaz Avatar asked Mar 28 '14 10:03

Raibaz


1 Answers

That used to be a common Java practice in situations like this. The main reason was probably due to it being slightly more efficient to return nothing than return an empty list (less work on the garbage collector).

like image 154
Mike Thomsen Avatar answered Oct 21 '22 03:10

Mike Thomsen