NOTIFICATION: These examples are provided for educational purposes. Using this code is under your own responsibility and risk. The code is given ‘as is’. I do not take responsibilities of how they are used.

Note: If you are not interested to know about the differences between Groovy and Java, skip to part 2.

Groovy is a dynamic language (which means many behaviors are executed at runtime instead of during compilation) based on the Java Virtual machine. This means that Groovy is tightly integrated with the Java language.

The biggest differences between Groove and Java are:

  1. The following packages and classes are imported by default (no need to use an explicit import to use them):
    • java.lang.*
    • java.math.BigDecimal
    • java.math.BigInteger
    • java.util.*
    • groovy.lang.*
    • groovy.util.*
  2. These are optionals:
    • Semicolons ‘;’ are optional
    • The keyword ‘return’ is optional
  3. The keyword ‘this’ can be used inside static methods.
  4. Since in Groovy there is no difference between checked and unchecked exceptions, the clause ‘Throw’ in a method signature is not checked
  5. There is no compile errors for using:
    • Undefined members
    • Passing Arguments of the wrong type
  6. The == operator and the method is()
    • The operator == will check for equality (checking if both object are equal) instead for identity (checking if both variables point the same object)
    • The method is() allows for checking for identity.
      • Example:
      • This doesn’t work on null. For checking null, you can still using == operator
  7. New features added to Groovy not available in Java:
    1. Closures: piece of code which is defined and then executed at a later; also known as “code block” or method pointer.
    2. Lists and maps  are native syntax which means that Groovy threads them as first class construct.
    3. Groovy provide native support for regular expressions
    4. Dynamic and static typing is supported
    5. Expressions can be embedded inside strings
    6. GPars (Groovy Parallel Systems) framework support for parallel processing. It supports:
      • Actors
      • Map and Reduce
      • Fork and Join
      • Dataflow
    7. GPath (Path Expression Language): Allows parts of nested structured data to be identified.
    8. GroovyMarkup: Native support for markup languages such as XML, HTML, W3C DOM, Ant Tasks, SAX, Swing, etc.
    9. Polymorphic interaction: Similar to STL standard iterators with these following differences:
      • Declarations wrapped in traversable template which makes the collection accessible
      • Constant iterator declaration is not scoped by the collection class. Instread the collection is passed in as a constructor argument.
    10. Switch statement:
      • In java, we can only use the int or int-derived type classifier.
      • In Groovy, we can any classifiers for a switch statement.
    11. Static and dynamic typing are supported.
      • If you wish to use a dynamic types variable you can use the keyword ‘def’
    12. Writing beans with simpler syntax for both properties and adding even listeners.
    13. Simplified way to check null which eliminate nasty nested ifs
    14. All variables are reference to objects. Groove doesn’t use primitive variables.
      • Even do you can write a primitive type as a short form, Groove will always translate it into the object.

Part 2 comming up soon…

© 2012, Alejandro G. Carlstein Ramos Mejia. All rights reserved.


Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.


Click to Insert Smiley

SmileBig SmileGrinLaughFrownBig FrownCryNeutralWinkKissRazzChicCoolAngryReally AngryConfusedQuestionThinkingPainShockYesNoLOLSillyBeautyLashesCuteShyBlushKissedIn LoveDroolGiggleSnickerHeh!SmirkWiltWeepIDKStruggleSide FrownDazedHypnotizedSweatEek!Roll EyesSarcasmDisdainSmugMoney MouthFoot in MouthShut MouthQuietShameBeat UpMeanEvil GrinGrit TeethShoutPissed OffReally PissedMad RazzDrunken RazzSickYawnSleepyDanceClapJumpHandshakeHigh FiveHug LeftHug RightKiss BlowKissingByeGo AwayCall MeOn the PhoneSecretMeetingWavingStopTime OutTalk to the HandLoserLyingDOH!Fingers CrossedWaitingSuspenseTremblePrayWorshipStarvingEatVictoryCurseAlienAngelClownCowboyCyclopsDevilDoctorFemale FighterMale FighterMohawkMusicNerdPartyPirateSkywalkerSnowmanSoldierVampireZombie KillerGhostSkeletonBunnyCatCat 2ChickChickenChicken 2CowCow 2DogDog 2DuckGoatHippoKoalaLionMonkeyMonkey 2MousePandaPigPig 2SheepSheep 2ReindeerSnailTigerTurtleBeerDrinkLiquorCoffeeCakePizzaWatermelonBowlPlateCanFemaleMaleHeartBroken HeartRoseDead RosePeaceYin YangUS FlagMoonStarSunCloudyRainThunderUmbrellaRainbowMusic NoteAirplaneCarIslandAnnouncebrbMailCellPhoneCameraFilmTVClockLampSearchCoinsComputerConsolePresentSoccerCloverPumpkinBombHammerKnifeHandcuffsPillPoopCigarette