Assigning an integer value to a Float wrapper in Java

Next works

float a=3;

but the following:

float a=3;

Shouldn't you automatically advertise 3 to float (since conversion expansion doesn’t need to be explicitly activated) and then put it in the Type field in the Type box?

Is it because of the rule that I read in the book of Khalid Mogul Java?

Conversion extension not possible. any boxing transformations

+3
source share
3 answers

, Float a=3; , , 3 Integer ( , : Float a = new Integer(3); ). Float - Integer ( Number).

:

Number a = 3;

:

Number a = new Integer(3);

, ,

Number a = Integer.valueOf(3);

, .

+3
Float               Integer
  ^                    ^
  |                    |
  |                    |
  v                    v
float <----------->   int

/unboxing , . Java ( int Float ).

+2

Float a = 3.0f; .

-1

Source: https://habr.com/ru/post/1763964/


All Articles