Basic Java Knowledge

Source: Internet
Author: User

    • 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

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.