| [ directory ] |
|
8.7 Relational OperatorsRelational operators are used mostly in the condition part of if-else and while constructs. 8.7.1 The == OperatorThe equality operator (==) is a binary operator and compares the two operands on each side of the operator for equality. For value types, the == operator returns true if the value of the operands is equal and false otherwise. For reference data types, the == operator returns true if the two references refer to the same object. For string objects, the == operator returns true if the contents of the two strings are equal. Java programmers should be careful of this feature of C#. In C#, the == operator on strings does what the equals() method of Java strings does. The == operator can be overloaded only if the != is overloaded along with it. Listing 8.7 demonstrates some important aspects of the == operator. Listing 8.7 The == Operator (C#)
using System;
public class Test {
public static void Main(string[] args) {
int ii = 1;
int jj = 1;
//Compares the value types : True
Console.WriteLine("1) "+(ii ==jj));
object i = 1;
object j = 1;
//Compares the boxed references : False
Console.WriteLine("2) "+(i ==j));
string a = "test";
string b = String.Copy(a);
string c = "test";
//Compares the string values: True
Console.WriteLine("3) "+(a == b));
//Compares the objects the individual strings point to:
//False
Console.WriteLine("4) "+((object)a == (object)b));
//Compares the objects the individual strings point to:
//True (because of string interning)
Console.WriteLine("5) "+((object)a == (object)c));
}
}
The output of Listing 8.7 is as follows: 1) True 2) False 3) True 4) False 5) True Line 1 prints True because we are comparing simple value types for equality. Line 2 prints False because we are actually comparing the reference types. Although both i and j have the same integral value, when assigned to an object they are boxed to different object references; and we know that the == operator will return false when comparing references pointing to different objects. Line 3 prints True because, unlike Java, the == operator in C# compares the value of the strings. Line 4 shows False because b is a copy of the string and hence a brand new object, although its value is the same as that of a. Line 5 returns true because of string interning. To conserve memory, the runtime interns strings, meaning that string literals of the same value point to the same object in memory. 8.7.2 The != OperatorThis binary operator compares its operands for inequality. It returns true if the operands are unequal, and false otherwise. 8.7.3 The < OperatorThis binary operator compares the LHS to the RHS and returns true if the LHS is less than the RHS. 8.7.4 The > OperatorThis binary operator compares the LHS to the RHS and returns true if the LHS is greater than the RHS. 8.7.5 The <= OperatorThis binary operator compares the LHS to the RHS and returns true if the LHS is less than or equal to the RHS. 8.7.6 The >= OperatorThis binary operator compares the LHS to the RHS and returns true if the LHS is greater than or equal to the RHS. Listing 8.8 shows the rest of the relational operators in action. Listing 8.8 The Other Relational Operators (C#)
using System;
public class Test {
public static void Main(string[] args) {
int a = 4;
int b = 5;
//Comparing value types
Console.WriteLine("4 < 5 "+(a < b));
Console.WriteLine("4 > 5 "+(a > b));
Console.WriteLine("4 <= 5 "+(a <= b));
Console.WriteLine("4 >= 5 "+(a >= b));
Console.WriteLine("4 != 5 "+(a != b));
string s = "abc";
string t = "def";
//Comparing value types
Console.WriteLine(s != t);
}
}
The output of Listing 8.8 is as follows: 4 < 5 True 4 > 5 False 4 <= 5 True 4 >= 5 False 4 != 5 True True Note that the <, >, <=, and >= operators cannot be applied to strings. All relational operators can be overloaded. If < is overloaded, then > must be overloaded and vice versa. Similarly, if <= is overloaded, then >= must be overloaded and vice versa. |
| [ directory ] |
|