How to swap two string variables in Java without using a third variable

后端 未结 18 763
梦谈多话
梦谈多话 2021-02-02 12:30

How do I swap two string variables in Java without using a third variable, i.e. the temp variable?

String a = \"one\"
String b = \"two\"
String temp = null;
temp         


        
18条回答
  •  梦谈多话
    2021-02-02 13:17

    You can also do this by using a temp variable but in a different way:

    String a = "one"
    String b = "two"
    String temp = null;
    
    temp=a.concat(b);
    b=temp.substring(0,a.length());
    a=temp.substring(a.length(),temp.length());
    
    System.out.println("After Swapping A:"+a+"B:"+b);
    

提交回复
热议问题