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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:52:39+00:00 2026-05-26T04:52:39+00:00

I use code like: Double getabsValue(final Object[] value){ if (value==null) return null; if (value.lengt==0)

  • 0

I use code like:

Double getabsValue(final Object[] value){

if (value==null) return null;
if (value.lengt==0) return null;

final Double absValue=Maths.abs(value[0]);

if (absValue>0) return absValue
else return null;

But in my application I have problem with performance.
How it can be optimized?

Maybe better use?

if (absValue>0) return absValue
else return absValue<0?-absValue:null;

Thanks

  • 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-26T04:52:39+00:00Added an answer on May 26, 2026 at 4:52 am

    Well, the code you’ve got at the moment won’t even compile – there’s no Math.abs(Object) call as far as I’m aware. However, assuming you actually have a cast to Double there, you’re going to be boxing all the time. You could avoid boxing when the value is already greater than 0, and avoid the call when the value is 0, like this:

    static Double getAbsValue(Object[] values) {
        if (values == null || values.length == 0) {
            return null;
        }
        Double value = (Double) values[0];
        return value > 0 ? value
             : value == 0 ? null
             : -value;
    }
    

    By the time we get to the final option, we already know that the value is negative, so we don’t really need the call to abs any more.

    It’s not really clear what the context is here. You say you’ve got a performance problem, but is it definitely in this code?

    EDIT: Your latest code shows:

    if (absValue>0) return absValue
    else return -1*absValue;
    

    That doesn’t do the same thing – it doesn’t return null if the array contains a boxed 0 value, as your original code does.

    You should focus on correctness before performance.

    What do you want your code to do with an input of 0? If you want it to return 0, then I would use:

    return value >= 0 ? value : -value;
    

    If you want it to return null, use the code I provided originally.

    Why include a multiplication by -1 rather than just use the unary negation operator, by the way? I would expect either the compiler or the JIT to get rid of that anyway, but fundamentally you don’t want to be performing multiplication – you want to perform negation. Make your code read as closely as possible to how you’d describe your aims.

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

Sidebar

Related Questions

I use C# code more-or-less like this to serialize an object to XML: XmlSerializer
so I am using a code like this: Double.TryParse(Value, NumberStyles.Any, CultureInfo.InvariantCulture, out result); and
I currently use the following code to print a double: return String.format(%.2f, someDouble); This
When using XML serialization in C#, I use code like this: public MyObject LoadData()
Assuming sr is an IEnumerable<string> , I want to use code like this to
This has been bothering me lately- when I use some code like below to
If i use some really simple code like : $('#elm').tipTip(); It will do what
What criteria should I use to decide whether I write VBA code like this:
There are some fairly powerful tools like SIMBL or Airfoil/Instant Hijack which use code
I would like to use some code written in python (it uses built in

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.