3

What is the difference between

    Float f1 = 120.0f;
Float f2 = 120.00f;

if(f1==120.00f)
{
    System.out.println("Equal");
}
     else
    System.out.println("Not Equal");

here i am getting Equal as output and

    if(f1==f2)
{
    System.out.println("Equal");
}
    else
    System.out.println("Not Equal");

here,i am getting not equal as output. What is difference between literal comparison and variable comparison

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
sravanalakshmi.sunkara
  • 1,161
  • 2
  • 13
  • 35
  • I don't know how to explain but in case 1 the value is compared where in case two the memory locations of f1 and f2 are compared. t is becase in case 1 you have a primitive type in the RHS, so java will do a autounboxing of LHS object and then compare the values – Arun P Johny Jun 06 '13 at 11:12
  • 4
    In the first case, the `Float` (Object) is converted to a `float` (native type) and the two are compared. In the second case, you are comparing two objects - and such a comparison has to happen with `.equals()`. – Dominik Sandjaja Jun 06 '13 at 11:13

5 Answers5

5

That thing what you called "variable comparison" is comparison of variables values. In your case your variables are objects, hence you are comparing their references, i.e. addresses of objects in memory. For two different objects this addresses will be also different.

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
5

In the first instance java compares the f1 to the actual number 120.00f. In the 2nd instance java is comparing memory reference. Even though f1 and f2 hold the same values, they were stored in different locations in memory and are therefore completely separate from one another. So when you type if(f1==f2) java checks the data the information stored at f1 and checks to see if f2 refers to this exact same bit of information.

4

The difference is the data types.

In the first example you're comparing an instance of the class Float against a primitive float. In this case the Float object is unboxed to get another primitive float, and these two are compared. When using == with two primitives it compares their values, which is why it says they're equal (because they are).

In the second example you're comparing an instance of the class Float with another instance of the class Float. In this case no such conversion takes place, and when using == with two objects it compares their references to see if they're the exact same object. It says they're not equal because they aren't - they're two distinct objects, they just happen to have the same value.

Anthony Grist
  • 38,173
  • 8
  • 62
  • 76
2

This is a side effect of auto boxing. Your saying Float f1 = 120.0f;

but the compiler makes it Float f1 = new Float(120.0f);

Now the comparison

(f1==120.00f) changes f1 to a float (not sure why but that is what it is happening. Where as f1==f2 is an object comparison and fails as they refer to different objects. See this code :

public static void main(String[] args) {
    Float f1 = new Float(120.0f);
    Float f2 = new Float(120.00f);
    System.out.println("f1 " + f1.floatValue() + ", f2 " + f2.floatValue());
    System.out.println("f1 val == f2 val :" + (f1.floatValue() == f2.floatValue()));
    if(f1==120.00f)
    {
        System.out.println("Equal");
    }
         else
        System.out.println("Not Equal");



        if(f1==f2)
    {
        System.out.println("Equal");
    }
        else
        System.out.println("Not Equal");

        if(120f==f2)
        {
            System.out.println("Equal");
        }
            else
            System.out.println("Not Equal");    

}

Get

f1 120.0,

f2 120.0

f1 val == f2 val :true

Equal

Not Equal

Equal

tgkprog
  • 4,493
  • 4
  • 41
  • 70
0

use java.lang.Float.compare(float1, float2).

Piyush Aghera
  • 965
  • 7
  • 13