定义两个变量 在不定义第三个变量的情况下交换两个变量的值

/**
 * 定义两个变量 在不定义第三个变量的情况下交换两个变量的值
 */
public class jiaoHuan {
 
    public static void main(String[] args) {
        String s1 = "00";
        String s2 = "abcd";
        System.out.println("交换之前 s1 = " + s1 + " s2 = " + s2);
        s2 = s1 + s2; //合并到s2
        System.out.println("s2 = " + s2); //00abcd
        s1 = s2.substring(s1.length());
        //截取前面长度为4的字符串 s1 = "abcd"
        s2 = s2.substring(0, s2.length() - s1.length());
        //截取最后长度为2的字符串 s2 = "00"
        System.out.println("交换后 s1 = " + s1 + " s2 = " + s2);
    }
 
 
}
 
/**
 * int类型
 */
class testInt {
 
    public static void main(String[] args) {
        int a = 1;
        int b = 9;
        System.out.println("交换之前 a = " + a + " ,b = " + b);
        a = a + b;
        b = a - b;
        a = a - b;
        System.out.println("交换之后 a = " + a + " ,b = " + b);
    }
}
原文地址:https://www.cnblogs.com/mryangbo/p/11272865.html