In this section, we will cover the fundamental concepts of Groovy programming. By the end of this module, you should have a solid understanding of the basic syntax and features of Groovy, which will serve as the foundation for more advanced topics.
Key Concepts
- Groovy Shell (groovysh)
- Basic Syntax
- Comments
- Data Types
- Variables
- Basic Input/Output
- Groovy Shell (groovysh)
The Groovy Shell, or groovysh, is an interactive command-line shell for evaluating Groovy expressions, defining classes, and running scripts. It is a great tool for experimenting with Groovy code.
Example:
$ groovysh
Groovy Shell (2.5.14, JVM: 1.8.0_292)
Type ':help' or ':h' for help.
groovy:000> println 'Hello, Groovy!'
Hello, Groovy!
- Basic Syntax
Groovy syntax is similar to Java but with some enhancements and simplifications. Here are some basic syntax rules:
- Semicolons: Optional at the end of statements.
- Parentheses: Optional in method calls if there is no ambiguity.
- Dynamic Typing: Variables can be defined without specifying their type.
Example:
println 'Hello, Groovy!' // No semicolon needed
def name = 'John' // Dynamic typing
println "Hello, $name" // String interpolation
- Comments
Comments in Groovy are similar to those in Java.
- Single-line comments: Use
// - Multi-line comments: Use
/* ... */ - Documentation comments: Use
/** ... */
Example:
// This is a single-line comment
/*
This is a multi-line comment
spanning multiple lines
*/
/**
* This is a documentation comment
* used for generating API docs
*/
- Data Types
Groovy supports various data types, including:
- Primitive types:
int,float,double,char,boolean - Wrapper types:
Integer,Float,Double,Character,Boolean - String:
String - Collections:
List,Map,Set
Example:
int age = 30
double price = 19.99
boolean isActive = true
String greeting = "Hello, Groovy!"
List<Integer> numbers = [1, 2, 3, 4, 5]
Map<String, String> user = [name: 'John', city: 'New York']
- Variables
Variables in Groovy can be declared using the def keyword or by specifying the type explicitly.
Example:
- Basic Input/Output
Groovy provides simple ways to handle input and output.
Example:
// Output
println 'Enter your name:'
// Input
def name = System.console().readLine()
println "Hello, $name!"Practical Exercises
Exercise 1: Hello World
Write a Groovy script that prints "Hello, World!" to the console.
Solution:
Exercise 2: User Input
Write a Groovy script that asks the user for their name and age, then prints a greeting message.
Solution:
println 'Enter your name:'
def name = System.console().readLine()
println 'Enter your age:'
def age = System.console().readLine().toInteger()
println "Hello, $name! You are $age years old."Exercise 3: Data Types
Create variables of different data types and print their values.
Solution:
int age = 25
double salary = 50000.50
boolean isEmployed = true
String name = 'Alice'
List<String> hobbies = ['Reading', 'Traveling', 'Cooking']
Map<String, String> address = [city: 'New York', country: 'USA']
println "Name: $name"
println "Age: $age"
println "Salary: $salary"
println "Employed: $isEmployed"
println "Hobbies: $hobbies"
println "Address: $address"Common Mistakes and Tips
- Forgetting to use
deffor dynamic variables: Always usedefwhen you don't specify a type. - String interpolation: Use double quotes (
") for strings that include variables. - Input handling: Remember to convert input strings to the appropriate data type.
Conclusion
In this section, we covered the basics of Groovy, including its syntax, data types, variables, and basic input/output operations. These fundamentals are crucial for understanding more advanced topics in Groovy. In the next module, we will delve deeper into Groovy's syntax and language features.
