02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Numbers, int And unit

Are you ready to learn two new variable types and compare them to the Number data type? There are three types of numbers in ActionScript 3.0 : Number, int and uint. What are they and when should we use them?

We have seen how to deal with variables and operators in this course. Now let's take a look at data types associated with these variables to wrap up the coding basics. There are a number of data types that can be worked with according to our need. But as of now, it's enough to understand about a few numeric data type such as Number, int and uint.

Numbers

Number is a common word we used till now. The use of this data type was explained before when we started working with variables. As we all know, Number is a data type used to declare a variable of type Number (such a 1.13 or 999.) We know the difference between a string and a number, and the fact that data type mismatches can cause errors. Now we will learn what happens if the number used is a floating point number. A floating point number has a decimal value, for example 3.33 or 999.99999. Number refers to a floating point value by default. No need to worry if the output is a floating point value because if the variable was declared as Number, the application reserves memory for the decimal values as well.

Int and uint

We know that a lot of memory is used for a Number because of that decimal point; if we know that the value used will always be an integer, then the memory reserved for the decimal points is of no use. In that case, we can use the keyword int to declare an integer variable: var b:int; This will reserve memory for a single integer and displays the same. it's also useful when the output is a floating point value (say 3.33333) and we don't want the decimal points to be shown on the output panel. For this to happen, we must declare the variable as data type int. This will not only save memory but will also round off the output to an integer (in this case 3.3333 rounded off to 3.) But this works only when you already know that the numbers you're dealing with are integers.

There are also a few possibilities where we not only need an integer but a positive integer. These situations are rare; so for the most part you should always use int unless you need to access really large numbers. As int stores both positive and negative numbers, the amount of numbers int can show is exactly 50% of what the unit can. Don't use uint unless you need to access really big numbers. Why do we say that? Mainly because if you put inside a uint the value '-5' you'll find a totally different number in it as it's out of bounds: the actual number that will be in it will be the maximum number -5 (and that is a very very big number.)

Video Transcript

00:00type pre-typed inside of our string which we've met before and our number
00:03that we've met before
00:05and I'm tracing out here
00:07basically what is the smallest and biggest number
00:10a number could be
00:11and you can see in the output panel that it's a very weird figure
00:16basically a floating number, a very weird floating number which is the
00:20the smallest number and
00:22same for the biggest number and it's not really too relevant for us
00:25right now if we get to really crazy
00:28floating numbers and the negatives are positives and we're probably doing
00:32something wrong because it shouldn't be done in Flash
00:37so numbers are very
00:40have a huge array of options because they're floating numbers but what if we
00:44only want to work with
00:45integers
00:47if we only wanted to work with integers
00:49in ActionScript 3 we have a way to define a variable as an integer
00:54so I'm going to create a new variable
00:56and I'm going to define it as an integer
00:59and that's all I had to do to define it as an integer
01:02now an integer what would I get from an integer that I wouldn't get out of a
01:05number
01:06an integer
01:07has much more
01:10it has a much larger space for
01:14you know
01:15integers because it doesn't have to save room for floats
01:18where basically means that we have
01:20one it's less
01:21intense on the processing
01:23if we know that we're only going to work with the integer
01:26then it makes life much easier
01:28now if you're asking yourself what's the values that are int could produce
01:32then I could just change your
01:34my values to check that out
01:36I'm just going to change my trace and don't worry about if you don't understand what
01:39this trace is doing right now
01:41because we're going to revisit
01:44these items after revisit
01:47a few more
01:48items that were kind of missing before we could start talking more into depth into this
01:52but
01:52but basically we're approaching the int
01:57object reference
01:58where it's the complex part which is what we didn't talk about yet that there's
02:02complex things and everything in flash everything is derived from
02:05from complex prototypes
02:08and we're getting the string value out of it but we're going to
02:11sorry not string we're getting
02:13a constant out of it and it's kind of like vague and don't worry about it we don't have
02:17to understand it right now all we have to understand is that this is going to tell us
02:20what is the smallest number
02:22and what is the largest number that an
02:24integer could produce
02:26and if we run that application one more time we're going to see that
02:29it's a much more rational number it's easier to read this number
02:33and this is the smallest integer
02:35that an integer could be
02:36and the biggest integer
02:38an integer could be
02:40so we know what a number is we know what an integer is we could do the exact same
02:44things that we could have done with a number with an integer
02:49only thing is that it cannot be a fraction
02:52that's our only problem so if we take an integer such as
02:55and you know
02:57we take int and let's say it equals ten
02:59and we take a number
03:01and we say it equals ten as well
03:03and if we trace out a number
03:06if we trace out our num
03:10divide by three
03:12and we trace out
03:14i divide by three
03:16let's see what are the different values that we're going to get
03:21so as you can see right now
03:22the values are exactly the same so you might be confused now but you
03:27said that they're different
03:28and yeah they are
03:30right now we took a value which was
03:32a number
03:33and divided it by a number which produced a new number
03:38now on the other hand when we took our i which was an integer
03:42and divided it by a number
03:44because flash saw
03:45that there was
03:47and that there was a floating number here
03:50it produced a floating number it converted it because it's not stored into a variable
03:54on the other hand if we came here and instead of saying num divide by three
03:58we would
03:59divide it by three before
04:02let me put that in its right place
04:06we divide it by three before we trace it out
04:10and we do the same thing to our i
04:12we're going to divide it by three before we trace it out
04:17and let's see the difference that we're going to get right now
04:23so you see the first line of num basically
04:26because it's a number it accepts
04:28it accepts floating numbers and it's just adding as many as it can
04:33as many floating elements as it can
04:35well as the
04:37integer
04:38will just get rid of the floating area
04:40it's just going to keep it as an integer because an integer
04:43could only be only store
04:46integer values
04:50so now let's look at one more
04:52option here one more primitive
04:55the next primitive we want to look at is the uint
04:58and both int and uint were introduced in actions script three
05:03and what is uint? uint
05:05is almost the same thing as int the only difference is
05:08that uint could only be a positive number
05:11which means it can't be in a negative
05:14it could only be positive
05:16so what is the smallest number uint could be?
05:19uint the smallest value that uint could be would be zero
05:23well the biggest number uint could be
05:26twice as much as an integer can be
05:29because it doesn't have to worry about the negative values
05:33and let's just comment out these traces
05:39and run our application
05:41oh interesting let's see what is the error that we're getting
05:44a conflict exists with definition in namespace
05:48i and namespace internal
05:50and we could see the reason is because we try to define the variable
05:54the same i variable twice
05:56so let's give this variable a name of ui
06:00for uint
06:02and let's run it one more time
06:06and this time around we could see that our uint smallest value is zero
06:10well the biggest value is twice
06:12the value that our int could be
06:15which could be very useful so if we know we're not going to work with
06:19with negatives and we're only going to work with positive values
06:23then maybe we would want to work with uint
06:26now from a
06:27performance perspective
06:29an int or uint are basically the same but it's really just a question of do you
06:33need negative values or not
06:35and if you don't need negative values
06:37then you use uint you're giving yourself more room for
06:42for numbers that are above zero
06:44okay so that's basically your decision between uint and int
06:47well
06:48the decision between
06:49uint and number are different so
06:53between int and uint is the decision of
06:55if it's above zero
06:58or it's not above zero
06:59and number is basically do we need fractions
07:03if we don't need fractions we would want to work with int
07:06because int
07:07takes less
07:08from flash's memory which will make our application run more smoothly
07:12so if we don't need a number and we don't need fractions
07:15then we probably want to use int or uint

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!