I have just started learning groovy and I am reading "Groovy in Action". In this book I came across a statement that it doesn’t matter whether you declare or cast a variable to be of type int or Integer.Groovy uses the reference type ( Integer ) either way.
So I tried to assign null value to a variable with type int
int a = null
But it is giving me below exception
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'null' with class 'null' to class 'int'. Try 'java.lang.Integer' instead at Script1.run(Script1.groovy:2)
Then I tried to assign null value to a variable with type Integer
Integer a = null
and it is working just fine.
Can anyone help me understand how groovy
behaves such way or the reason behind it?