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

(String) null  RSS feed

 
Philip Pomario
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is it possible that if I try to parse a null reserved word into String I don't get any type of error, at compilation or run time.

String myWord =(String) null; // That's all fine! What the heck!?
 
Rovas Kram
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what you mean by 'parse' but

String myWord =(String) null; is equivalent to String myWord;
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rovas Kram:
I don't know what you mean by 'parse' but

String myWord =(String) null; is equivalent to String myWord;


For a member variable, yes; for a local variable, no. Local variables are not initialized to any value by default.

Filipe, please read this and this and you should understand perfectly.
 
Rovas Kram
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Filipe, please read this and this and you should understand perfectly


Thanks for pointing that out but I think the simple answer is that you must initialize local variables - you get a compile error if you don't.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!