Passing values or references in java
1. Passing the original type parameter
public void badSwap(int var1, int var2) { int temp = var1; var1 = var2; var2 = temp; }
2. Passing reference type parameter
public void tricky(Point arg1, Point arg2) { arg1.x = 100; arg1.y = 100; Point temp = arg1; arg1 = arg2; arg2 = temp; } public static void main(St ring [] 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 and you will surely understand: Java manipulates objects 'by reference,' but it passes object references to methods 'by value.
Java callback mechanism
Spring uses a lot of Java callback mechanism. Here is a brief introduction to the Java callback mechanism:
In a word, a callback is a two-way calling mode. What does it mean? That is to say, the called party will also call the other party when it is called, which is called a callback. “If you call me, i will call back”.
See the following examples of callback mechanism:
Interface CallBackInterface:
public interface CallBackInterface { void save();}
ClassB:
public class ClassB implements CallBackInterface {public void save() {System.out.println("Execute save operation!");}//public void add(){ //Call the ClassA method here at the same time ClassssB will call back ClassB's save again Method new ClassA().executeSave(new ClassB()); }}
ClassA:
public class ClassA { public void executeSave(CallBackInterface callBackInterface) { getConn(); callBackInterface.save(); // you call me real se(); } public void getConn() { System.out.println("Get database connection!" ); } public void realse() { System.out.println("Release the database connection!"); }}
More classic examples about the use of callback functions (using java anonymous class) here to save source code