Java Reference
In-Depth Information
Stack
Heap
Stack
Heap
Activation record for the
createArray
method
Array of 100
characters
Array of 100
characters
char[] chars
: ref
Activation record for the
main
method
Activation record for the
main
method
char[] chars
: ref
char[] chars
: ref
(a) Executing
createArray
in line 5
(b) After exiting
createArray
in line 5
F
IGURE
7.8
(a) An array of 100 characters is created when executing
createArray
.
(b) This array is returned and assigned to the variable
chars
in the
main
method.
7.16
✓
✓
True or false? When an array is passed to a method, a new array is created and passed
to the method.
Check
Point
7.17
Show the output of the following two programs:
public class
Test {
public static void
main(String[] args) {
int
number =
0
;
int
[] numbers =
new int
[
1
];
public class
Test {
public static void
main(String[] args) {
int
[] list = {
1
,
2
,
3
,
4
,
5
};
reverse(list);
for
(
int
i =
0
; i < list.length; i++)
System.out.print(list[i] +
" "
);
}
m(number, numbers);
System.out.println(
"number is "
+ number
+
" and numbers[0] is "
+ numbers[
0
]);
}
public static void
reverse(
int
[] list) {
int
[] newList =
new int
[list.length];
public static void
m(
int
x,
int
[] y) {
x =
3
;
y[
0
] =
3
;
}
}
for
(
int
i =
0
; i < list.length; i++)
newList[i] = list[list.length -
1
- i];
list = newList;
}
}
(a)
(b)
7.18
Where are the arrays stored during execution? Show the contents of the stack and
heap during and after executing
displayArray
,
countLetters
,
displayCounts
in Listing 7.4.
A variable number of arguments of the same type can be passed to a method and
treated as an array.
Key
Point
You can pass a variable number of arguments of the same type to a method. The parameter in
the method is declared as follows:
typeName... parameterName
In the method declaration, you specify the type followed by an ellipsis (
...
). Only one
variable-length parameter may be specified in a method, and this parameter must be the last
parameter. Any regular parameters must precede it.
Search WWH ::
Custom Search