• 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

difference between null string and ""

 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.String s=""
2.String x=null

is 1& 2 are same??
cinux
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are not the same.

String s = ""; would be the same as String s = new String("");
You are implicitly instanciating a new String variable.

In your example, try to call s.length() and x.length()
[ January 10, 2006: Message edited by: Satou kurinosuke ]
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's ok but if run the below program it perfectly executes with out error saying that s is not initialized with string value.

public class Test
{
public static void main(String[] args) throws Exception {
step1 String s="";
step2 //String s2;//leads to compile time error because no value is initialized

System.out.println(s.length());

}
}

I think step1 an step2 are same. :roll: :roll:
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
remember that s and x are REFERENCES to objects, not the objects themselves. Think of them as envelopes.

when you say

String s = "";

you are saying "I want an envelop, and put an adress on the envelope, even though nobody lives at that address."

when you say

String x = null;

you are saying "i want an envelope with NO address on it."

if you mail the first envelope, it'll go somewhere. the second one will confuse the post office.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by saikrishna cinux:

I think step1 an step2 are same. :roll: :roll:



No, they are not the same. s is referring to a valid String object that has no characters in it. s2 is referring to nothing.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by fred rosenberger:
if you mail the first envelope, it'll go somewhere. the second one will confuse the post office.



Not often does tech talk make me laugh, but this sure did.

Nice analogy.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can get the program to be compiled if you write:
String x = null;

But of course then x.length() will throw a NullPointerException if you run the program.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by fred rosenberger:
...if you mail the first envelope, it'll go somewhere. the second one will confuse the post office.


Very nice.
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by fred rosenberger:
remember that s and x are REFERENCES to objects, not the objects themselves. Think of them as envelopes.

when you say

String s = "";

you are saying "I want an envelop, and put an adress on the envelope, even though nobody lives at that address."

when you say

String x = null;

you are saying "i want an envelope with NO address on it."

if you mail the first envelope, it'll go somewhere. the second one will confuse the post office.



My dear fred u really Rock!!! explained it in a gr8 way..
keep it up ..
and thanks a lot
bye
 
reply
    Bookmark Topic Watch Topic
  • New Topic