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 754407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:00:34+00:00 2026-05-14T15:00:34+00:00

For this example Java class: package foo; public class TestInterop { public String test(int

  • 0

For this example Java class:

package foo;
public class TestInterop
{   public String test(int i)
    { return "Test(int)"; }

    public String test(Object i)
    { return "Test(Object)"; }
}

When I start Clojure and try to call the test(int) method, the test(Object) method is called instead, because Clojure automatically boxes the integer into a java.lang.Integer object.

How do I force Clojure to call the test(int) method?

user=> (.test (new foo.TestInterop) 10)
"Test(Object)"

I want to call methods like Component.add(Component comp, int index) in AWT, but instead keep calling add(Component comp, Object constraints), so the buttons on my toolbar always appear in the wrong order.

  • 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-05-14T15:00:35+00:00Added an answer on May 14, 2026 at 3:00 pm

    A discussion is going on in the #clojure channel on Freenode just now on this very topic. Chris Houser (who was going to post an answer, but ultimately decided he was too busy to do it) has posted a Gist which demonstrates what happens with a boolean vs. Object overloaded method; it turns out that in some scenarios, in addition to a (boolean ...) cast, a type hint is required. The discussion was quite enlightening, with a few dark corners of Clojure compilation process becoming nicely illuminated. (See links to IRC log below.)

    Basically, if an object is created right in the method-calling form — (.foo (Foo.) ...), say — that type hint is not necessary; it is likewise not necessary if the object has been constructed as a value for a local in an enclosing let form (see update 2 below and my version of the Gist). If the object is obtained by Var lookup, though, a type hint is required — which can be provided either on the Var itself or, at the call site, on the symbol used to refer to the Var.

    The Java code from the Gist:

    package mypkg;
    
    public class Ugly {
        public Ugly(){}
        public String foo(boolean i) { return "bool: " + i; }
        public String foo(Object o) { return "obj: " + o; }
    }
    

    And the Clojure code:

    (.foo (mypkg.Ugly.) 5)
    ;=> "obj: 5"
    
    (.foo (mypkg.Ugly.) true)
    ;=> "obj: true"
    
    (.foo (mypkg.Ugly.) (boolean true))
    ;=> "bool: true"
    
    
    (def u (mypkg.Ugly.))
    (.foo u (boolean true))
    ;=> "obj: true"
    
    (.foo #^mypkg.Ugly u (boolean true))
    ;=> "bool: true"
    

    Note how the Clojure compiler needs a type hint on u to be able to compile a direct method call. Otherwise reflection-based code seems to be generated, which apparently loses track of the fact that the argument is meant to be a primitive along the way.

    My additions follow (and here’s my fork of the above Gist).

    ;; renamed mypkg.Ugly to foo.TestInterop2 when doing my tests
    user> (let [t (foo.TestInterop2.)]
            (.foo t (boolean true)))
    "bool: true"
    
    ;;; type-hinting the Var
    user> (def #^foo.TestInterop2 x (foo.TestInterop2.))
    #'user/x
    user> (.foo x (boolean true))
    "bool: true"
    

    The topic was first brought up at this point. Chouser posted the Gist half an hour later, with the discussion becoming more and more interesting after that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 368k
  • Answers 368k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You're attempting to fetch an entity by key, but no… May 14, 2026 at 6:08 pm
  • Editorial Team
    Editorial Team added an answer You can use a trigger, a unique or primary key… May 14, 2026 at 6:08 pm
  • Editorial Team
    Editorial Team added an answer The Sharepoint Content Deployment Wizard is awesome for this sort… May 14, 2026 at 6:08 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.