• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic