简体   繁体   中英

What is difference between literal comparison and variable comparison

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

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 , ie addresses of objects in memory. For two different objects this addresses will be also different.

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.

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.

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

使用java.lang.Float.compare(float1,float2)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM