Groovy For Java Programmers

Similar documents
Grails Seminar 11/12/09. Groovy And Grails. An Overview

Jussi Riihelä / Jussi Riihelä

Groovy A Language Introduction for Java Programmers

Groovy. Extending Java with scripting capabilities. Last updated: 10 July 2017

Scripting for the JVM using Groovy. Adil Khan Sr. Application Developer /Java Group Biomedical Informatics

Introduction to Groovy

Groovy & Grails Scripting for Modern Web Applications. Rohit Nayak Talentica Software

Groovy & Grails in Depth

Groovy, but without a cheesy presentation title...

Getting Groovy without the bad clothes by James Williams

A guide to learning the popular JVM programming language, Groovy 2.x, and its ecosystem

Groovy and Grails in Google App Engine

Beginning Groovy, Grails and Griffon. Vishal Layka Christopher M. Judd Joseph Faisal Nusairat Jim Shingler

Groovy Primer Chris Dail Groovy Primer (March 2010)

Closing the Case for Groovy (and Ruby, and Python)

Building Grails Applications with PostgreSQL. Brent Baxter and Ken Rimple PostgreSQL East - March 25, 2010

An Oracle White Paper March Introduction to Groovy Support in JDeveloper and Oracle ADF 11g

Notes from a Short Introductory Lecture on Scala (Based on Programming in Scala, 2nd Ed.)

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

SpringSource Tool Suite 2.7.1

1 Shyam sir JAVA Notes

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Groovy = Java Technology + Ruby + Python for the JVM

comparing groovy & jruby *

Chapter Goals. Chapter One: Introduction. Prerequisites. What Is Programming?

Object Model Comparisons

Getting Started with Java. Atul Prakash

CSE 341, Autumn 2015, Ruby Introduction Summary

Eclipse. JVM, main method and using Eclipse. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

LECTURE 2 (Gaya College of Engineering)

2/9/2012. Chapter One: Introduction. Chapter Goals

March 26, Groovy Magic How Java Got Its Groove On

Rules and syntax for inheritance. The boring stuff

Scala, Your Next Programming Language

Metaprogramming. Concepts of Programming Languages. Alexander Schramm. 2. November Institut für Softwaretechnik und Programmiersprachen

SpringSource Tool Suite M2

Overview of the Ruby Language. By Ron Haley

COMP 215: INTRO TO PROGRAM DESIGN. Prof. Chris Jermaine Chris Prof. Chris Dr. Chris

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

ICOM 4015: Advanced Programming

Client Usage. Client Usage. Assumptions. Usage. Hdfs.put( session ).file( localfile ).to( remotefile ).now() java -jar bin/shell.

Six Things Groovy Can Do For You

Grails Framework. Modern Web Applications written in Groovy CERN EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH. Eloy Reguero Fuentes.

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

CS 349 / SE 382 Scripting. Professor Michael Terry March 18, 2009

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

What readers are saying about Programming Groovy

Groovy Scripting for Java

Lecture 02, Fall 2018 Friday September 7

They are a block of code plus the bindings to the environment they came from (Ragusa Idiom / function object).

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Tutorial 1 CSC 201. Java Programming Concepts عؾادئماظربجمةمبادؿكدامماجلاصا

Introduction to Computers and Java

Chapter 8 Objects and Classes Dr. Essam Halim Date: Page 1

Life Without NetBeans

1.00 Tutorial 3. Methods, Classes Arrays & ArrayLists. September 26 & 27, 2005

SLIM and the future of FitNesse. Gojko Adzic

CS 231 Data Structures and Algorithms, Fall 2016

Scala : an LLVM-targeted Scala compiler

Simplify Enterprise Development With Scripting

Simple Java Programs. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

The Script Bowl Featuring Groovy, JRuby, Jython and Scala. Raghavan Rags N. Srinivas CTO, Technology Evangelism

This tutorial explains how you can use Gradle as a build automation tool for Java as well as Groovy projects.

How to make a "hello world" program in Java with Eclipse *

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

SpringSource Tool Suites M3

SDKs - Eclipse. SENG 403, Tutorial 2

Andy Clement, SpringSource/VMware SpringSource, A division of VMware. All rights reserved

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

SpringSource Tool Suites 3.0.0

Your First Ruby Script

CSE 341: Programming Languages

Date & Time Handling In JAVA

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Java Bytecode (binary file)

Character Stream : It provides a convenient means for handling input and output of characters.

A- Core Java Audience Prerequisites Approach Objectives 1. Introduction

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

Chapter 4 Java Language Fundamentals

Selected Questions from by Nageshwara Rao

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

CS111: PROGRAMMING LANGUAGE II

Lecture 1. basic Python programs, defining functions

Dynalink. Dynamic Linker Framework for Languages on the JVM. Attila Szegedi, Software Engineer, Twitter

Topic 6: Inner Classes

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Class, Variable, Constructor, Object, Method Questions

TypeScript. Types. CS144: Web Applications

Programming Language Concepts: Lecture 2

Why add Groovy to Java?

Assumptions. History

Lecture Set 2: Starting Java

S AMPLE CHAPTER. Kenneth A. Kousen. FOREWORD BY Guillaume Laforge MANNING

Chapter 2: Programming Concepts

Computer Programming

Lecture Set 2: Starting Java

Building Java Programs

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Project 1. Java Control Structures 1/17/2014. Project 1 and Java Intro. Project 1 (2) To familiarize with

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

Transcription:

Groovy For Java Programmers QCONSF 2010 Jeff Brown Core Grails Developer jeff.brown@springsource.com SpringSource - A Division Of VMware http://springsource.com/ Copyright 2010 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

What Is Groovy? Agile Dynamic Language For The JVM Inspired By Languages Such As... Python Ruby Smalltalk 2

Why Groovy Powerful Dynamic Language Relatively Easy To Learn Familiar Syntax For Java Programmers Integrates Really Well With Java Containers, Libraries, Existing Java Code 3

Installing Groovy Download Latest Release http://groovy.codehaus.org/ Extract Archive Set $GROOVY_HOME Add $GROOVY_HOME/bin to PATH 4

Groovy Tools groovy - Interpreter groovyc - Compiler groovysh - Shell groovyconsole - Swing Console 5

Give It A Spin $ groovy -version Groovy Version: 1.5.4 JVM: 1.5.0_13-119 $ groovy -e "println 'Groovy Rocks.'" Groovy Rocks. $ groovy -e "x=5; y=10; z=x*y; println z" 50 6

groovysh 7

groovyconsole 8

Groovy Class more on Groovy properties later... 9

Groovy Scripts Scripts Do Not Require A Class Definition no main method 10

Print Independence Day // PrintIndependenceDay.java import java.util.calendar; import java.util.date; public class PrintIndependenceDay { public static void main(string[] args) { Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.set(calendar.month, Calendar.JULY); calendar.set(calendar.date, 4); calendar.set(calendar.year, 1776); Date time = calendar.gettime(); } } System.out.println(time); 11

Print Independence Day // PrintIndependenceDay.groovy import java.util.calendar; import java.util.date; public class PrintIndependenceDay { public static void main(string[] args) { Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.set(calendar.month, Calendar.JULY); calendar.set(calendar.date, 4); calendar.set(calendar.year, 1776); Date time = calendar.gettime(); } } System.out.println(time); 12

No Utility Imports... // PrintIndependenceDay.groovy public class PrintIndependenceDay { public static void main(string[] args) { Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.set(calendar.month, Calendar.JULY); calendar.set(calendar.date, 4); calendar.set(calendar.year, 1776); Date time = calendar.gettime(); } } System.out.println(time); 13

No Semicolons... // PrintIndependenceDay.groovy public class PrintIndependenceDay { public static void main(string[] args) { Calendar calendar = Calendar.getInstance() calendar.clear() calendar.set(calendar.month, Calendar.JULY) calendar.set(calendar.date, 4) calendar.set(calendar.year, 1776) Date time = calendar.gettime() } } System.out.println(time) 14

No Getters... // PrintIndependenceDay.groovy public class PrintIndependenceDay { public static void main(string[] args) { Calendar calendar = Calendar.instance calendar.clear() calendar.set(calendar.month, Calendar.JULY) calendar.set(calendar.date, 4) calendar.set(calendar.year, 1776) Date time = calendar.time } } System.out.println(time) 15

No Static Typing... // PrintIndependenceDay.groovy public class PrintIndependenceDay { public static void main(string[] args) { def calendar = Calendar.instance calendar.clear() calendar.set(calendar.month, Calendar.JULY) calendar.set(calendar.date, 4) calendar.set(calendar.year, 1776) def time = calendar.time } } System.out.println(time) 16

No System.out.blah.blah... // PrintIndependenceDay.groovy public class PrintIndependenceDay { public static void main(string[] args) { def calendar = Calendar.instance calendar.clear() calendar.set(calendar.month, Calendar.JULY) calendar.set(calendar.date, 4) calendar.set(calendar.year, 1776) def time = calendar.time } } println(time) 17

No Class... // PrintIndependenceDay.groovy def calendar = Calendar.instance calendar.clear() calendar.set(calendar.month, Calendar.JULY) calendar.set(calendar.date, 4) calendar.set(calendar.year, 1776) def time = calendar.time println(time) 18

Optional Parens... // PrintIndependenceDay.groovy def calendar = Calendar.instance calendar.clear() calendar.set Calendar.MONTH, Calendar.JULY calendar.set Calendar.DATE, 4 calendar.set Calendar.YEAR, 1776 def time = calendar.time println time 19

Lets Go Meta... // PrintIndependenceDay.groovy def calendar = Calendar.instance calendar.with { clear() set MONTH, JULY set DATE, 4 set YEAR, 1776 println time } 20

Lets Compare... // PrintIndependenceDay.java import java.util.calendar; import java.util.date; public class PrintIndependenceDay { public static void main(string[] args) { Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.set(calendar.month, Calendar.JULY); calendar.set(calendar.date, 4); calendar.set(calendar.year, 1776); Date time = calendar.gettime(); // PrintIndependenceDay.groovy def calendar = Calendar.instance calendar.with { clear() set MONTH, JULY set DATE, 4 set YEAR, 1776 println time } } } System.out.println(time); 21

Everything Is An Object 22

Groovy Strings Single quoted Strings are java.lang.string Double quoted Strings are "GStrings" may contain embedded Groovy code 23

Groovy Strings Strings May Be Referenced Using [ ] 24

Groovy Collections Groovy Collections Are Standard java.util.collections Groovy Adds Many Useful Methods To Existing Collections Many Common Tasks Are Much More Simple In Groovy Compared To Java 25

Groovy List 26

Groovy Maps 27

Groovy Beans Groovy Beans / POGOs Similar To POJOs...but groovier eliminates boilerplate code 28

POJO 29

POJO 30

POJO Modern Java IDEs Generate Most Of That Code developer declares fields IDE generates constructors IDE generates getters/setters If the IDE can generate all of that code, why can't the compiler or the 31

Groovy Beans Groovy Beans Eliminate All Of The Boilerplate Code No Need To Write Getters/Setters Seldom Need To Write Constructors 32

Groovy Beans 33

Groovy Beans Property Access Looks Like Field Access 34

Groovy Beans 35

Closures A Block Of Code May Be Passed As Arguments May Accept Parameters May Return A Value Much More Powerful Than Anonymous Inner Classes 36

Closures Groovy Adds A 'times' Method To Number The 'times' Method Accepts A Closure As An Argument 37

Closures 38

Closures Closures May Declare An Argument List the times method is passing an argument into the closure 39

Closures The Implicit 'it' Argument 40

Closures Closures May Accept Multiple Arguments 41

Closures Closures Simplify Collection Iteration 42

Closures 43

Closures 44

Builders Builders Are A Powerful Concept Metaprogramming Makes Builders A Snap In Groovy Several Builders Are Bundled With Groovy SwingBuilder, MarkupBuilder, etc... You Can Write Your Own 45

MarkupBuilder 46

Q & A 47