Correct coding in groovy

ive looked over a few drivers but still unsure. when declaring variables you can use def but probably should use the correct object type, but some code ive seen people use float and others ive seen Float, ie the use of capitals
should you use capitals or lower case?

Bottom line, while in Java it makes a difference, in Groovy it does not. To make Groovy fully object-oriented, and because at the JVM level Java doesn’t support object-oriented operations such as method calls on primitive types, the Groovy designers decided to do away with primitive types. When Groovy needs to store values that would have used Java’s primitive types (e.g. float), Groovy uses the wrapper classes already provided by the Java platform (e.g. Float).

1 Like

This is, of course, accurate, but it would be fair to credit the source it was taken from verbatim: Groovy in Action (2nd ed.) by König, King, et. al, published by Manning in 2015.

Page 56 according to the book I found by my desk. :slight_smile:

3 Likes
5 Likes

but whats the difference between

int value = 10
Integer value = 10

int is the primitive, Integer is the wrapper class.

In the old days before auto-boxing there was a tremendous amount of difference. The primitive was very inflexible in terms of conversion or manipulation, but was faster and smaller in memory.

With auto boxing, they are largely interchangeable these days from an end user perspective (definitely true in Java, I assume true and groovy although I haven't really thought about it as I typically just use the wrapper classes anyway).

1 Like

all groovy scripts use wrapper classes

the compiler will convert but it is better to not make it do the work

2 Likes

Shows how little I truly know about the underpinnings of groovy... I'm a rank amateur.

What about writing to the dB, which is more efficient?

Ie
SendEvent...
Sendevent..
SendEvent...

Or

Cmd = []
Cmd << sendEvent....
Cmd << sendEvent...
Cmd<< sendEvent...
Return evt

Download the Hubitat app