• 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:

null String

 
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why doesn't this code give NullPointerException:



s contains the string "null". How is this happening?
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because whenever you use + with either operands as string,the result is a string
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks James. I understand that it's true with all primitives & objects that "+ String" converts the whole thing to a String. But null just indicates no obeject, nothing. How does that get converted to the literal string "null".

 
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well that is a rule in the jls...anything with + and null is converted to strings
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you are wrting
String s=null or "null"
the interprets it as String s=new String("null");
well if you want a null pointer exception use String h; and leave it uninitialised and perform any operation on it like toUpperCase();
then you will get null pointer exception


EDIT : ok my bad now i have corrected my error
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s="null" ....doesnt mean String s=new String(null)...i pretty sure am right
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HOW??
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
let me check
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s = new String(null); does not compile for me.

However, in the following code, it is clear that a null String is different from a String initialised as "null"


So I'm still wondering how String s = null + ""; becomes the literal "null"
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s=new String(null);
this doesnt compile
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there are two overloaded contuctors for strings String(String) and String(StringBuffer)
when you call new String(null).this gives you an ambiguous error....
String s=null + "" gives null as anything with + is converted to a string literal
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

well brother then how is this compiling??
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i guess i have corrected it now
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey it only happens when we put the value null into string ..we can concat a null value with other string constants....but String s="null" and String s=null are 2 different things..one is having string value null with four characters and other is having null value.....
try doing this...

You get a null pointerException since you are trying to perform operation on a string having null value....i hope i am right....anybody correct me if i am wrong ..
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why wont this compile? what did i say that maked you thing that this wont compile? actually String(null) means String s=new String(null) and not String s=null; i was referring to the first one
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so am I good now
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are a good boy Mr.Carter
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nidhi Sar wrote:Why doesn't this code give NullPointerException:



s contains the string "null". How is this happening?



Hi I saw the discussion. I want to add something.



null is a value which can be assigned to a reference object. It specifies the reference object has no object instance associated to it. In Java 'null' is a reserved word that can not be used as identifer and null means nothing or no value. 'null' is the default value all the uninitialized static & non-static reference objects.


 
reply
    Bookmark Topic Watch Topic
  • New Topic