Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between returns a null value and an empty string?

Tags:

java

servlets

Suppose you have an input field named "adminNo". What is the difference when you call to getParameter("adminNo") method returns a null value and when it returns an empty string ""?

like image 663
Abby Avatar asked Oct 24 '12 13:10

Abby


People also ask

What is the difference between null value and empty value?

The value null represents the absence of any object, while the empty string is an object of type String with zero characters. If you try to compare the two, they are not the same.

What is the difference between null and empty string in SQL?

Sometimes strings can be empty or NULL. The difference is that NULL is used to refer to nothing. However, an empty string is used to point to a unique string with zero length.

Should you return null or empty string?

The function is meant to return a single value, such as findPerson(). If it was designed to return a collection e.g. findAllOldPeople() then an empty collection is best. A corollary of this is that a function which returns a collection should never return null.

What is the difference between null and empty list?

An empty collection isn't the same as null . An empty collection is actually a collection, but there aren't any elements in it yet. null means no collection exists at all.


2 Answers

A call of getParameter("adminNo") returns an empty String if the parameter called adminNo exists but has no value, and null is returned if there was no such parameter.

like image 68
Sergey Kalinichenko Avatar answered Sep 28 '22 00:09

Sergey Kalinichenko


From the JavaDoc:

Returns the value of a request parameter as a String, or null if the parameter does not exist.

What this means in reality is:

  • when the return value is null the HTML form didn't have an input with the parameter name in it
  • when the value is an empty String the HTML form did have an input with the parameter name in it but that no value was set.
like image 25
Nick Holt Avatar answered Sep 28 '22 02:09

Nick Holt