PT - Casting and Ranges of Variables (Lesson)
Casting and Ranges of Variables
Introduction
Do you remember playing dress up as a kid? Did you love going trick or treating on Halloween? Wasn’t it great pretending to be something else? Even as we get older it would be fun to play a different role just for a little while. Actors do this all the time. They get to pick up a script and play a different role with each show they perform. Surprise, surprise! Our data can do that too! However, it is not just for fun or pretend. We can have data that starts out as one type and ends up as another. Nothing is at it seems…
Type Conversions
Remember when we talked about order of operations and how String concatenation is on the same level as addition and subtraction?
Recall this example:
Or think about how in your Arithmetic Assignment you had to put your mathematical expression in parenthesis in order to have it evaluate before being printed with the String.
String concatenation is an example of type conversion. In the example above (without the parenthesis) the data is being converted to a new type. 7 + 3 were integers but when we added them to the String, they became a String.
Type conversions are sometimes necessary when programming. Usually they will happen between numbers. It is possible for values to be widened or narrowed.
Widening is when you are going from a type that has a smaller memory storage to a larger memory storage. Converting from an int to a double is an example of widening.
Narrowing is going in the other direction. It is converting from a type with a larger memory storage to a smaller memory storage. For example, double to int. In this situation we risk losing data since you would be cutting off the decimal.
Arithmetic Promotion
Arithmetic promotion is a widening conversion that will change the type so that the arithmetic operations can be performed. We have seen this already:
This is an example of arithmetic promotion since the 7 and 3 got promoted to Strings so that they could be concatenated together.
This is another example of arithmetic promotion. The 3 gets promoted to a double so that the decimal division can take place. Java will do this conversion automatically.
Type Casting
It is possible to force a variable to change type by doing something called type casting. This is done by putting the type of the variable you want in parenthesis in front of the variable you want to change.
This will change the 7 to 7.0 and then do the division.
When we type cast a double to an int the decimal digits are truncated which means they are cut off.
This will change num to a 7. The .42 has been truncated.
Type casting can be useful when we want to round double values. To round a double to the nearest int use the following. (Assume that x is a double value.)
positive numbers | negative numbers |
(int)(x + 0.5) | (int)(x - 0.5) |
int Overflow
Did you know that integers don’t go on forever? Well, in real life they do but not in Java. Everything on the computer has been programed so it must start and stop somewhere.
Integer values in Java are stored using a finite amount of memory so they cannot continue into infinity. An int value must be in the range from Integer.MIN_VALUE to Integer.MAX_VALUE inclusive.
Type Integer.MIN_VALUE and Integer.MAX_VALUE into the interactions pane of Dr. Java to see what their values are.
If an expression would evaluate to an int value outside of that range, an integer overflow occurs. This could result in an incorrect value within the allowed range.
Type 2147483647+1 into the interactions pane of Dr. Java.
Click on "Runestone Academy" below to open the required reading that is listed.
Runestone Academy Links to an external site.: AP CSA – Java Review:
READ: 3.6 – Casting Variables
Casting and Ranges of Variables Activity
Click below to begin the casting and ranges of variables activity.
Progress Check
Primitive Types Test Review
- Click on AP Classroom Links to an external site..
- Complete Progress Check 1.
Contact your teacher if you do not have access to AP Classroom.
Java Input
(This topic is not tested on the AP Exam but makes our labs more interesting.)
The Scanner class allows us to create a Scanner to allow input from the user of a program. This will make it possible for the user to type information using the keyboard in order to interact with our programs.
To create a Scanner object you will need this line of code:
Scanner class. The keyboard is just a reference variable so that name could be anything. Keep in mind that we always want variable names to make sense. It is appropriate to use keyboard for the name since a user will be entering information from the computer keyboard.
The Scanner class has methods that allow it to do certain actions.
These lines of code allow the user to enter an integer and that value to be stored in the variable age. The nextInt() method allows us to receive and store an int. Other methods that are useful are nextDouble() which allows us to receive a double, next() which takes in a single word String, and nextLine() which takes a whole String.
In order to use the Scanner class, you must write an import statement before the class heading in your java file.
Type this program into a new Dr. Java file:
Be sure that you understand each line of code and all the output. Notice that it is only necessary to create one Scanner object. The keyboard gets reused each time we call a method from the Scanner class. We will learn more about this later.
Click on "Introduction to Computer Science Using Java" below to open the required reading that is listed.
Introduction to Computer Science Using Java Links to an external site.:
READ: Chapter 12: Input and Output
IMAGES CREATED BY GAVS