Groovy 구문과 기본 타입
Groovy의 유연한 구문, 동적 타이핑, 일반적인 데이터 타입과 연산자를 이해합니다.
Groovy 구문과 기본 타입은(는) CoddyKit의 무료 Groovy & Gradle: JVM Automation and Build Engineering 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 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 구문과 기본 타입” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Groovy & Gradle: JVM Automation and Build Engineering 강의 전체를 잠금 해제할 수 있습니다. Groovy & Gradle: JVM Automation and Build Engineering 강의에는 총 4개의 강의가 포함되어 있습니다.
“Groovy 구문과 기본 타입”에서 뭘 배우나요?
Groovy의 유연한 구문, 동적 타이핑, 일반적인 데이터 타입과 연산자를 이해합니다. 브라우저에서 직접 실행하는 실습 코드로 Groovy & Gradle: JVM Automation and Build Engineering을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Groovy & Gradle: JVM Automation and Build Engineering을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Groovy & Gradle: JVM Automation and Build Engineering은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.
“Groovy 구문과 기본 타입” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Groovy & Gradle: JVM Automation and Build Engineering 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Groovy & Gradle: JVM Automation and Build Engineering 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- Groovy와 JVM 입문
- Groovy 구문과 기본 타입
- Groovy 스크립트와 GShell
- Groovy 문자열과 GString