Difference between null and empty string
String s1 = "";
means that the empty String
is assigned to s1
.
In this case, s1.length()
is the same as "".length()
, which will yield 0
as expected.
String s2 = null;
means that (null
) or "no value at all" is assigned to s2
. So this one, s2.length()
is the same as null.length()
, which will yield a NullPointerException
as you can't call methods on null
variables (pointers, sort of) in Java.
Also, a point, the statement
String s1;
Actually has the same effect as:
String s1 = null;
Whereas
String s1 = "";
Is, as said, a different thing.
Difference between null and empty () Java String
"" is an actual string, albeit an empty one.
null, however, means that the String variable points to nothing.
a==b
returns false because "" and null do not occupy the same space in memory--in other words, their variables don't point to the same objects.
a.equals(b)
returns false because "" does not equal null, obviously.
The difference is though that since "" is an actual string, you can still invoke methods or functions on it like
a.length()
a.substring(0, 1)
and so on.
If the String equals null, like b, Java would throw a NullPointerException
if you tried invoking, say:
b.length()
If the difference you are wondering about is == versus equals, it's this:
== compares references, like if I went
String a = new String("");
String b = new String("");
System.out.println(a==b);
That would output false because I allocated two different objects, and a and b point to different objects.
However, a.equals(b)
in this case would return true, because equals
for Strings will return true if and only if the argument String is not null and represents the same sequence of characters.
Be warned, though, that Java does have a special case for Strings.
String a = "abc";
String b = "abc";
System.out.println(a==b);
You would think that the output would be false
, since it should allocate two different Strings. Actually, Java will intern literal Strings (ones that are initialized like a and b in our example). So be careful, because that can give some false positives on how == works.
Difference between NULL and Blank Value in Mysql
NULL
is an absence of a value. An empty string is a value, but is just empty.NULL
is special to a database.NULL
has no bounds, it can be used forstring
,integer
,date
, etc. fields in a database.NULL
isn't allocated any memory, thestring
withNULL
value is just a pointer which is pointing to nowhere in memory. however, Empty IS allocated to a memory location, although the value stored in the memory is""
.
Any difference between String = null and String.isEmpty?
The empty string is a string with zero length. The null value is not having a string at all.
- The expression
s == null
will returnfalse
if s is an empty string. - The second version will throw a
NullPointerException
if the string is null.
Here's a table showing the differences:
+-------+-----------+----------------------+
| s | s == null | s.isEmpty() |
+-------+-----------+----------------------+
| null | true | NullPointerException |
| "" | false | true |
| "foo" | false | false |
+-------+-----------+----------------------+
What is the difference between null and isEmpty?
Of course, null and isEmpty() is different.
This is a simple explanation of the difference:
Let's take an example of a String.
Null means that your object (String) is not instantiated yet. It's still pointing to nothing. While isEmpty() is a method to check if a String contains no character.
You can try to make 2 different String objects, where one String defines as a null, while the other one is empty, as below.
String nullString = null;
String emptyString = "";
try {
System.out.println(nullString); // will produce NullPointerException
nullString.isEmpty(); // will produce NullPointerException
} catch (NullPointerException e) {
System.out.println(emptyString); // OUTPUT: (empty)
emptyString.isEmpty(); // OUTPUT: true
}
I hope it's clear.
-CMIIW-
How to differentiate between empty string and null with OpenCSV
I found the solution: strictQuotes can be used to get nulls:
CSVReader reader = new CSVReader(isReader, ',', '"', true);
while ((col = reader.readNext()) != null) {
String c1 = col[1];
if (null != c1) {
// ...
}
}
Related Topics
String's Maximum Length in Java - Calling Length() Method
Initialising a Multidimensional Array in Java
Add Image Thumbnails to a Layout in a Grid
Why Does the Tostring Method in Java Not Seem to Work for an Array
Dynamically Add Components to a Jdialog
How to Get Current Moment in Iso 8601 Format with Date, Hour, and Minute
How to Catch an Exception from a Thread
How to Connect to Oracle Using Service Name Instead of Sid
How to Resolve the "Java.Net.Bindexception: Address Already in Use: Jvm_Bind" Error
Do I Need <Class> Elements in Persistence.Xml
Comparator.Reversed() Does Not Compile Using Lambda
Powermockito Mock Single Static Method and Return Object
Java.Text.Parseexception: Unparseable Date
Java.Lang.Runtimeexception: Uncompilable Source Code - What Can Cause This