Chapter 3. Simple Groovy datatypes


This chapter covers

  • Groovy’s approach to typing
  • Operators as method implementations
  • Strings, regular expressions, and numbers

Do not worry about your difficulties in mathematics. I can assure you mine are still greater.

Albert Einstein

Groovy supports a limited set of datatypes at the language level; that is, it offers constructs for literal declarations and specialized operators. This set contains the simple datatypes for strings, regular expressions, and numbers, as well as the collective datatypes for ranges, lists, and maps. This chapter covers simple datatypes; the next chapter introduces collective datatypes.

Before we go into details, we’ll talk about Groovy’s general approach to typing. With this in mind, you can appreciate Groovy’s approach of treating everything as an object and all operators as method calls. You’ll see how this improves the level of object orientation in the language compared to Java’s division between primitive types and reference types.

We then describe the natively supported datatypes individually. By the end of this chapter, you’ll be able to confidently work with Groovy’s simple datatypes and have a whole new understanding of what happens when you write 1+1.

3.1. Objects, objects everywhere

3.2. The concept of optional typing

3.3. Overriding operators

3.4. Working with strings

3.5. Working with regular expressions

3.6. Working with numbers

3.7. Summary