[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
This problem is quite interesting. Nice article, it will help you better understand some of the cursor. Part pointers in java you say is correct. But at a deeper level, there are some of you may not understand the nature of the problem. I have a few reviews make you protest:
– In c, c , obj-c hay java, which you transmit to content, in all cases, reference or by value, it is just the value of the variable, or, a copy is also. “pass-by-reference, it actually creates an alias to the true parameter” This is not right where you sir. This is just a copy of the value only. You never change the value of the arguments passed in.
– pass-by-value means that the parameters you pass in just 1 constant, you can not change anything.
– pass-by-reference means that the parameters you pass in a pointer, This pointer from which you can change the value of the pointer variable that can refer to. However, you can not change the value of this pointer.
private void swap(Type arg1, Type arg2) {
Type temp = arg1;
arg1 arg2 =;
arg2 = temp;
}
In this example, point # between java and C is what:
– with java: type of argument is essentially the Type *: You can change the value of guy arg1.
– with java: parameter type is Type: you can not change the value of guy arg1.
If you write in C :
private void swap(Type * arg1, Type * arg2) {
}
If you think you can change the value of the argument, you have misconceptions.
First you change the variable Type, Your arguments are passed to the type Type * muscle.
Shame really, c h before her only and should not know that java c with pass-by-reference. Last night my first learn c . And I confuse Reference to obj with pass-by-reference. This morning my read and understood. Must try to forget the stupid cmt which proved another dangerous new DC. ^^.
Thank you for visiting the blog. 😀
Overall, it's a little part of this complex, Sure you have to learn and revise their concepts are. If anything goes wrong you may wish to share.
Cho mình hỏi tại sao hàm swap bạn ko để trong class Number luôn mà phải tạo thêm class MySwap chi vậy?
Thực ra để ở class nào cũng được bạn ah. Nhưng để thuận tiện thì mình viết vào đó, vì nếu để ở class Number thì sẽ dùng 1 đối tượng của clas Number là a, b hoặc bạn khai báo thêm 1 đối tượng khác để gọi hàm swap.
cảm ơn Bạn, now that an object is an object reference is pass by value. before I thought 1 object as a pointer.
complex so you can get more understandable further examples are not ?
pointer in c his part also not understand anymore.