Basic syntax and data types in Groovy
Groovy has a concise and expressive syntax that is designed to make it easy to write and maintain code. Here is a quick overview of the basic syntax and data types in Groovy:
Variables: Variables in Groovy are declared using the def keyword, followed by the variable name. For example:
pythonCopy codedef name = "John Doe"Groovy is a dynamically typed language, so the type of a variable is determined at runtime, not compile-time.
Strings: Strings in Groovy can be defined using single quotes ('') or double quotes (""). For example:
pythonCopy codedef message = "Hello, world!"Groovy provides several features to make working with strings easier, such as string interpolation, multiline strings, and the ability to call methods on strings directly.
Numbers: Groovy supports the standard numeric types (int, long, float, and double) as well as BigDecimal and BigInteger for precise decimal and integer arithmetic. For example:
pythonCopy codedef x = 42 def y = 3.14Booleans: Booleans in Groovy represent true or false values. For example:
javaCopy codedef isValid = trueLists: Lists in Groovy are ordered collections of values. Lists can contain elements of any type, and elements can be added or removed from a list dynamically. For example:
pythonCopy codedef names = ['John', 'Jane', 'Jim']Maps: Maps in Groovy are collections of key-value pairs. Maps can contain values of any type, and the keys and values can be added or removed dynamically. For example:
pythonCopy codedef person = [ name: 'John Doe', age: 42, address: '123 Main St' ]
These are just a few examples of the basic syntax and data types in Groovy. Groovy also provides support for arrays, ranges, regular expressions, and other data structures, making it a versatile language for a wide range of applications.
Leave a Comment