Exchange Values of two variables in Java and Exchange Values of two variables in java

Source: Internet
Author: User

Exchange Values of two variables in Java and Exchange Values of two variables in java

During the class, we talked about switching variables. Students said that I used third-party variables to exchange low, haha. Okay...

I will summarize some methods of switching variables in java that I know. Code:

Package test; public class Test {public static void main (String [] args) {int a, B; a = 10; B = 12; System. out. println (a + "" + B);/** this algorithm is easy to understand with third-party variables. It is especially suitable for beginners to understand the characteristics of computer programs and is a classic Application of Value assignment statements. * In actual software development, this algorithm is simple and clear, and does not produce ambiguity, facilitating communication between programmers. Generally, this algorithm (standard algorithm) should be used to exchange variable values ). */Int temp = a; a = B; B = temp; System. out. println (a + "" + B);/** the principle of the arithmetic algorithm is to regard a and B as points on the number axis and calculate the distance between two points. Specific process: * In the first sentence "a = B-a", find the distance between the two points of AB and save it in; * In the second sentence "B = B-a", find the distance from a to the origin (the difference between the distance from B to the origin and the distance between the two points), and save it in B; * In the third sentence "a = B + a", find the distance from B to the origin (the sum of the distance between a and AB) and save it in. Complete the exchange. * This algorithm has three more computing processes than the standard algorithm, but does not use temporary variables. (Arithmetic algorithm) */a = B-a; B = B-a; a = B + a; System. out. println (a + "" + B);/** the difference between an exclusive or identical value and 0 is determined by the characteristics of the exclusive or operation, some bits in the data can be flipped by the exclusive or operation, while other BITs remain unchanged. * This means that any number and any given value are two consecutive times without changing the value. * That is, a ^ B =. If a = a ^ B is substituted into B = a ^ B, B = a ^ B = a. Similarly, a = B ^ a = B; easy exchange. */A = a ^ B; // 1010 ^ 1100 1001 B = a ^ B; // 1001 ^ 1100 1010 a = a ^ B; // 1001 ^ 1010 1100 System. out. println (a + "" + B );}}

I personally think it is different or interesting.

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.