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

后端 未结 18 794
梦谈多话
梦谈多话 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:13

    public class SwapStringVariable {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            String a = "test";
            String b = "paper";
    
            a = a + b;
            b = a.substring(0, a.length()  - b.length());
            a = a.substring(b.length(), a.length());
    
            System.out.println(a + " " + b);
    
    
        }
    
    }
    

提交回复
热议问题