Fundamentals 7 min read

Demonstrating Operator Overloading in Groovy with Java-Compatible Code

This article demonstrates Groovy's operator overloading capabilities through Java-compatible code examples, including arithmetic, shift, and increment operators, shows console output, integrates a performance‑testing thread multiplication demo, and highlights Groovy's ability to overload even the dot operator.

FunTester
FunTester
FunTester
Demonstrating Operator Overloading in Groovy with Java-Compatible Code
Overloading is fun at first, and staying overloaded is always fun.

Recently reading Groovy in Action and revisiting some Groovy syntax features, I fell in love with the operator overloading feature; it is incredibly enjoyable, so I share a demo.

Because Groovy syntax differs slightly from Java but is essentially fully compatible, this demo is written using Java‑style syntax for easier understanding.

package com.FunTester.demo

import com.fun.frame.SourceCode

class demo1 extends SourceCode {
    public static void main(String[] args) {
        def s = "fun" * 3 << "fan"

        println s

        Demo demo = new Demo()
        Demo a = new Demo()

        Demo demo1 = demo + a

        Demo sssss = demo + 3

        Demo fsa = demo * 3

        Demo demo2 = demo / 9

        Demo demo3 = demo << a

        Demo demo4 = demo >> a

        Demo demo5 = demo++

        def i = 2 >>> 1
    }

    static class Demo {
        def plus(Demo demo) {
            output("加法对象")
            this
        }

        def plus(int s) {
            output("加法")
            this
        }

        def multiply(int a) {
            output("乘法")
            this
        }

        def div(int a) {
            output("除法")
            this
        }

        def leftShift(Demo demo) {
            output("<<操作")
            this
        }

        def rightShift(Demo demo) {
            output(">>操作")
            this
        }

        def next() {
            output("++操作")
            this
        }
    }
}

Console output

Surprised? Unexpected!

Below, combined with the performance‑testing framework's thread class, write a small demo:

RequestThreadTimes requestThreadTimes = new RequestThreadTimes(FanLibrary.getHttpGet(""), 100);
List
threads = requestThreadTimes * 100;
new Concurrent(threads).start();

Multiplication overload is as follows:

/**
 * 乘法
 *
 * @param i
 * @return
 */
public List
multiply(int i) {
    ArrayList
threads = new ArrayList<>(i);
    i.times {
        threads << this.clone();
    }
    threads
}

Ha, ha, ha!

Another big secret: Groovy can even overload the "." operator.

Statement: The article was first published on the public account “FunTester”; third‑party (except Tencent Cloud) re‑publication is prohibited.

Technical Article Selection

Java one‑line code to print a heart shape

Linux performance monitoring tool netdata Chinese version

Performance testing framework, second edition

How to happily conduct performance testing on Linux CLI

HTTP mind map illustration

Automatically convert Swagger docs into test code

Build a static blog with five lines of code

Exploring a linear Java interface testing framework

Selenium 4.0 Alpha changelog

Selenium 4.0 Alpha practical guide

Unified interface testing: functional, automation, and performance cases

Non‑Technical Article Selection

Why choose software testing as a career?

Programming mindset for everyone

Seven skills to become an automation testing engineer

How to introduce automated testing into DevOps

Common reasons for Web automation test failures

Common excuses used by testers

2019 Browser market share ranking

API testing basics

API automation testing guide

The future of QA engineers

javaperformance testingGroovyoperator overloadingDemo
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.