Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

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;
	}
}
Comment

PREVIOUS NEXT
Code Example
Java :: range in java 
Java :: how to make a copy of an array java 
Java :: how to add an image to a gui in java windowbuilder 
Java :: literals in java 
Java :: from string to date 
Java :: changer version java terminal 
Java :: java put a char array into a string 
Java :: odd number in java 
Java :: Java make numbers 
Java :: arrays.tostring java 
Java :: keytool error: java.io.FileNotFoundException: c:UsersUSER_NAMEupload-keystore.jks (The system cannot find the path specified) 
Java :: double quotes in string java 
Java :: java method return list 
Java :: java restart while loop 
Java :: max heap java 
Java :: fibonacci number in java 
Java :: pretty print json in console 
Java :: java while loop example 
Java :: string vs stringbuffer 
Java :: byte array to file java 
Java :: java read next line 
Java :: how to close a jframe in netbeans 
Java :: set path in windows 
Java :: java string array to one string 
Java :: testing the web layer without authentication spring 
Java :: java booleans 
Java :: java iterator to stream 
Java :: java coalesce 
Java :: java random int between 2 numbers 
Java :: random int in range java 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =