Learn Java the third day

Source: Internet
Author: User
Tags arithmetic operators bitwise bitwise operators integer division

operator Classification editing operator indicates how the operand is to be operated. There are many kinds of Java operators that make up expressions. Operators are divided by the number of operands they require, which can have a single-eye operator, a binocular operator, and a three-mesh operator, which correspond to one, 2, and 3 operands, respectively. Operators are divided by their functions, with arithmetic operators, assignment operators, relational operators, logical operators, bitwise operators, and other operators. [1]arithmeticMonocular: + (Take positive)-(minus) + + (self-increment 1)--(self-minus 1) Binocular: +-*/% (residual) Three mesh: A>b?true:false Description: When a is greater than B, true (that is, the value before the colon), otherwise false This entire operator includes a relational operator (can be ">" "<" "! =" and so on), one "? ", a": ", you need to have two expressions or a value or an object before and after a colon.Relationshipequals symbol := =, not equal to symbol :! =, greater than symbol :Less than the symbol :<, greater than equals symbol :>=, less than equals symbol :<=.Bits and LogicBitwise operators are associated with (&), non-(~), or (|), XOR (^) &: Binocular operators, which convert the operands to binary and then compare, rule: When the same bit is 1 o'clock the result is 1, otherwise the result is 0.:1010& 1101, to binary: 10001001101&1111110010 comparison results: 1000000 to decimal: 64 so 1010&1101=64;| : When the bit on both sides of the operand has a side of 1 o'clock, the result is 1, otherwise 0. such as 1100|1010=1110~:0 change to 0^: The two sides of the bit is not the same, the result is 1, otherwise 0. such as 1100^1010=0110 logical operator with (&&), non (!), or (| | )Assign Value= + = = *=/=%= &= ^= |= <<= >>=instanceofThe operator is a binocular operator, the action element on the left is an object, and the right is a class. When the object on the left is the object created by the right-side class, the operator evaluates to true, otherwise false.operator OverviewThe Java expression is a Java-compliant formula that is concatenated with operators. The precedence of an operator determines the order in which operations are performed in an expression. For example, x<y&&!z corresponds to (x<y) && (!z), There is no need to memorize the priority level of the operation symbol, you can use parentheses when writing the program to achieve the order of operations you want, so as not to produce difficult to read or ambiguous calculation Order. The binding of operators determines the order of operators of the same level, for example, the binding of the addition and subtraction is left to right, 8-5+3 Equivalent to (8-5) +3. The binding of the logical no operator is right to left and x equals! (!x). Table 3.4 is the precedence and binding of all Java operators.Displacement<< left shift >> sign right shift >>> no number right shiftOperator PrecedenceRank from highest to lowest by priority as follows: [] () + +--! ~ instanceof */% +-<< >> >>> <> < = > \ = = = &^& & | | ? : = op=.3 type conversion editing Coercion and conversionThe Java language and the interpreter restrict the use of coercion and conversion to prevent an error from causing the system to crash. Integers and floating-point operators can be cast back and forth, but integers cannot be coerced into groups or objects. An object cannot be coerced to a base type. In Java, when an integer operator is an integer operation, if the operand is of type long, the result of the operation is a long type, otherwise the int type is never a byte,short or char type. Thus, if the variable i is declared as short or byte,i+1, the result will be int. If the result exceeds the range of values for that type, the maximum value for that type is modeled.operator OperationThe operator "+", if necessary, automatically converts the operand to a string type. If the operand is an object, it can define a method, ToString (), to return a string of the object, such as Floata=1.0print ("Thevalueofais" +a+ "\ n"), and the example used by the + operator strings= "a=" +A; The + = operator can also be used with string. Note that the left (S1 in the example below) is evaluated only once. s1+=a;//s1=s1+a//If A is not a string type, it is automatically converted to string. Second, integer arithmetic operation of the exception is due to the addition of 0 or by 0 modulo. It will throw an arithmetic exception. Underflow produces 0, overflow causes cross-border. For example: plus 1 exceeds the maximum value of the integer, after modulo, it becomes the minimum value. An op= assignment operator, which is used with the binocular integer operators in the table above, to form an expression. The integer relational operators <,>,<=,>=,== and! = produce a Boolean type of data. The array operator array operator is as follows: <expression>[<expression>] A value that can be given to an element in a group. The valid range of values is from 0 to the length of the array minus 1. Four, the object operator binocular operator instanceof Tests whether an object is an instance of a specified class or its subclasses. For example: if (myobjectinstanceofmyclass) {myclassanothermyobject= (MyClass) myObject; ...} Is the instance that determines whether MyObject is an instance of MyClass or its subclasses. The floating-point operator floating-point operators can use a combination of general operators: such as single-mesh operators + + 、--, binocular operators + 、-、 * and/, and assignment operators +=,-=,*=, and/=. In addition, there are modulo operations:% and%= can also be used for floating-point numbers, for example: A%b and A-((A/b) *b) have the same semantics. This means that the result of the a%b is the remaining floating-point part after the end. Floating-point expressions with single-precision operands are evaluated by single-precision operations, resulting in single-precision results. If a floating-point expression contains one or more double-precision operands, the result is a double-precision floating-point number. The combination operation of a Boolean operation Fubur (Boolean) variable or expression can produce a new Boolean value, Fales and true (remember lowercase). Monocular operator! is Boolean not. Binocular operators &,| and ^ are logical and,or and XOR operators, which force two operands to seek a Boolean value. To avoid the right-hand operand redundancy evaluation, the user can use the short-circuit evaluation operator && and | |. Seven, users can use = =and! =, the assignment operator can also be used with &=, |=, ^=. The ternary condition operator is the same as in C language. The eight, + + operators are used to represent direct plus 1 operations. Incremental operations can also be done indirectly by adding operators and assignment operations. ++lvalue (Lvalue indicates that Lvalue+=1,++lvalue also represents lvalue=lvalue+1. The nine 、--operator is used to represent a minus 1 operation. The + + and--operators can be either prefix operators or suffix operators. The binocular integer operator is: operator Operation **+ Plus-minus * multiply/divide% modulo & bit with | bit or ^ bitwise XOR or << left shift >> right Shift (Signed) >>> add 0 Right shift integer division by 0 rounding. Division and modulo conform to the following equation:
(A/b) *b+ (a%b) ==a
Java Operators& is a bit && is the logic when the & side is an integer when the bitwise operation is performed, and the two sides are Boolean values when performing a logical operation, such as: 3&6 is the execution of the bitwise operation, the result is an integer: 2true&false The logical operation is performed, and the result is that the logical operation of a Boolean value:false& and the && logic operation are calculated on both sides of a certain different & logic operation, and && If the left is false then the direct return is false no longer calculates the right example: 1:int[] A={1,2,3};if (a[0]==2&a[3]==4) {System.out.println ("true")}2:int[] a={ 1,2,3};if (a[0]==2&&a[3]==4) {System.out.println ("true")} These two examples, the first one throws an exception, And the second one, what does not output will not throw an exception this is because the first example in the IF statement is &amp, so both sides of the calculation, when the calculation of a[3]==4 thrown array subscript out of bounds exception the second example in the calculation of the first formula a[0]==2 found that the result is false no longer calculates the right, Returns false directly, so the example does not output anything. One of the most used three-mesh operators in 3:java: A==3?B:C; If the a=3 is established, the result is B, if not, the result is C;operator DemoThe operator (Operator) is provided in Java, and the arithmetic operator has an operator (+), minus (-), multiply (*), except (/). There is also a very common modulo operator (%). This type of operator is called arithmetic operator (arithmetic Operator), which is dominated by mathematical operations. Examples of several arithmetic operations: int a = 5+4;//a=9int b = a*2;//b=18int c = B/4;//c=4int d = b-c;//d=14int e =-D;//e=-14int f = e%4;//f=- 2 here is simply the modulo operator (%), in fact, the remainder after the division, the example of int f = e%4, that is-14 to 4 to take the remainder, the result is-2. Unlike C, C + +, for modulo operator%, the operand can be a floating-point number, such as 37.2%10=7.2, and the Java add operator expands to enable it to concatenate strings, such as "abc" + "de", to get the string "ABCDE". But this is just a simple extension of the string connection, and the string does not support other operations such as subtraction. Interestingly, the characters are supported for addition and subtraction, and their participation in the operation is the ASCII value of this character. For example, the ASCII value of B is 1 larger than a, so the result (' B '-' a ') is 1.three shiftsJava left shift:<< signed right:>> unsigned right shift:>>> "< <", "> >", "> > >" in Java are left, signed right shift, and unsigned right shift operator. The shift operator operates on an int value only, and if it is not int, the compiler will make an error. In Java, the length of an int is always 32bit, which is 4 bytes. 1) left move operator: Moves the number of operations to the left, the number of bits moved is specified, the left shift (2) Right moves the operator: in turn, the operand is moved to the right, and the number of bits moved is also specified by the right operand. (3) No sign right shift operator (>>>): with 0 supplement, meaning that test {main (string[] args) {m=-7; System. out. println ("M binary code is:" +integer. tobinarystring(m)); System. out. println ("M>>2 binary code is:" +integer. tobinarystring(m>>2)); System. out. println ("(m>>2) =" + (m>>2)); System. out. println ("M<<2 binary code is:" +integer. tobinarystring(m<<2)); System. out. println ("(m<<2) =:" + (M&LT;&LT;2)); System. out. println ("m>>>24 binary code is:" +integer. tobinarystring(m>>>24)); System. out. println ("m>>>24:" + (M&GT;&GT;&GT;24));}} -7 anti-code: 11111111111111111111111111111000 Complement: 11111111111111111111111111111001

Learn Java the third day

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.