Is there a difference between the two statements?

  • float ff = 1.2f;
  • Float fo = new Float(1.2f);
  • double fg = 3.2d;
  • Double fh = new Double(2.1d);

Is it possible to use '=' between (1) and (3) or between (2) and (4) ??

+3
source share
8 answers

Yes.

  • Creates a simple old data type (AKA - primitive type) called "float".
  • Creates a Java Float object that contains this value, which is identical to (1)

Answering editing questions:

You will see

  • The message "Possible loss of accuracy" if you try ff = fg.
  • "incompatible types" if you try fo = fh.
  • fg = ff will work fine (float fits in double).
  • fh = fo will still give you "incompatible types".
+21

, 2 .

+7

, , - , float, , , . ( , Java 1.5), - . .

+7

. float 1.2.

float, float, .

+3

new Float (1.2f) Float, .

factory Float.valueOf(1.2f) JVM Float . , Float .

Float.valueOf(1.2f) Float (1.2f).

, - equals ==.

float x1 = 1.2f;
float x2 = 1.2f;

x1 == x2  // true

Float f1 = new Float(1.2f);
Float f2 = new Float(1.2f);

f1 == f2 // false
+2

, NULL, . , Float , .

+1
  • float . - , , ( ). .

  • , .

float - , .

3 = 1 , .

+1

float Float, , double Double.

0

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


All Articles