aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes null String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "null String" Watch "null String" New topic
Author

null String

Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

Why doesn't this code give NullPointerException:



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


"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
because whenever you use + with either operands as string,the result is a string
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

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".

Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
well that is a rule in the jls...anything with + and null is converted to strings


scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
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

Joined: Dec 10, 2009
Posts: 521
String s="null" ....doesnt mean String s=new String(null)...i pretty sure am right
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
HOW??
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
let me check
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

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

Joined: Dec 10, 2009
Posts: 521
String s=new String(null);
this doesnt compile
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
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

Joined: Nov 04, 2009
Posts: 114

well brother then how is this compiling??
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
i guess i have corrected it now
maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 186
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

Joined: Dec 10, 2009
Posts: 521
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

Joined: Nov 04, 2009
Posts: 114
so am I good now
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
you are a good boy Mr.Carter
Rajiv Chopra
Ranch Hand

Joined: Dec 19, 2008
Posts: 62
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.



Rajeev Kumar
SCJP 6.0 Certified 96%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: null String