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

后端 未结 18 762
梦谈多话
梦谈多话 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 do in this way.

    public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            String a = "one";
            String b = "two";
    
            System.out.println(a);
            System.out.println(b);
    
            a = a+b;
            b = "";
    
            System.out.println("*************");
             b = a.substring(0, 3);
             a = a.substring(3, 6);
    
             System.out.println(a);
             System.out.println(b);
    
        }
    

提交回复
热议问题