Swap two strings in Java, by passing them to a utility function, but without returning objects or using wrapper classes

前端 未结 9 2248
既然无缘
既然无缘 2020-12-16 20:31

I am trying to swap two strings in Java. I never really understood \"strings are immutable\". I understand it in theory, but I never came across it in practice.

Also

9条回答
  •  死守一世寂寞
    2020-12-16 21:13

    The s1 and s2 variables in your function swap are local to that function.

    You need to define s1 and s2 as private variables for your class, or return an array of strings from your function.

提交回复
热议问题