Java from small white to getting started, Day2 (operators and expressions)

Source: Internet
Author: User
Tags arithmetic operators binary to decimal bitwise bitwise operators decimal to binary

The number of actions on the operand is divided into: one mesh operator: + + 、--、 + (plus sign),-(minus),! , ~ (bitwise non); (operand only 1 bits)

Two-mesh operator: + (plus),-(minus), *,/,%, etc. (operand 2 digits)

Trinocular operator:? :。 Type: Code 1? Code 2: Code 3, (Code 1-bit Boolean type, code 2 and code 3-like), indicating if code 1 is true, then the substitution code 2 is the result, if code 1 is false, then the substitution code 3 is the result.

Partitioning by Function: conversion operators, arithmetic operators, logical operators, bitwise operators, assignment operators.

Conversion operators: Converts data of a type to other types of data.

numeric type → numeric type: a type with a small range is converted to a large range of types, using implicit conversions for safe conversions. Conversely, using explicit conversions, the conversion is unsafe.

Implicit conversions: For example, an int a=1; Double b=a;

Explicit conversions: For example, double a=1; int b= (int) A;

Return type: Is the type of the result of a code operation.

Arithmetic operators: Used for mathematical operations (+ 、-、 *,/,%), can be used for numeric types, or for char types. The return type of an arithmetic operation is the type of the operand with a large range of values.

Logical operator: The return type is a Boolean type. (>, <, = =,!) =, &&, | |,! such as

For a comparison of strings, you must use: String 1.equals string 2, return type is Boolean type.

&&,| | : The left and right ends must be of type Boolean.

! (non): the right must be a Boolean type.

Bitwise operators: Used to perform operations on the binary format of numbers.

|: Or operation (two yuan), the two-bit two-level system is 0, take 0; Conversely, take 1.

&: and operation (two yuan), the two-bit two-level system is 1, take 1; conversely, take 0.

^: xor (two Yuan), the two-bit two-level system of different, take 1; conversely, take 0.

~: Non-op (unary), reverse the binary format of the number on the right (0 change to 0)

<<: Left shift (two yuan), moves the left digit's binary format to the left by the specified number of digits.

>>: Right Shift (two RMB), move the binary format of the number on the right to the right of the specified number of digits.

Binary to decimal: for example, 0011, the arithmetic process (right-to-left, 1*20+1*21+0*22+0*23) = 3, and so on.

Decimal to binary: for example, 22, the arithmetic process (24 closest to 22,22-24=6,;22 closest to 6,6-22=2;21=2), so there is 0*20+1*21+1*22+0*23+1*24, binary is represented as 10110, and so on

Assignment operator: Used to assign the count result of the right side of an assignment symbol to the left variable. (=, + =,-=,/=, *=,%=)

Type: a+=b+c→ equivalent to a=a+ (B+C)

additive, decrement operator: (+ + 、--), the return value is the same as the variable type of action.

a++: Indicates that the result of the calculation is the same as the variable and then the variable +1.

++a: Indicates that the variable is +1, and then the result is the same as the variable.

Operation Priority: When there are multiple operators involved in the operation;

1, the code is overall left to right, 2, if you encounter parentheses (), the parentheses are computed first. 3, if a function call is encountered, the result of the function call is calculated immediately.

Expression: Any one part of a statement and statement consisting of an operator and an operand. All expressions have a return type. Any expression can be evaluated with other expressions as long as the return type satisfies the requirement.

1, the return type of the assignment expression is the type of the variable being assigned, and the result is the value of the variable.

2, the statement that defines the variable has no return type (void).

3, the output statement does not have a return type.

Example 1:

int i=1;
I= (i=i+ (i+ (i=2) +i) +i) +3+i;
/*JVM Operation principle: I= (i=i+ (i+ (i=2) +i) +i) +3+i
* i=i+ (i+ (i=2) +i) +i
* i+ (i+ (i=2) +i) +i
* 1+ (i+ (i=2) +i) +i I=1
* 1+ (1+ (i=2) +i) +i I=1
* 1+ (1+2+i) +i i=2
* 1+ (1+2+2) +2
* 8 i=8
* 8+3+i
* 19
*/
System.out.println (i);

Example 2:

int a=1,b=2,c=3;
System.out.println (A+B+C);
/*a+b+c in the JVM
* 1, go to the memory for a corresponding data
* 2, read the next budget symbol +
* 3, Read + after the operand B, at the same time read the expression after the B, see operators, see whether to run first, +b+
* 4, calculate the result of the a+b, recorded as temp,
* 5, read the next budget symbol +,
* 6, Read + after the operand C, at the same time read C after the expression, no operator, direct temp+c, calculation results
*/

Example 3:

System.out.println ( -11%-7);
/* The preceding operand provides the symbol for the result of the final operation,
* The absolute value of the preceding operand and the absolute value of the subsequent operand provide the final result
* -11%-7 converted to-(11%7)
*/

Java from small white to getting started, Day2 (operators and expressions)

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.