|
|
Boolean
The final primitive data type is the only one that cannot be interpreted as a number. This is the boolean. A boolean has two possible values: true and false. In Java source code, these are boolean literals. They are not the same as 1 and 0. They are not the same as the strings "true" and "false." They are simply true and false. Thats all.
At the level of the virtual machine, things are a little different. The virtual machine does not have instructions that operate on boolean data. Instead, expressions that involve booleans are compiled using integer instructions. The integer constant 1 is used to represent true, and the integer constant 0 is used to represent false. Dont try to take advantage of this when writing Java source code, though. It wont work.
However, for the purposes of efficiency, Java does allow arrays of booleans to be stored more compactly than arrays of ints. Suns virtual machines make arrays of booleans out of arrays of bytes. In these arrays, true is 01 and false is 00. Other implementations are free to use even more compact representations for boolean arrays, perhaps as little as one bit per value.
Cross-Platform Issues
The preceding section described how primitive data types are represented in Java. This matches fairly closely how numbers are represented on Sparc-Solaris systems. This shouldnt be surprising, given that Java was created by Sun Microsystems programmers who were accustomed to Sparc-Solaris systems.
However, not all systems represent data in the same way. Most annoyingly, roughly half of computer architectures are Little-Endian rather than Big-Endian. (Little-Endian and Big-Endian architectures are discussed shortly). Furthermore, some programming languages allow the use of unsigned numeric quantities. And although Javas native integer format is 32 bits, many other systems prefer 16-bit or 64-bit ints. Although Java is supposed to be above such concerns, when you have to deal with legacy data from programs written in other languages, you need to be aware of these differences.
Byte order
Which two mighty powers have, as I was going to tell you, been engaged in a most obstinate war for six and thirty moons past. It began upon the following occasion. It is allowed on all hands, that the primitive way of breaking eggs, before we eat them, was upon the larger end: but his present Majestys grandfather, while he was a boy, going to eat an egg, and breaking it according to the ancient practice, happened to cut one of his fingers. Whereupon the Emperor his father published an edict, commanding all his subjects, upon great penalties, to break the smaller end of their eggs. The people so highly resented this law, that our histories tell us there have been six rebellions raised on that account; wherein one Emperor lost his life, and another his crown. These civil commotions were constantly fomented by the monarchs of Blefuscu; and when they were quelled, the exiles always fled for refuge to that empire. It is computed that eleven thousand persons have, at several times, suffered death, rather than submit to break their eggs at the smaller end. Many hundred large volumes have been published upon this controversy: but the books of the Big-Endians have been long forbidden, and the whole party rendered incapable by law of holding employment. During the course of these troubles, the Emperors of Blefuscu did frequently expostulate by their ambassadors, accusing us of making a schism in religion, by offending against a fundamental doctrine of our great prophet Lustrog, in the fifty-fourth chapter of the Blundecral (which is their Alcoran). This, however, is thought to be a mere strain upon the text: for the words are these: That all true believers shall break their eggs at the convenient end: and which is the convenient end, seems, in my humble opinion, to be left to every mans conscience, or at least in the power of the chief magistrate to determine. Now the Big-Endian exiles have found so much credit in the Emperor of Blefuscus court, and so much private assistance and encouragement from their party here at home, that a bloody war has been carried on between the two empires for six and thirty moons with various success; during which time we have lost forty capital ships, and a much greater number of smaller vessels, together with thirty thousand of our best seamen and soldiers; and the damage received by the enemy is reckoned to be somewhat greater than ours.
Jonathan Swift, Gullivers Travels, Chapter IV
|