views:

413

answers:

7
  1. float ff = 1.2f;
  2. Float fo = new Float(1.2f);
  3. double fg = 3.2d;
  4. Double fh = new Double(2.1d);

Can I use '=' between the (1) and (3) or between the (2) and (4)??

+7  A: 

Yes, 2 creates an Object.

Telos
+3  A: 

Yes. The first declares a variable of the primitive type float and initializes it to 1.2.

While the second declares a variable of the reference type Float, creates an object of type Float and then assigns a reference to the variable.

Aaron Maenpaa
+19  A: 

Yes.

  1. Makes a plain old data type (AKA a primitive type) called "float."
  2. Makes a Java Object called Float that holds that value that happens to be identical to (1)

Responding to the edit questions:

You will see

  1. "possible loss of precision" message if you try ff = fg.
  2. "incompatible types" if you try fo = fh.
  3. fg = ff will work fine (the float fits in a double).
  4. fh = fo will still give you an "incompatible types".
Bob Cross
+7  A: 

Yes, first one is a primitive type and second is a boxing class which wraps capabilities of primitive float type, we need second for example for use in the collections. Before you have had to deal a lot with type conversion (I think until Java 1.5) now the existence of wrappers classes takes those capabilities. More information. here

Artem Barger
Understanding autoboxing is key to understanding this. Java will autoconvert from Float to float to double to Double if you ask it the right way.
Alex Feinman
+1  A: 

Yeah primitive types can't be NULL, Objects can. Also the Float object has a bunch of useful utility functions attached to it.

Martin Dale Lyness
+2  A: 

new Float(1.2f) creates a new Float object every time, consuming memory.

If you use factory method Float.valueOf(1.2f) JVM may reuse existing Float object instances for the same value. It could create a new object instance only if there isn't already a Float instance with the same value.

Usually you'll want to use Float.valueOf(1.2f) instead of new Float(1.2f).

Also note that primitives and objects work differently with equals operator ==.

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
Juha Syrjälä
@Juha, this doesn't appear to be a firm contract. When I look at the source for Float.java, I see `public static Float valueOf(float f) { return new Float(f); }`I don't think you can count on the valueOf() working in all cases (unless you can produce a JVM specification to the contrary).
Bob Cross
Dear,I have read that '==' can be used for equality of references,but I think x1 and x2 haven't got any references and for f1 and f2 I think it can be true because they are object types!!!!!
Johanna
@Bob Cross: At least when autoboxing, Floats and Doubles are not required to be cached. JLS §5.1.7 (http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.7 ) says: "If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2." Since even `Float f1 = 1.2f; Float f2 = 1.2f; System.out.println(f1 == f2);` produces `false`, I find it rather unlikely that there *is* a factory method which caches.
Michael Myers
@mmyers, there's at least one. Boolean.valueOf(true) will return Boolean.TRUE. It uses a ternary operator. Admittedly, it's not very interesting when you could just use Boolean.TRUE itself.... The idea of a Double.valueOf() that caches would be interesting but I would never count on the values returned being equal.
Bob Cross
@Bob, I didn't actually check the code, just read the javadoc. Edited the answer.
Juha Syrjälä
What I meant was simply that although some types are guaranteed to be cached when boxed, floats are not.
Michael Myers
@Juha, okay, that answer I agree with. +1
Bob Cross
A: 

In real applications I suggest you not use float or Float, its not very accurate and almost never the right solution, use double or Double instead.

Peter Lawrey