Java 2 Primer Plus

   

Java™ 2 Primer Plus

By Steven Haines, Steve Potts

Table of Contents
Appendix C.  Answers to Review Questions

A1:

b. label

A2:

Syntax defines the rules by which a program must adhere to be processed by the compiler. Semantics defines the logical rules that make a computer program do what you want it to.

A3:

Computers represent data as a sequence of 1s and 0s.

A4:

A data type is a human-readable tag that represents a specific usage of a computer's memory.

A5:

A short is represented by two bytes while an int is represented by four bytes.

A6:

The first two statements are legal but the third is not because the result of adding two shorts is automatically converted to an integer; an explicit cast to a short is required for this statement to be correct:

short result = ( short )( s1 + s2 );

A7:

Arithmetic promotion means that a variable of a certain data type is automatically converted to a "wider" type during an arithmetic operation.

A8:

You can only assign a byte or a short to a short.

A9:

Casting is explicitly interpreting a variable as a more narrow type; the result of this is dropping bits from the variable. It is accomplished by prefacing the variable with the type to cast the variable to enclosed in parentheses:

int integer = 50; short s = ( short ) integer;

A10:

You define a variable to be a constant by prefacing its data type with the keyword final:

final int i = 50;


       
    Top
     

    Категории