0Pricing
Groovy & Gradle: JVM Automation and Build Engineering · 课时

Groovy 语法与基本类型

了解 Groovy 简洁灵活的语法、动态类型、常见数据类型和运算符。

Groovy 语法与基本类型 是 CoddyKit 上的免费 Groovy & Gradle: JVM Automation and Build Engineering 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Groovy & Gradle: JVM Automation and Build Engineering 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Groovy & Gradle: JVM Automation and Build Engineering 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Groovy's Relaxed Style

Groovy's syntax is deliberately relaxed — you can skip a lot of what Java forces on you and write the same logic in far fewer lines.

Optional Semicolons & Parens

Two big relaxations: semicolons are optional, and parentheses around method arguments often are too. The code below shows both.

public class Main {
  // Define a simple static method for demonstration
  static void printMessage(String msg) {
    System.out.println(msg)
  }

  public static void main(String[] args) {
    // Semicolons are optional at the end of a line
    System.out.println("Hello from Groovy")
    System.out.println("CoddyKit makes learning fun") // Semicolon also optional

    // Parentheses for method calls with arguments can be omitted
    printMessage "Less typing, more coding!" // Calling without parentheses
    printMessage("It's concise!") // Calling with parentheses
  }
}

Dynamic `def` for Variables

Use def to declare a variable without naming its type — Groovy figures it out at runtime, and it can even hold different types.

public class Main {
  public static void main(String[] args) {
    def myVariable = "A string value"
    System.out.println("Value: " + myVariable + ", Type: " + myVariable.getClass().getName())

    myVariable = 12345
    System.out.println("Value: " + myVariable + ", Type: " + myVariable.getClass().getName())

    myVariable = true
    System.out.println("Value: " + myVariable + ", Type: " + myVariable.getClass().getName())
  }
}

Groovy's Smart Type Inference

Even without def, Groovy uses type inference — it reads the assigned value and picks the right type for you behind the scenes.

public class Main {
  public static void main(String[] args) {
    // Groovy infers 'productName' as a String
    productName = "Groovy Course"
    System.out.println("Product: " + productName + ", Type: " + productName.getClass().getName())

    // Groovy infers 'version' as an Integer
    version = 4
    System.out.println("Version: " + version + ", Type: " + version.getClass().getName())

    // Groovy infers 'price' as a Double
    price = 29.99
    System.out.println("Price: " + price + ", Type: " + price.getClass().getName())
  }
}

Numbers: Integers & Decimals

Groovy handles numbers naturally: Integer/Long for whole values, Double for decimals, plus BigInteger and BigDecimal for exact math.

public class Main {
  public static void main(String[] args) {
    def quantity = 150 // Inferred as Integer
    def temperature = -4.5 // Inferred as Double
    def largeId = 987654321098765L // Explicit Long using 'L' suffix
    def exactPi = 3.1415926535G // Explicit BigDecimal using 'G' suffix

    System.out.println("Quantity: " + quantity + " (Type: " + quantity.getClass().getName() + ")")
    System.out.println("Temperature: " + temperature + " (Type: " + temperature.getClass().getName() + ")")
    System.out.println("Large ID: " + largeId + " (Type: " + largeId.getClass().getName() + ")")
    System.out.println("Exact Pi: " + exactPi + " (Type: " + exactPi.getClass().getName() + ")")
  }
}

Strings: Text in Groovy

Single quotes give a plain String; double quotes give a GString that interpolates variables and expressions with $x or ${...}.

public class Main {
  public static void main(String[] args) {
    def user = "Alice"
    def itemPrice = 25

    // Single-quoted string: no interpolation
    def message1 = 'Hello, $user! Your item costs $itemPrice.'
    System.out.println("Literal string: " + message1)

    // Double-quoted string (GString): allows interpolation
    def message2 = "Hello, $user! Your item costs $itemPrice."
    System.out.println("Interpolated string: " + message2)

    // Interpolating an expression
    def total = itemPrice * 2
    def message3 = "Two items for $user cost ${itemPrice * 2} (or $total).
    System.out.println("Expression interpolation: " + message3)
  }
}

Basic Arithmetic Operators

All the usual arithmetic operators work. One twist: Groovy's / does decimal division by default, even on two integers.

public class Main {
  public static void main(String[] args) {
    def num1 = 20
    def num2 = 6

    System.out.println("Addition: " + (num1 + num2)) // 26
    System.out.println("Subtraction: " + (num1 - num2)) // 14
    System.out.println("Multiplication: " + (num1 * num2)) // 120
    System.out.println("Division: " + (num1 / num2)) // 3.3333333333333335 (decimal division)
    System.out.println("Modulo: " + (num1 % num2)) // 2 (remainder)
  }
}

Comparison & Logical Operators

Compare values with ==, !=, <, > and combine the booleans with the logical operators &&, ||, and !.

public class Main {
  public static void main(String[] args) {
    def val1 = 10
    def val2 = 20
    def val3 = 10

    System.out.println("val1 == val3: " + (val1 == val3)) // true
    System.out.println("val1 != val2: " + (val1 != val2)) // true
    System.out.println("val1 < val2: " + (val1 < val2)) // true

    def condition1 = (val1 < val2) // true
    def condition2 = (val1 == val3) // true

    System.out.println("condition1 && condition2: " + (condition1 && condition2)) // true
    System.out.println("condition1 || (val1 > val2): " + (condition1 || (val1 > val2))) // true
    System.out.println("!condition1: " + (!condition1)) // false
  }
}

Groovy Syntax & Types Check

Choose the TRUE statements about Groovy syntax and basic types:

Lesson Recap: Groovy Basics

You covered relaxed syntax, def and type inference, numbers and GStrings, plus the operators. The Groovy building blocks are yours now.

常见问题解答

「Groovy 语法与基本类型」课时是免费的吗?

是的 — 「Groovy 语法与基本类型」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Groovy & Gradle: JVM Automation and Build Engineering 课程的其余内容,请升级到 CoddyKit PRO。 Groovy & Gradle: JVM Automation and Build Engineering 课程共包含 4 节课。

「Groovy 语法与基本类型」这节课中我会学到什么?

了解 Groovy 简洁灵活的语法、动态类型、常见数据类型和运算符。 你通过在浏览器中直接运行的动手代码来练习 Groovy & Gradle: JVM Automation and Build Engineering,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Groovy & Gradle: JVM Automation and Build Engineering 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Groovy & Gradle: JVM Automation and Build Engineering 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「Groovy 语法与基本类型」课时需要多长时间?

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

我能在这节 Groovy & Gradle: JVM Automation and Build Engineering 课中编写并运行代码吗?

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

此课程中的所有课时

  1. Groovy 与 JVM 入门
  2. Groovy 语法与基本类型
  3. Groovy 脚本与 GShell
  4. Groovy 字符串与 GString
← 返回 Groovy & Gradle: JVM Automation and Build Engineering