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

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

    public class ex{
        public static void main(String[] args){
    
            String s1="abc";
            String s2="def";
    
            System.out.println(s1);
            System.out.println(s2);
    
            s3=s2.replaceAll(s1,s2=s1);
    
            System.out.println(s1);
            System.out.println(s2);
    
        }
    }
    

提交回复
热议问题