A/C + + and Java reach different swap functions

Source: Internet
Author: User

First, let's take a look at the swap performance

void  Swap (int & A, int & B)    {        int  Temp;        temp = A;        A = b;        b = temp;    }

So is it possible to do so in Java. It is obvious that there is no address reference symbol in Java.

First, let's look at the differences between C + + and Java.

Essential differences

The nature of the swap function in C + +: Exchange The value in the variable address by passing the variable address (pointer or reference).


Java boasts a very big improvement in C/s + +: Java blocks the concept of variable addresses against program apes, reducing the misuse of pointers.



In the Java world, a function or a method's entry is passed by means of a value copy:

    • Primitive type (char. Int. Double, etc.) are passed directly by copying the value of the variable;
    • Object types are referenced by reference copies (unlike references in C + +), which can change the intrinsic values of the objects to which they point. However, changing the reference value is only visible to the inside of the function, and the outside of the function remains unchanged;
From the above can be seen through the way the address in Java is not possible to achieve. Because Java strictly follows value passing(Pass-by-value).


< Span style= "line-height:25px" >java Pass is copy


But there is no solution to the problem, we can use other methods to achieve:
METHOD1: Exchanging values with an array (for exchange of elements in an array, so it is often seen in the sort)

Public  static  void  swap (int [] data,  int  A,  int  b) {        int  t = Data [a];            data [A] = data [b];            data [b] = t;    }

METHOD2: Using the Redefine Class (custom)
Class Myinteger {Private int x;  //The X as the only data member public myinteger (int xIn) {x = XIn;}   //Constructor public int getValue () {return x;}  //Get the value public void insertvalue (int xIn) {x = XIn;}  //Change the value of the ' public Class swapping {//Swap:pass object references static  void swap (Myinteger rwrap, Myinteger swrap) {//change the value of the process int                  t = Rwrap.getvalue ();                  Rwrap.insertvalue (Swrap.getvalue ());           Swrap.insertvalue (t);                  The public static void main (String [] args) {int a = all, B = 47; System.out.println ("before.                  A: "+ A +", B: "+ B");                  Myinteger awrap = new Myinteger (a);                  Myinteger bwrap = new Myinteger (b);                  Swap (Awrap, bwrap); A = Awrap.GetValue ();                  b = Bwrap.getvalue (); System.out.println ("After.           A: "+ A +", B: "+ B"); }    }

METHOD3: External Inline method
Public Class Swap2 {public static void main (String args []) {SWAP2 SW = new Swap2 (1, 2);             System.out.println ("I is" + sw.i);             System.out.println ("J is" + SW.J);             Sw.swap ();             System.out.println ("I is" + sw.i);         System.out.println ("J is" + SW.J);         } int i, J;             Public Swap2 (int i, int. J) {this. i = i; this.         j = J;             } public void Swap () {int Temp;             temp = i;             i = j;         j = temp; }} public Class swap1 {public static void Swap1 (integer A, integer b) {integer TEMP             = A;             A = b;         b = temp;             } public static void main (String args []) {Integer A, B;             A = new Integer (10);             b = new Integer (20);             Swap1.swap1 (A, b); System.out.println ("A is" + a);         System.out.println ("B is" + B); }     }


Summary:exchange of elements in an array . can use the following Public Static voidSwap (int[] Data,intA,intb) methods;
Element Exchange in a non-array . Ability to use the most primitive switching methods. Can be exchanged, and can use the above mentionedHow to redefine a class , Externally inline Way, subject to availability

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

A/C + + and Java reach different swap functions

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.