Category Archives: Java platform

HOW DO YOU CLONE AN OBJECT?

How do you clone an object?

var obj = {a: 1 ,b: 2}
var objclone = Object.assign({},obj);
Now the value of objclone is {a: 1 ,b: 2} but points to a different object than obj.
Note the potential pitfall, though: Object.clone() will just do a shallow copy, not a deep copy. This means that nested objects aren’t copied. They still refer to the same nested objects as the original:
let obj = {
a: 1,
b: 2,
c: {
age: 30
}
};

var objclone = Object.assign({},obj);
console.log(‘objclone: ‘, objclone);

console.log(‘After Change – obj: ‘, obj); // 45 – This also changes
console.log(‘After Change – objclone: ‘, objclone); // 45

GROOVY PROGRAMMING LANGUAGE

Groovy Programming Language

The only thing constant in this world is change! In the 1990s, Java shook up the computing world by bringing managed runtime (virtual machines) to mainstream programming. Today, there are many new languages emerging to replace languages that are popular.

Do you know that there are more than two hundred languages that target the Java platform? Languages like Scala, Clojure, Groovy, and JRuby are few important languages that target the Java platform. Of these, of interest to us is Groovy since it is getting extensive attention and gaining steady popularity in the last few years. So explore this article and know more about Groovy.

MEET GROOVY

Groovy is an object-oriented programming language for the Java platform. It is a dynamic language with features similar to those of Python, Ruby, Perl, and Smalltalk. If you are familiar with languages like Java, C/C++, Python, Ruby, or JavaScript, then you will see many resemblances in Groovy. Groovy code is compiled to byte code that is executed by the Java Virtual Machine (JVM). Groovy programming language is always known for its simplicity and flexibility, as-well-as the performance and stability of the JVM. As Groovy is compiled to byte code that runs on the JVM Java Virtual Machine (JVM), most of the Java code is valid for Groovy and the standard Java libraries are available to Groovy programs.

The user can write Groovy code online and can execute it too quickly to play around with some snippets. Please notice, that the playground currently has some limitations, like System.out.println is not working.

HELLO WORLD

Groovy nearly is a superset of Java, which means most of the Java code is also valid in Groovy code. It just adds a lot of syntactic sugar on top of Java. Let’s see a short example.

System.out.println(“Hello World”);

This would be valid in Java and Groovy. Except that the user would need at least a class with a main method around it in Java to run. In Groovy user can just place this inside a file, execute it through the console and it will start working. But in Groovy we can shorten this line to:

println “Hello World”

VARIABLES

In contrast to Java, user can also use dynamic typing in Groovy. To define a variable, use the keyword “def”. That way Groovy will determine the type of the variable at runtime and the type might even change. For instance, the following snippet is valid for Groovy code:

def x = 42

println x.getClass()

x = “Hello World”

println x.getClass()

This script produces the following output:

class java.lang.Integer

class java.lang.String

The variable x changed its type during runtime. The user is still free to define x with a type like int.

STRINGS

Groovy has a String implementation called GString which allows the user to add variables into the String.

def x = “World”

println “Hello, $x”

This would produce the output Hello, World. The content of variable x is inserted into the string. If the user wants to use more difficult statements in the string, then the user need to add curly braces after the dollar sign.

IMPLICIT TRUTHY

Alike JavaScript Groovy evaluates every object to a Boolean value if required.

if(“foobar”) …

if(42) …

if(someObject) …

REGULAR EXPRESSIONS

Groovy has some syntactic sugar around regular expressions. The user can define a regular pattern class by placing the ~ in front of a string. Besides the already mentioned single quote and double quotes,there is a third method to define a string, which is primarily meant for regular expressions. The user can define a string inside two slashes /../. The main difference (in contrast to “””) is, that the user does not need to escape a backslash character, which they often need in regular expression patterns.

def pattern = ~/a slash must be escaped \/ but backslash, like in a digit match \d does not/
println pattern.getClass()