Java中实现引用传递的不同方法
有两种类型的参数,一种是形式参数,第二种是实际参数。形式参数是在函数定义期间定义的那些参数,实际参数是在其他函数中的函数调用期间传递的那些参数。
通过代码展示形参和实参:
Java
// Java program to show the difference
// between formal and actual parameters
import java.io.*;
class GFG {
static int sum(int a,int b) // Formal parameters
{
return a+b;
}
public static void main (String[] args) {
int a=5;
int b=10;
System.out.println(sum(a,b)); //This is actual parameters
}
}
C
// C program o show the call by reference support
#include
// formal parameters
void swap(int* x, int* y)
{
int temp;
// save the value at address x
temp = *x;
// put y into x
*x = *y;
// put temp into y
*y = temp;
return;
}
int main()
{
int a = 15;
int b = 5;
printf("Value of a %d\n", a);
printf("Value of b %d\n", b);
// swapping the value using call by reference
swap(&a,&b);
printf("Value of a %d\n", a);
printf("Value of b %d", b);
return 0;
}
Java
// Java program to swap two numbers
import java.io.*;
class GFG {
static void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
System.out.println("Value of a in swap function "
+ a);
System.out.println("Value of b in swap function "
+ b);
return;
}
public static void main(String[] args)
{
int a = 5;
int b = 10;
// original value of a
System.out.println("Value of the a "
+ a);
// original value of b
System.out.println("Value of the b "
+ b);
// swap the numbers
swap(a, b);
System.out.println("Value of the a " + a);
System.out.println("Value of the b " + b);
}
}
Java
// Java program to make a particular variable
// of a particular datatype as a class member
import java.io.*;
class GFG {
int Number;
void GFG() { Number = 0; }
static void update(GFG ob) { ob.Number++; }
public static void main(String[] args)
{
GFG ob = new GFG();
System.out.println("Number value " + (ob.Number));
update(ob);
System.out.println("Updated Number value "
+ (ob.Number));
}
}
Java
// Java program to passing collections
// as parameters
import java.io.*;
import java.util.*;
class GFG {
static void update(int a[])
{
// formal parameter
a[0]++;
}
static void update2(ArrayList A)
{
for (int i = 0; i < A.size(); ++i) {
// update the item of
// the arraylist
A.set(i, A.get(i) + 1);
}
}
public static void main(String[] args)
{
// using single array of element
int a[] = new int[1];
System.out.println("Number Value " + a[0]);
// actual parameter
update(a);
System.out.println("Number Value " + a[0]);
// using Arraylist collection
ArrayList A = new ArrayList<>();
A.add(1);
A.add(2);
System.out.println("List " + A);
// actual parameter
update2(A);
System.out.println("Updated List " + A);
}
}
Java
// Java program to show the return
// value getting updated
import java.io.*;
class GFG {
static int update(int number)
{
// update the number
return ++number;
}
public static void main(String[] args)
{
int number = 3;
// printing the number
System.out.println("Number " + number);
int returnnumber = update(number);
// update the number
number = returnnumber;
// printing the updated number;
System.out.println("Updated number " + number);
}
}
输出
15
- 功能可以根据参数进行区分。
- 在函数中传递参数有两种方式: call by value和 c all by reference 。
- C/C++ 支持按引用调用,因为 在引用调用中,我们使用指针传递实际参数的地址来代替形式参数。
- 并且Java不支持指针,这就是为什么Java不支持按引用调用但 c/c++ 支持指针,因此这些语言支持按引用调用。
Swapping of Two numbers in C/C++ language is done by the call by Reference approach
下面是C中Call by Reference的实现
C
// C program o show the call by reference support
#include
// formal parameters
void swap(int* x, int* y)
{
int temp;
// save the value at address x
temp = *x;
// put y into x
*x = *y;
// put temp into y
*y = temp;
return;
}
int main()
{
int a = 15;
int b = 5;
printf("Value of a %d\n", a);
printf("Value of b %d\n", b);
// swapping the value using call by reference
swap(&a,&b);
printf("Value of a %d\n", a);
printf("Value of b %d", b);
return 0;
}
输出
Value of a 15
Value of b 5
Value of a 5
Value of b 15
在Java中,如果我们尝试在Java中交换数字,则两个数字状态的变化仅在特定的交换方法中有效。
在Java中交换两个数字:
Java
// Java program to swap two numbers
import java.io.*;
class GFG {
static void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
System.out.println("Value of a in swap function "
+ a);
System.out.println("Value of b in swap function "
+ b);
return;
}
public static void main(String[] args)
{
int a = 5;
int b = 10;
// original value of a
System.out.println("Value of the a "
+ a);
// original value of b
System.out.println("Value of the b "
+ b);
// swap the numbers
swap(a, b);
System.out.println("Value of the a " + a);
System.out.println("Value of the b " + b);
}
}
输出
Value of the a 5
Value of the b 10
Value of a in swap function 10
Value of b in swap function 5
Value of the a 5
Value of the b 10
Java中有一些方法可以代替引用调用来实现引用传递:
1. 将特定数据类型的特定变量作为类成员
- 可以在整个类定义中访问类成员,并且当存在与特定函数体或函数定义中存在的类成员名称相同的局部变量时,我们可以使用this关键字访问成员以避免混淆。
- 为了传递引用,我们传递类的对象来代替实参,类对象类型的形参彼此具有相同的引用,这就是为什么借助类的形参对象any更改将反映在对象的正式对象和实际对象中。
Java
// Java program to make a particular variable
// of a particular datatype as a class member
import java.io.*;
class GFG {
int Number;
void GFG() { Number = 0; }
static void update(GFG ob) { ob.Number++; }
public static void main(String[] args)
{
GFG ob = new GFG();
System.out.println("Number value " + (ob.Number));
update(ob);
System.out.println("Updated Number value "
+ (ob.Number));
}
}
输出
Number value 0
Updated Number value 1
2. 传递集合或单元素数组作为参数:
- 我们可以将ArrayList、stack等集合或单元素数组作为实参传递,函数的实参和形参都对内存地址有相同的引用。
Java
// Java program to passing collections
// as parameters
import java.io.*;
import java.util.*;
class GFG {
static void update(int a[])
{
// formal parameter
a[0]++;
}
static void update2(ArrayList A)
{
for (int i = 0; i < A.size(); ++i) {
// update the item of
// the arraylist
A.set(i, A.get(i) + 1);
}
}
public static void main(String[] args)
{
// using single array of element
int a[] = new int[1];
System.out.println("Number Value " + a[0]);
// actual parameter
update(a);
System.out.println("Number Value " + a[0]);
// using Arraylist collection
ArrayList A = new ArrayList<>();
A.add(1);
A.add(2);
System.out.println("List " + A);
// actual parameter
update2(A);
System.out.println("Updated List " + A);
}
}
输出
Number Value 0
Number Value 1
List [1, 2]
Updated List [2, 3]
3. 更新返回值:
- 通过简单地用前一个值更新返回值来执行引用的朴素方法。
Java
// Java program to show the return
// value getting updated
import java.io.*;
class GFG {
static int update(int number)
{
// update the number
return ++number;
}
public static void main(String[] args)
{
int number = 3;
// printing the number
System.out.println("Number " + number);
int returnnumber = update(number);
// update the number
number = returnnumber;
// printing the updated number;
System.out.println("Updated number " + number);
}
}
输出
Number 3
Updated number 4