Doubt about Boxing and Unboxing in Java

2

I have been practicing the topic of Boxing and Autoboxing in Java and I have a doubt. I know that each type of primitive data has its "Wrapper" class to be treated as an object. But specifically in the following code:

public static void main(String[] args) {
    Integer a = 1;
    Double b = 2.0;

    System.out.println(a.equals(1) ? a : b);
}

The output on the screen gives me 1.0. Why do you print 1.0 as Double if it is declared as Integer?

    
asked by Fernando Jimenez 18.04.2018 в 07:03
source

2 answers

2

In these cases you should check the Java Language Specification or JLS

Specifically at the point 15.25.2 we talk about the ternary operator that you are using and that, indeed, is the cause of your problem:

  

Numerical conditional expressions are unique expressions.

     

The type of a numerical conditional expression is determined as follows:

     

If the second and third operands have the same type, then that is the type of the expression.

     

If one of the last two operands is a primitive of type T , and the other is the result of applying boxing to T , then the type is T .

     

[...]

     

In any other way, the promotion applies numerical binary to the types of the operands, and the type of the expression is the promoted type of [o bien] the second [o bien] the third operand .

(Translated and highlighted by me)

It is this last point that interests us. In this case, the numerical promotion is being applied, for which Java tries to accommodate the most restrictive type (in this case Integer ) at least restrictive (in this case Double )

    
answered by 18.04.2018 / 07:44
source
1

The problem is not related to boxing or unboxing. What happens is that the ternary operator (? :) has a somewhat peculiar behavior. For example, in the following code:

a.equals(1) ? a : b

a.equals(1) corresponds to the conditional expression which determines what varores (in this case a or b ) will "return" as a result. If the expression to the left of ? is evaluated as true then the value resulting from evaluating the expression will be returned to the left of the colon (:); and if evaluated as false , the value resulting from evaluating the expression to the right of the two points is returned. Now, it is recommended that the data types that return the evaluation of the expressions to the left and right of the two points be the same, to avoid errors and / or strange behaviors like in this case.

What is happening to you, that a is type Integer and b % type Double , so the compiler determines that the type of data that returns as a result of the statement a.equals(1) ? a : b will be Double , since you can not convert a Double to Integer because data would be lost (at first because the decimal part is lost), but on the other hand if you can convert a Integer to Double without being lose data.

For all the above I reiterate the recommendation: "try that the expressions to the left and to the right of the two points (:) are evaluated to the same type of data".

    
answered by 18.04.2018 в 07:43