Java Reference
In-Depth Information
boolean
The boolean data type indicates whether something is true or false. In fact, those two words ( true and
false ) are the only two values boolean data types can have.
Note true and false are reserved words in Java. You can't use them for anything other than the value of a
boolean variable. For example, trying to create an int called true throws an error.
char
The char data type holds two 8-bit bytes and is meant to represent characters. It's stored as an unsigned
16-bit integer with a minimum value of 0 and a maximum value of 65,535. However, you should never
use a char to store a number, because that can lead to confusion. Use char variables to hold individual
characters, and you'll avoid trouble.
So why is the maximum so big when relatively few characters exist? Well, when you look at all the
character sets in use all around the world, 65,535 isn't so unreasonable. In fact, it's not enough when
dealing with traditional Chinese.
Tip If you're curious about how to handle characters from other languages, look up Unicode, which is a
standard that defines all the world's characters. For more on handling multi-byte characters, look up variable-
width encoding.
The Special Type: String
String is a type that has some of the characteristics of both a primitive and an object. Strictly speaking, it
is an object; that is, a String class defines it. A String object is a sequence of characters (and Java
provides utilities for turning a String object into a collection of char primitives and for making a String
object from such a collection). It's often handy to work on the collection (the string) rather than on each
character, so Java (and nearly all other programming languages) provides a String object.
Java offers special support for the String class that lets String objects act a little like primitives. In
particular, you can create a String object by using the equals sign ( = ), and you can concatenate String
objects with the plus sign ( + ), as shown in Listing 3-1. Concatenation applies only to strings, by the way.
If you use a plus symbol with the other data types, you either get an error (try adding two boolean values
and you'll see it) or you get the mathematical plus operation that you usually associate with the plus
symbol.
Search WWH ::




Custom Search