Java Operators
Almost all operators can only manipulate "basic type" = "," = = "and"! = ", they are capable of manipulating all objects the String class supports" + "and" + = "
Integer: Int,short,byte,long floating-point: float,double character: Char Boolean: Boolean
Type |
Bytes |
Boolean |
1/8 |
Byte |
1 |
Char |
2 |
Short |
2 |
Float |
4 |
Int |
4 |
Long |
8 |
Double |
8 |
Priority Level
First multiplication and then minus
String Connection: String followed by a "+", + after non-string type is automatically converted to string type
Assign Value
Base type assignment: Copy the contents of one place to another (the underlying type stores the actual value) object Assignment (alias behavior): Copy the reference from one place to another (be careful)
Method alias: void f (Letter t) {t.c = ' a ';} The contents of the T object will be changed
Arithmetic Operators
Binocular operators include: (plus) +, (minus)-, (multiply) *, (except)/, (redundancy)% + =,-=, *=,/=,%= monocular operations include: + (positive),-(negative), + + (self-increment),-(self-decrement)
- When you use the + =,-=, *=,/=,%=, operators to perform operations on basic types, follow these rules
The value to the right of the operator is first cast to the same type as the left value of the operator, and then the operation is performed with the same result as the left-hand numeric type. Short S1 = 1; S1 = s1 + 1; Error, high turn low need to cast/low-turn high-level auto-turn, assign int to short need to cast short S1 = 1; S1 + = 1; Correct, s1+=1 equivalent to s1= (short) (s1+1)
unary minus-(negative) symbol for transforming data unary Plus + (positive) is used to promote smaller operands to int
- Self-increment (+ +), auto Minus (-) operator
+ + in the front is the "First plus fu" (++i) + + in the latter is "first fu Hou Plus" (i++)
1) The operands of these operators can be byte, short, int, long, float, double, char types, where the char type is automatically converted to int at operation. 2) in Java, an integer is removed by 0 or the remainder of 0 is an illegal operation and will throw atrthemticexcerption. 3) The remainder operation (%) of the two operands can be not only an integer, can also be a floating-point type, not only can be a positive integer, it can be a negative integer, its calculation of the result of the symbol and the remainder operator on the left side of the Operation object symbol is the same. 4) If the two operands involved in the division are integral, then the operation is an integer integer, and if you want to get the quotient value of the fractional part, you need to cast the type of one of the operands. The operand of the A/(float) B5) operator "+" can make a string whose operation means to concatenate two strings.
Relational Operators
Greater than (>), greater than or equal (>=), less than (<), less than Equals (<=), equals (= =), and not equals (! =) equals and does not equal to all base types, other comparators do not apply to Boolean types
= = and! = also apply to all objects, compared to references to objects
Compare the actual contents of the object, using Equals (), this method does not apply to the base type, the base type directly uses = = and! = To note: the Equals () default behavior is a comparison reference, and most class libraries implement the Equals () method to compare the contents of an object To achieve this effect in a class of your own definition, you need to override the method.
logical Operators
With (&&), or (| |), NON (!)
"With", "or", "non" applies only to Boolean values
If you use a Boolean value where a string value should be used, the Boolean value is automatically converted to the appropriate text form
In a logical expression, if the value of the entire expression can be inferred from the value of the expression on the left side of the logical operator, the expression to the right of the logical operator will no longer be executed. (&&,| | )
Direct Constants
- The suffix character following the direct constant marks its type
Uppercase (lowercase) l, which represents the long uppercase (lowercase) f, which represents the float capital (lowercase) d, which represents a double note: If the compiler recognizes the type correctly, you do not have to append the character to the value
Hex: Prefix 0x (0X), followed by 0-9 or lowercase (uppercase) a-f to denote octal: prefix 0, followed by 0-7 to indicate
Integer.toBinaryStringLong.toBinaryString
Exponential counting Method
In Java, E represents the power of 10, and Eg:10e2//10 's 2-//10-5-square compiler typically handles the exponent as a double-precision number (double)
Bitwise Operators
The bitwise operator performs a Boolean algebra operation on the corresponding bits in the two parameters and eventually produces a result
With: 1&1==1 1&0==0 0&0==0//Same as 1 o'clock, output 1; otherwise output 0 or: 1|1==1 1|0==1 0|0==0//Same as 0 o'clock, output 0; otherwise output 1 non: ~0==1 ~1==0//Take counter operation, belong to unary operator , cannot be combined with "=" Using xor: 1^0==1 1^1==0 0^0==0//different 1, same as 0
Bitwise operators can be used in conjunction with an equal sign (=) to combine operations and assign values: &=,|= and ^=
You can treat a Boolean type as a single-bit value, and you can perform a bitwise and, or, or XOR operation on it, and you cannot perform a bitwise non. Eg:true & False
Public int[] Swapnum (int num1, int num2) { num1 = num1 ^ num2; num2 = num2 ^ num1; NUM1 = num1 ^ num2; Int[] Nums = { num1, num2 }; return nums; }
shift operator
Moves the operand to the left of the operator to the left and the low 0
Move the operand to the left of the operator to the right, such as the symbol is positive, high-level insert 0; If the symbol is negative, the high position is inserted 1
Moves the operand to the left of the operator to the right, either positive or negative, by inserting 0 at a high
The operand of the shift operator operation is also the binary "bit"
The shift operator is used only to handle integer types
The shift processing of char, Byte, or short is converted to an int type before being shifted, and the resulting result is also of type int
The shift can be combined with the equal sign (<<= or >>= or >>>=), and may result in incorrect results when >>>= to char or short
ternary operator
result = Boolean-exp? value0:value1; if Boolean-exp evaluates to True, result is vaulue0, otherwise result is value1
Type conversion Operators
Java allows any basic data type to be converted to another base data type, except for Boolean types
Converting a data type that can hold more information into a type that does not hold that much information requires a cast to be displayed because of the risk of information loss
When a narrowing conversion occurs, when a floating-point number is converted to an integer, the floating-point number is truncated and is not rounded (Math.Round () can be implemented)
Do not have to show any type of conversion, because the new type will certainly accommodate the original type of information, without causing any loss of information
Short S = 1;int i = s;
If you perform arithmetic or bitwise operations on the base type, as long as the type is smaller than int (that is, char,byte,short), the values are automatically converted to int before the operation.
Typically, the largest data type in an expression determines the data type of the final result of the expression. Eg:float is multiplied by double, and the result is double
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Operator of Java Basics