Null or not null
I did the easy thing - I converted my entire project to kotlin. A StackOverflow question told me that I need to go to Code menu in Android studio and give the option "convert java to kotlin".
As simple as that. I opened each and every java source file and converted them into .kt file.
Then came the fun part - hundred of errors. Because this automatic conversion does not consider the subtle points of your code.
override : In java, when an inherited class redefines a base class function, it uses @Override before the function name. But kotlin uses "override". Automatic conversion does not convert @Override to override. You will have to do it yourself.
Null, not null and null safety : Kotlin is very particular about null safety. It uses the following ? and !! for this purpose.
A variable defined as ? in front of its type name is nullable.
e.g.
var str1:String="Hello world"
var str2:String?="Hello world"
str2 is a nullable string. And str1 is not nullable string.
So when you convert a java program in Android studio to kotlin, many of the Activity class methods use nullable parameters and hence the overriding methods also need to specify ? in front of the parameter type.
e.g.
override
fun onCreate(savedInstanceState:Bundle?){}
So we are specifying that the parameter savedInstanceState can be null.
Unless and until specify this nullable operator (?), the IDE keeps telling you that there is an "accidental override error. "
There is an operator !! which will convert any type to non-null type if possible and throws an exception if it is null.
What I have been doing so far is try ?, if it does not work, try !!. And if that fails as well shut down the IDE.
And I have taken more than a week (7 working days and 3 hours a day :) ), so far and still keep finding plenty of red lines in these kt files. Am I going in Circles? Here just like in life?
As simple as that. I opened each and every java source file and converted them into .kt file.
Then came the fun part - hundred of errors. Because this automatic conversion does not consider the subtle points of your code.
override : In java, when an inherited class redefines a base class function, it uses @Override before the function name. But kotlin uses "override". Automatic conversion does not convert @Override to override. You will have to do it yourself.
Null, not null and null safety : Kotlin is very particular about null safety. It uses the following ? and !! for this purpose.
A variable defined as ? in front of its type name is nullable.
e.g.
var str1:String="Hello world"
var str2:String?="Hello world"
str2 is a nullable string. And str1 is not nullable string.
So when you convert a java program in Android studio to kotlin, many of the Activity class methods use nullable parameters and hence the overriding methods also need to specify ? in front of the parameter type.
e.g.
override
fun onCreate(savedInstanceState:Bundle?){}
So we are specifying that the parameter savedInstanceState can be null.
Unless and until specify this nullable operator (?), the IDE keeps telling you that there is an "accidental override error. "
There is an operator !! which will convert any type to non-null type if possible and throws an exception if it is null.
What I have been doing so far is try ?, if it does not work, try !!. And if that fails as well shut down the IDE.
And I have taken more than a week (7 working days and 3 hours a day :) ), so far and still keep finding plenty of red lines in these kt files. Am I going in Circles? Here just like in life?
Comments
Post a Comment