Differences between Groovy and Java
In the previous post we saw some differences between Groovy and Java, let’s remember that Groovy is based on Java, Python, Smalltalk among others as I mentioned in the post. Well, some of the points that we must touch to remember would be the following.
Case |
Groovy | Java | Difference |
Variables | def variable def list = [] | String variable; ArrayList list = new ArrayList(); | In Java you always have to define a variable type, in Groovy it is not necessary to specify what type it is, nor are the ; except when 2 or more variables are declared on one line. |
Assigning values | variable = "Hello Goovy World" variable = 12 | variable = "Hello World"; variable = 12; // data type error | Groovy allows the flexibility of changing the data type at runtime, very useful but remember not to abuse, you can lose track of what it does your code. |
Print to console | println variable | System.out.println(variable); | Groovy reminds us a bit of the way that we printed our C/C++ codes saves us writing unnecessary stuff. |
Conditions | Groovy Truth (Groovy Truth) | boolean comparison; | In Java conditions only work with Boolean types (true/false) |
First steps with Groovy
I hope that this table, although it is not a lot of information, will help you remember what was in the previous post, however, it is important that you take into account some points from now on so that you begin to adopt a certain style in your programming. In my words I will try to explain to you what it is.
Before starting to program (Conventions)
Convention may refer to:
- The uses and customs
- An international treaty
- Code guidelines
Broadly speaking, as listed in the 2 previous points, the conventions are the customs that we have when programming (standards) or whatever they like to call it in their way of understanding it, they help our code to be more readable to the user. time to program but not only that, given that in development matters we will reach a point where I will begin to explain the development of Groovy on Grails for web applications, it is essential that you follow these simple instructions that I show you below.
Note: Later we will see in depth the importance of conventions and why use them, but if I recommend that you start using them from this Time for them to get used to them.
In Java it is very common to see these conventions, possibly we had not even noticed but let’s start
with the creation of libraries, the same ones that you add to your classes with the word import
.
Let’s imagine that we are participating in a project where multiple companies of development,
the conventions for the libraries that we create should bear the name of the page of our company.
Suppose that the following url www.mywebpage.com
the way we should organize our libraries (package) would be in the following way com.mywebpage.MyClassName
The convention is almost self-explanatory, it begins with the url of our company and from there the convention for classes and others begins, it would be something like the following.
ClassName
: The first letter of each word is capitalizedmethodName()
: Methods take the first lowercase and each word extra goes first in uppercasevariableName
: Same convention as methods
These are some simple but very useful recommendations that I remind you take into account I add an example.
class MyFirstClass {
def variable
def variableLarge
def largestVariable
static ExampleMethod(){
}
}
New operators and new features
Let’s remember that Groovy is a functional, dynamic, flexible language, described by some as almost “magical” now I will show you why, this is due to operators like these
?
(Null-Safe Object Navigation)
Checks that a reference is not null before calling a method on it. Goodbye NullPointerException
Example:
if (obj?.value != null) {
...
}
?:
(Elvis)
Is it an if...then...else
structure that replaces the ternary operator
<condition> ? <expression1> : <expression2>
Example: Java
String name = (person.getName() != null) ? person.getName() : "<unknown>"
Example: Groovy
String name = person.getName() ?: "<unknown>" // Groovy truth
*.
(The Spread-Dot Operator)
Use:
<collection>*.<property>
Example:
List names = people*.getName()
List names = people*.name
<=>
(Spaceship)
This operator returns a value of (1,0,-1) depending on whether it is greater(<), equal(=) or less(>) and is used to compare two objects with each other, just like the equals() method of the Object class that all objects have but I show them with an example:
Java example:
public int compare(int i1, int i2) {
if (i1 == i2) return 0;
else if (i1 < i2) return -1;
else return 1;
}
Example in Groovy:
int compare(int i1, int i2) {
i1 <=> i2
}
There is still more to come, but I think that this post is enough, I recommend you do some tests in the GroovyConsole, we will continue next time with the so-called closures of Groovy and how they are used and make our lives easier in a little way conventional, I leave you a screenshot to make the operation of these operators a little clearer.