Java to pass a value or to pass a reference
1. Original type parameter Pass
public void Badswap (int var1, int var2)
{
int temp = VAR1;
var1 = var2;
var2 = temp;
}
2. Reference type parameter passing
public void tricky (point arg1, point arg2)
{
arg1.x = m;
Arg1.y = m;
Point temp = arg1;
Arg1 = arg2;
arg2 = temp;
}
public static void Main (String [] args)
{point
pnt1 = new Point (0,0);
Point pnt2 = new Point (0,0);
System.out.println ("X:" + pnt1.x + "Y:" +pnt1.y);
System.out.println ("X:" + pnt2.x + "Y:" +pnt2.y);
System.out.println ("");
Tricky (Pnt1,pnt2);
System.out.println ("X:" + pnt1.x + "Y:" + pnt1.y);
System.out.println ("X:" + pnt2.x + "Y:" +pnt2.y);
}
Run these two programs, I'm sure you'll understand: Java manipulates objects ' by reference, ' but it passes object references to methods ' by value.
Java callback mechanism
Spring uses a large number of Java callback mechanisms, and here's a simple introduction to the Java callback mechanism:
In a word, a callback is a two-way invocation pattern, meaning, that is, the caller will call each other when called, which is called a callback. "If You call me, I'll call back."
Look at the following example of a callback mechanism:
Interface Callbackinterface:
Public interface Callbackinterface {
void Save ();
}
Class ClassB:
public class CLASSB implements Callbackinterface {public
void Save () {
System.out.println ("Perform save operation!");
the public void Add ()
{
//) invokes the ClassA method at the same time CLASSSB the Save method
new ClassA () of CLASSB. Executesave (New CLASSB ());
}
Class ClassA:
public class ClassA {public
void Executesave (Callbackinterface callbackinterface)
{
getconn ();
Callbackinterface.save (); Call me
realse ();
}
public void Getconn ()
{
System.out.println ("Get database Connection!");
public void Realse ()
{
System.out.println ("Free database connection!");
}
A more classical example of the use of callback functions (using Java anonymous classes) eliminates the source code here