Win a copy of Kotlin in Action this week in the Kotlin forum!
    Bookmark Topic Watch Topic
  • New Topic

Simpe question on string  RSS feed

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Report post to moderator
Originally posted by Jesper Young:
str = NULL will not compile.


Unless NULL is defined somewhere, of course, for example as a field.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Report post to moderator
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).
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please don't post multiple threads with the same subject.

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

Bu.
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!