简体   繁体   English

文字比较和变量比较有什么区别

[英]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 在这里我得到Equal作为输出和

    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. 在第一个实例中,java将f1与实际数字120.00f进行比较。 In the 2nd instance java is comparing memory reference. 在第二个实例中,java正在比较内存引用。 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. 即使f1和f2保持相同的值,它们也存储在存储器中的不同位置,因此彼此完全分离。 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. 因此,当您键入if(f1 == f2)时,java会检查数据中存储在f1的信息,并检查f2是否指向此完全相同的信息位。

The difference is the data types. 不同之处在于数据类型。

In the first example you're comparing an instance of the class Float against a primitive float . 在第一个示例中,您将Float类的实例与原始float In this case the Float object is unboxed to get another primitive float , and these two are compared. 在这种情况下, Float对象被取消装箱以获得另一个原始float ,并对这两个进行比较。 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 . 在第二个例子中,你要比较的类的实例Float与另一个类的实例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; 你的说法Float f1 = 120.0f;

but the compiler makes it Float f1 = new Float(120.0f); 但是编译器使它成为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 : (f1 == 120.00f)将f1更改为浮点数(不确定原因,但这就是它发生的情况。其中f1 == f2是对象比较,因为它们引用不同的对象而失败。请参阅此代码:

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, f1 120.0,

f2 120.0 f2 120.0

f1 val == f2 val :true f1 val == f2 val:true

Equal 等于

Not Equal 不平等

Equal 等于

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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