[Java] Permutation functions in Java – Swap in Java

The implementation of the function swap (swapped – conversion) in java it is somewhat different from C / C or Pascal,… To change the value of the variable out of a function, you must use the object. You can refer to 2 VD below to swap content then if you want to read details below or if you find it confusing to read the code, please read the explanation below first, then go back to the code.

VD1: Using objects

package VietSource.net.temp;

import java.util.Scanner;

class Number {				// lop Number
	int num;

	public Number(int num) {		// ham khoi tao
		this.num = num;
	}

	public int getNum() {			// ham tra ve gia tri num
		return num;
	}

	public void setNum(int num) {	// ham set gia tri num
		this.num = num;
	}
}

public class MySwap {
	private void swap(Number a, Number b) {		// ham hoan vi
		int temp = a.getNum();					// gan num cua a cho temp
		a.setNum(b.getNum());					// gan num cua b cho a
		b.setNum(temp);							// gan temp cho num cua b
	}

	public static void main(String[] args) {
		Number a, b;								// hai doi tuong Number a va b
		MySwap sw = new MySwap();					// doi tuong sw thuoc lop MySwap
		Scanner scan = new Scanner(System.in);

		System.out.print("Nhap a = ");
		a = new Number(scan.nextInt()); 			// khoi tao a voi num la so nhap vao
		System.out.print("Nhap b = ");
		b = new Number(scan.nextInt()); 			// khoi tao b voi num la so nhap vao
		scan.close();
		System.out.println("Befor swap: a = " + a.getNum() + " b = " + b.getNum());
		sw.swap(a, b);								// su dung ham swap
		System.out.println("After swap: a = " + a.getNum() + " b = " + b.getNum());
	}
}

CEO 2 Use the last segment (substance is also an object array)

package VietSource.net.temp;

import java.util.Scanner;

public class SwapArray {
	private void swapArray(int arr[], int i, int j){
		int temp = arr[i];
		arr[i] = arr[j];
		arr[j] = temp;
	}
	
	public static void main(String[] args) {
		SwapArray sw = new SwapArray();
		Scanner scan = new Scanner(System.in);
		int arr[] = new int[2];
		
		System.out.print("Nhap a = ");
		arr[0] = scan.nextInt();
		System.out.print("Nhap b = ");
		arr[1] = scan.nextInt(); 
		
		System.out.println("Befor swap: a = " + arr[0] + " b = " + arr[1]);
		sw.swapArray(arr, 0, 1);
		System.out.println("After swap: a = " + arr[0] + " b = " + arr[1]);
		
		scan.close();
	}
}

One of the difficulties (and controversial) for C programmers to Java programming as the pass-by-value or
pass-by-reference. Example:

Object a = new Object("Object A"); //kỳ quặc với C++: Object* a = new ...
Object b = a;
b.setNewName("Object B");

According to the understanding of C Object b devs are clearly changing the name has nothing to object a whole. In fact according to the different Java, Object will have a name as "Object B" is like Object b both. To Object b completely "independent" with the object a: Object b = a.clone();

But the function swap() Java to operate in such a way pass-by-reference.

private void swap(Type arg1, Type arg2) {
	Type temp = arg1;
	arg1 = arg2;
	arg2 = temp;
}

So there!!! Java actually how? Hide behind the controversial employing a maze so what?

First it is necessary to understand that the pass-by-value is transmitted by a copy of the parameter values ​​in a "variable" is used within the function. So when the function returns, it will destroy the parameters copied, and the parameter passed is not affected changes. Contrary to it is pass-by-reference is actually an alias to create real parameters. When content changes in the parameters that it also directly change the parameter passed. Failure to make a copy in C after saving CPU with a large object, with the copy / initialize complex.
However, the swap function in Java does not work because it works in a way pass-by-value. After the swap function returns arg1 and arg2 is not to switch sides. But oddly enough, if the function body swap call a method of changing object Type, eg: arg1.setValue(“Change”);

According to this way of thinking, the pass-by-value after exiting the function swap arg1 not be changed. The reality is different, arg1 called setValue() so when exiting the function, the value of arg1 has changed. Recall that in C there is no concept of reference (&). But there pointer C (pointer) and it makes strong C flexibility without having reference. Java does not have pointers? Many people believe that: Object is the pass-by-reference is also primitives are pass-by-value (In Java, Objects are passed by reference, and primitives are passed by value.) This clause is only half right. Object không phải được pass-by-reference mà là object reference được pass-by-value. We will know in the answer:
The answer to all of the above in Java odd is that Java was "clean" pointer syntax in C. Java has pointer but the pointer is declared and used a very clean. And of course it but not as strong pointer in C, but it also "fix" all the potential errors and dangerous (casting, releasing pointer hay pointer arithmetic) of pointers in C.
In Java:
Object a;

like in C / C
Object* a;

So when
Object b = a;

means in C / C
Object* b = a; // b and a are pointer

and
b.setName(“Object B”);

equivalent to
b->setName(“Object B”);

So b.setName(”Object B”) also placed object "point" by a named "Object B" for a and b point to the same object.
And

private void swap(Object a, Object b)
{
	Object temp = a;
	a = b;
	b = temp;
}

corresponding to

void swap(Object* a, Object* b)
{
	Object* temp = a;
	a = b;
	b = temp;
}

Clearly this function in C does not work unless this change:

void swap(Object* a, Object* b)
{
	Object temp = *a;
	*a = *b;
	*b = temp;
}

"Cursor" a and b are passed style pass-by-value (can understand that Java is like C, All parameters are passed style pass-by-value) so after the function returns a swap, b unchanged or assignment a = b does not make sense. But a.setValue("...") the changed object pointed by the cursor.

Reference:
http://javadude.com/articles/passbyvalue.htm
http://java.forumvi.net/t23-topic