Java has no pointers, and in cases where the parameters need to be brought back directly to the result, there are some differences compared to the C language, any type in C can be passed by the pointer, in Java, you need to customize a class (the standard type cannot bring back parameters), and this custom class can return the result through the parameters of the function. Achieves a similar effect as a C pointer.
public class Testparameter
{public
testparameter ()
{
} public
static void Main (string[] args)
{
//function incoming standard type parameter, test reference
//-------------------------------------------
Integer a = 0;
Integer b = 0;
References cannot be
getValue (A, b);
No expected results
System.out.println (a);
System.out.println (b);
function to pass in the custom class parameter, test reference
//-------------------------------------------
myvalue value = new MyValue ();
Value.a = 0;
value.b = 0;
Reference valid
GetValue (value);
Get the desired result
System.out.println (VALUE.A);
System.out.println (value.b);
}
The parameter is a custom class
private static void GetValue (MyValue v)
{
V.A =;
V.B = +;
}
parameter is a standard type
private static void GetValue (integer A, integer b)
{
a =;
b = Panax Notoginseng
;
}
Custom class
myvalue
{
//variable initialization public
int a = 0;
public int b = 0;
}
The parameters of GetValue (myvalue) are assigned to the function, and the variables in the class are changed as the result is still brought out by the class.
The parameters of GetValue (integer, integer) are changed within the function, but when the function returns, the incoming arguments are not changed, and the second String type does not bring the result back through the parameter, only the object created by new.
String, you can use string[to pass as a parameter, or you can bring back the result of the run inside the function
public static void Main (string[] args)
{
try
{
string[] str = new STRING[1];
Str[0] = "";
Test2 (str);
The result of the display of the Test2 is shown
System.out.print (str[0]);
}
catch (Exception e)
{
e.printstacktrace ();
}
}
private static void Test2 (string[] str) throws Exception
{
str[0] = "Hello world.";
}
blog.csdn.net/joyous/article/details/9372279
Q Group discussion: 236201801