Show List

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:

  1. Variables: Variables in Groovy are declared using the def keyword, followed by the variable name. For example:

    python
    Copy code
    def name = "John Doe"

    Groovy is a dynamically typed language, so the type of a variable is determined at runtime, not compile-time.

  2. Strings: Strings in Groovy can be defined using single quotes ('') or double quotes (""). For example:

    python
    Copy code
    def 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.

  3. 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:

    python
    Copy code
    def x = 42 def y = 3.14
  4. Booleans: Booleans in Groovy represent true or false values. For example:

    java
    Copy code
    def isValid = true
  5. Lists: 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:

    python
    Copy code
    def names = ['John', 'Jane', 'Jim']
  6. 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:

    python
    Copy code
    def 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


  • captcha text