Java Record -32-value Exchange

Source: Internet
Author: User

1. implement two variable value exchanges:

    public static void main (String[] args)  {         int a = 3;        int  b = 5;                 int temp = a;        a = b;         b = temp;         System.out.println (a);         system.out.println (b);         system.out.println ("----------------");         a = a + b;        b = a  - b;        a = a - b;         sYstem.out.println (a);         system.out.println (b);     }


The above code is when a and b variable values are interchanged, two ways are implemented: one is to use temporary variables, and the other is to take advantage of the addition and subtraction of two variables without resorting to temporary variables.

2.

Public class swaptest2 {    public static void change (char[]  cs, char c) {        cs[0] =  ' C ';         c =  ' D ';     }    public  static void swap (CHAR[] CS) {        char  temp = cs[0];        cs[0] = cs[1];         cs[1] = temp;    }     Public static void main (String[] args)  {         char[] ch = {' A ', ' B '};        swap (CH);         SYSTEM.OUT.PRINTLN (CH);                  change (ch, ch[1]);         SYSTEM.OUT.PRINTLN (ch);     }}

Java is a value pass, and for an array, the first address of the array is passed. The use of arrays allows for value exchange.


Java Record -32-value Exchange

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.