Then brother combed the Java Knowledge Point-Operator (v)

Source: Internet
Author: User
Tags arithmetic operators bitwise operators logical operators

Operator: is a special symbol used to represent the operation, assignment, and comparison of data.

1. Arithmetic operators (+ 、-、 *,/,%, + + 、--)

  A) In addition to:

int i =n; int j = i/5;            // 2 double d1 = I/5;        // 2.0 double d2 = (double) i/5;  // 2.4 double d3 = i/5.0;        // 2.4            

b) difference between front + + and after + +

Before + +: Add a value to yourself first, then do the operation assignment

After + +: Do the arithmetic assignment, then add a value to yourself

int a = ten;  int//a=11 b=11       int//a=11 c=10

The increment and decrement operators can only be used for operation variables and cannot be used directly to manipulate values or constants! For example, 5++, 8--and so on are wrong!

2. Assignment operators (+ =,-=, *=,/=,%=, =)

int i = ten+ = 3; // The result of the operation equals i = i + 3;

The result of the i+=3 operation is equivalent to i = i + 3, but there are some differences.

For example:

 Short s = ten// Compile error s + = 3;    // the operation can be implemented without changing the data type of S

"Knowledge points": assignment operations + =,-=,/=,%= are performed but do not change the data type of the variable

Written questions:

1, the evaluation assignment operator uses

int A = 1*= 0.1;       System.out.println (a); // 0       a++;       System.out.println (a); // 1

    2, the difference between the examination = and = =

Boolean false    ;  if (b==true) {System.out.println ("= = result is true")       ;       } Else {           System.out.println ("= = result is false")       ;       } if (b=true) {           System.out.println ("= result is true");       }        Else {           System.out.println ("= result is false");       }

Output Result:

= = result is False
= result is True

3, comparison operators (= =,!) =, <, >, <=, >=, instanceof)

  The result of the comparison operator is a Boolean type

4, logical operators (&-logic and, &&-short circuit and, | | -Short circuit or 、!-logic non-, ^-logic XOR)

A) The variables on the left and right side of the logical operator are Boolean.

b) The results of & and && are identical, | and | | The results are identical.

c) What is the difference between & and &&?

&: Regardless of whether the left is true or false, the right side will operate.

&&: When the left side is false, then the right side does not operate.

So as long as there is false on the left there is a difference, if it is true, it is identical.

Of course, we definitely recommend && in the process of development, which can reduce the overhead of right-side operation.

| and | | The difference is no longer elaborated the same idea.

c) XOR, as the name implies, returns True when A and B are inconsistent, and returns false.

5, bitwise operators (<<, >>, >>>, &, |, ^, ~)

  A) << left shift (several left, followed by 0)

3<<2 = 3*2*2=12

0000 0000 0000 0011 = 3

0000 0000 0000 1100 = 2 of 3 times + 2 of 2-square = 12

b) >> right shift (right shift several, the front needs to see whether the first bit is 0 or 1, if 1 is used 1 to complement, if 0 is used 0 to complement)

31>>2 = 7

c) >>> unsigned Right shift (right shift several, front whether the first bit is 0 or 1, all with 0 to complete)

31>>>2 = 7

d) & is with operations.

E) | is different or calculated

f) ^ xor

In the case of variable type conversions, swap the values of a and B without declaring a third-party variable. Using the Xor method is absolutely perfect.

int a = 10;

int b = 15;

Answer:

A = a ^ b;

b = a ^ b;

A = a ^ b;

6, ternary operator (?) :)

Used to complete simple selection logic, which is to select one of the two choices according to the condition.

 (conditional expression)? Expression 1: Expression 2

If the expression 1 and expression 2 must be of the same type of data type

Then brother combed the Java Knowledge Point-Operator (v)

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.