What's the difference between an empty string and a null string? And both of them will have an address in the memory? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What's the difference between an empty string and a null string? And both of them will have an address in the memory?

23rd Jan 2020, 4:02 PM
Hichem GOUIA
Hichem GOUIA - avatar
2 Answers
+ 2
String s="" ; Will reference to empty string. String s=null; will not point to any reference. Edit: For differences run this.. public class Program { public static void main(String[] args) { String s=null; String ss=""; System.out.println(s instanceof String); System.out.println(ss instanceof String); if(s==null) System.out.println(""+null); if(ss!=null) System.out.println("not null"+ss); if(s==ss) System.out.println("same"); else System.out.println("not same"); if(ss.equals(s)) System.out.println("same value"); } } //s. equals(ss) gives null pointer exception..
23rd Jan 2020, 4:19 PM
Jayakrishna 🇮🇳
0
Thanks for this detailed answer😊
23rd Jan 2020, 5:03 PM
Hichem GOUIA
Hichem GOUIA - avatar