Sort out the basic descriptors and operators commonly used in Java programming

  • 2020-05-05 11:16:47
  • OfStack

descriptor
descriptors are keywords that you add to those definitions to change their meaning. The Java language has many descriptors, including the following:

Accessible descriptors inaccessible descriptors apply descriptors, and you can add keywords to classes, methods, and variables. The descriptor is declared first, as shown in the following example (in italics) :

public 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
}

has access to the descriptor
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 descriptors are required.

Only visible to classes (private) all visible (public) visible to encapsulation and subclasses (protected)

is not accessible to the descriptor

Java provides some inaccessible descriptors to satisfy other functions.

The Static descriptor is used to create class methods and variables. The Final descriptor is used to finalize and implement classes, methods, and variables. The Abstract descriptor is used to create classes and methods that are not allowed to be instantiated. The synchronized and volatile descriptors are used as lines.

basic operator
Java provides a rich set of operators for control variables. We can divide all the Java operators into the following groups:

Arithmetic operator relational operator bit operator logical operator assignment operator other operators arithmetic operator

The use of arithmetic operators in mathematical expressions is the same as their use in algebra. The following table lists the arithmetic operators:

Suppose there are 10 global variables A and 20 global variables B, then:

Sample

运算符 描述 例子
+ 加法 � 在运算符的另一端增加 A+B 为 30
- 减法 � 将右侧的操作数从左侧的操作数中减去 A - B 为-10
* 乘法 � 将运算符两端的值相乘 A * B 为200
/ 除法 � 用右侧操作数除左侧操作数 B / A 为2
% 系数 - 用右侧操作数除左侧操作数并返回馀数 B % A 为0
++ 增量 � 给操作数的值增加1 B++ 为21
-- 减量 � 给操作数的值减去1 B―为19

relational operator
Following are the relational operators that the Java language can support.
Suppose the variable A has 10 and the variable B has 20, then:

Sample

运算符 描述 例子
== 检查双方操作数的值是否相等,如果相等那么条件为真 (A == B) 不为真。
!= 检查双方操作数的值是否相等,如果不相等那么条件为真 (A != B) 为真。
> 检查左侧的操作数是否大于右侧的操作数,如果大于那么条件为真 (A > B) 不为真。
< 检查左侧的操作数是否小于右侧的操作数,如果小于那么条件为真 (A < B) 为真。
>= 检查左侧的操作数是够大于等于右侧的操作数,如果是那么条件为真 (A >= B) 不为真。
<= 检查左侧的操作数是否小于等于右侧的操作数,如果是那么条件为真 (A <= B) 为真。

The bit operator
Java defines several operators that can be applied to integer types, long, int, short, character and byte types.

The bit operator ACTS on the standard of transfer between binary systems and performs bitwise operations. So let's say that a is equal to 60; b is equal to 13; Now in binary form they look like this:


a = 0011 1100

b = 0000 1101

-----------------

a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a = 1100 0011 

The following table lists the bit operators:

Suppose the integer variables A have 60 and B have 13 then:

Sample

运算符 描述 例子
& 二进制AND运算符在结果上复制一位如果在双方操作数同时存在 (A & B) 为12,即 0000 1100
| 二进制OR运算符在结果上复制一位如果在任何一个操作数上存在 (A | B) 为61,即0011 1101
^ 二进制XOR 运算符复制位,如果它是设置在一个操作数上而不是两个。 (A ^ B) 为49, 即0011 0001
~ 二进制补充运算符是一元的,b并有“翻转”位的影响 (~A ) 为 -61,由于是带符号的二进制数,那2的补位形式为1100 0011
<< 二进制左移运算符。左侧操作数的值根据右侧操作数指定的位的数量移至左侧。 A << 2 为240 ,即1111 0000
>> 二进制右移运算符。左侧操作数的值根据右侧操作数指定的位的数量移至右侧。 A >> 2 为 15即1111
>>> 右移补零运算符。左侧操作数的值根据右侧操作数指定的位的数量移至右,并且转移的值用零补满。 A >>>2 为15 ,即0000 1111

logical operator
The following table lists the logical operator
:

If the Boolean system variable A is true and B is false, then:

Sample

运算符 描述 例子
&& 称为逻辑与运算符。如果双方操作数都不为零,那么条件为真。 (A && B) 为真.
|| 称为逻辑或运算符. 如果双方操作数其中的任何一个都不为零,那么条件为真。 (A || B) 为真.
! 称为逻辑非运算符. 用作翻转操作数的逻辑状态。如果一个条件为真,那么逻辑非运算符为假。 !(A && B) 为真.

The assignment operator
Here are the assignment operators supported by the Java language:

Sample

运算符 描述 例子
= 简单及运算符, 将右侧操作数的值赋给左侧操作数 C = A + B 会将 A + B 的值赋给 C
+= 增加及赋值运算符, 它将右侧的操作数增加到左侧的操作数并且结果赋给左侧操作数 C += A 同等于 C = C + A
-= 减去及赋值运算符,它将右侧操作数从左侧操作数中减去并将结果赋给左侧操作数 C -= A 同等于C = C - A
*= 乘以及赋值运算符,它将右侧操作数与左侧相乘并将结果赋给左侧操作数 C = A 同等于 C = C A
/= 除以及赋值运算符,它将右侧操作数除左侧操作数并将结果赋给左侧操作数 C /= A 同等于 C = C / A
%= 系数及赋值运算符 需要系数运用两个操作数并且将结果赋给左侧操作数 C %= A is 同等于 C = C % A
<<= 左移和赋值运算符 C <<= 2 同等于C = C << 2
>>= 右移和赋值运算符 C >>= 2 同等于 C = C >> 2
&= 按位和赋值运算符 C &= 2 同等于C = C & 2
^= 按位异或及赋值运算符 C ^= 2 同等于 C = C ^ 2
|= 按位可兼或及赋值运算符 C |= 2 同等于C = C | 2

other operators
here are some other operators supported by the Java language:

conditional operator (? :)

The conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean mathematical system expressions. The purpose of this operator is to determine which values should be assigned to a variable. The operator is written as follows:


variable x = (expression) ? value if true : value if false 

Here's 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 results in


Value of b is : 30
Value of b is : 20

Instanceof

This operator is used only for object reference variables. This operator checks to see if the object is of a unique type (type or interface type). The Instanceof operator is written as:

( Object reference variable ) instanceof   (class/interface type)
The result is true if the right type or interface type of the object referred to by the variable on the left of the operator passes the IS-A check. Here's 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 results in

true

This operator will still return to true if the object being compared is an assignment that is compatible with the type on the right. Here's 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 result in the following:


true


is the priority of the Java operator The operator priority determines the grouping of terms in an expression. It affects how an expression is evaluated. Certain operators have higher priority than others; For example, the multiplication operator has a higher priority than the addition operator:

For example, x=7+3 2; Here x is assigned 13, not 20, because the operator has a higher priority than the operator +, so it first multiplies 3 times 2, and then adds 7.

Here, the highest-priority operators are at the top of the table, and the lowest-priority operators are at the bottom. In an expression, the higher priority operators are evaluated first.

运算符 关联性
后缀 () [] . (dot operator) 从左到右
一元 ++ - - ! ~ 从右到左
乘法的 * / % 从左到右
加法的 + - 从左到右
移位 >> >>> << 从左到右
关系的 > >= < <= 从左到右
相等 == != 从左到右
位与 & 从左到右
位异或 ^ 从左到右
位或 | 从左到右
逻辑与 && 从左到右
逻辑或 || 从左到右
有条件的 ?: 从右到左
赋值 = += -= *= /= %= >>= <<= &= ^== 从右到左
逗号 , 从左到右


Related articles: