The difference between == and equals confused me for sometime until I decided to have a closer look at it. Many of them say that for comparing string you should use equals and not == . Hope in this answer I will be able to say the difference. The best way to answer this question will be by asking a few questions to yourself. so let's start: What is the output for the below program: String mango = "mango";
String mango2 = "mango";
System.out.println(mango != mango2);
System.out.println(mango == mango2);
if you say, false
true
I will
say you are right but why did you say that? and If you say the output is, true
false
I will say you are wrong but I will still ask you, why you think that is right? Ok, Let's try to answer this one: What is the output for the below program: String mango = "mango";
String mango3 = new String("mango");
System.out.println(mango != mango3);
System.out.println(mango == mango3);
Now If you say, false
true
I will say you are wrong but why is it wrong now? the correct output for this program is true
false
Please compare the
above program and try to think about it. Ok. Now this might help (please read this : print the address of object - not possible but still we can use it.) String mango = "mango";
String mango2 = "mango";
String mango3 = new String("mango");
System.out.println(mango != mango2);
System.out.println(mango == mango2);
System.out.println(mango3 != mango2);
System.out.println(mango3 == mango2);
// mango2 = "mang";
System.out.println(mango+" "+ mango2);
System.out.println(mango != mango2);
System.out.println(mango == mango2);
System.out.println(System.identityHashCode(mango));
System.out.println(System.identityHashCode(mango2));
System.out.println(System.identityHashCode(mango3));
can you just try to think about the output of the last three lines in the code above: for me ideone printed this out
(you can check the code here): false
true
true
false
mango mango
false
true
17225372
17225372
5433634
Oh! Now you see the identityHashCode(mango) is equal to identityHashCode(mango2) But it is not equal to identityHashCode(mango3) Even though all the string variables - mango, mango2 and mango3 - have the same value, which is "mango", identityHashCode() is still not the same for all. Now try to uncomment this line // mango2 = "mang"; and run it
again this time you will see all three identityHashCode() are different. Hmm that is a helpful hint we know that if hashcode(x)=N and hashcode(y)=N => x is equal to y I am not sure how java works internally but I assume this is what happened when I said: mango = "mango";
java created a string "mango" which was pointed(referenced) by the variable mango something like this mango ----> "mango"
Now in the next line when I said: mango2 = "mango";
It actually reused the same string "mango" which looks something like this
mango ----> "mango" <---- mango2
Both mango and mango2 pointing to the same reference Now when I said mango3 = new String("mango")
It actually created a completely new reference(string) for "mango". which looks something like this, mango -----> "mango" <------ mango2
mango3 ------> "mango"
and that's why when I put out the values for mango == mango2 , it put out true . and when I put out the value for mango3 == mango2 , it put out false (even when the values were the same). and when you uncommented the line // mango2 = "mang"; It actually created a string "mang" which turned our graph like
this: mango ---->"mango"
mango2 ----> "mang"
mango3 -----> "mango"
This is why the identityHashCode is not the same for all. Hope this helps you guys. Actually, I wanted to generate a test case where == fails and equals() pass. Please feel free to comment and let me know If I am wrong. Skip to main content This browser is no longer supported. Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. Comparison Operators (Visual Basic)- Article
- 09/15/2021
- 5 minutes to read
In this article
The following are the comparison operators defined in Visual Basic. < operator
<= operator
> operator
>= operator
= operator
<> operator
Is Operator IsNot Operator Like Operator These operators compare two expressions to determine whether or not they are equal, and if not, how they differ. Is , IsNot , and Like
are discussed in detail on separate Help pages. The relational comparison operators are discussed in detail on this page. Syntaxresult = expression1 comparisonoperator expression2
result = object1 [Is | IsNot] object2
result = string Like pattern
Partsresult Required. A Boolean value representing the result of the comparison.
expression1 , expression2 Required. Any expression.
comparisonoperator Required. Any relational comparison operator.
object1 , object2 Required. Any reference object names.
string Required. Any String
expression.
pattern Required. Any String expression or range of characters.
RemarksThe following table contains a list of the relational comparison operators and the conditions that determine whether result is True or False .
Operator | True if | False if |
---|
< (Less than)
| expression1 < expression2
| expression1 >= expression2
| <= (Less than or equal to)
| expression1 <= expression2
| expression1 > expression2
| > (Greater than)
| expression1 > expression2
| expression1 <= expression2
| >= (Greater than or equal to)
| expression1 >= expression2
| expression1 < expression2
| = (Equal to)
| expression1 = expression2
| expression1 <> expression2
| <> (Not equal to)
| expression1 <> expression2
| expression1 = expression2
|
Note The = Operator is also used as an assignment operator. The Is operator, the IsNot operator, and the Like operator have specific comparison functionalities that differ from the operators in the preceding table. Comparing NumbersWhen
you compare an expression of type Single to one of type Double , the Single expression is converted to Double . This behavior is opposite to the behavior found in Visual Basic 6. Similarly, when you compare an expression of type Decimal to an expression of type Single or Double , the Decimal expression is converted to Single or Double . For Decimal expressions, any fractional value less than 1E-28 might be lost. Such fractional value loss may cause two values to compare as
equal when they are not. For this reason, you should take care when using equality (= ) to compare two floating-point variables. It is safer to test whether the absolute value of the difference between the two numbers is less than a small acceptable tolerance. Floating-point ImprecisionWhen you work with floating-point numbers, keep in mind that they do not always have a precise representation in memory. This could lead to unexpected results
from certain operations, such as value comparison and the Mod Operator. For more information, see Troubleshooting Data Types. Comparing
StringsWhen you compare strings, the string expressions are evaluated based on their alphabetical sort order, which depends on the Option Compare setting. Option Compare Binary bases string comparisons on a sort order derived from the internal binary representations of the characters. The sort order is determined by the code page. The following example shows a typical binary sort order.
A < B < E < Z < a < b < e < z < À < Ê < Ø < à < ê < ø
Option Compare Text bases string comparisons on a case-insensitive, textual sort order determined by your
application's locale. When you set Option Compare Text and sort the characters in the preceding example, the following text sort order applies:
(A=a) < (À= à) < (B=b) < (E=e) < (Ê= ê) < (Ø = ø) < (Z=z)
Locale DependenceWhen you set Option Compare Text , the result of a string comparison can depend on the locale in which the application is running. Two characters might compare as equal in one locale but not in another. If you are using a string comparison to make important decisions, such as whether to accept an attempt to
log on, you should be alert to locale sensitivity. Consider either setting Option Compare Binary or calling the StrComp, which takes the locale into account. Typeless Programming with Relational Comparison OperatorsThe use of relational comparison operators with Object expressions is not
allowed under Option Strict On . When Option Strict is Off , and either expression1 or expression2 is an Object expression, the run-time types determine how they are compared. The following table shows how the expressions are compared and the result from the comparison, depending on the runtime type of the operands.
If operands are | Comparison is |
---|
Both String
| Sort comparison based on string sorting characteristics.
| Both numeric
| Objects converted to Double , numeric comparison.
| One numeric and one String
| The String is converted to a Double and numeric comparison is performed. If the String cannot be converted to Double , an InvalidCastException is thrown.
| Either or both are reference types other than String
| An InvalidCastException is thrown.
|
Numeric comparisons treat Nothing as 0. String comparisons treat Nothing as "" (an empty string). OverloadingThe relational comparison operators (< . <= , > , >= , = , <> ) can be overloaded, which means that a class or structure can redefine their behavior when an operand has the type of that class or structure. If your code uses any of these operators on such a class or structure, be
sure you understand the redefined behavior. For more information, see Operator Procedures. Notice that the = Operator can be overloaded only as a
relational comparison operator, not as an assignment operator. ExampleThe following example shows various uses of relational comparison operators, which you use to compare expressions. Relational comparison operators return a Boolean result that represents whether or not the stated expression evaluates to True . When you apply the > and < operators to strings, the comparison is made using the normal alphabetical sorting order of the strings. This
order can be dependent on your locale setting. Whether the sort is case-sensitive or not depends on the Option Compare setting. Dim x As testClass
Dim y As New testClass()
x = y
If x Is y Then
' Insert code to run if x and y point to the same instance.
End If
In the preceding example, the first comparison returns False and the remaining comparisons return True . See also- InvalidCastException
- = Operator
- Operator Precedence in Visual Basic
- Operators Listed by Functionality
- Troubleshooting Data Types
- Comparison Operators in Visual Basic
FeedbackSubmit and view feedback for
What does === mean in Java?
On the other hand === is known as strictly equality operator. It's much similar Java's equality operator (==), which gives compilation error if you compare two variables, whose types are not compatible to each other. In fact, you should always use "===" operator for comparing variables or just for any comparison.
What is the difference between and == in Java?
The “=” is an assignment operator is used to assign the value on the right to the variable on the left. The '==' operator checks whether the two given operands are equal or not. If so, it returns true. Otherwise it returns false.
What is && and || in Java?
The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. These operators exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed.
Is a term to indicate when multiple methods are written with the same name but with different parameter lists?
Method overloading means two or more methods have the same name but have different parameter lists: either a different number of parameters or different types of parameters.
|