Groovy Söz Dizimi ve Temel Türler
Groovy'nin esnek söz dizimini, dinamik türlemeyi, yaygın veri türlerini ve işleçlerini anlayın.
Groovy Söz Dizimi ve Temel Türler, CoddyKit'te ücretsiz bir Groovy & Gradle: JVM Automation and Build Engineering dersidir. Bu, 4 dersinin 2. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Groovy & Gradle: JVM Automation and Build Engineering öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Groovy & Gradle: JVM Automation and Build Engineering kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
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.
Sıkça Sorulan Sorular
“Groovy Söz Dizimi ve Temel Türler” dersi ücretsiz mi?
Evet — “Groovy Söz Dizimi ve Temel Türler” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Groovy & Gradle: JVM Automation and Build Engineering kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Groovy & Gradle: JVM Automation and Build Engineering kursu toplamda 4 dersten oluşur.
“Groovy Söz Dizimi ve Temel Türler” dersinde ne öğreneceğim?
Groovy'nin esnek söz dizimini, dinamik türlemeyi, yaygın veri türlerini ve işleçlerini anlayın. Groovy & Gradle: JVM Automation and Build Engineering ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Groovy & Gradle: JVM Automation and Build Engineering öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Groovy & Gradle: JVM Automation and Build Engineering, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 2. dersidir.
“Groovy Söz Dizimi ve Temel Türler” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Groovy & Gradle: JVM Automation and Build Engineering dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Groovy & Gradle: JVM Automation and Build Engineering dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Groovy ve JVM'e Giriş
- Groovy Söz Dizimi ve Temel Türler
- Groovy Betikleri ve GShell
- Groovy Dizeleri ve GString'ler