Comparison Between Different Types
What happens when you compare different data types in Flash? In this video we'll go over some of the basic operations when comparing different date types. The most valuable lesson you can take from this video is how Flash deals with such a comparison when working with a regular comparison operator(’==' and '!='.)
In this course, we have compared different variables of the same data type. We used a number of rational and equality operators to compare the variables or values which are of same type. Now we'll see how to compare the variables of different data types.
Let us declare a variable 'a' with type int and a variable 'b' with data type number. Let the value be a=1 and b=2 which satisfies the respective data types. Now we can use trace(a==b) to check whether these two values are equal. If yes, you get true or else false. Here we can see that the two values are not equal and hence, the output is false. However, if we use trace(a!=b), the result would be true because the comparison now is for inequality and the condition for inequality is met as the values are unequal. This was explained earlier too but now it is a kind of revision for us.
You might think, comparing variables of type int with type Number is well and good, but what if the comparison is to be done between type int and type Boolean (or type number and type Boolean?) We are going to discuss this here. We can explain this with the help of an example. Let's have variables a:int and b:Number . Assume that a=1 and b=0. Let's assign a Boolean variable now with value true:
var isTrue:Boolean = true;We can say trace(isTrue) is true. If the value assigned to it is false then we get trace(isTrue) as false. Here we need to remember one thing that, a true value is considered to be 1 and a false value is considered to be 0 as said in the earlier videos.
So now we have a=1,b=0 and isTrue = true (=1). If we try to compare int and Boolean variables we have trace(a==isTrue), this will result as true. If we compare number type and Boolean type that is trace(b==isTrue), the result will be false because b=0 and isTrue=1 as it is true. Similarly, if isTrue is false then the value of isTrue=0, the results will now take a reverse shift.
We have compared variables of different types whose value is either 0 or 1. But if the value of integer variable a is 5, and if we trace(a==isTrue), the result would be false for both Boolean values (true or false) which is equal to 0 or 1. Neither 0 or 1 is equal to 5; the output will be false every time. In fact if we use the command trace(a!=isTrue), the result would be true as it satisfies the inequality.
One more key point we can take from this video is never use keywords as variables! We know that we can use any letter or word for a variable name but there are a few exceptions here as well. Data types and a few other keywords are already defined by the developers and are stored in the library for specific use. Therefore, usage of such keywords as variables in our program can cause problems. Hence, make sure that the variables declared don't match with any of the keywords.
Ready to Level Up Your Skills?
Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!
Enroll Now for Free!