For primitive numeric types, enum types, value types, and boolean types, the equality operator (==) returns true if the values of its operands are equal, false otherwise. For reference types, including java.lang.String, == returns false if its two operands refer to different objects, even if they contain identical values. For string constants, however, == compares the values of the strings and returns true if the two strings are identical.
Parameters
- expr1
-
An expression of primitive numeric type, enum type, value type, boolean type, or reference type.
- expr2
-
An expression of primitive numeric type, enum type, value type, boolean type, or reference type.
If the promoted type of the operands is int or long, then an integer equality test is performed; if the promoted type is float or double, then a floating-point equality test is performed.
If the operands of an equality operator are both of type boolean, then the operation is boolean equality.
If the operands of an equality operator are both of reference type or the null type, then the operation is object equality.
If the operands of an equality operator are both of value type, then the operation is value equality.
// operator_equality.jsl
class MyClass
{
public static void main(String[] args)
{
// Numeric equality:
System.out.println((2 + 2) == 4); // true
// Reference equality: different Objects, same value.
String s = new String("1");
String t = new String("1");
System.out.println(s == t); // false
// Define some Strings:
String a = "hello";
String b = String.Copy(a);
String c = "hello";
// Compare string values for a constant values.
// true
System.out.println(a == c);
// Compare String objects;
// a is a constant but b is an object:
System.out.println(a == b); // false
}
}
true
false
true
false
Reference
!= Operator (Visual J#)
Other Resources
J# Operators