0Pricing
Java Academy · 课时

运算符

运算符

运算符 是 CoddyKit 上的免费 Java Academy 课时。 这是第 1 节课,共 1 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Java Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Java Academy 课程共包含 1 节课。

介绍

您好,

Java 为我们提供了许多用于处理数据的运算符。借助这些运算符,我们可以操作已有的数据。

它们是算术运算符,也就是我们目前最熟悉的、源自数学的运算符类型。

加法运算符

加法运算符

我们使用 + 运算符进行加法运算。

class Main{

public static void main(String[] args){
   int num1 = 10;
   int num2 = 20;
   int num3 = num1 + num2;
   System.out.println(num3);
 }
}

减法

减法运算符

我们使用 (-) 运算符进行减法运算。

让我们来看一个示例。

class Main{

public static void main(String[] args){
   int num1 = 10;
   int num2 = 20;
   int num3 = num1 - num2;
   System.out.println(num3);
 }
}

乘法运算

乘法运算符

我们使用 (*) 运算符进行乘法运算。

class Main{

public static void main(String[] args){
   int num1 = 10;
   int num2 = 20;
   int num3 = num1 * num2;
   System.out.println(num3);
 }
}

除法运算符

除法运算符

我们使用 (/) 运算符进行除法运算。

让我们来看一个示例。

class Main{

public static void main(String[] args){
   int num1 = 10;
   int num2 = 20;
   int num3 = num1 / num2;
   System.out.println(num3);
 }
}

取模运算符

取模运算符

我们使用 (%) 运算符获取一个数的模。

class Main{

public static void main(String[] args){
   int num1 = 20;
   int num2 = 4;
   int num3 = num1 % num2;
   System.out.println(num3);
 }
}

递增运算符

递增运算符

我们使用 (++) 运算符将一个数加一。

class Main{

public static void main(String[] args){
   int num1 = 20;
   ++num1;
   // Does the same job as the above line
   num1 = num1 +1; 
   System.out.println(num3);
 }
}

递减运算符

递减运算符

我们使用 (-) 运算符将一个数减一。

int num1 = 20;

--num1;

赋值运算符

赋值运算符

现在让我们来看最常用的赋值运算符。

我们将使用的第一个运算符是 =,它可能是 Java 中使用最频繁的赋值运算符。

在下面的示例中,我们使用 = 运算符将值 0 赋给变量 count。

int count = 0;

附加赋值

让我们来看附加赋值运算符。

该运算符提供了一种执行加法的快捷方式。

int count = 0;
count += 10;
// It is an abbreviation of count = count + 10.

减法运算符

减法运算符

减法运算符提供了一种执行减法的快捷方式。

int count = 20;
count -= 10;
// It is an abbreviation of count = count - 10.

乘法运算符

乘法运算符

乘法运算符提供了一种执行乘法的快捷方式。

int count = 20;
count *= 10;

// It is an abbreviation of count = count * 10.

比较运算符

除此之外,我们还可以类似地使用语法,通过赋值运算符执行除法、取模等许多操作。

现在让我们来看比较运算符。

比较运算符用于比较 Java 中的值。

等于运算符

我们使用 == 运算符比较两个对象或基本值。

class Main{

public static void main(String[] args){
   int x = 5;
    int y = 3;
   boolean result = x == y;
   //false
 }
}

不等于运算符

使用 != 运算符可以检查不等于条件。

int x = 5;
int y = 3;

boolean result = x != y;
//true

大于和小于运算符

与数学中一样,我们使用 (>) 和 (<) 运算符对两个值进行代数比较。

int x = 5;
 int y = 3; 

//is x greater than y x > y 
//is x less than y x > y  
// is greater or equal than y x >= y  
// is less or equal than y x <= y

逻辑运算符

现在让我们来看逻辑运算符。

逻辑运算符用于在布尔类型之间进行逻辑比较。

int x = true;
int y = false;
int status;


// The AND (&&) operator returns true if both values are true.
// returns false for all cases other than that 
status = x && y;
// status will be false.

示例(续)

让我们继续看这个示例。


/ *
For the OR (||) operator, one of the two values 
compared to true is sufficient for the result to be true.
* /
status = x || y;
// status will be true.

// In the Logical Not operator, it gets the note equivalent of the logic value we have.
status = !(x || y)

/ *
Although the value inside the parentheses returned true, the not operator made the result false.
* /

status  = !(x && y;);

/ *
Although expression with the AND operator in the parenthesis 
returns false, the not operator ensures that the result is true.
* /

恭喜

恭喜您 🎉

在本节中,我们学习了 Java 中的运算符。 

下一课再见。 🥳

运算符 — 插图 19

常见问题解答

「运算符」课时是免费的吗?

是的 — 「运算符」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Java Academy 课程的其余内容,请升级到 CoddyKit PRO。 Java Academy 课程共包含 1 节课。

「运算符」这节课中我会学到什么?

运算符 你通过在浏览器中直接运行的动手代码来练习 Java Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Java Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Java Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 1 节。

「运算符」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Java Academy 课中编写并运行代码吗?

能。每节 Java Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

← 返回 Java Academy