Skip to content

Commit 5f1b1b0

Browse files
ramansahasimaibin
authored andcommitted
BAEL-1725 Java Pass-by-reference vs Pass-by-value - First commit (eugenp#4058)
* BAEL-1725 Java Pass-by-reference vs Pass-by-value - First commit * updated test cases
1 parent dba41e1 commit 5f1b1b0

4 files changed

Lines changed: 109 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.parameterpassing;
2+
3+
public class NonPrimitives {
4+
public static void main(String[] args) {
5+
FooClass a = new FooClass(1);
6+
FooClass b = new FooClass(1);
7+
8+
System.out.printf("Before Modification: a = %d and b = %d ", a.num, b.num);
9+
modify(a, b);
10+
System.out.printf("\nAfter Modification: a = %d and b = %d ", a.num, b.num);
11+
}
12+
13+
public static void modify(FooClass a1, FooClass b1) {
14+
a1.num++;
15+
16+
b1 = new FooClass(1);
17+
b1.num++;
18+
}
19+
}
20+
21+
class FooClass {
22+
public int num;
23+
24+
public FooClass(int num) {
25+
this.num = num;
26+
}
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.parameterpassing;
2+
3+
public class Primitives {
4+
public static void main(String[] args) {
5+
int x = 1;
6+
int y = 2;
7+
8+
System.out.printf("Before Modification: x = %d and y = %d ", x, y);
9+
modify(x, y);
10+
System.out.printf("\nAfter Modification: x = %d and y = %d ", x, y);
11+
}
12+
13+
public static void modify(int x1, int y1) {
14+
x1 = 5;
15+
y1 = 10;
16+
}
17+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.parameterpassing;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class NonPrimitivesUnitTest {
7+
@Test
8+
public void whenModifyingObjects_thenOriginalObjectChanged() {
9+
Foo a = new Foo(1);
10+
Foo b = new Foo(1);
11+
12+
// Before Modification
13+
Assert.assertEquals(a.num, 1);
14+
Assert.assertEquals(b.num, 1);
15+
16+
modify(a, b);
17+
18+
// After Modification
19+
Assert.assertEquals(a.num, 2);
20+
Assert.assertEquals(b.num, 1);
21+
}
22+
23+
public static void modify(Foo a1, Foo b1) {
24+
a1.num++;
25+
26+
b1 = new Foo(1);
27+
b1.num++;
28+
}
29+
}
30+
31+
class Foo {
32+
public int num;
33+
34+
public Foo(int num) {
35+
this.num = num;
36+
}
37+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.parameterpassing;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class PrimitivesUnitTest {
7+
@Test
8+
public void whenModifyingPrimitives_thenOriginalValuesNotModified() {
9+
10+
int x = 1;
11+
int y = 2;
12+
13+
// Before Modification
14+
Assert.assertEquals(x, 1);
15+
Assert.assertEquals(y, 2);
16+
17+
modify(x, y);
18+
19+
// After Modification
20+
Assert.assertEquals(x, 1);
21+
Assert.assertEquals(y, 2);
22+
}
23+
24+
public static void modify(int x1, int y1) {
25+
x1 = 5;
26+
y1 = 10;
27+
}
28+
}

0 commit comments

Comments
 (0)