Wednesday, 10 August 2016

Types

When we assign a variable a value such as int i = 1; we’re making an interesting assumption.
We assume using 1 automatically infers a value type that matches the variable type.
Computers use a wide variety of different types of data for storing numbers.
So far we’ve been seeing the word int being thrown around as though you know what an int is. In our everyday lives we hardly think there’s any difference between numbers 1 and 1.0 or even the word one. As humans we can easily conceptualize numbers as units of measure and converting between them. The conversion between the word one and the number 1 isn’t so easy for a computer.
The keyword int is short for integer. Integers are whole number values. The integer is a C# built-in type. Basically, this means that integers are a fundamental part of C#.
Other built-in types include float, double, string, bool, and object. Less commonly used built-in types are char, short, and byte. Altogether, there are 15 different built-in types. All of these, except object and string, are value types.
Every type of data you’re able to build must be based on all of these built-in types. The system which creates all of these floats, doubles, and bools is rooted in the origin of computing.
Remember the punch tapes and pieces of paper with holes in them? Those were records of 1s and 0s which were fed into computers for storing in the form of mechanical switches, either on or off. The patterns represented numbers or instructions and logic.
Today, we still use the same system of 1s and 0s, or binary, only we don’t need to punch holes in paper to tell the computer what to do. The methods of creating and storing the instructions have become many times more complex, but nonetheless the principle of the 1 and 0 are the same.
These are called bits; one possible origin for that name is the little bits of paper that were left over from punching all of the holes in the paper cards.

Value and Reference Types
The int, float, double, and bool are commonly used value types. The basics of this usage pattern relate to the system that stores and organizes the computer’s memory. When you talk about computer storage you use the word megabyte or gigabyte. Value types are stored with a very specific purpose in mind: to keep track of a numeric value.
NOTE: The word mega refers to how many millions of bytes a component in your computer can store. Giga indicates how every many billions of bytes are being stored. Have you ever thought about what a byte actually is?
A byte is what is called an 8-bit unsigned integer, or a system of using 8 bits to form a number. What this means is that it’s a whole number like 0, 7, or 32,767. The word unsigned indicates that the number cannot be negative, like −512.
A 1 or 0 in computer terms is called a bit. We won’t go into detail on how computers use bits to count, and it’s a rather fun thing to learn on your own. However, it’s important to know that a byte has a limited range, from 0 to 255. I’ll just leave you with the idea that you could count up to 1023 on your 10 fingers if you used binary rather than decimal. Your fingers can be used to represent a 10-bit unsigned integer.
The computer’s calculation capabilities used to be far more limited than they are today. An 8-bit game console made in the 1980s had a limited number of colors, 256 to be exact. This limitation was based on the number of bits that the processor could handle. The processor had a limited number of transistors which could be used at any one time. Shortly after, floating point coprocessors were introduced, which had a much larger numeric range allowed, by having closer to 32 bits to work with.
In all of these cases, when you use a value type, that number is actually stored as 1s and 0s in your computer’s RAM. This should be considered fairly remarkable. In the past you’d have to go through flaming hoops to store a value.
Now you just type in float f = 3.1415926535; and you can measure the circumference of the universe accurately to within the width of a single atom.
What all of these types have in common is that they are stored as a single element. Large values like double gigawatts = 1210000000.0; and double mint = 2.0; use the same amount of memory.

Your computer doesn’t assign one double or the other more space in memory. These types are referred to as primitive types. The only difference between an int and a float is the number of bits they use at a time, 8 and 32 respectively; doubles use 64 bits.
The string and object types differ a bit in how they are stored. These types are a composite of any number smaller elements. The bigger a string or object the bigger chunk of memory the computer opens up to place that object or string. These are called reference types or sometimes called nullable types because C# doesn’t look to a single element in memory to get data from it.
We will explain nullable types in Section 4.4.1.2, but in short a nullable type is a space in memory which is reserved for data that has yet to be fulfilled. Primitive or value types are commonly reserved and assigned at the same time. There are systems which allow this to be changed, but we’re getting ahead of ourselves for now.
In Unity 3D, a commonly used type is the Vector3. Vectors, in the math world, are directions in x, y, and z with a length. Unity 3D uses vectors to keep track of a position in 3D space. The Vector3 type is a composite of three float variables. This means that a Vector3 is made up of different components. From this, we may infer that a Vector3 is a nullable type. Each float is labeled x, y, and z. However, unlike the primitive types the Vector3 needs to be instanced. This means that you can’t simply use the following syntax.
int i = 0;//a valid initialization of an int.
Vector3 v1 = (x = 0,y = 0,z = 0);//not valid initialization.
Vector3 v2 = new Vector3();//this is how it’s done.

Nullable types need to be initialized; that is, they need some form of initial value. We’ll go into further detail on this later. It’s important to remember there are differences between types when they are being initialized. Later on, as you begin to create and use objects and variables it’ll be important to remember how to initialize the different variable types.
No comments :

No comments :

Post a Comment