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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:05:10+00:00 2026-06-10T05:05:10+00:00

In the following class, the return type of the two methods is inconsistent with

  • 0

In the following class, the return type of the two methods is inconsistent with the idea that the ternary operator:

return condition?a:b;

is equivalent to

if(condition) {
    return a;
} else{ 
    return b;
}

The first returns a Double and the second a Long:

public class IfTest {
    public static Long longValue = 1l;
    public static Double doubleValue = null;

    public static void main(String[] args) {
        System.out.println(getWithIf().getClass());// outpus Long
        System.out.println(getWithQuestionMark().getClass());// outputs Double
    }

    public static Object getWithQuestionMark() {
        return doubleValue == null ? longValue : doubleValue;
    }

    public static Object getWithIf() {
        if (doubleValue == null) {
            return longValue;
         } else {
            return doubleValue;
        }
    }
}

I can imagine this has to do with the compiler narrow casting the return type of getWithQuestionMark() but is that language wise ok? It’s certainly not what I would have expected.

Any insights most welcome!

Edit: there’s very good answers below. Additionally, the following question referenced by @sakthisundar explores another side effect of the type promotion occurring in the ternary operator: Tricky ternary operator in Java – autoboxing

  • 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-10T05:05:12+00:00Added an answer on June 10, 2026 at 5:05 am

    Basically it’s following the rules of section 15.25 of the JLS, specifically:

    Otherwise, if the second and third operands have types that are convertible (§5.1.8) to numeric types, then there are several cases:

    • […]

    • Otherwise, binary numeric promotion (§5.6.2) is applied to the operand types, and the type of the conditional expression is the promoted type of the second and third operands.

    So section 5.6.2 is followed, which will basically involves unboxing – so this makes your expression work as if longValue and doubleValue were of types long and double respectively, and the widening promotion is applied to the long to get an overall result type of double.

    That double is then boxed in order to return an Object from the method.

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

Sidebar

Related Questions

I have the following class written by someone else that I'm trying to understand
The following code gets the first type parameter class declared generically in the interface
I have added the following method in my User Domain class String getNameAttribute(){ return
I have the following member of class foo. foo &foo::bar() { return this; }
I the following code returns every text that has the price class associated to
if i have the following links : <a href="#" onclick="return navigateTo(this)" id="menuList:0:menu" class="normalLink"> <span
The following class acts as a simple cache that gets updated very infrequently (say
I have a C++ class, which has the following methods: class Bar { ...
Given is the following example: class Foo(object): def __init__(self, value=0): self.value=value def __int__(self): return
The following two methods are used to wrap deserialization using Google Gson: public static

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.