Skip to content

Instantly share code, notes, and snippets.

@ghtali
Last active December 27, 2017 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghtali/51ddc1b738eead34985dea0e356df8fb to your computer and use it in GitHub Desktop.
Save ghtali/51ddc1b738eead34985dea0e356df8fb to your computer and use it in GitHub Desktop.
Passing parameters by value in java
import java.lang.reflect.Method;
public class Example {
public static void main(String[] args) {
int a = 30;
int b = 45;
System.out.println("Before swapping, a = " + a + " and b = " + b);
// Invoke the swap method
swapFunction(a, b);
System.out.println("\n**Now, Before and After swapping values will be same here**:");
System.out.println("After swapping, a = " + a + " and b is " + b);
}
private static void swapFunction(int a, int b) {
// TODO Auto-generated method stub
System.out.println("Before swapping(Inside), a = " + a + " b = " + b);
// Swap n1 with n2
int c = a;
a = b;
b = c;
System.out.println("After swapping(Inside), a = " + a + " b = " + b);
}
}
// A personal training, source: https://www.tutorialspoint.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment