Week 6 · Objectives 第 6 周 · 学习目标 ← Back to Hub ← 返回主页

Operators & Expressions 运算符与表达式

Learning Objectives 学习目标

  • Construct Java expressions using arithmetic, relational, and logical operators.
  • Differentiate between integer division and floating-point divisions, and calculate modulo remainders.
  • Translate standard mathematical algebraic formulas into executable Java statements.
  • 使用算术、关系和逻辑运算符构建 Java 表达式。
  • 区分整数除法和浮点数除法,并计算取模余数。
  • 将标准数学代数公式转换为可执行的 Java 语句。
Bilingual Navigation:双语导航: Read and toggle languages using the globe button at the top. Test your understanding at the end of this page. 阅读并使用顶部的语言按钮切换中英文。在页面底部进行自我检测。

2 · Arithmetic Operators & Division Rules

Java provides standard arithmetic operators: addition (+), subtraction (-), multiplication (*), division (/), and modulo (%). However, integer division and the modulo operator follow special rules: Java 提供了标准的算术运算符:加法 (+)、减法 (-)、乘法 (*)、除法 (/) 和取模 (%)。但是,整数除法和取模运算符遵循特殊的规则:

Integer Division (/)整数除法 (/)

If both operands are integers, the fractional part is truncated.
Example: 17 / 5 evaluates to 3 (not 3.4). To get decimal results, one operand must be a double: 17.0 / 5 evaluates to 3.4.
如果两个操作数都是整数,小数部分将被截断。
示例:17 / 5 的结果为 3(而非 3.4)。要获得小数结果,至少一个操作数必须是 double:17.0 / 5 的结果为 3.4

Modulo Operator (%)取模运算符 (%)

Calculates the remainder of integer division.
Example: 14 % 3 evaluates to 2 because 3 goes into 14 four times (12) with a remainder of 2.
Example: 15 % 4 evaluates to 3.
计算整数除法的余数。
示例:14 % 3 的结果为 2,因为 14 除以 3 商为 4(积 12),余数为 2。
示例:15 % 4 的结果为 3

3 · Relational & Logical Operators

Relational operators compare values and return boolean (true/false) outcomes. Logical operators combine relational expressions: 关系运算符比较两个值并返回布尔(真/假)结果。逻辑运算符组合多个关系表达式:

Operator运算符 Meaning含义 Example Code代码示例 Evaluated Result评估结果
== Equal to等于 5 == 5 true
!= Not equal to不等于 5 != 3 true
&& Logical AND (both must be true)逻辑与(两者都必须为真) (5 > 3) && (2 < 1) false
|| Logical OR (at least one is true)逻辑或(至少有一个为真) (5 > 3) || (2 < 1) true
! Logical NOT (negates boolean value)逻辑非(否定布尔值) !(5 > 3) false

4 · Translating Mathematical Formulas to Java Code

In algebra, multiplication is implicit (e.g., ab). In Java, all operators must be explicit. Parentheses are used to group operators and enforce desired order of evaluation: 在代数中,乘法通常是隐式的(例如 ab)。在 Java 中,所有运算符都必须显式写出。括号用于对运算符进行分组并强制执行所需的计算顺序:

Standard Algebraic Expression标准代数公式 Equivalent Executable Java Code等效的可执行 Java 代码
\(y = ax^2 + bx + c\) y = a * x * x + b * x + c;
\(Average = rac{a+b+c}{3}\) average = (a + b + c) / 3.0;
\(Area = rac{1}{2} \cdot base \cdot height\) area = 0.5 * base * height;

Applied Case Study: Discount & Modulo Calculations应用案例研究:折扣与取模计算

Company: Kompas FinTech.
We write a utility check system to calculate values: applying discounts, and mapping odd transaction indices.
公司: Kompas FinTech。
我们编写一个工具系统来计算数值:应用折扣,并映射奇数交易序号。

Transactions Processor Source Code交易处理程序源代码

public class TransactionProcessor {{
    public static void main(String[] args) {{
        double rawAmount = 150.0;
        double discountRate = 0.15; // 15% discount

        double discount = rawAmount * discountRate;
        double finalAmount = rawAmount - discount;

        int txnId = 147;
        boolean isOddTxn = (txnId % 2 != 0); // Check for odd using modulo

        System.out.println("Raw Bill: RM " + rawAmount);
        System.out.println("Discount: RM " + discount);
        System.out.println("Final Bill: RM " + finalAmount);
        System.out.println("Transaction ID: " + txnId);
        System.out.println("Is odd transaction ID? " + isOddTxn);
    }}
}}
💡 Hint / 提示

Review variables operators operations. To determine odd values, check remainder of division by 2: odd numbers leave a remainder of 1 (value != 0).复习变量操作符的运行。要确定奇数值,请检查除以 2 的余数:奇数除以 2 余数为 1(值不为 0)。

✅ Show Expected Console Output / 显示预期控制台输出
Raw Bill: RM 150.0
Discount: RM 22.5
Final Bill: RM 127.5
Transaction ID: 147
Is odd transaction ID? true

Check Your Understanding自我检测

Q1. Evaluate the output of expression 14 % 3 in Java.评估 Java 表达式 14 % 3 的输出结果。

Q2. Evaluate the output of expression 17 / 5 in Java using integer arithmetic.使用整数算术评估 Java 表达式 17 / 5 的输出结果。

Q3. Which logical operator represents logical negation ('NOT') in Java?哪个逻辑运算符代表 Java 中的逻辑非('NOT')?

Q4. Translate standard formula \(y = 3x - 1\) to correct Java expression:将标准公式 \(y = 3x - 1\) 转换为正确的 Java 表达式: