Primitives, Casting and Parsing

Primitives are data types defined by how they are interpreted and how much memory they take up. For example, a char is a sixteen bit series of 0s and 1s in memory, which is interpreted according to the ASCII code set, and so each char is treated as a character. Whereas an int is thirty-two 0s and 1s in memory interpreted as integer values, and short is a sixteen bit series of 0s and 1s interpreted as integer values; naturally, the range of possible int values is much more than short values.

The most common primitives we will work with are boolean, char, int, and double.


A boolean is a type which returns either true or false. It is used to evaluate conditional expressions, which in turn determine the flow of control of a program. For example:

boolean b = true;
    System.out.println("The first possible path chosen.");
System.out.println("The second possible path chosen."); }

More than one boolean can make up compound boolean expression, but ultimately all boolean expressions evaluate to either trur or false. The Java operators and (&&) and or (||) are used to evealuate compound boolean expressions, with parentheses taking precedence over and, and and taking precedence over or. For example:

boolean b1 = true;
boolean b2 = false; 
boolean b3 = true;
boolean b4 = false;
if((b1 || b2) && b3 || b4){ System.out.println("Compound condition ultimately evaluates to true."); } else{ System.out.println("Compound condition ultimately evaluates to false."); }

& Casting

char is the Java 16 bit type which can represent any one of 65,536 possible characters, usually from the standard UNICODE set. And so characters like 'a' and 'A' and '1' and '!' and lots of other international language charcters are what char represents. It is often useful to convert types, and in particular with char, it is often useful to convert a char to its integer equivalent. One reason for this is to determine its lexicographic order for sorting and alphabetization purposes. Another reason is that UNICODE numbers can be used to generate specific UNICODE characters.

To cast is to change one type to another. There are two ways that this can occur, implicity, or explicitly. An implicity cast ocurrs any time we add together types of different sizes. The smaller type is cast to the larger type. So if a short (16 bit integer) is added to an int (32 bit integer), the short is cast into being an int. Which makes sense, since the other way around it would not work if the larger type was so large a value that it could not "fit" into the number of bits of the smaller type.

But if we really do want to convert a larger type to a smaller types, as would be the case in converting a int (32 bits) to its char value (16 bits), we have to be explicity about it. And we need to be careful in doing so since we could easily make a logic error occurs if we try to convert a value which will not be able to be represented by the smaller type. Anyway, if we are sure the particular value being represented by the bigger type will fit into the smaller type, here's how we do it:

int i = 65;
char c = (char) i;

The output of this would be A since 65 is the decimal equivalent of A in UNICODE.

char c1 = 'B';
int i1 = c1 + 4;

The output woud be 70, since c1 converted to an integer would be 66, and 66 + 4 = 70. There is no need for explicit casting in this case, since a char and an int will result in an int result, and that is what is being assigned to.

int, double and Parsing

int the most common Java type for working with integers; it is 32 bits. The other types used for integers in Java are short (16 bits) and long (64 bits).

doubleis the most common Java type for working with real numbers (those which may include a fractional portion; i.e. decimal numbers). double uses 64 bits. The other Java type for real numbers is float, and it only used 32 bits, which results in low accuracy in values which should have many decimal places.

When any of these are added together without using explicity casting, as with chars and ints, the larger type is the result type. And when assigning one to the other, a smaller type can be assigned to a larger type, but if trying to go the other way, casting is needed. For example:

int i = 2342;
double d = 987.85;
double d1 = i;

Above no explicit casting is required, but below it will be

int  i = 2342;
double d = 987.85;
i = (int) d;

Note that in the second example, the fractional portion will be dropped, with no rounding.


Parse is a word which means to change from one type to another. Sometimes we need to use class functions to convert from one type to another and this is the case for converting Strings to number types. For example, as in a GUI environment where the user types in a number to a text field, it must be converted to an in using the Wrapper class Integer's method parseInt.

int i = Integer.parseInt(jTextField1.getText());

But goint the other way; from an int to a String, since we have a smaller type and a bigger type, it happens implicitly if we simply add an empty string, as in this example:

String s = 2342 + "";

See Also from the first spiral:

Console Input & Java Types

More on Java Types