This week's giveaways are in the MongoDB and Jobs Discussion forums.
We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Simpe question on string Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Simpe question on string

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Can someone expalin me the difference betwwen this
Consider an 'str'as variable of String

str=NULL;

and str=null

which among 2 will throw Java NULLExpection error


Thanks<br />Dinesh
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

str = NULL will not compile. 'NULL' is not a Java keyword.

Also str = null will not throw a NullPointerException by itself. A NullPointerException is thrown when you try to call a method on a variable that is null. So if you set str to null and then call a method on str, then you will get a NullPointerException.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jesper Young:
str = NULL will not compile.


Unless NULL is defined somewhere, of course, for example as a field.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

To expand on this, remember that Java is case sensitive. So the keyword "null" is not the same as "Null" or "NULL" or any other combination of upper/lowercase. None of these other case variations have any meaning (unless you specifically define them as your own identifier).


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Please don't post multiple threads with the same subject.

The same (+-) was asked here:
http://www.coderanch.com/forums/

Bu.


all events occur in real time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simpe question on string
 
Similar Threads
Problem: string concatenation
String
String Question
Garbage Collection
how to read lines in BuffredReader