Of Special Interest to C Programmers

   

Because Java is an evolutionary outgrowth of C and C++, the expression syntax for the three languages is similar in many ways. If you have programmed in either of the other two languages, you've no doubt seen functionality in Java by now that is identical to what you've used before. You have also seen cases where Java has added functionality not found elsewhere. It is also likely that you've noticed some of the cases where Java has intentionally taken away functionality because it proved itself to be troublesome or error-prone elsewhere. It takes time to get used to these additions and deletions, but at least deletions are easy to keep track of because the compiler will quickly let you know when you try to use a feature that doesn't exist. Learning the new features added by Java is part of what you're doing right now, so that will come with time. The hardest part is the case that's left. You will often use syntax in Java that looks the same as that of another language but does not function in the same way. This is where an attention to detail and a good knowledge of Java fundamentals are key.

One difference to consider is that order of evaluation is guaranteed in Java, and is generally undefined or implementation-specific in C. There are also differences in how operators can be applied. In Java, the remainder (%), increment (++), and decrement ( “ “) operators are defined for all primitive data types (except boolean ); in C, they are defined only for integers.

There is also a slight difference related to how you might be accustomed to using expressions. It isn't necessary for the result of an expression in C/C++ to be used. For example,

 x + y; 

is a perfectly valid C++ statement, assuming x and y have been declared. This syntax is used when the side effects introduced by the evaluation of the operands are useful, but the value of the overall expression is not. This is a compiler error in Java; the result of an expression like this must be used as part of an assignment statement or as a method parameter.

You have already seen that Java's strong typing is employed heavily for the boolean type. Relational and equality operators in Java produce a boolean result; in C, they produce results of type int. Furthermore, the logical operators in Java are restricted to only work with boolean operands.

In C, using the right-shift operator (>>) on a signed quantity results in implementation- specific behavior. As discussed in the next section, Java avoids this confusion by using two different right-shift operators: one that preserves the sign bit and another that treats it like any other bit.

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net