2. Order of evaluation is guaranteed (generally left to right). In particular, the sequence point rule from C++ is not needed. Thus nonsense such as x + + + x + + has a precise behavior in Java. 3. Only the C-style type conversion is allowed. 4. b o o l e a n is a primitive type, thus removing many of the common errors in C++, such as if(x=y) ... . 5. There is no comma operator, except in f o r loop expressions. 6. Java provides a labelled b r e a k statement. 7. All functions must be class methods. 1.2 Solutions To Exercises In Short 1.1 Java source files end in . j a v a . Compiled files (containing j-code or byte-codes) end in . c l a s s . 1.2 / / , which extends to the end of the line and / * and / * * , both of which extend to a * / . Comments do not nest. 1.3 b o o l e a n , b y t e , s h o r t , c h a r , i n t , l o n g , f l o a t , and d o u b l e . 1.4 * multiplies two primitive values, returning the result and not changing its two arguments. * = changes the left-hand argument to the product of the left-hand argument and the right-hand argument. The right-hand argument is unchanged. 1.5 Both the prefix and postfix increment operators add one to the target variable. The prefix operator uses the new value of the variable in a larger expression; the postfix operator uses the prior value. 1.6 The w h i l e loop is the most general loop and performs a test at the top of the loop. The body is exe- cuted zero or more times. The d o loop is similar, but the test is performed at the bottom of the loop; thus the body is executed at least once. The f o r loop is used primarily for counting-like iteration and consists of an initialization, test, and update along with the body. Full file at
1.7 b r e a k is used to exit a loop. A labelled b r e a k exits the loop that is marked with a label. b r e a k is also used to exit a s w i t c h statement, rather than stepping through to the next case. 1.8 The c o n t i n u e statement is used to advance to the next iteration of the loop. 1.9 Method overloading allows the reuse of a method name in the same scope as long as the signatures (parameter list types) of the methods differ. 1.10 In call-by-value, the actual arguments are copied into the method’s formal parameters. Thus, changes to the values of the formal parameters do not affect the values of the actual arguments. In Theory 1.11 After line 1, b is 6, c is 9, and a is 13. After line 2, b is 7, c is 10, and a is 16. After line 3, b is 8, c is 11, and d is 18. After line 4, b is 9, c is 12, and d is 21. 1.12 The result is t r u e . Note that the precedence rules imply that the expression is evaluated as (true&&false) || true . 1.13 The behavior is different if s t a t e m e n t s contains a c o n t i n u e statement. 1.14 Because of call-by-value, x must be 0 after the call to method f . Thus the only possible output is 0 .
You've reached the end of your free preview.
Want to read all 30 pages?
- Spring '13
- Object-Oriented Programming, derived class, Subroutine , Cosc 3331 Data Structures and Algorithm Analysis in Java