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

  • Home
  • SEARCH
  • 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 9253171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:09:03+00:00 2026-06-18T11:09:03+00:00

public class Test { public static final Double DEFAULT_DOUBLE = 12.0; public static final

  • 0
public class Test {
    public static final Double DEFAULT_DOUBLE = 12.0;
    public static final Long DEFAULT_LONG = 1L;

    public static Double convertToDouble(Object o) {
        return (o instanceof Number) ? ((Number) o).doubleValue()
                : DEFAULT_DOUBLE;
    }

    public static Long convertToLong(Object o) {
        return (o instanceof Number) ? ((Number) o).longValue() : DEFAULT_LONG;
    }

    public static void main(String[] args){
        System.out.println(convertToDouble(null) == DEFAULT_DOUBLE);
        System.out.println(convertToLong(null) == DEFAULT_LONG);
    }
}
  • 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-18T11:09:04+00:00Added an answer on June 18, 2026 at 11:09 am

    EDIT

    The ternary operator does some type conversions under the hood. In your case you are mixing primitives and wrapper types, in which case the wrapper types gets unboxed, then the result of the ternary operator is “re-boxed”:

    If one of the second and third operands is of primitive type T, and the type of the other is the result of applying boxing conversion (§5.1.7) to T, then the type of the conditional expression is T.


    So your code is essentially equivalent to (apart from the typo where longValue should be doubleValue):

    public static void main(String[] args){
        Double d = 12.0;
        System.out.println(d == DEFAULT_DOUBLE);
    
        Long l = 1L;
        System.out.println(l == DEFAULT_LONG);
    }
    

    Long values can be cached on some JVMs and the == comparison can therefore return true. If you made all the comparisons with equals you would get true in both cases.

    Note that if you use public static final Long DEFAULT_LONG = 128L; and try:

    Long l = 128L;
    System.out.println(l == DEFAULT_LONG);
    

    It will probably print false, because Long values are generally cached between -128 and +127 only.

    Note: The JLS requires that char, byte and int values between -127 and +128 be cached but does not say anything about long. So your code might actually print false twice on a different JVM.

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

Sidebar

Related Questions

public class Test extends HttpServlet { private static final long serialVersionUID = 1L; public
import org.joda.time.LocalDate; public class Test { public static void main(String[] args) { long time=System.currentTimeMillis();
public class test { public static final int nThreads = 2; public static void
what i basicly want is this: public class Test { private static final Integer
public class Test { private static final String str1 = new String(en); private static
Here is my code: public class Test { static { main(null); } public static
public class Test { public static void main(String[] args) { DemoAbstractClass abstractClass = new
I have following code public class TEST { public static void main(String arg[]){ try
Take a look at this code: public class Test { public static void main(String...
import javax.swing.*; public class test { public static void main(String[] args) throws Exception {

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.