
- 496 pages
- English
- ePUB (mobile friendly)
- Available on iOS & Android
eBook - ePub
About this book
Groovy Programming is an introduction to the Java-based scripting language Groovy. Groovy has much in common with popular scripting languages such as Perl, Python, and Ruby, but is written in a Java-like syntax. And, unlike these other languages, Groovy is sanctioned by the Java community for use on the Java platform. Since it is based on Java, applications written in Groovy can make full use of the Java Application Programmer Interfaces (APIs). This means Groovy can integrate seamlessly with applications written in Java, while avoiding the complexities of the full Java language. This bare-bones structure also means Groovy can be used as an introduction to Java and to programming in general. Its simpler constructions and modern origins make it ideal as a first language and for introducing principles such as object-oriented programming.This book introduces all the major aspects of Groovy development and emphasizes Groovy's potential as a learning tool. Case studies and exercises are included, along with numerous programming examples. The book begins assuming only a general familiarity with Java programming, and progresses to discuss advanced topics such as GUI builders, Groovlets, Unit Testing, and Groovy SQL.
- The first comprehensive book on Groovy programming that shows how writing applications and scripts for the Java platform is fast and easy
- Written by leading software engineers and acclaimed computing instructors
- Offers numerous programming examples, code samples, detailed case studies, exercises for self-study, and a companion website with a Windows-based Groovy editor
Frequently asked questions
Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Perlego offers two plans: Essential and Complete
- Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
- Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes! You can use the Perlego app on both iOS or Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Yes, you can access Groovy Programming by Kenneth Barclay,John Savage in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming Languages. We have over one million books available in our catalogue for you to explore.
Information
CHAPTER 1 GROOVY
This first chapter introduces Groovy as a unique scripting language designed to augment the Java platform. It offers Javalike syntax, native support for Maps and Lists, methods, classes, closures, and builders. With its dynamic weak typing and seamless access to the Java Applications Programming Interface (API), it is well suited to the development of many small- to medium-sized applications.
1.1 WHY SCRIPTING?
Generally, scripting languages such as Groovy are more expressive and operate at higher levels of abstraction than systems programming languages such as Java. This often results in more rapid application development and higher programmer productivity. However, scripting languages serve a different purpose than their systems language counterparts. They are designed for “gluing” applications together rather than implementing complex data structures and algorithms. Therefore, to be useful, a scripting language must have access to a wide range of components.
In general, scripting languages do not replace systems programming languages. They complement them (Ousterhout, 1998). Typically, systems programming languages should be used in applications that
- require the development of complex algorithms or data structures
- are computationally intensive
- manipulate large datasets
- implement well-defined, slowly changing requirements
- are part of a large project.
However, scripting languages should be used for applications that
- connect preexisting components
- manipulate a variety of different entities that change rapidly
- have a graphical user interface
- have rapidly evolving functionality
- are part of a small- to medium-sized project.
A major strength of scripting languages is that the coding effort they require is relatively small as compared to code written in a systems programming language. Often, the latter appears to be overly complex and difficult to understand and maintain. This is because it requires extensive boilerplate or conversion code.
These systems languages are strongly typed to ensure the safety and robustness of the code. With strong typing, variables must been given a type and they can only be used in a particular way. Although strong typing makes large programs more manageable and allows a compiler to (statically) detect certain kinds of errors, it can be intrusive. For example, strong typing is not helpful when it is difficult or impossible to decide beforehand which type of a variable it is. This situation occurs frequently when connecting components together.
To simplify the task of connecting components, scripting languages are weakly typed. This means that variables can be used in different ways under different circumstances. However, illegal use of variables is only detected when the code is actually executing. For example, although Groovy (statically) checks program syntax at compile time, the (dynamic) check on the correctness of method calls happens at runtime. As a result, there is the danger that a Groovy script that compiles cleanly may throw an exception and terminate prematurely.
Weak typing does not necessarily mean that code is unsafe or that it is not robust. Advocates have promoted Extreme Programming (Beck, 2004) as a software development process. This approach is characterized by an emphasis on testing. The result is a comprehensive suite of unit tests (Link, 2003) that drive the development. As a consequence, they help ensure the safety and robustness of the code by executing it in a wide variety of different scenarios. This is the basis of the approach we take when developing Groovy scripts. In fact, experience has shown that the combination of weak typing and unit testing in a scripting language is often better than strong type checking in a traditional systems programming language (see http://www.mindview.net/WebLog/log-0025). We have both the flexibility of weak typing and the confidence of unit testing.
1.2 WHY GROOVY?
The Java compiler produces bytecodes that execute on the Java Virtual Machine (JVM). Groovy classes are binary compatible with Java. This means that the bytecodes produced by the Groovy compiler are the same as those produced by the Java compiler. Hence, Groovy is Java as far as the JVM is concerned. This means that Groovy is able to immediately exploit the various Java APIs such as JDBC for database development (Fisher et al., 2003) and Swing for developing GUI applications (Topley, 1998).
Groovy aims to shift much of the “heavy lifting” from the developer to the language itself. For example, when adding a button to a GUI, we simply specify the code to execute when the button is pressed. There is no need to add an event handler to the button as an instance of a class implementing a particular interface. Groovy does this for us.
Groovy is an object-oriented scripting language in which everything is an object. Unlike Java, there are no exceptions to this rule. This brings an important element of uniformity to the language. Groovy is also dynamically typed so that the notion of a type lies within the object, not the variable that references it. An immediate consequence is that Groovy does not require the declaration of the type of a variable, method parameter, or method return value. This gives it the beneficial effects of significantly shrinking the code size and giving the programmer the freedom to defer type decisions to runtime.
Groovy also seeks to unify instance fields and methods declared in classes by supporting the concept of a property. A property removes the distinction between an instance field (attribute) and a method. In effect, a client considers a property as the combination of the instance field and its getter/setter methods.
Important data structures, Lists and Maps, are native to the Groovy language. A List object or a Map object can be directly expressed in a Groovy script. For novice developers and professionals alike, the immediacy of Lists and Maps can make their programming tasks that much simpler. Complementing Lists and Maps are iterator methods, such as each, that simplify how the elements in these collections are to be processed. The processing itself is described by a closure—an object that represents a code block. This immensely useful construct can be referenced by variables, parameterized to generalize its applicability, passed as a parameter to methods and other closures, and can be an instance field of classes. It has a huge effect on programming in Groovy.
Hierarchical data structures like XML can also be directly represented in a Groovy script with Groovy builders. Using notations found in XPath (see http://www.w3.org/TR/xpath20/), Groovy readily expresses the traversal of these structures and how to reference their parts. Once again, an iterator and a closure provide the mechanism to process them.
Groovy builders are generally applicable to any nested tree-structure. For example, they can be used to describe a graphical application that is assembled from various component widgets. Here, too, closures play a part, this time operating as event handlers for components such as menu items and buttons. Standard Query Language (SQL) processing also has the same uniform approach. Again, an iterator method such as eachRow combines with a closure to express how to process the rows of a database table.
CHAPTER 2 NUMBERS AND EXPRESSIONS
In this chapter, we are concerned with how we manipulate basic numeric values in Groovy. When doing so, we must be especially conscious that Groovy has been constructed as an object-oriented language. This means that everything in Groovy is ultimately an object—an instance of some class. For example, we are all familiar with the integer value 123. In Groovy, this is actually an object instance of the class Integer. To make an object do something, we know that we must invoke one of the methods declared in its class. Hence, to obtain the absolute value of such an integer, the Groovy environment invokes the method abs with the expression 123. abs (). Equally, to ask 123 for the value that follows it (124), the Groovy environment calls the successor method, next, as in 123. next ().
Because of this, if we wish to find the arithmetic sum of the values 123 and 456, then we might expect the Groovy environment to invoke the + method on the Integer object 123 as in 123.+ (456). The Integer object 456 is the method parameter. This, of course, is hopelessly counterintuitive to the arithmetic skills we developed at school. Fortunately, Groovy also supports operator overloading (see Appendix I). This way, the + method can be presented as a binary operator between its operands, resulting in the more natural expression 123 + 456. However, we should always be prepared to recollect that this is ultimately a method call to one object with the other object as a method parameter. In truth, for this example, the actual method call used by Groovy is entitled plus as in 123.plus(456).
This chapter deals with the manipulation of arithmetic values in a relatively straightforward manner. However, it is an important field of study in its own right, and we present a more detailed discussion in Appendix C.
2.1 NUMBERS
Groovy supports integer and floating point numbers (literals). An integer is a value that does not include a f...
Table of contents
- Cover
- Title Page
- Dedication
- Copyright
- Foreword
- Preface
- About the authors
- Table of Contents
- Chapter 1: Groovy
- Chapter 2: Numbers and Expressions
- Chapter 3: Strings and Regular Expressions
- Chapter 4: Lists, Maps, and Ranges
- Chapter 5: Simple Input and Output
- Chapter 6: Case study
- Chapter 7: Methods
- Chapter 8: Flow of Control
- Chapter 9: Closures
- Chapter 10: Files
- Chapter 11: Case study
- Chapter 12: Classes
- Chapter 13: Case study
- Chapter 14: Inheritance
- Chapter 15: Unit testing (junit)
- Chapter 16: Case study
- Chapter 17: Persistence
- Chapter 18: Case study
- Chapter 19: XML Builders and Parsers
- Chapter 20: GUI Builders
- Chapter 21: Template Engines
- Chapter 22: Case study
- Chapter 23: Server-side Programming
- Chapter 24: Case study
- Chapter 25: Epilogue
- Software Distribution
- Groovy
- More on Numbers and Expressions
- More on Strings and Regular Expressions
- More on Lists, Maps, and Ranges
- More on Simple Input and Output
- More on Methods
- More on Closures
- More on Classes
- Advanced Closures
- More on Builders
- More on GUI Builders
- Bibliography
- Index
- Instructions for online access