OK. If I really want to pass by reference, How can I do

I want to write a method called
public void swap(int a, int b){
public static void main(String[] args){
int a=1;
int b=2;
swap(a, b);
System.out.print(a);
System.out.print(b);
And I want the output to be 21, how can I achieve this in Java?
Thanks!

You can achieve the same effect that you're after, just not exactly the way you're attempting to - you just need to define a class as a data structure to hold your int values.
class IntHolder
          public int a;
          public int b;
}Then pass an object instance of your class to the swap method.
     public static void main(String[] args)
          IntHolder nums = new IntHolder();
          nums.a = 1;
          nums.b = 2;
          System.out.println("Main - before");
          System.out.print(nums.a);
          System.out.print(nums.b);
          swap(nums);
          System.out.println("Main - after");
          System.out.print(nums.a);
          System.out.print(nums.b);
     static void swap(IntHolder ih)
          Integer tmp;
          tmp = ih.a;
          ih.a=ih.b;
          ih.b=tmp;
          System.out.println("In Swap");
          System.out.print(ih.a);
          System.out.print(ih.b);
     }

Similar Messages

Maybe you are looking for

  • Deploying EAR file

    I seem to have a problem deploying an EAR file to OC4j. I am using admin.jar to deploy and the deployment itself seems to go ok except that the jsp file does not get translated to java and get precompiled and is not in application-deployments/<AppNam

  • PHP and Mail

    I wanted to share with the community a work around we have developed for using the mail function within PHP when compiled with NSAPI (Iplanet SunOne). Quick summary of the problem. Description: mail() stops working a few minutes after Sunone 6 web se

  • Cisco ISE integration with third-party firewalls

    Can Cisco ISE be integrated with a third-party firewall (such as Checkpoint), to provide authentication/authorization services to remote VPN user devices (based on device MAC address)? The remote user would establish a VPN connection to a third-party

  • Transferring pc to macbook pro

    I need help transferring all my photos and information from my hp pc onto my new macbook pro please.

  • 10g2 Oracle HTTP Server - Upgrade PHP?

    The new 10g2 Apache server comes with PHP 4.3.9 ready to go; however, I do not wish to use an older version, and want to upgrade it! Is this possible? When I try to disable it, I get all sorts of apache complaints about php being compiled wrong, or i