| [ directory ] |
|
9.2 TypesThe numbers in all the previous examples have been integers: numbers with no decimal part.[1] Division on integers works slightly differently from division on numbers in the real world. For example, the following evaluates to 3, as 3 is the largest integer that, when multiplied by 2, is less than 7:
7 / 2 If a program wanted this to evaluate to 3.5, the numbers should be Java doubles, or double-precision floating-point numbers, instead of integers. This would be expressed by using decimal points, as in 7.0 / 2.0 This example illustrates an important and fundamental aspect of the Java programming language. Everything in Java has a value, such as 2 or 3.1415, and a type. This was also true in SQL; when defining a column, it is necessary to assign a name, such as article_id, as well as a type, such as int. If Java is expecting an expression of a certain type in a particular context, that will restrict the possible values that can be used in that context. Java supports a number of built-in, or primitive, types. We have already seen doubles and integers, which Java calls ints. Java can also manipulate text, using the String type. Strings are represented by surrounding them with quote marks: "This is a Java string!" It is important to keep in mind that "2" and 2 are very different things to Java, even though they may look the same. Do not be fooled by what they look like; the types are different, which is what matters. String expressions can also be more complex: "This is a " + "Java string!" Here, the + operator is used to denote concatenation, which simply means appending two strings together. The result of this expression is the same as the previous one. This is the only important instance in which an operator does two different things based on the type of the expression it is operating on.[2] Technically, + applied to two doubles does something different from + applied to two integers, but for the most part, differences like this can be ignored.
Under some circumstances, Java automatically converts part of an expression from one type to another. In the following, for example, the 2 will be internally converted to 2.0, and the result of the expression will be a double: 7.0 / 2 The full set of type-conversion rules is available in any book on Java, but the general rule is that Java never automatically goes from one type to another with less information. The double-precision floating-point number 2.0 has more information than 2, so this conversion can happen automatically. Java will also automatically convert most types to a String, when the result of an expression should be a String. The following expression will evaluate to the string "22": "2" + 2 The second 2 is first converted to a String, yielding "2", which will then be appended to the first String. It is also possible to convert a number of one type explicitly into another type, which is done by specifying the target type in parentheses before the value: (double) 3 This example specifies a double number built from 3, which will be equivalent to 3.0. It is also possible to do conversions that lose data in this way, such as (int) 6.75 This will yield the value 6, the result of simply chopping off the decimal part. Java would never perform such a conversion automatically, but it is perfectly valid for a programmer to do so. This kind of moving from one type to another is called casting. It may help to think of an actor being "type cast," meaning forced into a particular role. Casting will become very important once objects and classes have been introduced. |
| [ directory ] |
|