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

What are Primitives?

We have two variables under our belt and it's time we group them into a variable type. This is a critical concept that we will revisit as we learn what objects are, but for now it's good for us just to understand that we have a different type of variable. A few new topics we are about to learn are dependent upon the logic that this only works for primitive values. (Have no clue what primitive values are, well jump in and let's figure it out!)

We can describe primitive variables as the containers that store data for a time. These variables don't preserve large amounts of data to be used later. In other words, we can say that these are instant variables. As said earlier, the concept is clearly understood when compared with objects. Objects are created using classes: these objects are used to store buckets of information at a time. In the case of primitive variables, only one value at a time can be assigned to a primitive variable. If you assign another value to this variable, the variable now contains the new value and the old value is gone.

For example let's declare a variable num and assign a value 5 to it: var num:int=5; . Now, according to the definition of this variable, num=5. If we use num in any part of the program, the compiler assumes this as the integer 5. So let's now assign another value, 10, to the same variable num: num=10;. We would expect num to have both the values 5 and 10. But once you assign a new value to a primitive variable, the new value overrides the old value – the variable now contains the new value and not the old one. The variable always contains the most recent value and all the old values are erased. Therefore, these variables are considered as primitive variables. So the value of num is now equal to 10 (num=10.)

Now we'll consider another example. If we declare a variable a and assign a value 3 to it, we can say that a=3. In the next line, declare another variable b and assign a to it:

var a: int = 3; var b: int = a;

At first glance, we think b = a, but this is not always the case! In fact, b takes the initial value of a, 3: a=3 then b=a so b now equals 3.

For extra credit: later on in your code, you change the value of a: a = 420. What does b equal now? If you said 420, beep! b still equals 3. Primitives hold their initial values until changed: you'd need to add the line b=a; again if you wanted b to have a's NEW value.

These types of conversions are performed with primitive variables. The knowledge of primitive variables is necessary before you start dealing with objects.

Video Transcript

00:00We've played around with a few primitives,
00:03and we haven't seen yet objects.
00:07And before you even see them, let's
00:08try to solidify what actually primitives are.
00:11And we're going to see objects in a later course.
00:14But the easiest way to explain what primitives
00:17are compared to objects, which we don't know what they are
00:20yet, objects are basically buckets
00:23that contain multiple, could contain multiple items.
00:28They don't store only one piece of information.
00:31Versus primitive variables, primitive values
00:34will always store a basic value, such as a number, a string,
00:39a Boolean value, which we'll look into in the next video.
00:43Well, as objects are more complex objects,
00:47and they could contain an array or a list of items,
00:51a large amount of different properties,
00:53and they're not constrained to only hold one value at a time.
00:57With that differentiation, it's very important to notice
01:00and to be able to understand the difference between a variable
01:06that is a primitive and a variable that is an object.
01:09We haven't seen objects yet, but we will.
01:12A primitive always holds directly the data
01:15that's inside of it.
01:16It's not a linkage to another place.
01:20It's not a placeholder for large amounts of data.
01:24It's actually storing directly in Flash the information
01:27that we want to work with.
01:30So if I create a variable, and I call it i,
01:33for example, which is a number, I'm
01:36storing directly inside of that variable the number.
01:40And if I would create another variable, such as t, which
01:44would be a number as well, I could place into it i,
01:48but I'm not really placing i into it,
01:49because i is a primitive value.
01:52And as it's a primitive value, I'm
01:53basically duplicating the data from i
01:57and placing it inside of t.
02:00And in other words, t now equals 3.
02:03It does not equal i.
02:05i now could change, but it would not change t.

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!