Introduction
An operator is a special symbol. An operator is a specific symbol that operates one or more operands through certain operation rules and generates a result. The effective combination of an operator and operands is called an expression.
Operators in Java are mainly divided into the following categories:
Assignment operators
Arithmetic operators
Relational operators
Logical operators
Conditional operators (ternary operators)
Bitwise operators
Assignment operator
The assignment operator is used to assign a value to a variable or constant. The symbol of the assignment operator is "=".
Example
int a = 1; //定义时直接赋值int b;int c; b = c = 2; //可以在一个语句内为多个变量赋值 int d = b + 3; //变量运算后再赋值,先执行右边,再执行左边
Arithmetic operators
Java supports basic mathematical operations such as addition, subtraction, multiplication, division and remainder. They are the following:
Addition operator: +
double a = 1.1;double b = 1.2;double sum = a + b;
System.out.PRintln(sum); //Output 2.3
Subtraction operator: -
double a = 2.2;double b = 1.1;double sub = a - b;
System.out.println(sub); //Output 1.1
Multiplication operator: *
int a = 11;double b = 1.2;double multiply = a * b;
System.out.println(multiply); //Output 1.32
Division operator: /
The division operator is a bit special: if the operation Both values are int, and the result obtained is also of type int. The decimal point will be removed directly and will not be rounded.
int a = 10; int b = 4; double c = a/b;
System.out.println(c); //The original result of 10chu 4 is 2.5, but the result of dividing two int type numbers is an int type number. Although the result value is assigned to double,
//But the output result becomes 2.0, which is equivalent to removing the decimal point when the operation is completed, and then converting it to 2.0double a2 = 5.2;double b2 = 3.1;double c2 = a2/b2;
System.out.println(c2); //Operation on two double values, output result: 1.6774193548387097System.out.println(5 / 0.0); //The divisor is double type 0.0, and the output is negative infinity System.out.println(5 / 0); //If the divisor is 0, an error will be reported during runtime
Remainder operator: %
int a = 5;int b = 3;double c = 3.2;
System.out.println(a%b) ; //Output 2System.out.println(a%c); //1.7999999999999998System.out.println(0%5); //Output 0System.out.println(5%0); //An error will occur during operation
Find the negative: -
int i = -1;int i2 = -i;
System.out.println(i2); //Output 1
Self-increment: ++
int i = 1;
i++ ; //Equivalent to adding 1 to the value of i; System.out.println(i); //Output 2
The self-increasing symbol can be placed in front of the variable or behind the variable. Put it in front first Add 1 to the operand, and then perform the operation of the expression. Putting it after it will do the opposite.
int i1 = 1;int i2 = 1;int i3 = i1++; //At this time, the value of i3 is 1 and the value of i1 is 2; it first assigns the value of i1 to i3, and then adds 1 to i1 ;int i4 = ++i2; //At this time, the value of i4 is 2, and the value of i2 is also 2; it first adds 1 to the value of i2, and then assigns the value to i4;
Decrement: --
The function is similar to self-adding
int i1 = 1;int i2 = 1;int i3 = i1++; //At this time, the value of i3 is 1 and the value of i1 is 0; it first assigns the value of i1 to i3, then decrement i1 by 1; int i4 = ++i2; //At this time, the value of i4 is 0, and the value of i2 is also 0; it first decrements the value of i2 by 1, and then assigns the value to i4;
Relational operators (comparison operators)
Relational operators can test the relationship between two operands (but will not change the value of the operand). The result of the relational expression is boolean true/false:
System.out.println(4 == 4); //The result is trueSystem.out.println(4 != 3); //The result is trueSystem.out.println(true == false); //The result is false
Logical operators
Logical operators are used to operate boolean type variables or constants
See examples
System.out.println(!true); //The result is falseSystem.out.println (2 > 1 && 1 > 1); //The result is falseSystem.out.println(2 > 1 || 1 > 1); //The result is trueSystem.out.println(true ^ false); / /The result is true, XOR, which is equivalent to inverting the first previous value true, and then performing an "OR" operation
Let's take a look at the difference between | and ||
int a = 1; int b = 1;if(a == 1 | b++ > 1){
System.out.println(b); //The output value of b is 2, bitwise OR, although the result on the left side of the | symbol is true, it still Will execute the code to the right of the | symbol}
and change | to ||
int a = 1;int b = 1;if(a == 1 || b++ > 1){
System.out.println(b); //The output value of b is 1, and the result on the left side of the || symbol is true, the code to the right of the || symbol will no longer be executed}
Conditional operator (ternary operator)
Its general form is:
Expression 1 ? Expression 2 : Expression 3
Determine whether to execute expression 2 or expression 3 based on the result of expression 1. If the result of expression 1 is true, execute expression 2, otherwise execute expression 3;
Conditional operators can be substituted in some cases Small if...else statement.
String s = 1 > 2 ? "1 is greater than 2" : "1 is not greater than 2";
System.out.println(s); //Output 1 is not greater than 2
bit operator
bit operation Symbols are two data that participate in operations, and operations are performed based on binary bits. There are seven bitwise operators in Java: bitwise AND (&), bitwise OR (|), bitwise NOT (~), bitwise XOR (^), left shift operator (>), unsigned right shift operator (>>>).
For detailed introduction, please refer to the following article:
http://www.cnblogs.com/yezhenhan/archive/2012/06/20/2555849.html
Priority of operators
In many cases , an expression consists of multiple operators, and the priority determines the calculation order of the operators:
Although operators have priorities, an expression will be evaluated sequentially according to the priority of the expression operators, but In actual programming, if an expression is very long, it is not recommended to write it like this. Instead, it is written in several steps, because the readability is too poor when written together.
The above is [Java Introduction Notes] Java Language Basics (3): The content of operators. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

随着时代的发展,农业领域也开始借助现代科技手段升级转型,智慧农业也因此应运而生。Java作为一种性能优异、可移植性强的计算机编程语言,具备着较高的普及度和应用价值,成为智慧农业应用开发的重要解决方案之一。本文旨在介绍Java语言中智慧农业应用的开发流程、应用场景和优势点等方面的内容。一、Java语言中智慧农业应用的开发流程智慧农业应用的开发流程分为需求分析、

JSON可以用作数据交换格式,它是轻量级的且与语言无关。一个JSONArray可以解析文本字符串以生成类似于向量的对象,并支持java.util.List接口。我们可以使用org.json.CDL类将JSON数组转换为CSV格式,它提供了一个静态方法toString(),用于将JSONArray转换为逗号分隔的文本。我们需要导入org.apache.commons.io.FileUtils包,以使用writeStringToFile()方法将数据存储在CSV文件中。语法publicstaticj

ChatGPTJava:如何构建一个精准的语义搜索引擎,需要具体代码示例引言:随着互联网的迅速发展,信息爆炸式增长,人们在获取所需信息的过程中,往往会遇到搜索结果质量不佳、不准确的问题。为了提供更精准、高效的搜索结果,语义搜索引擎应运而生。本文将介绍如何使用ChatGPTJava构建一个精准的语义搜索引擎,并给出具体的代码示例。一、了解ChatGPTJ

Java语言中的图像处理算法介绍随着数字化时代的到来,图像处理已经成为了计算机科学中的一个重要分支。在计算机中,图像是以数字形式存储的,而图像处理则是通过对这些数字进行一系列的算法运算,改变图像的质量和外观。Java语言作为一种跨平台的编程语言,其丰富的图像处理库和强大的算法支持,使得它成为了很多开发者的首选。本文将介绍Java语言中常用的图像处理算法,以及

二叉树是计算机科学中常见的数据结构,也是Java编程中常用的一种数据结构。本文将详细介绍Java中的二叉树结构。一、什么是二叉树?在计算机科学中,二叉树是一种树形结构,每个节点最多有两个子节点。其中,左侧子节点比父节点小,右侧子节点则比父节点大。在Java编程中,常用二叉树表示排序,搜索以及提高对数据的查询效率。二、Java中的二叉树实现在Java中,二叉树

Java语言是一种广泛应用于金融领域的编程语言。由于其强大的功能和高效的性能,Java语言成为了金融机构开发软件的首选语言。本文将介绍Java语言在金融应用开发中的重要性,并介绍一些常见的金融应用程序。一、Java语言在金融领域中的应用Java语言在金融领域中的应用已经十分广泛,其主要优势包括:1.跨平台能力Java语言具有跨平台能力,这意味着同一段Java

Java语言下对接百度AI接口实现图像风格迁移的方法与步骤引言:图像风格迁移是一种有趣的技术,在艺术和媒体创作中具有广泛的应用。百度AI提供了图像风格迁移的API,使开发者能够方便地使用这一功能。本文将介绍如何使用Java语言对接百度AI接口,实现图像风格迁移的方法与步骤,并提供示例代码。步骤一:申请百度AI接口的访问权限要使用百度AI提供的图像风格迁移AP

Java语言中的SpringCloud框架介绍随着云计算和微服务的流行,SpringCloud框架成为了Java语言中构建云原生应用的首选框架之一。本文将介绍SpringCloud框架的概念和特点,以及如何使用SpringCloud构建微服务架构。SpringCloud简介SpringCloud框架是基于SpringBoot的微服务框架。它为


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
