07-20-2023, 03:49 PM
**Value to `val` variable can be assigned only once.**
val address = Address("Bangalore","India")
address = Address("Delhi","India") // Error, Reassigning is not possible with val
Though you can't reassign the value but you can certainly modify the properties of the object.
//Given that city and country are not val
address.setCity("Delhi")
address.setCountry("India")
That means you can't change the object reference to which the variable is pointing but the underlying properties of that variable can be changed.
**Value to var variable can be reassigned as many times as you want.**
var address = Address("Bangalore","India")
address = Address("Delhi","India") // No Error , Reassigning possible.
Obviously, It's underlying properties can be changed as long as they are not declared val.
//Given that city and country are not val
address.setCity("Delhi")
address.setCountry("India")
val address = Address("Bangalore","India")
address = Address("Delhi","India") // Error, Reassigning is not possible with val
Though you can't reassign the value but you can certainly modify the properties of the object.
//Given that city and country are not val
address.setCity("Delhi")
address.setCountry("India")
That means you can't change the object reference to which the variable is pointing but the underlying properties of that variable can be changed.
**Value to var variable can be reassigned as many times as you want.**
var address = Address("Bangalore","India")
address = Address("Delhi","India") // No Error , Reassigning possible.
Obviously, It's underlying properties can be changed as long as they are not declared val.
//Given that city and country are not val
address.setCity("Delhi")
address.setCountry("India")