Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

What does this print and why?

Java
Object o = true ? 1 : "a";
Object p = true ? Integer.valueOf(1) : Double.valueOf(2.0);
System.out.println(o + " " + p);

It prints 1 1.0. In the first ternary the operands are an Integer and a String, so the result is a reference and the boxed 1 is kept as it is. In the second, both operands are numeric wrapper types, so Java applies binary numeric promotion: it unboxes both, widens the int to double, and the result is the double 1.0, even though the true branch was chosen. The same rule can unbox a null and throw NullPointerException when a wrapper meets a primitive, so when operand types differ we should use if/else or make the types explicit.

Why this question is asked

The conditional operator, condition ? a : b, looks like a compact if/else, and most of the time it behaves like one. But it is an expression, so it must have a single type, and the rules that choose that type can change the value we get. Interviewers show this snippet because it looks impossible: the true branch is Integer.valueOf(1), yet the program prints 1.0. In this answer, we will recap what the type of a ternary expression means, work through both lines, see the NullPointerException that the same rule can cause in production code, and finish with how to write ternaries that do what they look like.

1A Quick Recap: A Ternary Has One Type

An if/else statement runs one branch or the other, and each branch can do anything. A ternary is different: it is an expression that produces a value, and the compiler must decide the type of that value when it compiles the code, before it knows which branch will run. So it looks at the types of both operands and picks one type that can hold either.

The Java Language Specification splits ternaries into three kinds by their operands. If both are boolean, it is a boolean conditional. If both are numeric, whether primitive or wrapper types like Integer and Double, it is a numeric conditional, and the result type comes from numeric promotion. Otherwise it is a reference conditional, and the result is simply one of the two objects, unchanged. Our two lines fall into different kinds, and that is the whole puzzle.

2Line One: Integer and String

In Object o = true ? 1 : "a";, the first operand is the int 1 and the second is a String. A String is not numeric, so this is a reference conditional. The int is boxed to an Integer, and the result type is a common supertype of Integer and String. Since the condition is true, o is the Integer 1, and printing it gives 1.

Java
Object o = true ? 1 : "a";
System.out.println(o); // 1
System.out.println(o.getClass()); // class java.lang.Integer

3Line Two: Integer and Double

In Object p = true ? Integer.valueOf(1) : Double.valueOf(2.0);, both operands are wrapper types that can be converted to numbers, so this is a numeric conditional. The rule for numeric conditionals is binary numeric promotion, the same rule that decides the type of 1 + 2.0: both operands are unboxed, and if either is a double, both become double. So the type of the whole expression is double.

The condition is true, so the value is the first operand, but converted to the expression's type: the int 1 becomes the double 1.0. Then, because we assign it to an Object, the double is boxed into a Double. Printing it gives 1.0. The surprising part is that the type of the branch we did not take changed the value of the branch we did take.

Java
Object p = true ? Integer.valueOf(1) : Double.valueOf(2.0);
System.out.println(p); // 1.0
System.out.println(p.getClass()); // class java.lang.Double
System.out.println(o + " " + p); // 1 1.0
Step throughHow the compiler types the second ternary
  1. Look at both operand typesInteger and Double
  2. Are both convertible to numeric types?
    YesNumeric conditional: apply numeric promotionNoReference conditional: keep the object
  3. Unbox both and promote to the wider typeint 1 becomes double 1.0
  4. Evaluate the conditiontrue, so take the first operand
  5. Box the double for the Object variablep is the Double 1.0

5 steps in order. Play them, or step through them one at a time.

The type is chosen at compile time from both operands, before the condition is known.

4The Same Rule Throws NullPointerException

The promotion rule also applies when one operand is a wrapper and the other is a primitive of the same kind. If one side is Integer and the other is int, the type of the expression is int, so the Integer is unboxed. If that Integer is null, unboxing it throws NullPointerException. This shows up in real code that reads from a map.

Java
Map<String, Integer> stock = new HashMap<>();
boolean tracked = true;
Integer qty = tracked ? stock.get("pen") : 0; // NullPointerException
// the type of the ternary is int, so the null from get() is unboxed

Notice that qty is declared as Integer, which can hold null, so it looks safe. But the null never reaches qty. It is unboxed inside the ternary, because 0 is an int and that made the whole expression an int. The fix is to make both operands the same reference type, for example tracked ? stock.get("pen") : Integer.valueOf(0), or better, to avoid the ternary with stock.getOrDefault("pen", 0).

5Writing Ternaries That Do What They Look Like

The rule of thumb is to use a ternary only when both operands have the same type. When they differ, either convert one explicitly so the type is obvious to every reader, or use if/else, where each branch assigns its own value and no promotion happens.

Java
Object p;
if (true) p = Integer.valueOf(1); else p = Double.valueOf(2.0);
System.out.println(p); // 1, an Integer
Number n = true ? (Number) Integer.valueOf(1) : Double.valueOf(2.0);
System.out.println(n); // 1, the cast makes it a reference conditional

Notice the cast in the second version. Number is not convertible to a primitive, so the ternary becomes a reference conditional and the Integer passes through unchanged. Static analysis tools and IDE inspections can also warn about ternaries that mix boxed and primitive operands.

6Follow-up questions

What is the type of true ? 1 : 'a'?

Both are numeric, and the int 1 is a constant that fits in a char, so the specification makes the type char and the result is the character with code 1, not the number 1. Rules like this one are exactly why mixing operand types in a ternary is best avoided.

Does the same promotion happen in a switch expression?

Yes, a switch expression whose arms are numeric is typed in a similar way, so arms of int and double give a double. Keeping all arms of one type avoids the surprise there too.

Why does the specification work this way?

So that a ternary used in arithmetic behaves like arithmetic: x > 0 ? 1 : 2.5 should be a number you can compute with, not an Object. The cost is that wrapper types get unboxed, which is surprising when we only wanted to pick one of two objects.

7Conclusion

In this answer, we saw that a ternary has a single type chosen at compile time from both operands. Integer and String make a reference conditional, so o stays the Integer 1. Integer and Double make a numeric conditional, so both are unboxed and promoted to double and p becomes 1.0. The same promotion unboxes a null when a wrapper meets a primitive and throws NullPointerException, so when operand types differ we use if/else or make the types explicit.

What to say in the interviewAbout 80 seconds aloud

Open with

It prints 1 1.0. The key is that a ternary is an expression with one type, chosen at compile time from both operands. In the first line the operands are an int, boxed to Integer, and a String.

Then, if they want more

A String is not numeric, so it is a reference conditional: the result is just the chosen object, and o is the Integer 1. In the second line both operands are numeric wrapper types, Integer and Double, so it is a numeric conditional and binary numeric promotion applies, the same as in 1 + 2.0.

Both are unboxed, the int is widened to double, and the expression's type is double. The condition picks the first operand, but it is converted to 1.0, and then boxed into a Double for the Object variable. So the type of the branch not taken changed the value of the branch taken.

The same rule causes a real production bug: flag ? map.get(key) : 0 has type int, so if get returns null it is unboxed and throws NullPointerException, even though the target variable is an Integer. So when operand types differ, I use if/else, getOrDefault, or an explicit cast so both sides have the same type.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What does this print and why?": https://scrivio-interview-library.netlify.app/java/operators-and-control-flow/6f87aa44c0/