#13 - Dart Operators - Everything you need to know + Comparing 2 Objects in Dart

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what is going on everyone i'm wicked welcome back to my dart from novice to expert complete course i hope you're doing really well in this tutorial we're going to hop on the flattery train and move over to the next important station of our dart language tour and that is dart operators so without further ado let's get right into it beforehand though i want to send a token of appreciation to everyone supporting me as official youtube members especially to diana and michael aka aza on discord if you want to become a member all you have to do is to click the join button right next to my channel and pick your desired membership having that said let's continue with our tutorial now having in mind that in the previous tutorials we learned everything about dart variables types and functions now it's finally time to glass over dart operators what you actually need to understand at first is that the majority of the operators that we're going to discuss today are no more than functions it may seem a little bit strange at first but take a look at the num class for example you can clearly see that plus minus division and multiply operators are basically special functions having their names set to the specific operator sign the plus operator for example takes two operands the one calling it and the one that wants to be added to the caller denoted by the other parameter here one thing to be extracted from here is that for operators that take two operands the leftmost operand determines which method is used for example if we have an integer a variable and a double b variable if we call the plus operator by writing a plus b then the plus function that is going to be called is the one from inside the integers class since that's the leftmost operand in our case though both types extend num so it doesn't really matter since the num class implements the plus operator because calling a plus b or b plus a will eventually call the same method from inside the num class however if you have something like these two classes both having their own specific plus operator you can see that if i call a plus b the program uses the plus operator from class a and if i call b plus a it uses the one from the b class highlighting what i said earlier think of these operators as being written like this a period plus b and b period plus a since that's what is actually happening in the background both objects are accessing their own plus method via the dot operator so having this in mind let's glance over all the operators that we're going to discuss today these are actually all the operators dart supports and if you pause the video you'll notice that most of them have been already used and covered in previous tutorials each of these operators can be split between multiple categories so that will facilitate the learning process we can see arithmetic operators relational operators equality operators and so on and so forth nevertheless in order to make this tutorial not boring at all for each category of operators i'll only discuss the important things you need to know because well i guess you already know how to use the plus operator to add two integers right before we get started there's still one more thing to be mentioned about the order in which the operators are displayed here each operator has higher precedence than the operators in the rows that follow it so for example if we have this quite unreadable but correct line of code we can reduce the order in which each operation is executed by looking at the table we can see that first and foremost dart is going to use the unary postfix list access operator to retrieve the value from the list at the specific index then it will calculate all multiplicative arithmetic operations then additive ones then it will take a look at the relations and equality operators then it will calculate the expressions by using the logical operators and then only at the end when the entire expression is calculated the resulting value is assigned to the variable by surrounding specific expressions with parentheses you can tell dart to evaluate those first now you heard me a lot of times talking about expressions but what are expressions well when we use operators we actually create expressions adding two values by using the plus operator creates an expression comparing the equality of two variables by using the equals equals operator creates also an expression even assigning a variable to another variable creates an expression everywhere you'll see operators will also see expressions finally after having this set let's get to the first type of operators you'll find inside dart arithmetic operators i'm displaying each and every one of them on the screen right now and from what i can deduce you should be already familiar with each and every one of these since we used them a lot in the previous tutorials in this section i want to discuss the division and modulus operators first so when dividing two numbers that are not divisible you have two choices you either retrieve the double value resulting from the division by using this divide operator or what you could do is retrieve the integer part plus the reminder part of this division by using the tilde slash operator and the percent operator denoting the modulo operation in this case 7 divided by 2 equals an integer of 3 and a reminder of 1 since 3 multiplied by 2 plus 1 equals 7 and another important fact to be discussed about arithmetic operators is that dart supports both prefix and postfix increment and decrement operators take a look at the following example if we have an integer value a equals 5 and we sequentially call a plus plus then print the value of a then call plus plus a and print again the value of a we shall see that a got incremented twice once to six and once to seven so if we assign these values to two variables a1 and a2 and print them again we shall see the same result right well as you can observe it's not like that it seems like it didn't increment for the first time but it did for the second time pretty strange right what a plus plus actually does is that it first assigns the value to the a variable then only after that it increments it whereas plus plus a increments the a value at first and only then it assigns it to the variable this is really important to be taking in mind and this is what actually happened in our case dart assigned the a1 variable to the current value of a which was 5 then it incremented the value of a to 6 a step which we cannot see in the code unfortunately then when we called plus plus a dart incremented the current value at first which is 6 and took the result of 7 and assign it to the variable the same thing applies with the minus minus operator as well and i think this is pretty much everything you need to know about these arithmetic operators it's time to move on to the next category equality and relational operators here's the list of all operators in this category these are for the equality and these are relational i'm sure all of you know what greater and less than means when comparing to integers for example so i'm not going to get deeply into that what i want to discuss however is how dart calculates the equality of two objects by default in dart two objects are equal if and only if they point to the same object in memory so if we have two integers both holding the same value of 2 they are equal because as we have previously learned 2 is a constant value object therefore it doesn't matter how many times we'll instantiate it it will always be the same constant instance however if we instantiate two lists holding the same values we learn that these are not constant lists unless we prefix them with the const keyword so comparing list 1 with list 2 will end up in a false value one function that checks whether two references are pointing to the same object is identical and by default in dart the equal equal operator is performing the same as this identical function so then why are two methods doing the same thing in dart well because compared to the identical method which will always check whether two references are pointing to the same object the equal equal operator can be overridden in specific cases for example if we have a class a holding two values of integer if we create two instances of a with the same values inside and check whether they're equal you'll see that both identical and equal equal operator return false because indeed they're holding references to two completely different objects in memory but in this case we may want the equal equal operator to return true if two instances have the same integer values inside by using the vs code dart data class generator plugin we can override the equal equal operator really really easy by hitting control path period and choosing generate equality without any hassle the plugin automatically generated the overridden equal equal operator when overriding this operator we also need to override the hash code getter and you can see that the plugin takes care of that too by storing the hashcodes of all fields all together the generated code is even optimized since there's no need to check whether the values inside are equal if dar detects they point to the same object in memory by default now if we check if these two objects are equal and identical again we can see that thanks to the overridden equal equal operator they are equal since what dart does now is compare the values from inside of it but they're definitely not identical because they're not pointing to the same object in memory we can optimize this code even more by turning this class into a constant one with a constant constructor now if we run the program again we'll see that those objects are equal and identical at the same time and a quick tip i can give you when you want to implement this equality in a more neat way is that you can use the equatable package as a dependency to your package so get the equatable package then all you need to do is to extend your class with it import the package at the top and then as you can see override the props list the props list represent the fields from inside your class that you want to be compared inside the equal equal operator function so we'll pass both variables since we want two objects to be equal only if they have the same values set to a and b if we run the program again we actually receive the same result as before but this time the code looks more structured and much easier to read and understand having this said i hope you understood how dart compares objects by default and how you can override the equal operator in order to compare your objects field by field it's now time to move to the next category the type test operators this is a really important one so make sure you pay as much attention as possible there are three operators in this section and all are equally as important so let's cover each one of them right away the as keyword is used most of the time to cast a type to another type however it has another functionality serving as the keyword preceding library prefixes so when you want to have an alias for an imported library you can write import math.dart as math and access the library by using that specific alias now let's talk a little bit about the typecast functionality you might remember when i told you that you can have a list of multiple objects of any type right well what if you want to access the specific methods from inside each of these individual type objects what does dart currently know is that each of the objects inside the list are of a nullable type object so we need to specifically cast each object we need to the right type we can do that by using the as operator just like you can see on the screen right now but there's one potential issue with that because you should use the as typecast only when you are 100 sure that the object you're casting to is indeed of a correct subtype if the object can't be cast to that type you'll end up with a typecast exception so if you're not 100 sure that an object is of that type you want to cast to dart comes with another two important operators is and is not the is operator returns true only if the object is of the specified type for example instead of typecasting each of the values to their type we could iterate through the list by using for each and for each element we can check if it's of type integer double string and so on and so forth by using the ease operator note that dart is smart enough to automatically convert the element to the right type inside the scope of the if statement so that you will be able to access the specific methods directly and i think you can imagine by yourself that the is not operator does the complete opposite meaning it evaluates to true only if the object doesn't have the specified type it's finally time to move on to another category the assignment operators now i'm sure a lot of you already know what equal operator does so i'm not going to bother you with that however i can bet a lot of you didn't know that you have a shorthand syntax for whenever you want to assign a value to a variable only if the variable hasn't been assigned yet i.e it's no you can do that by prefixing the equal operator with two question marks just like this this is quite useful when you have an object that you don't want to instantiate multiple times now we have previously seen in the arithmetic operator section that you can increment a value quickly by writing a plus plus or plus plus a those are great when you want to quickly increment by one but what if you want to increment by three and you don't want to use the long syntax like a equals a plus three if that's the case you can use compound assignment operators one of them being plus equal to do it so you can write a plus equal three and pay attention because this is equal to writing a equals a plus three or plus plus a plus plus a plus plus a and not a plus plus a plus plus and a plus plus remember the letter assigns the value before incrementing it there are a bunch of compound assignment operators like minus equal division equal modulo equal multiply equal and you can even use the bitwise and shift operators with those but we'll talk about them a little bit later on now let's talk a little bit about logical operators these are some operators you'll find yourself using most of the time especially when you want to combine multiple boolean expressions one after another so yeah one thing to be noted here is that they only work on expressions that evaluate into boolean values the exclamation mark before an expression inverts it ie changes false to true and true to false you can see that the logical or and end operators have double vertical slashes and double ampersands note that in dart there are also operators having only one vertical slash and one ampersand those are bit operators so let's talk about them up next say for example we have an integer initialized with 6 this in binary code is written like 0 1 1 0 because 2 to the power of 1 is 2 plus 2 to the power of 2 is 4 2 plus 4 is 6 which is our value and let's say we have another value b which is equal to 5. this is equal to 0 1 0 1 in binary code having the same calculations in mind so if we apply the and operator between them then the result will be 0 1 0 0 which is equal to 4 and if we apply the or operator between them then the result will be 0 1 1 1 which is equal to the value of 7. if we apply the xor operator between them the result will be 0 0 1 1 which is equal to the value of 3. we can also apply the left shift operator on a then the result will be 1 1 0 0 which is equal to the value of 12 and if we apply the right shift operator on our a value then the result will be 0 0 1 1 which is 3. we can also find the unary bitwise complement of a number by prefixing it with a tilde bit operators can be really useful in some situations when you want to calculate values really fast however i find myself using the solutions to these situations from stack overflow now it's time to move on to conditional expressions because these are one of the most important time saving techniques you'll ever use inside dart and flutter during your development workflow that is because instead of writing a function like this with an if-else statement you can have something like this so everywhere you want to use an if-else simple statement like this you can use this conditional expression if this condition is true then it will evaluate the first expression and if it's false it will evaluate the second expression after the column operator what's even cooler is that if you want to write something like this you have a much cleaner way to write it and that is this in this case if expression1 evaluation is not null it returns its evaluation otherwise it evaluates and returns the value of expression 2. i mean from this entire function we initially wrote we arrived at this shorthand really cool alternative there are a couple of more operators to cover but i'm sure you already know them by now since i explained them bit by bit during the built-in type tutorial so make sure to revisit it by clicking the card in the top right corner of the screen having that said it is finally time to end this tutorial on dart operators i hope you understood them in great detail in the next tutorial we'll move over to the next station on this dart language tour in which we'll discuss all the control flow statements in dart as always if you like this tutorial don't forget to smash that like button subscribe to my channel and share the video with all of your friends and colleagues in pursuit of top tier development until next time as always take care wiggit is out bye bye
Info
Channel: Flutterly
Views: 1,302
Rating: undefined out of 5
Keywords: dart, dart tutorial, dart course, dart full course, dart complete course, dart operators, arithmetic operators, logical operators, relational operators, equality operators, bit operators, conditional expressions, dart conditional expressions, dart expressions, dart bit operators, dart equality operators, dart logical operators, dart arithmetic operators, a++ vs ++a
Id: UKyKGwyls_I
Channel Id: undefined
Length: 20min 18sec (1218 seconds)
Published: Mon Jul 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.