Descriptor
Descriptors are keywords you add to those definitions to change their meaning. The Java language has many descriptors, including the following:
public class class className { // ...}private boolean myFlag;static final double weeks = 9.5;protected static final int BOXWIDTH = 42;public static void main(String[] arguments) { // body of method} Accessible descriptors
Java provides a series of accessible descriptors to set the access levels of classes, variables, methods, and constructors. The four access levels are as follows:
By default, visible to encapsulation. No descriptor is required.
Inaccessible descriptor
Java provides some inaccessible descriptors to satisfy other functions.
Basic operators
Java provides a rich set of operators for manipulating variables. We can divide all Java operators into the following groups:
The use of arithmetic operators in mathematical expressions is the same as their use in algebra. The following table lists the arithmetic operators:
Assuming that there are 10 overall variable A and 20 variable B, then:
Example
| Operators | describe | example |
|---|---|---|
| + | Addition is added at the other end of the operator | A+B is 30 |
| - | Subtraction subtracts the operand on the right from the operand on the left | A - B is -10 |
| * | Multiplication multiplies the values at both ends of the operator | A * B is 200 |
| / | Divide the left operand by the right operand | B/A is 2 |
| % | Coefficient - Divide the left operand with the right operand and return the remainder | B % A is 0 |
| ++ | Increment increases the value of the operand by 1 | B++ is 21 |
| -- | Decrement subtracts 1 to the operand value | B-is 19 |
Example
| Operators | describe | example |
|---|---|---|
| == | Check whether the values of the operands of both sides are equal. If they are equal, the condition is true. | (A == B) Not true. |
| != | Check whether the values of the operands of both sides are equal. If not equal, the condition is true. | (A != B) is true. |
| > | Check whether the operand on the left is greater than the operand on the right. If it is greater then the condition is true | (A > B) Not true. |
| < | Check whether the operand on the left is smaller than the operand on the right. If it is smaller, then the condition is true | (A < B) is true. |
| >= | Check that the operand on the left is greater than or equal to the operand on the right. If so, the condition is true | (A >= B) Not true. |
| <= | Check whether the operand on the left is less than or equal to the operand on the right. If so, the condition is true | (A <= B) is true. |
The bit operator acts on the transmission standards between binary systems and performs bitwise operations. Suppose if a is equal to 60; b is equal to 13; now in binary form they are as follows:
a = 0011 1100b = 0000 1101---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The following table lists bit operators:
Assume that there are 60 integer variables A and 13 B, then:
Example
| Operators | describe | example |
|---|---|---|
| & | Binary AND operator copy one on the result if both operands exist at the same time | (A & B) is 12, i.e. 0000 1100 |
| | | The binary OR operator copies a bit on the result if it exists on any operand | (A | B) is 61, i.e. 0011 1101 |
| ^ | The binary XOR operator copies the bit if it is set on one operand instead of two. | (A ^ B) is 49, i.e. 0011 0001 |
| ~ | The binary supplement operator is unary, and b has the effect of "flip" bits | (~A ) is -61. Since it is a signed binary number, the complement form of 2 is 1100 0011 |
| << | Binary left shift operator. The value of the operand on the left moves to the left according to the number of bits specified by the operand on the right. | A << 2 is 240, i.e. 1111 0000 |
| >> | Binary right shift operator. The value of the operand on the left moves to the right according to the number of bits specified by the operand on the right. | A >> 2 is 15 i.e. 1111 |
| >>> | Move right to fill zero operator. The value of the left operand is moved to the right according to the number of bits specified by the right operand, and the transferred value is filled with zero. | A >>>2 is 15, i.e. 0000 1111 |
Assuming that the Boolean mathematical system variable A is true and B is false, then:
Example
| Operators | describe | example |
|---|---|---|
| && | It is called logic and operator. If both operands are not zero, then the condition is true. | (A && B) is true. |
| || | Called a logic or operator. If neither of the operands are zero, then the condition is true. | (A || B) is true. |
| ! | Called a logical non-operator. It is used as a logical state of flip operands. If a condition is true, then the logical non-operator is false. | !(A && B) is true. |
Example
| Operators | describe | example |
|---|---|---|
| = | Simple and operator, assign the value of the operand on the right to the operand on the left | C = A + B will assign the value of A + B to C |
| += | Add and assign operator, which increases the operand on the right to the operand on the left and assigns the result to the operand on the left | C += A is equal to C = C + A |
| -= | Subtraction and assignment operator, which subtracts the right operand from the left operand and assigns the result to the left operand | C -= A is equal to C = C - A |
| *= | Multiply and assignment operator, which multiplies the right operand with the left and assigns the result to the left operand | C = A is equal to C = C A |
| /= | Dividing and assignment operator, which divides the right operand on the left and assigns the result to the left operand | C /= A is equal to C = C / A |
| %= | The coefficient and assignment operator require the coefficient to use two operands and assign the result to the operand on the left. | C %= A is equal to C = C % A |
| <<= | Left shift and assignment operators | C <<= 2 equals C = C << 2 |
| >>= | Right shift and assignment operators | C >>= 2 equals C = C >> 2 |
| &= | Bitwise and assignment operators | C &= 2 equals C = C & 2 |
| ^= | Bitwise XOR and assignment operators | C ^= 2 equals C = C ^ 2 |
| |= | Bitwise or assign operators | C |= 2 equals C = C | 2 |
Conditional operator (?:)
Conditional operators are also called ternary operators. This operator consists of three operands and is used to evaluate Boolean mathematical expressions. The purpose of this operator is to determine which values should be assigned to the variable. This operator is written as follows:
variable x = (expression) ? value if true : value if false
Here is an example:
public class Test { public static void main(String args[]){ int a , b; a = 10; b = (a == 1) ? 20: 30; System.out.println( "Value of b is : " + b ); b = (a == 10) ? 20: 30; System.out.println( "Value of b is : " + b ); }}This will result in the following:
Value of b is : 30Value of b is : 20
Instanceof symbol
This operator is only used for object reference variables. This operator checks whether an object is a unique type (type or interface type). The Instanceof operator is written as:
( Object reference variable ) instanceof (class/interface type)
If the object to the left of the operator referred to by the variable is the type or interface type to the right by passing IS-A check, the result is true. Here is an example:
public class Test { public static void main(String args[]){ String name = "James"; // following will return true since name is type of String boolean result = name instanceof String; System.out.println( result ); }}This will produce the following results:
Copy the code as follows: true
This operator will still return to true if the object being compared is an assignment compatible with the right type. Here is another example: class Vehicle {}public class Car extends Vehicle { public static void main(String args[]){ Vehicle a = new Car(); boolean result = a instanceof Car; System.out.println( result ); }}This will produce the following results:
true
Java operator priority
Operator priority determines the grouping of terms in an expression. It affects how an expression is evaluated. A certain operator has a higher priority than other operators; for example: the multiplication operator has a higher priority than the addition operator:
For example, x=7+3 2; here x is assigned to 13, not 20, because the operator is higher priority than the operator +, so it first operates multiplication 3*2, and then adds 7.
Here, operators with the highest priority are on the highest level of this table, and those with the lowest priority appear at the bottom. In an expression, the higher the priority operator is evaluated first.
| kind | Operators | Relevance |
|---|---|---|
| suffix | () [] . (dot operator) | From left to right |
| One dollar | ++ - - ! ~ | From right to left |
| Multiplication | */ % | From left to right |
| Additive | + - | From left to right |
| Shift | >> >>> << | From left to right |
| Relationship | > >= < <= | From left to right |
| equal | == != | From left to right |
| bit and | & | From left to right |
| Extraordinary position or | ^ | From left to right |
| bit or | | | From left to right |
| Logic and | && | From left to right |
| Logical or | || | From left to right |
| Conditional | ?: | From right to left |
| Assignment | = += -= *= /= %= >>= <<= &= ^== | From right to left |
| comma | , | From left to right |