|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
CHAPTER 37
|
Table 37.1 Basic Language Constructs (Java Types) | ||
---|---|---|
Type | Example | Notes |
boolean | boolean flag=false; | A Java boolean is just true or false. It cannot be cast to char or int. |
char | char c[]={A,\uu42',C}; | A Java char is a 16-bit Unicode character. Use the Java String class to manage Unicode strings. |
byte | byte b=0x7f; | 8-bit signed integer (127 .. 127). |
short | short s=32767; | 16-bit signed integer (32,768 .. 32,767). |
int | int i=2; | 32-bit signed integer. |
long | long l=2L; | 64-bit signed integer. Use the suffix L for a long (decimal) literal. |
float | float x=2.0F; | 32-bit IEEE754 number. Use the suffix F for a float literal. |
double | double d=2.0; | 64-bit IEEE754 number (15 significant digits). |
NOTE: Some languages, such as JavaScript and Visual Basic, are weakly typedthey enable the programmer to make new variables on-the-fly and to freely interchange numbers, text, or other values.Java is a strongly typed language. The programmer must explicitly declare the type of each variable. You cant arbitrarily mix or inter-convert types.
To deal with strong typing, focus on classes rather than primitive data types.
By thinking at this higher levelthe class levelyou will need fewer primitive types and they will be less likely to interact with each other in troublesome ways. In addition, you will end up with simpler, more robust programs.
The operators in Table 37.2 are arranged in order of precedence. The compiler will treat the expression 2 + 2 * 2 ^ 2 as 2 + (2 * (2 ^ 2)), for example, executing 2 XOR 2 first, 2 * the result next, and so on.
Table 37.2 Basic Language Constructs (Java Operators) | |
---|---|
Operator | Description |
. | member selection |
[] | array subscript |
() | parentheses/function call |
++, -- | auto-increment/auto-decrement |
*, /, % | arithmetic: multiply, divide, modulo |
+, -, | arithmetic: add, subtract |
<<, >>, >>> | bitwise: shift left, arithmetic shift right, and logical shift right |
<=, <, >, >= | equality: less than or equal to, less than, greater than, greater than or equal to |
==, != | equality: equal to, not equal to |
&, |, ^, ~ | bitwise: AND, OR, Exclusive Or (XOR), and NOT |
&&, ||, ! | logical: AND, OR, and NOT |
? : | conditional expression |
= | simple assignment |
*=, /=, %=, +=, -=, &=, | assignment with operation |
|=, ^=, <<=, >>=, >>>= |
In your own Java programs, use parentheses liberally. Compare how much easier it is to read 2 + (2 * (2 ^ 2)) than 2 + 2 * 2 ^ 2. Not only is the first version clearer, but using parentheses rather than relying on the default precedence hierarchy will also help you avoid a common source of defects. | |
Previous | Table of Contents | Next |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |