Answers for "swap method 2 variables in java"

1

How to swap two values in Java using a supporting method?

public class SwapValues {
	/*
	 * To effect the swapping, arrays of size 1
	 * are used since array variables store the
	 * address of the value.
	 */
	public static void main(String[] args) {
		int[] val1 = new int[] { 12 }, val2 = new int[] { 13 };
		System.out.println(val1[0] + ", " + val2[0]); // 12, 13
		swapValues(val1, val2);
		System.out.println(val1[0] + ", " + val2[0]); // 13, 12
	}

	private static void swapValues(int[] val1, int[] val2) {
		int temp = val1[0];
		val1[0] = val2[0];
		val2[0] = temp;
	}
}
Posted by: Guest on April-22-2022
0

swapping techniques using java

Swapping
Posted by: Guest on August-18-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language