Java Reference
In-Depth Information
frame or activation record. When a method call is made, the stack frame for that method call is
pushed onto the method-call stack. When the method returns to its caller, its stack frame call is
popped off the stack and the local variables are no longer known to the program.
• If there are more method calls than can have their stack frames stored on the method-call stack,
an error known as a stack overflow (p. 210) occurs. The application will compile correctly, but
its execution causes a stack overflow.
Section 6.7 Argument Promotion and Casting
• Argument promotion (p. 210) converts an argument's value to the type that the method expects
to receive in its corresponding parameter.
• Promotion rules (p. 210) apply to expressions containing values of two or more primitive types
and to primitive-type values passed as arguments to methods. Each value is promoted to the
“highest” type in the expression. In cases where information may be lost due to conversion, the
Java compiler requires you to use a cast operator to explicitly force the conversion to occur.
Section 6.9 Case Study: Secure Random-Number Generation
• Objects of class SecureRandom (package java.security ; p. 213) can produce nondeterministic
random values.
SecureRandom method nextInt (p. 214) generates a random int value.
• Class SecureRandom provides another version of method nextInt that receives an int argument
and returns a value from 0 up to, but not including, the argument's value.
• Random numbers in a range (p. 214) can be generated with
int number = shiftingValue + randomNumbers.nextInt( scalingFactor );
where shiftingValue specifies the first number in the desired range of consecutive integers, and
scalingFactor specifies how many numbers are in the range.
• Random numbers can be chosen from nonconsecutive integer ranges, as in
int number = shiftingValue +
differenceBetweenValues * randomNumbers.nextInt( scalingFactor );
where shiftingValue specifies the first number in the range of values, differenceBetweenValues rep-
resents the difference between consecutive numbers in the sequence and scalingFactor specifies
how many numbers are in the range.
Section 6.10 Case Study: A Game of Chance; Introducing enum Types
•An enum type (p. 221) is introduced by the keyword enum and a type name. As with any class,
braces ( { and } ) delimit the body of an enum declaration. Inside the braces is a comma-separated
list of enum constants, each representing a unique value. The identifiers in an enum must be
unique. Variables of an enum type can be assigned only constants of that enum type.
• Constants can also be declared as private static final variables. Such constants are declared by
convention with all capital letters to make them stand out in the program.
Section 6.11 Scope of Declarations
• Scope (p. 222) is the portion of the program in which an entity, such as a variable or a method,
can be referred to by its name. Such an entity is said to be “in scope” for that portion of the pro-
gram.
• The scope of a parameter declaration is the body of the method in which the declaration appears.
• The scope of a local-variable declaration is from the point at which the declaration appears to the
end of that block.
Search WWH ::




Custom Search