Java record-32-value exchange

Source: Internet
Author: User

Java record-32-value exchange
1. Implement the exchange of two variable values:

    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 code above implements two methods when swapping values of a and B variables: one is to use temporary variables, and the other is to use addition and subtraction of two variables without using 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 the value transfer. For an array, the first address of the array is passed. Array can be used for 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.