In Java are these 2 statements the same?
String a = null;
String b = "";
It feels a bit of a dumb question but have a complete mental block currently.
The empty string and null
are different. The empty string is a string with no characters, null
is not having a string at all.
You can call methods on an empty string but if you try to call a method on null you will get an exception.
public static void main(String[] args)
{
String a = null;
String b = "";
System.out.println(b.length());
System.out.println(a.length());
}
Output:
0 Exception in thread "main" java.lang.NullPointerException at Program.main(Program.java:12)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With