public class TestDemo { public static void swap(int[] array){ int tmp = array[0]; array[0] = array[1]; array[1] = tmp; } public static void main(String[] args) { int[] array = {10,20}; System.out.println("交换前: "+array[0]+" "+array[1]); swap(array); System.out.println("交换后: "+array[0]+" "+array[1]); }
Print results:
The above is the detailed content of How to exchange the values of two variables in Java?. For more information, please follow other related articles on the PHP Chinese website!