Java Programing

June 22, 2007

4.3.1 Primitive Types Numbers, characters, and boolean values

Filed under: Java Programming — webmaster @ 7:11 pm

4.3.1 Primitive Types Numbers, characters, and boolean values are fundamental elements in Java. Unlike some other (perhaps more pure) object-oriented languages, they are not objects. For those situations where it’s desirable to treat a primitive value as an object, Java provides ” wrapper” classes (see Chapter 9). One major advantage of treating primitive values as such is that the Java compiler can more readily optimize their usage. Another important portability feature of Java is that primitive types are precisely defined. For example, you never have to worry about the size of an int on a particular platform; it’s always a 32bit, signed, two’s complement number. Table 4.2 summarizes Java’s primitive types. Table 4.2, Java Primitive Data Types Type Definition Boolean trueor false Char 16-bit Unicode character Byte 8-bit signed two’s complement integer Short 16-bit signed two’s complement integer Int 32-bit signed two’s complement integer Long 64-bit signed two’s complement integer Float 32-bit IEEE 754 floating-point value Double 64-bit IEEE 754 floating-point value If you think the primitive types look like an idealization of C scalar types on a 32-bit machine, you’re absolutely right. That’s how they’re supposed to look. The 16-bit characters were forced by Unicode, and ad hoc pointers were deleted for other reasons. But overall, the syntax and semantics of Java primitive types are meant to fit a C programmer’s mental habits. 4.3.1.1 Floating-point precision Floating-point operations in Java are standardized to follow the IEEE 754 international specification, which means that the result of floating-point calculations will generally be the same on different Java platforms. More recent versions of Java have been enhanced to allow for extended precision on platforms that support it. This can introduce extremely small-valued and arcane differences in the results of high-precision operations. Most applications would never notice this, but if you want to ensure that your application will produce exactly the same results on different platforms, use the special keyword strictfp as a class modifier on the class containing the floating-point manipulation. 4.3.1.2 Variable declaration and initialization Variables are declared inside of methods or classes in C style. For example: int foo; double d1, d2; boolean isFun; Variables can optionally be initialized with an appropriate expression when they are declared: int foo = 42; double d1 = 3.14, d2 = 2 * 3.14; boolean isFun = true; - 68

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Sorry, the comment form is closed at this time.

Powered by Java Web Hosting