1.6 More About Variables and Constants

Normally when you put a value in a variable they should both have the same type. For instance, you could store the int constant 25 in an int variable called number. There are situations where you are allowed to mix types. For instance that same int value 25 could also be stored in a float variable called cost. You can imagine that Processing just stores the number as 25.0. This is called a widening conversion since the float has a wider range of values than an int. Processing will automatically allow any widening conversions because they are safe. There will be no loss of information. The following table shows all the allowed conversions:

FromTo
byte
short
char
int
long
float
short, int, long, float, double
int, long, float, double
int, long, float, double
long, float, double
float, double
double
Any other conversions can result in the loss of data. Suppose the number 3052 was stored in an int variable. If you tried to copy that into a byte it wouldn't fit. This is a narrowing conversion. But suppose the number 30 was stored in the int variable. That would fit into byte variable. Since it might not fit, Processing does not allow an automatic conversion. You can force Processing to allow a conversion by using a cast. You perform a cast by putting the type you want to convert to in brackets in front of the value you want to convert. This tells Processing you are aware that information could be lost but you want to do it anyway.

Examples

long num = 123456789;
int i = num; 
This will produce an error because going from a long to an int is a narrowing conversion.
int value = 300;
byte b = (byte)value; 
println(b); 
This will give output of
44
because 300 is too large to fit in a byte (more than 127) only part of the number can be stored. You can see more details here.
long num = 4294983648L;
int i = (int)num;
println(i); 
Note that for a very large integer constant you need to add the letter L (either lower-case or upper case) to the end of the constant. This number is too large to fit in an int so only part of the number can be stored. The output is
16352
You can see more details here.
int i = 12345;
float f = i; 
These statements are legal since this is a widening conversion

You can use a cast to convert from real values to integer values. The numbers after the decimal will be truncated (dropped). Note that the decimals are simply dropped, no rounding occurs.

ProgramOutput
float cost = 72.99;
int noDecimals = (int)cost;
println("cost = " + cost + " and noDecimals = " + noDecimals);
double number = -8732.786549932;
long dropped = (long)number;
println("number = " + number + " and dropped = " + dropped); 
cost = 72.99 and noDecimals = 72
number = -8732.7861328125 and dropped = -8732

Constants

So far we have been mostly using literal constants in our programs. Some examples of literal constants:

Literal ConstantType
"Pamela Wallin"string
25int
0.13float

You can also use named constants. You declare a named constant like a variable except that you include the keyword final in front of the declaration. This can make you program easier to read. Using the identifier TAX_RATE instead of the literal 0.l3 makes things clearer. Using named constants will also make it easier to modify your program in the future. Suppose the tax rate changes. Instead of trying to track down the many places you may have used the literal 0.13 in your program you only need to change the one line where you declared the constant. Since you are only making one change, your changes will be more reliable as well. With the short programs we've used so far, using named constants might not seem that important. But when your program get very large you'll be glad you went to the trouble of using named constants. To replace the literals in the above example we could write the following code:

final String SENATOR = "Pamela Wallin";
final int UNIFORM_NUMBER = 25;
final float TAX_RATE = 0.13; 

Exercise 1.6

  1. Suppose that the following declarations have been made:
    int number;
    double mass;
    float density;
    char symbol;
    byte age;
    long limit; 
    State, with reasons, which assignments are illegal.
    1. number = 32.0;
    2. mass = 37;
    3. density = 1.52e0;
    4. symbol = +;
    5. age = 19;
    6. limit = 10000;
  2. Which of the following conversions always require a cast?
    1. int to char
    2. byte to int
    3. double to float
    4. float to short
    5. double to long
    6. int to float
  3. Write a program fragment that declares the short variable subTotal, then declares the int variable grandTotal, initializing grandTotal to zero, and finally copies the value from grandTotal into subTotal.
  4. Find and correct the errors.
    1. short s = 25;
      int i = int(s); 
    2. int i = 50;
      short s = i;