Casting
In this lesson we are going to try and familiarize ourselves with a new idea in programming... We are going to push a piece of data from one type of variable into another. In many cases this can be handled easily. In some cases this is much more complicated.
To cast a piece of data from a smaller variable to a larger is easily accomplished. For example moving data from a byte to an int is simple. A byte is a small variable that can store numbers between -255 to 255. An int is a much larger variable and can store numbers from -4294967296 to 4294967295. So moving a number from a byte (small) to an int (larger) is relatively simple. The move from an int (large) to a byte (small) is much more difficult and many times occurs in data loss.
To move data from a large variable to a small requires a method called casting. Casting is basically a method of forcing the computer to overwrite its built in measures of not allowing this switch to happen. Normally it will throw an exception error because it usually involves data loss to accomplish this move.
This process is further complicated if we are trying to convert a string (meant for characters and words) into an int. If the data stored in the string is actually a number... We will need to convert it before we can cast it into the int.
Boy that was fun to type out... Am i speaking a foreign language? it feels like i am speaking a foreign language!!!
If we send that same data to a byte (smaller) it makes things even more interesting...
To cast a piece of data from a smaller variable to a larger is easily accomplished. For example moving data from a byte to an int is simple. A byte is a small variable that can store numbers between -255 to 255. An int is a much larger variable and can store numbers from -4294967296 to 4294967295. So moving a number from a byte (small) to an int (larger) is relatively simple. The move from an int (large) to a byte (small) is much more difficult and many times occurs in data loss.
To move data from a large variable to a small requires a method called casting. Casting is basically a method of forcing the computer to overwrite its built in measures of not allowing this switch to happen. Normally it will throw an exception error because it usually involves data loss to accomplish this move.
This process is further complicated if we are trying to convert a string (meant for characters and words) into an int. If the data stored in the string is actually a number... We will need to convert it before we can cast it into the int.
Boy that was fun to type out... Am i speaking a foreign language? it feels like i am speaking a foreign language!!!
If we send that same data to a byte (smaller) it makes things even more interesting...
Videos
The Assignment
Here is your assignment
1. Have a greeting printed to the screen
2. Create a string named s and assign the value of 25 into it
3. Print a message saying something like "we have a string variable with " +s "in it"
4. Convert this string s into a variable type int named i
5. Print int i out to the console screen with a message saying something like "we converted the data to an int and it is still" +i
6. Convert int i to a byte named b
7. Print byte b out to the console with a message saying something like "we converted the data to a byte and it is still" +b
1. Have a greeting printed to the screen
2. Create a string named s and assign the value of 25 into it
3. Print a message saying something like "we have a string variable with " +s "in it"
4. Convert this string s into a variable type int named i
5. Print int i out to the console screen with a message saying something like "we converted the data to an int and it is still" +i
6. Convert int i to a byte named b
7. Print byte b out to the console with a message saying something like "we converted the data to a byte and it is still" +b