Bits, Types, Characters and Type Casting in Java. Learning objectives and exercises from the University of London BSc in Creative Computing.
LEARNING OBJECTIVES
1. Understand that different variables of different types have different sizes.
A variable consists of some bytes (8 bits) of RAM. Different variable types have a different amount of memory required. A boolean variable only requires one bit as it can only be true or false. For storing characters, Java allocates two bytes which provides space for 216 different characters to be stored (which is 65,536 characters). For storing int variables, Java allocates 4 bytes, which is 232 integers.
2. Explain type casting.
You can force Java to treat an expression as belonging to a particular type by including the type in brackets before the expression. So the expression System.out.println((char)37); will print out the char ‘%’ rather than the int 37.
3. Explain the relationship between characters and Unicode.
Unicode is a method of allocating a particular integer value to a character so that the computer is able to display different types of natural language and alphabet. Each character and symbol in a specific alphabet is allocated a number within a specific Unicode encoding. There are many Unicode encodings. Some examples are UTF-8, UTF-16, ASCII, Mac OS Roman etc. Each encoding has a different mapping of characters to integers. Follow the Unicode link to Wikipedia for more information.
4. Explain why the read method returns an int.
As Java allocates all 216 possible spaces in char memory to characters there is no room for any ‘special characters’ like a ‘line return’ or ‘end of file’. These special characters have to therefore be stored in a variable type that has more memory space. The read() method uses the return type int which has 232 possible integers that it can store. Therefore we can use the read() method to input characters other than just char characters. Because they are stored as Unicode integers, we must type cast the output of read() back to char for proper display.
EXERCISES
Q: Research each type in Java and find out how big it is.
The size of primitive data types (those that are automatically part of the Java program language) can be found here. Below are the sizes of the key data types:
int: 32 bits
long: 64 bits (twos-complement)
float: 32 bit (floating point)
double: 64 bit (floating point)
boolean: 1 bit
char: 16 bits
short: 16 bits (twos-complement)
byte: 8 bits (two-complement)
String is not strictly a primitive data type, rather it is an object from the class java.lang.String.
Q: Write a program to check whether or not an int can be cast as a boolean.
An int cannot be cast as a boolean. The compiler complains if you try to do this.
Q: Write a program to check whether or not a boolean can be cast as an int.
No, this does not work either – a boolean cannot be cast as an int.
Q: Write a program to check whether or not a float can be cast as an int.
CODE: FloatToInt
class FloatToInt
{
public static void main(String[] args)
{
float in = 1.0000f; //the'f' defines the value as a float
int out = (int)in;
System.out.println(out);
}
}
Yes, it is possible to cast a float as an int. You will necessarily lose data and accuracy by doing this.
Q: Write a program to check whether or not a double can be cast as a float.
CODE: DoubleToFloat.java
class DoubleToFloat
{
public static void main(String[] args)
{
double in = 1.0;
float out = (float)in;
System.out.println(out);
}
}
Yes, a double can be cast to a float variable.
Q: Write a program to check whether or not an int can be cast as a float.
CODE: IntToFloat.java
class DoubleToFloat
{
public static void main(String[] args)
{
double in = 1.0;
float out = (float)in;
System.out.println(out);
}
}
Yes, it is possible to cast an int as a float.
Q: Write a program to check whether or not a float can be cast as a char.
CODE: FloatToChar.java
class FloatToChar
{
public static void main(String[] args)
{
float in = 100.0f; //f designates the value is a float
char out = (char)in;
System.out.println(out);
}
}
Yes, it is possible to cast a float as a char.
Q: Write a program to check whether or not an int can be cast as a short.
class IntToShort
{
public static void main(String[] args)
{
int in = 1000;
short out = (short)in;
System.out.println(out);
}
}
Yes, an int can be cast as a short but for large integers (above 32,768 and below -32,768) a loss of data and accuracy will occur.
Pages
-
Categories
-
Archives

