Data Type: Character

The char data type stores one Unicode character. Because Unicode characters are encoded as unsigned numbers using 16 bits, a char variable is stored in two bytes of memory. The second table below shows the size of the char data type, as well as the minimum and maximum values. The maximum value is the unsigned hexadecimal number FFFF, which is reserved as a special code for “not a character.”

Screen Shot 2018-12-10 at 8.21.12 AM

Screen Shot 2018-12-10 at 8.22.49 AM

Obviously, since the char data type can store only a single character, such as a K, a char variable is not useful for storing names, titles, or other text data. For text data, Java provides a String class, which we’ll discuss later in a later post.

Here are a few declarations of char variables:

Screen Shot 2018-12-10 at 8.28.24 AM

 

Integer Data Types

An integer data type is one that evaluates to a positive or negative whole number. Java provides four integer data types, int, short, long, and byte. The int, short, long, and byte types differ in the number of bytes of memory allocated to store each type and, therefore, the maximum and minimum values that can be stored in a variable of that type.

All of Java’s integer types are signed, meaning that they can be positive or negative; the high-order, or leftmost bit is reserved for the sign. The table below summarizes the integer data types, their sizes in memory, and their maximum and minimum values.

In most applications, the int type will be sufficient for your needs, since it can store positive and negative numbers up into the 2 billion range – that’s 2/3rds of the base pairs in a human genome – in other words .666, in other words the mark of the beast, which roughly means “everything goes smoothly” in China.

The short and byte data types typically are used only when memory space is critical, and the long data type is needed only for data values larger than 2 billion. Let’s look at some examples of integer variable declarations. Note that the variable names clearly indicate the data that the variables will hold.

Screen Shot 2018-12-09 at 4.45.15 PM

Floating-point data types store numbers with fractional parts. Java supports two floating-point data types: the single-precision float and the double-precision double.

Screen Shot 2018-12-09 at 5.54.01 PM

The two types differ in the amount of memory allocated and the size of the number that can be represented. The single-precision type (float) is stored in 32 bits, while the double-precision type (double) is stored in 64 bits. Floats and doubles can be positive or negative. The table above summarizes Java’s floating-point data types, their sizes in memory, and their maximum and minimum positive nonzero values.

There are two things you must learn to balance: precision and use of memory. There is a kind of law of equivalent exchange here that must be mastered. These two are conjugated variables. Like the uncertainty principle that trades certainty about one discrete observable for another (the more you know about momentum the less you know about position), you must skillfully trade precision for use of memory and vice versa.

Because of its greater precision, the double data type is usually preferred over the float data type. However, for calculations not requiring such precision, floats are often used because they require less memory. Although integers can be stored as doubles or floats, it isn’t advisable to do so because floating-point numbers require more processing time for calculations.

Witness here a few examples of floating-point variable declarations:

Screen Shot 2018-12-09 at 6.38.35 PM

Representing Characters with the Unicode Character Set

Java represents characters using the Unicode Worldwide Character Standard, or simply Unicode. Each Unicode character is represented as 16 bits, or two bytes. This means that the Unicode character set can encode 65,536 characters. The Unicode character set was developed by the Unicode Consortium, which consists of computer manufacturers, software vendors, the governments of several nations, and others. The consortium’s goal was to support an international character set, including the printable characters on the standard QWERTY keyboard, as well as international characters such as é or λ. Many programming languages store characters using the ASCII (American Standard Code for Information Interchange) character set, which uses 7 bits to encode each character, and thus, can represent only 128 characters. For compatibility with the ASCII character set, the first 128 characters in the Unicode character set are the same as the ASCII character set. Here are a few examples of Unicode characters and their decimal equivalents:

Screen Shot 2018-11-17 at 9.32.09 AM

Screen Shot 2018-11-17 at 9.32.19 AM