Home  >  Article  >  Java  >  How to exchange the values ​​of two variables in Java?

How to exchange the values ​​of two variables in Java?

WBOY
WBOYforward
2023-04-27 17:55:071877browse

Exchange the values ​​of two variables

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:

How to exchange the values ​​of two variables in Java?

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete