Code examples that illustrate the passing and callback mechanisms of Java share _java

Source: Internet
Author: User
Tags call back

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.