BAEL-1725 Java Pass-by-reference vs Pass-by-value - First commit (#4058)
* BAEL-1725 Java Pass-by-reference vs Pass-by-value - First commit * updated test cases
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.parameterpassing;
|
||||
|
||||
public class NonPrimitives {
|
||||
public static void main(String[] args) {
|
||||
FooClass a = new FooClass(1);
|
||||
FooClass b = new FooClass(1);
|
||||
|
||||
System.out.printf("Before Modification: a = %d and b = %d ", a.num, b.num);
|
||||
modify(a, b);
|
||||
System.out.printf("\nAfter Modification: a = %d and b = %d ", a.num, b.num);
|
||||
}
|
||||
|
||||
public static void modify(FooClass a1, FooClass b1) {
|
||||
a1.num++;
|
||||
|
||||
b1 = new FooClass(1);
|
||||
b1.num++;
|
||||
}
|
||||
}
|
||||
|
||||
class FooClass {
|
||||
public int num;
|
||||
|
||||
public FooClass(int num) {
|
||||
this.num = num;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.parameterpassing;
|
||||
|
||||
public class Primitives {
|
||||
public static void main(String[] args) {
|
||||
int x = 1;
|
||||
int y = 2;
|
||||
|
||||
System.out.printf("Before Modification: x = %d and y = %d ", x, y);
|
||||
modify(x, y);
|
||||
System.out.printf("\nAfter Modification: x = %d and y = %d ", x, y);
|
||||
}
|
||||
|
||||
public static void modify(int x1, int y1) {
|
||||
x1 = 5;
|
||||
y1 = 10;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user