当前位置:网站首页>Chapter 4: operators
Chapter 4: operators
2022-07-25 16:53:00 【Take charge of [email protected]】
Chapter four : The operator
4.1、 Introduction to operators
Operators can be used to manipulate data values , Including arithmetic operators 、 Bit operators 、 Relational operators and equality operators .
4.2、 Integer operator
brief introduction : It can be done to byte、short、int、long Operators that operate .
classification : Unary operators 、 Binary operators
4.2.1、 Unary integer operator
brief introduction : Only one operand can be operated ( Generally speaking, only one value can be operated )
| The operator | Practical effect | Case study |
|---|---|---|
| - | Change the sign of an integer , Take the opposite | -x amount to -1*x |
| ~ | Reverse bit by bit , It belongs to bit operation | ~x |
| ++ | Add 1 | x++ |
| – | reduce 1 | x– |
Be careful :++ and – Will change its value and - and ~ Does not change the value of the variable itself .
++ It can also be divided into pre ++ And post ++
x++ // Indicates that after a program is executed x Add the value of itself 1
++x // First execute a program , And then x The value of the add 1
– It can also be divided into pre – And post –
x-- // Indicates that after a program is executed x Its own value minus 1
--x // First execute a program , And then x The value of the reduction 1
4.2.2、 Binary integer operator
brief introduction : Operators that can operate on two operands .
| The operator | Practical effect | Case study |
|---|---|---|
| + | Add operation | a+b |
| - | Subtraction operation | a-b |
| * | Multiplication | a*b |
| / | In addition to the operation | a/b |
| % | Take the remainder operation | a%b |
| & | And operation | a&b |
| | | Or operations | a|b |
| ^ | Exclusive or operation | a^b |
| << | Move arithmetic left | a<<b |
| >> | Count right | a>>b |
| >>> | Logical shift right | a>>>b |
4.3、 Floating point operators
brief introduction : Most integer operators are also applicable to floating-point operators , The usage is similar to that of integer operators . The type of the operation result is consistent with the type that represents the largest range in the operand .
Be careful :
- Floating point numbers do not support bit operations
- Floating point data types have two special values -Infinity and Infinity, They can be used to represent the operation results of floating-point numbers , for example : An integer divided by 0 It's infinite , Negative divided by 0 Get negative infinity .
4.4、 Comparison operators and logical operators
brief introduction : The results are all boolean type , except ! Other than binary operators
| The operator | The actual meaning |
|---|---|
| < | Less than |
| > | Greater than |
| <= | Less than or equal to |
| >= | Greater than or equal to |
| == | be equal to |
| != | It's not equal to |
| && | Short circuit and |
| & | Non short circuit and |
| || | Short circuit or |
| | | No short circuit or |
| ! | Not |
The difference between short circuit and non short circuit :
Non short circuiting always executes Boolean expressions on both sides of the operator .
Short circuit as long as the Boolean expression on one side of the operator is executed, the value of the expression can be determined, and the other side will not be executed .
4.5、 Special operators “?:”
brief introduction :Java There is a special ternary operator in “?:”
grammar : Boolean expression ? expression 1: expression 2
explain : If the Boolean expression holds , Execute expression 1, Execution expression does not hold 2
4.6、 String join operator “+”
** brief introduction :** The operator + Can connect strings , And generate a new string .
matters needing attention :
- One of the two operands is String Type of , The other is any type of use + The operator must return String Type of .
- If both operands are not String Type of , Then the ratio of two operands must be division boolean Types other than .
- For multiple + Operator expression ,Java Will be based on + Operators carry out left combination characteristics , Evaluate the expression from the left . Determine according to the type of operands + Properties of operators .
- except + Operators can connect outside the string , No other operator can connect strings .
4.7、 The operator “==” With the object equals() Method
4.7.1、 The operator “==”
brief introduction : Compare whether the two operands are equal , The two operands can be basic data types , It can also be a reference data type .
When both sides of the operator are reference data types , These two variables must both reference the same object , The result is true.
Integer int1=new Integer(1);
Integer int2=new Integer(1);
Integer int3=int1; //int3 and int1 From the same object , So use int3==int1 The output is zero true, however int1 and int2 Not the same object ,int2==int1 The return value is false
The operator “==” And polymorphism
For variables of reference type ,Java The compiler will compile according to the type of variable being declared .“==” When comparing reference type variables , The types declared on both sides of the operator must be the same type or have inheritance relationship ( That is, they are all on the same branch of the inheritance tree ).
The operator “==” Used for array types
boolean b1=new int[4]==new long[5]; // Compilation error , The reason is that the types on both sides are inconsistent boolean b2=new int[4]==new int[4]; // legal return false int[]array1=new int[4]; int[]array2=array1; boolean b3=array1=array2; // return true
4.7.2、 Object's equals() Method
brief introduction :equals() The method is in Object Methods defined in class , The format of the statement is as follows :
public boolean equals(Object obj)
Compare the rules : When parameters obj When the referenced object is the same as the current object, it returns true, Otherwise return to false
public boolean equals(Object obj){
if(this==obj){
return true;
}else{
return fasle;
}
}
4.7.3、 Simple summary
- “==” And objects equals() Methods are used to compare whether two operands are equal .
- “==” The actual comparison is whether their memory addresses are equal .
- Object's equals() Methods actually compare whether their values are equal .
4.8、instanceof The operator
brief introduction :instanceof The operator is used to determine whether the referenced object is an instance of a class . To the left of the operator is an object , On the right is often a class or interface .
grammar :
obj instanceof Class name 、 Interface
An instance of a class contains content : The class itself 、 Instances of all direct or indirect subclasses .
instanceof And polymorphism
For variables of reference type ,Java The compiler will compile according to the type of variable being declared .instanceof The types declared on both sides of the operator must be the same type or have inheritance relationship ( That is, they are all on the same branch of the inheritance tree ).
instanceof Used for array types
boolean b1=new int[4] instanceof long[]; // Because the types on both sides are inconsistent , The return value is false boolean b2=new int[4] instanceof int[]; // return true
4.9、 Assignment and type conversion of variables
brief introduction :“=” The use of operators in the development process is the most mundane , The meaning expressed is to assign the value on the right side of the operator to the variable on the left side of the operator .
The rules :
- A direct number can be assigned directly to a variable of the same type .
- The assignment between variables of the same type can be carried out directly
- Assignment between different types of variables , Type conversion required
4.9.1、 Basic data type conversion
- Automatic type conversion
Conversion rules :
(byte、int、short、long、char、float) option( For operation ) (double)——double
(byte、int、short、long、char) option( For operation ) (float)——float
(byte、int、short、char) option( For operation ) (long)——long
(byte、short、char) option( For operation ) (int)——int
(byte、short、char) option( For operation ) (byte、short、char)——int
- Cast
Assigning a high-order type to a low-order type must be cast , Otherwise, it will compile and report errors .
Forced type conversion will lead to the loss of data precision or data overflow .
4.9.2、 Type conversion of reference type
brief introduction : When assigning values between variables of reference type , Subclasses are assigned to direct or indirect parent classes , Automatic type conversion . When a parent class assigns a value to a child class, it needs to be cast .
版权声明
本文为[Take charge of [email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/206/202207251650543100.html
边栏推荐
- easyui修改以及datagrid dialog form控件使用
- 如何安装govendor并打开项目
- 【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part5-完结):信息检索与结果组装
- 【目标检测】YOLOv5跑通VOC2007数据集(修复版)
- Emqx cloud update: more parameters are added to log analysis, which makes monitoring, operation and maintenance easier
- HCIP笔记十二天
- GTX1080Ti 光纤HDMI干扰出现闪屏1080Ti 闪屏解决方法
- Talk about how to use redis to realize distributed locks?
- In the eyes of 100 users, there are 100 QQS
- Communication between processes (pipeline details)
猜你喜欢

Jenkins' role based authorization strategy installation configuration

WPF 实现用户头像选择器

Fudan University EMBA peer topic: always put the value of consumers in the most important position

【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part4):结合问题分类的问题解析与检索语句生成
![[OBS] Reprint: what about the serious delay of OBS live broadcast and Caton?](/img/fd/54fcae2bc3f4313e9401c735ef74b8.png)
[OBS] Reprint: what about the serious delay of OBS live broadcast and Caton?

博云容器云、DevOps平台斩获可信云“技术最佳实践奖”

After 20 years of agitation, the chip production capacity has started from zero to surpass that of the United States, which is another great achievement made in China

第三章、数据类型和变量

备考过程中,这些“谣言”千万不要信!

免费的低代码开发平台有哪些?
随机推荐
【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part4):结合问题分类的问题解析与检索语句生成
首页门户分类查询
Hcip notes 12 days
数据分析与隐私安全成 Web3.0 成败关键因素,企业如何布局?
[target detection] tph-yolov5: UAV target detection based on Transformer's improved yolov5
easyui下拉框,增加以及商品的上架,下架
【小5聊】公众号排查<该公众号提供的服务出现故障,请稍后>
Unity is better to use the hot scheme Wolong
WPF implements user avatar selector
使用Huggingface在矩池云快速加载预训练模型和数据集
SAP Fiori 的附件处理(Attachment handling)
What is chain game system development? How to make chain game system development
QT listview list display component notes
文字翻译软件-文字批量翻译转换器免费
激荡20年,芯片产能从零起步到反超美国,中国制造的又一大成就
jenkins的Role-based Authorization Strategy安装配置
Frustrated Internet people desperately knock on the door of Web3
C#入门基础教程
Fudan University emba2022 graduation season - graduation does not forget the original intention and glory to embark on the journey again
自定义mvc项目登录注册和树形菜单