Recitation 4

Today, we went over some of the homework problems in the Java Exercises sheet. If you're still confused, send me email with detailed questions.

Contents:

char versus String type variables

Casting

Installing the tio package

Bonus: Thinking about how to program


Subtle Differences of Variable Types

A "char" variable is almost the same as an "int" type variable. For all technical purposes, both types of variable are just placeholders for integers.

More specifically, though, a char type only uses 2 bytes of memory, whereas an int type takes 4 bytes. Bottom line? We use char because sometimes it is impractical to waste the memory storage space used up by a 4-byte int variable.

String classes are made up of arrays of characters. In your HelloWorld programs, the "hello, World!" string is a sequence of individual characters.

Each character has a corresponding integer value. The popular one to remember is capital A, starting at 65. B is 66, C is 67, and so on. If you're interested, take a look at the ASCII Decimal to Character chart.

If you look at the chart, you will see lowercase a corresponds to the decimal integer value 97, and capital B corresponds to 66. If you add these together, you'll get 163. (Hence the answer to part 15c of the Java Exercises Worksheet. The reason why the characters were interpreted as integers, however, is just because of the context in which they were used.

 

char a = 'z';

System.out.println("a = "+a);  starts with a string (because of the double quotes). The concatenation operator (the + sign) will expect the right-hand side of the equation to match the left-hand side. So if the left-hand side is a string type, then the right-hand side should probably be interpreted as a string also.

Thus the output is

a = z

System.out.println(a+a); gives you no clue as to what the arguments of the println() function should be interpreted as. The left-hand side of the + operator could be anything for all we know. In this case, Java will just try to use the variable a as an integer type. This will convert the character 'z' to its corresponding ASCII value in the chart, which is 122. Thus the answer will be

244

Hopefully that is more clear now.


Casting

"Casting" is when you purposely try to force the value of one variable to be another type. For instance, you can cast char type variables as integers, which has the effect of presenting that variable's value as a number rather than an alphanumeric character.

To cast a variable to a different type, put the type of the variable you want to cast to in parenthesis. This should go in front of the expression you would like to recast.

 

For instance,

char a = 'z';        //declare a char type variable with contents 'z'

System.out.println(a);        //prints out the character z, because the default interpretation is to assume the variable a is a char type.

System.out.println((int)a);  //prints out 122, because we re-cast the variable a to behave like an integer. The ASCII value for the character z is 122.

 

Casting a decimal value to integer will chop off everything to the right of the decimal place.

double myVal = 3.1416;            //Declare a decimal value

System.out.println((int)myVal);         //Cast to integer. The program will print 3 to the screen.

 


Installing the tio package

You will need to install the tio package to make your life simpler for reading in input from the keyboard.

The fourth HelloWorld program covers the basic usage of tio.

 

To install the tio package, you need to use the commands

cp ~hclane/public/tio.jar .

jar xvf tio.jar

while you are in your working directory.