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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:59:41+00:00 2026-06-12T13:59:41+00:00

The following program prints respectively ‘false’ and ‘true’: Number n = true ? new

  • 0

The following program prints respectively ‘false’ and ‘true’:

Number n = true ? new Long(1) : new Double(2.0);
System.out.println(n instanceof Long);
System.out.println(n instanceof Double);

So it will not be a Long but a Double. However, it works as intended on normal classes:
Having

class B {}
class D1 extends B {}
class D2 extends B {}

this will print ‘true’:

B b = true ? new D1() : new D2();
System.out.println(b instanceof D1);

meaning that it is not working the same like the above example.

I’m sure that there is something related to autoboxing, but is it really the way it should work? Why she uses boxing, when the Number class is a superclass of both Long and Double, so that expression could be evaluated to Number?

It’s really a pain, because when printing the n, it prints as a double value. (I know it is easy to workaround, but drove me crazy)

  • 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-12T13:59:43+00:00Added an answer on June 12, 2026 at 1:59 pm

    Let’s take out the language lawyer’s book here: JLS §15.25

    The type of a conditional expression is determined as follows:

    • If the second and third operands have the same type (which may be the null type), then that is the type of the conditional expression.

    Long and Double are not the same type – does not apply.

    • 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.

    Neither value is primitive – does not apply.

    • If one of the second and third operands is of the null type and the type of the other is a reference type, then the type of the
      conditional expression is that reference type.

    Neither value is null – does not apply.

    • Otherwise, if the second and third operands have types that are convertible (§5.1.8) to numeric types, then there are several cases:
      • [… special cases for byte/short/char and their boxed equivalents…]
      • 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.

    This rule does apply here, which means that the result type of the conditional operator is as if both values were unboxed. Is suppose the reasoning behind that was that otherwise Number n = bool ? 1 : 2.0 and Number n = bool ? new Long(1) : new Double(2.0) have different values. This behaviour would also be unexpected and – worse – inconsistent.

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

Sidebar

Related Questions

The following program stores every word and then prints them with a number of
For some reason when I run the following code the program prints out JDN4
The program below prints the following data: Wed,Jun,13,10:37:34,2012,759,41,0,30,10,0,0,1 Wed,Jun,13,10:38:34,2012,767,33,0,25,6,0,0,2 Wed,Jun,13,10:39:34,2012,758,42,0,32,10,0,0,0 Wed,Jun,13,10:40:35,2012,758,42,0,29,11,0,0,2 Wed,Jun,13,10:41:35,2012,761,39,0,34,5,0,0,0 Wed,Jun,13,10:42:35,2012,769,31,0,22,6,0,0,3 Wed,Jun,13,10:43:35,2012,754,46,0,29,17,0,0,0
The following example Python program prints an updating progress bar. progress.py : import sys
Consider the following program: $x=12345678901.234567000; $y=($x-int($x))*1000000000; printf(%f:%f\n,$x,$y); Here's what is prints: 12345678901.234568:234567642.211914 I was
In the following code, am executing a java program Newsworthy_CA. The program prints the
The following C# program (built with csc hello.cs ) prints just Hello via Console!
Due to a bug that was fixed in C# 4, the following program prints
Why following program every time prints I'm string and not I'm object. or I'm
I have the following program: module Main where import System (getArgs) import Control.Concurrent import

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.