- Three ways to swap locations for two integers:
Package Com.dengchaoqun.ht;import Java.util.scanner;public class Test {/** * * * Two integers in Java, allowing the centralized method of data exchange */public static void Main (string[] args) {//TODO auto-generated method stub Scanner scan=new Scanner (system.in); System.out.println ("Enter two integers:"); int a=scan.nextint (); int b=scan.nextint ();//The first method, with the help of intermediate variable int c=a;a=b;b=c; System.out.println ("Two numbers after exchanging data is:" +a+ "\ T" +b);//The second method does not use the intermediate variable System.out.println ("Enter two numbers:"); int d=scan.nextint (); int E=scan.nextint ();d =d+e; You can also use multiplication to do this. It is only possible that overflow error e=d-e;d=d-e may occur; SYSTEM.OUT.PRINTLN ("The data after Exchange is:" +d+ "\ T" +e);//The third method, XOR operation ^system.out.println ("Input two number:"); int f=scan.nextint (); int g= Scan.nextint (); f=f^g;g=f^g;f=f^g; SYSTEM.OUT.PRINTLN ("The data after Exchange is:"); System.out.println (f); System.out.println (g);
In these three methods the bitwise operation is the most efficient. Directly binary Conversion}}
- A classic interview question: is the following code correct
Short s=10;
s=s+5;
System.out.println ("s");
The answer is: incorrect, because the s=s+5 has been implicitly converted to the int type, the int type has a higher precision than the short precision, so the type of S compiler cannot judge, so it will error.
Basic Java Knowledge