This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
"" is an emptry String. That is a String of charactors that has no charactors in it. Even though there are no charactors, the Object reference is pointing to somewhere in memory and is allocated on the heap.
null, means the String object dosnt actual represent anything in memory, thus the object reference is NOT pointing to somewhere in memory and these is NO allocation on the heap.
generally speaking, NULL and empty should be treated as two distinct definitions.
You are printing out the first String, followed by what you get from the 2dn String. The 2nd String is null, so the + operator adds "null" to whatever it was going to print out. So it prints out AB: then thisfollowed by BC: null. And the bit it prints out before "BC: null" looks like nothing.
There are three possible String objects which beginners confuse
The null String, which is your bc
The empty String, which is a String with no content, "", like your ab
A space String containing a single space, " "
The "" String is NOT a null String; it actually exists. A null String is a reference to a String which does not point to any object.
Actually, the problem is that invoking any method on an Object reference pointing to null will result in a NullPointerException, so bc.equals() will throw a NullPointerException regardless of what argument it gets.
The argument being null is a minor problen, for the equals() methods of a lot of classes simply return false if the argument Object is null, but will not throw a NullPointerException.