• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Diff. between null & ""

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can some body please tell me difference between
String s = "" and
String s1 = null;
I think a object is made in first case and not in second case, but then what is the value contained in first case, also what is difference values in s = "" and s = " "(i.e. with a space)
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Akhil.
You are correct, String s = ""; creates an Object, whereas String s = null; does not. As for the contents of s when it contains an empty string, according to a comment in the source code of java.lang.String, "There is an empty string at index 0 in an empty string." Paradoxical, but it explains why s.endsWith( "" ) and s.startsWith( "" ) return true.
Also, String s = ""; is completely different from String s = " ";. An empty string is not the same as a String containing a space.
Hope this helps, Akhil.
Art
reply
    Bookmark Topic Watch Topic
  • New Topic