Int gets Concatenates using Interface with class


#1

i have an interface and a class is using it but the problem is i want to add 10 in the final result but the 10 is getting concatenated with the final result while Division is working fine please check the code.

> interface MainInterface {
>     fun sum(n1:Int, n2:Int){ println("Sum is "+n1+n2)}
>     fun div(n1:Int, n2:Int){ println("div is "+n1/n2)}
>     
> }
> 
> class Admin:MainInterface{
>     override fun sum(n1: Int, n2: Int) {
>         println("Sum is "+ (n1+n2)+10)
>     }
> 
>     override fun div(n1: Int, n2: Int) {
>         println("div is "+ (n1/n2)/5)
>     }
> 
> }
> 
> //Main function
> 
> fun main(args:Array<String>){
>     val admin = Admin()
>     admin.sum(20,25)
>     admin.div(100,5)
> 
> }
> 
> 
> //Output
> Sum is 4510
> div is 4

#2

That it why kotlin discourages the use of string concatenation.
"Sum is "+ (n1+n2)+10 is a string expression, not a number one, so first it adds n1+n2 to the string and gets the string, and first it adds 10 as a string. In order to get what you want, you need to change the order of operations adding braces: "Sum is "+ ((n1+n2)+10). But the correct kotlinish way to do it is "Sum is ${(n1+n2)+10}".


#3

thanks a lot i got the desired results and i’ll keep that in mind in future to avoid this problem thanks once again.