Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7945843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:59:17+00:00 2026-06-04T00:59:17+00:00

I have the following Groovy code snippet that is attempting to use operator overloading

  • 0

I have the following Groovy code snippet that is attempting to use operator overloading for increment, decrement and equals. All this does is create two instances, perform the increment and a decrement one one of the instances and then compare the two instances using the overloaded methods equals. When I do the comparison it fails. Both should be 100 when this code completes. (the print statements show it, but one of the toString() functions appear to be wrong). What am I doing wrong here?

This is with groovy 1.8.6

class Overload {
    def value

    public Overload(value = 0) {
        this.value = value
    }

    def next() {
        value = value + 1;
    }

    def previous() {
        value = value - 1;
    }

    def boolean equals(other) {
        if (value == other?.value) {
            return true
        }

        return false
    }

    def String toString() {
        "value is = ${value}"
    }
}

def cls1 = new Overload(100)
def cls2 = new Overload(100)

cls1++
cls1--
if (cls1 == cls2) {
    println("cls1 == cls2")
}
else {
    println("cls1 != cls2")
}

println(cls1.toString())
println(cls2.toString())

Output:

cls1 != cls2
100
value is = 100
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:59:18+00:00Added an answer on June 4, 2026 at 12:59 am

    The problem is the increment and decrement methods of the Overload instance.

    Groovy has the feature of implicit return for the last expression evaluated. When you call to cls1++, now the object is an Integer, that’s the reason we not see the output from the overrided toStringmethod.

    def next() {
        value = value + 1
        return this
    }
    
    def previous() {
        value = value - 1
        return this
    }
    

    Check now:

    assert cls1.class == Overload
    assert cls2.class == Overload
    assert cls1 == cls2
    assert cls1 == 100
    assert cls2 == 100
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in Groovy 1.8.5 and to me it looks valid.
I have the following code: InputStream reportFile = MyPage.this.getClass().getResourceAsStream(test.jrxml); HashMap<String, String> parameters = new
I have the following piece of code in ItemController.groovy def list = { params.max
I have the following Groovy code but it wont work: xml = new groovy.xml.MarkupBuilder()
I have the following Groovy code : def number = 246 as List def
I have run the following code in this page RsyntaxTextArea using Java and i
Hi i have the following Groovy code: package fp; abstract class Function { public
Lets assume that I have the following configuration in my conf/InjectionConfig.groovy file: x {
I have the following snippet that loads the images: String imgName = /assets/ +
Hi I have the following Code Snippet; class StringCalci { static def plus(Integer self,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.