📜  Java中的相等 (==)运算符及示例

📅  最后修改于: 2022-05-13 01:54:31.776000             🧑  作者: Mango

Java中的相等 (==)运算符及示例

==运算符Java中的一种关系运算符,用于检查相等关系。它在比较之后返回布尔结果,并广泛用于循环语句和条件 if-else 语句。

句法:

LHS value == RHS value

但是,在比较这些值时,通常会出现三种情况:

情况 1:当 LHS 和 RHS 值都是原始值时

这是案例中最简单的。由于原始数据存储在堆栈内存中,因此在这种情况下,从堆栈内存中取出双方的实际值并进行比较。如果它们相等,则返回 true,否则返回 false。

句法:

Actual value == Actual value

例子:

Java
// Java program for using == operator
 
import java.io.*;
 
public class GFG {
    public static void main(String[] args)
    {
        // Declaring primitive values
        int a = 4;
        int b = 4;
        int c = 5;
 
        // Comparing a and b using == operator
        System.out.println("Are " + a + " and " + b
                           + " equal? " + (a == b));
 
        // Comparing b and c using == operator
        System.out.println("Are " + b + " and " + c
                           + " equal? " + (b == c));
    }
}


Java
// Java program for using == operator
 
import java.io.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Declaring primitive value
        int a = 4;
 
        // Declaring reference value
        int[] b = { 1, 2, 3, 4 };
 
        // Comparing a and b using == operator
        System.out.println("Are " + a + " and " + b
                           + " equal? " + (a == b));
    }
}


Java
// Java program for using == operator
 
import java.io.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Declaring reference value
        int[] a = { 1, 2, 3, 4 };
        int[] b = { 1, 2, 3, 4 };
        int[] c = b;
 
        // Comparing a and b using == operator
        // Though they both have the same value
        // the output will be false because
        // they both have a different address in the memory
        System.out.println("Are a and b equal? "
                           + (a == b));
 
        // Comparing b and c using == operator
        // Though they both have the same value
        // the output will be true because
        // they both have same address in the memory
        System.out.println("Are b and c equal? "
                           + (b == c));
    }
}


输出
Are 4 and 4 equal? true
Are 4 and 5 equal? false

情况 2:当 LHS 和 RHS 值之一是原始值,而另一个是参考值时

在这种情况下,对于原始端,从堆栈内存中获取实际值进行比较。但是对于引用端,当一个数组被声明和初始化时,数据存放在堆内存中,引用指针存放在栈内存中。所以堆栈内存中的所有内容都是内存地址。

句法:

Actual value == Address value
    OR
Address value == Actual value

因此,当比较原始值和参考值时,程序不会编译并抛出错误:

Java代码中的编译错误:-

prog.java:20: error: incomparable types: int and int[]
                           + (a == b));
                                ^
1 error

这是因为原始端的值很容易从堆栈内存中获取,但对于引用端,由于值在堆内存中,因此无法获取该值。因此错误。

例子:

Java

// Java program for using == operator
 
import java.io.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Declaring primitive value
        int a = 4;
 
        // Declaring reference value
        int[] b = { 1, 2, 3, 4 };
 
        // Comparing a and b using == operator
        System.out.println("Are " + a + " and " + b
                           + " equal? " + (a == b));
    }
}
prog.java:17: error: bad operand types for binary operator '=='
                           + " equal? " + (a == b));
                                             ^
  first type:  int
  second type: int[]
1 error

情况 3:当 LHS 和 RHS 值都是参考时

在这种场景下,对于双方来说,在声明和初始化一个数组时,数据存储在堆内存中,而引用指针则存储在栈内存中。所以这两个变量,它们的地址都被检查了。如果两个变量都指向相同的内存地址,则此运算符返回 true。否则返回false。

句法:

Address value == Address value

例子:

Java

// Java program for using == operator
 
import java.io.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Declaring reference value
        int[] a = { 1, 2, 3, 4 };
        int[] b = { 1, 2, 3, 4 };
        int[] c = b;
 
        // Comparing a and b using == operator
        // Though they both have the same value
        // the output will be false because
        // they both have a different address in the memory
        System.out.println("Are a and b equal? "
                           + (a == b));
 
        // Comparing b and c using == operator
        // Though they both have the same value
        // the output will be true because
        // they both have same address in the memory
        System.out.println("Are b and c equal? "
                           + (b == c));
    }
}
输出
Are a and b equal? false
Are b and c equal? true