Our previous examples always extracted string information from text fields. We may also obtain numeric and logical information; however, this information must be converted from a string to the appropriate type before the information is usable.
If you know C or C++, the Java primitive data types will come as no surpise. These primitive types are listed in the following table. Several aspects of this table are worth noting:
| Type name | Encoding | Wrapper Class |
|---|---|---|
| boolean | true, false | Boolean |
| byte | 8-bit signed integer | Byte |
| char | 16-bit unsigned integer in UniCode characters | Character |
| short | 16-bit signed integer | Short |
| int | 32-bit signed integer | Integer |
| long | 64-bit signed ineger | Long |
| float | 32-bit in IEEE 754 standard | Float |
| double | 64-bit in IEEE 754 standard | Double |
The Primitive program uses the Stdin/TT> class to read in each of the primitive types. The link to the program is contained in the following table.
| Primitive Types Program |
Java also provided three additional numeric classes. Two of these classes, BigInteger and BigDecimal, let a programmer create numbers with greater precision than the primitive types. Thus, we can create objects that have a greater integer precision than long, or a greater floating point precision than double. The remaining class, math contains methods that implement trigonometric and other common mathematical functions, e.g., exponentiation, square roots, and flooring and ceiling operations.
The Wrapper Classes
Each of the wrapper classes contains constructors that have either the corresponding primitive type as a parameter or a string. The constructors create a new object of the wrapper class. We will use the wrapper classes when we create polymorphic functions, that is, functions that work with many types of data.
The following table uses the Integer class to illustrate wrapper class methods. Each of the remaining numeric wrapper classes contains the same methods as listed in this table. Of course, the name of the constructor matches the name of the wrapper class.
| Method | Explanation |
| Integer(int i) | Constructor|
| Integer(String str) | Constructor |
| double doubleValue() | Return double equivalent |
| float floatValue() | Return float equivalent|
| int intValue() | Return int equivalent |
| long longValue() | Return long equivalent |
| short shortValue() | Return short equivalent |
| boolean equals(Object obj) | Return true if parameter matched Integer object; return false otherwise. |
| String toString(int i) | Return String equivalent |
| String toString(int i, int radix) | Return String equivalent using specified radix. |
Operator Hierarchy
The Java math operators and precedence hierarchy is identical to the hierarchy used by C, C++, JavaScript and Perl. A table of the operators, listed by precedence follows:
Arithmetic Operators
| Operator | Description |
|---|---|
++ -- + - |
Increment Decrement Unary Plus Unary Minus |
* / % |
Multiplication Division Modulus |
+ - |
Addition Subtraction |
<< >> >>> |
Left bit shift Right bit shift, sign extension Right bit shift, zero extension |
|
Click Me! Complete Java Operator Hierarchy |
robohelp server 9 Xilisoft DVD Creator 3 MAC microsoft mappoint 2009 north america adobe contribute cs4 mac CodeGear RAD Studio 2007 Architec cs5.5 creative suite 5 master collection mac Corel PhotoImpact X3 ACDSee Pro 4 office 2010 illustrator cs5 Native Instruments Massive microsoft office 2007 ultimate adobe dreamweaver cs3 Parallels Desktop 4 MAC microsoft autoroute 2010 europe microsoft office 2010 home and student adobe captivate 5.5