java ch7 Part1

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/33

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 11:40 PM on 4/14/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

34 Terms

1
New cards

Array objects:

Data structures consisting of related data items of the same type. Make it convenient to process related groups of values. Remain the same length once they are created.

2
New cards

Variable-length argument lists:

Can create methods are with varying numbers of arguments.

3
New cards

ArrayList collection:

Similar to arrays. Dynamic resizing; resize as necessary to accommodate more or fewer elements.

4
New cards

Array:

Group of variables (called elements) containing values of the same type. Arrays are objects so they are reference types. Elements can be either primitive or reference types.

5
New cards

Refer to a particular element in an array:

Use the element’s index. Array-access expression, the name of the array followed by the index of the particular element in square brackets, [].

6
New cards

The first element in every array has an

index of zero.

7
New cards

The highest index in an array is

one less than the number of elements in the array.

8
New cards

Array names follow the same conventions as

other variable names.

9
New cards

An index must be a

nonnegative integer. Can use an expression as an index.

10
New cards

An indexed array name is an

array-access expression. Can be used on the left side of an assignment to place a new value into an array element.

11
New cards

Every array object knows its own

length and stores it in a length instance variable. length cannot be changed because it’s a final variable.

12
New cards

an index must be an

int value or a value that can be promoted to an int.

13
New cards

Array object creation:

Created with keyword new. You specify the element type and the number of elements in an array-creation expression, which returns a reference that can be stored in an array variable.

14
New cards

Declaration and array-creation expression for an array of 12 int elements

int[] c = new int[12];

Can be performed in two steps as follows:

int[] c; // declare the array variable c = new int[12]; // creates the array

15
New cards

In an array declaration, square brackets following a type indicate that

a variable will refer to an array (i.e., store an array reference).

16
New cards

When an array is created, each element of the array receives a

default value; Zero for the numeric primitive-type elements, false for boolean elements and null for references.

17
New cards

When the element type and the square brackets are combined at the beginning of the declaration,

all the identifiers in the declaration are array variables. For readability, declare only one variable per declaration.

18
New cards

Every element of a primitive-type array contains a value of the

array’s declared element type. Every element of an int array is an int value.

19
New cards

Every element of a reference-type array is a reference to an

object of the array’s declared element type. Every element of a String array is a reference to a String object.

20
New cards

Array initializer:

A comma-separated list of expressions (called an initializer list) enclosed in braces. Used to create an array and initialize its elements. Array length is determined by the number of elements in the initializer list. int[] n = {10, 20, 30, 40, 50}; Creates a five-element array with index values 0–4. Compiler counts the number of initializers in the list to determine the size of the array.

21
New cards

final Keyword:

used to restrict changes and make code more secure and predictable. It can be applied to variables, methods, and classes to prevent modification, overriding, or inheritance. This helps in creating constant values, stable methods, and immutable classes.

22
New cards

final variables must be initialized before they are used and cannot be

modified thereafter. An attempt to modify a final variable after it’s initialized causes a compilation error. An attempt to access the value of a final variable before it’s initialized causes a compilation error.

23
New cards

constant variables are also called

named constants. they often make programs more readable.

24
New cards

Many programs present data to users in a

graphical manner. Numeric values are often displayed as bars in a bar chart. Longer bars represent proportionally larger numeric values.

25
New cards

A simple way to display numeric data is with

a bar chart that shows each numeric value as a bar of asterisks (*).

26
New cards

Using the Elements of an Array as Counters:

Sometimes, programs use counter variables to summarize data, such as the results of a survey.

27
New cards

Array frequency must be large enough to store six counters.

We use a seven-element array in which we ignore frequency[0]. More logical to have the face value 1 increment frequency[1] than frequency[0].

28
New cards

If a piece of data in the responses array is an invalid value, such as 14, the program attempts to add 1 to frequency[14], which is outside the bounds of the array. Java doesn’t

allow this. JVM checks array indices to ensure that they are greater than or equal to 0 and less than the length of the array—this is called bounds checking. If a program uses an invalid index, Java generates a so-called exception to indicate that an error occurred in the program at execution time.

29
New cards

An exception indicates a problem that occurs while a program executes. The name “exception” suggests that the problem occurs

infrequently, if the “rule” is that a statement normally executes correctly, then the problem represents the “exception to the rule.” Exception handling helps you create fault-tolerant programs that can resolve (or handle) exceptions. When the JVM or a method detects a problem, such as an invalid array index or an invalid method argument, it throws an exception, that is, an exception occurs.

30
New cards

To handle an exception, place any code that might throw an exception in a

try statement. The try block contains the code that might throw an exception.

31
New cards

The catch block contains the code that

handles the exception if one occurs. You can have many catch blocks to handle different types of exceptions that might be thrown in the corresponding try block.

32
New cards

When the program encounters the invalid value 14 in the responses array, it attempts to add 1 to frequency[14], which is outside the bounds of the array, the frequency array has only six elements (with indexes 0–5). Because array bounds checking is performed at execution time, the JVM generates an

exception—specifically an ArrayIndexOutOfBoundsException to notify the program of this problem. At this point the try block terminates and the catch block begins executing—if you declared any local variables in the try block, they’re now out of scope.

33
New cards

The catch block declares an exception parameter (e) of type (IndexOutOfRangeException). Inside the catch block, you can use

the parameter’s identifier to interact with a caught exception object.

34
New cards

The exception object’s toString method returns the error message that’s implicitly stored in the

exception object. The exception is considered handled when program control reaches the closing right brace of the catch block.