Java Syntax Basics operator Learning notes sharing _java

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise class definition integer division logical operators

One, operator

The operators include the following:

Arithmetic operator assignment operator comparison operator logical operator bitwise operator THREE-mesh operator

The least common is the bitwise operator, but it is also closest to the bottom of the computer.

1. Arithmetic operators

(1) + several uses: addition, positive, string connector

(2) Divide the time to pay attention to a problem: integer division, can only get integers. To get decimals, you can *1.0 the data itself, converting the data itself to a floating-point type.

2, assignment operator

Symbol = = = = *=/=%=

Note: = is the basic assignment operator, and the other is an extended assignment operator

Interview questions:

(1) Short S=1, s = s+1;

(2) Short S=1, s+=1;

The above two code has no question, if has, where has the question?

The Answer: code (1) is wrong, loss of precision, byte,short in the definition, they receive is actually an int type of value. This is done by themselves a data detection, if no longer within their range, the error. The effect is as follows:

The code (2) has no errors because: the extended assignment operator actually implies a coercion type conversion.

In other words, i = 1; is not equivalent to i = i + 1; But is equivalent to i = (data type of I) (S + 1);

3. Relational operators

Note 1: The result of the comparison operator is Boolean, which is either true or false.

Note 2: The comparison operator "= =" Can not be mistakenly written "=". Example:

4. Logical operators

(1) Logical operators are used to concatenate Boolean expressions, which cannot be written as 3<x<6 in Java, and should be written as X>3 & X<6.

(2) The difference between "&" and "&&"? Similarly, "|" and "| |" The difference?

A: The end result is the same. For example: A and B must be true at the same time, the results of A&b and A&&b are true. The b:&& has a short-circuit effect, false on the left, and no execution on the right. Has a short-circuit effect, the left is true, and the right is not executed.

Note: Logical operators commonly used in development: &&,| |,!

(3) Xor or (^) with or (|) The difference is that when the left or right is true, the result of the XOR is false.

5, bitwise operator:

Although the development of the use of a few, but will be seen in many source code, because the computer at the bottom of the calculation are bitwise operations.

face Test 1: realize the exchange of two integer variables

The code is as follows:

*
  Interview Question: Please realize the exchange of two integer variables
/
class Operatortest {public
  static void Main (string[] args) {
    int a = 10;< C6/>int B =;
    
    System.out.println ("A:" +a+ ", B:" +b);
    
    Mode 1: Use a third party variable (developed)    
    int c = A;
    A = b;
    b = C;
    System.out.println ("A:" +a+ ", B:" +b);
    System.out.println ("------------");
    
    
    Mode 2: With a bit XOR or implementation (for interview)
    //Left: A,b,a
    //right: a ^ b    
    a = a ^ b;
    b = a ^ b; A ^ b ^ b = a
    = a ^ b;//a ^ b ^ a = b
    System.out.println ("A:" +a+ ", B:" +b);
    
    
    Mode 3: The practice of adding variables by    
    a = a + B;//a=30
    b = a-b;//b=10
    a = a-b;//a=20
    System.out.println ("A:" +a+ ", B:" +b );
    *
    
    //Mode 4: A word to fix
    B = (a+b)-(a=b);//b=30-20=10,a=20
    System.out.println ("A:" +a+ ", B:" +b);
  }

face Test 2: Write the result of calculating 2 times 8 in the most efficient way.

Answer: 2 * 8 equivalent to 2 << 3

Knowledge Review:

<<: Left to the left highest bit discarded, the right 0 >>: The right to move the highest bit is 0, the left to complement 0, the highest is 1, the left to be filled 1 >>>: Unsigned right move whether the highest digit is 0 or 1, left

6, three-mesh operator:

Format: (relationship expression)? Expression 1: Expression 2;

If the condition is true, the result of the operation is an expression 1;

If the condition is false, the result of the operation is an expression 2;

Example:

Get the largest number of two numbers:

Copy Code code as follows:
int x=3,y=4,z;z = (x>y)? The x:y;//z variable stores a large number of two digits.

Gets the maximum value in three integers:

Way one: Two steps
    //a: First compare the maximum value of the A,b
    //b: The maximum value of the a,b is compared with C.
    temp = ((A > B)? a:b);
    SYSTEM.OUT.PRINTLN (temp);
    int max1 = (temp > C temp:c);
    System.out.println ("Max1:" +max1);
    
    Mode two: One step to fix
    int max2 = (a > B)? (A > C)? A:C):((b > C)? B:C);
    This practice is not recommended:
    //int max2 = a > B?a > c? a:c:b > C b:c;
    System.out.println ("MAX2:" +MAX2);

Suggested by way of one. In addition, in mode two, lines 10th and 12th are the same, and are nested uses of the three-mesh operator.

Compares two integers for equality:

Compare two integers equal 
    int m = n;
    int n =;
    
    Boolean flag = (M = = N)? True:false;
    Boolean flag = (M = = N);
    SYSTEM.OUT.PRINTLN (flag);

Second, the keyboard input data

We are currently writing programs, data values are fixed, but the actual development, the data value is definitely changed, so I am ready to improve the data for keyboard input, improve the flexibility of the program.

How to achieve keyboard input data? (now remember to use)

(1) Guide package (position to the top of class definition): Import Java.util.Scanner;

(2) Create keyboard Entry object: Scanner sc = new Scanner (system.in);

(3) Get the data through the object: int x = Sc.nextint ();

code example:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.