0
public class Main
{
    public static void main(String args[])
    {
        process(true ? 1 : 2L);
    }


    static void process(Object object)
    {
        System.out.println(object instanceof Integer);
    }
}

My expected output is true.

But actual out put is false.

My understanding is between integral data types the largest type will be assigned. If so what is this called?

  • 1
    Err, why would you expect that the "larger" type of 1 Integer and 2L Long ... would be Integer? – GhostCat Jan 31 '20 at 12:41
  • 1
    By the way... please type always `L` instead of `l`. Regarding the font, it can be easily misread as `1` (the number) . – Luis Iñesta Jan 31 '20 at 12:46

2 Answers2

1

The then and else parts of true ? 1 : 2l are int and long. The result is the widest, long, the the then-part is cast to long. See JLS.

In Computer Science a term for this is balancing types.

34 / 2.0        // double, more a case of _widening a type_.
c ? 2.0 : 34    // double
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

Like stated here, a ternary expression with int and long will be subjected to binary numeric conversion, resulting in a long.

Nightara
  • 117
  • 5