Ch.1 Flashcards
(32 cards)
Section1.1 “We begin our Java primer with a program that prints “Hello Universe!” on the screen, which is shown in a dissected form in Figure 1.1. In Java, executable statements are placed in functions, known as methods, that belong to ….” A high level program language, Java uses a class system , with brackets, to contain a class, the main body in a Java program. From the class, methods (or functions) are used to contain actual code that runs from the Java [class]. There are names that can be used which are called an [id…r], and can be combined with letters, numbers, or underscores but with no [sp…s]. There are also reserved words, shown in a table, where any [Un…e cha…r set] are usable. Show section number and page to [blank] answer.
[identifier] [spaces] [Unicode character…]1.1 3.
Section 1.1 "For the most commonly used data types, Java provides the following base types (also called primitive types): boolean : a boolean value true or false char: ...." There are several base types in the Java language, and these include: [bo...n], char, [b...e], [sh...t], [i...t],[lo...g], [f...t], [d...e]. These values are given as types, and a variable may be initialized as such type. If they are uninitialized, they may have default values given, when in a class structure.
[boolean], [byte], [short], [int], [long], [float], [double] 1.1 4&;5
In more complex Java programs, the primary
“actors” are [ob..s]. Every object is an [i..e] of a
class, which serves as the [t..e] of … and …modifying that
data. The critical [m..s] of a class in Java are….
[I..e va..s], which are also called [f..s],
represent the data associated
with an object of a class. Instance variables
must have a [t..e], which can
either be a base type (such as [i..t], [f..t], or
[d..e]) or any class type (also
known as a [r..e t..e] for reasons we soon
explain).
• [M..s] in Java are blocks of code that can
be called to perform actions
(similar to functions and procedures in other
high-level languages)… A method that returns information to the caller without changing … [a..r
m..d], while an [u..e ][ m..d]
is one that may change one or more instance
variables when called.
[objects][instance][type][members][Instance variables][fields][type][int][float][double][reference type][Methods][accessor method][update method] 5&6
This establishes the identifier, c,as a variable of
type Counter, but it does not create
a Counter instance. Classes are known as
[r..e t..s] in Java, and a variable of
that type (such as c in our example) is known as
a [r..e v..e]. A reference
variable is capable of storing the location (i.e.,
[m..y a..s]) of an object from
the declared class. So we might assign it to
reference … [n..l].
[reference types][reference variable][memory address][null] 6
In Java, a [n..w] object is created by using the
new operator followed by a call to
a constructor for the desired class; a constructor
is a method that always shares the
same name as its class. The [n..w] operator returns
a [r..e] to the newly created
instance; the returned reference… .
[new][new][reference] 6
(Such a zero-parameter constructor
is known as a [d..t c..r].) At line 9 we
construct another counter using a
one-parameter form that allows us to specify a
nonzero initial value for the counter.
www.it-ebooks.info
1.2.Classes andObjects
7
Three events occur as part of the creation of a new
instance of aclass:
• A new object is dynamically allocated in memory,
and all instance variables
are initialized to standard default values. The
default values are [n..l] for reference variables and 0 for all base types except [b..n] variables (which
are [f..se] by default).
[default constructor][null][boolean][false] 6&7
…so the object variable [r..rs] to this newly created
object.
[refers] 7
A method’s name combined with the
number and types of its parameters is called a
method’s [s..re], for it takes all of these parts… to
…method call.
[signature] 7
Immediately before the definition of a class, instance
variable, or method in Java, keywords known as
[m..rs] can be placed to convey additional
stipulations about that definition.
Access Control Modifiers
The first set of modifiers …
[modifiers] 9
[a..s c..l m..s], as they control the level of
access (also known as [v..y]) that the defining
class grants to other classes inthe context of a larger
Java program. The ability to limit access among
classes supports a key principle of object-orientation
known as encapsulation (see Section 2.1)…[p..c] … of Code
Fragment 1.2 designates
[p..c c..s] … instances of the Counter class, as
well as to declare variables and
parameters of type Counter. In Java, each
public class must…
[access control modifier][visibility][public][public class] 9
For example, line 5 of Code Fragment 1.2 designates [p..c i..t] getCount() {[r..n] count; } which is why the CounterDemo class may call c.getCount(). If an instance variable is declared as public, dot notation can be used to directly access the variable by code in any other class that possesses a reference to an instance of this class.
[public int][return] 9
The [p..d] class modifier designates that access to the defined aspect is only granted to the following groups of other classes: ◦ Classes that are designated as [s..s] of the given class through inheritance... (We will discuss inheritance as the focus of Section 2.2.)... that belong to the same [p..e] as the given class... • The [p..e] class modifier designates that access to a defined member of a class be granted only to code within that class. Neither subclasses nor...
[protected][subclasses][package][private] 10
Finally, we note that if no explicit access control
modifier is given, the de-fined aspect has what is
known as [p..e-p..e a..s] level. This allows
other classes inthe same package (see Section 1.8)
to have access …
The static Modifier The [s..c] modifier in Java can be declared for any variable or method of a class (or for a nested class, as we will introduce in Section 2.6). When a variable of a class is declared as [s..c], its value is associated with the class as a whole... When a method of a class is declared as [s..c], it too is associated with the class itself...
[package-private access][static][static][static] 11
An array can be initialized by a String[]… A [m..x] for an Array is formatting, that allows [i..n] to be entered for code entry, data, etc. The matrix format …
1.2.Classes andObjects
11
The abstract Modifier
A method of a class may be declared as [a..t], in
which case its signature is provided but without an
implementation of the method body. Abstract
methods are an advanced feature…
A class with one or more abstract methods must
also be formally declared as [a..t], because it is
essentially incomplete. (It is also permissible to
declare aclass as abstract even if it does not contain
any abstract methods.) As a result…
The final Modifier
A variable that is declared with the [f..l] modifier
can be initialized as part of that declaration, but can
never… again be assigned a new value. If it is a base
type, then it is a constant. If a reference variable is
[f..l], then it will always refer to the same…
[matrix][information][abstract][abstract][final][final] nts & 11
If a member variable of a class is declared as [f..l], it will typically be declared as [s..c] as well, because it would be unnecessarily wasteful to have every instance store the identical value ...
[final][static] 11
The vector class inputs data as a matrix for an [a..y], and then uses a variety of [a..s] to calculate the [v..r] directions... The project that is discussed in class...[o..t] an array as a message. The [i..t] is to...
[array][arrays][vector][output][intent] nts
We have already discussed the
significance of modifiers such as [p..c], [p..e],
and [s..c]. The returnType designation defines the
type of value returned by the method. The
methodName can be any valid Java identifier. The list
of parameters and their types declares the local
variables … Each type
declaration typei can be any Java type name …
[public][private][static] 12
Return Types A method definition must specify the type of value the method will return. If the method does not return a value (as with the increment method of the Counter class), then the keyword [v..d] must be used. To return a value in Java, the body of the method must use the [r..n] keyword, followed by a value of the appropriate return type. Here is an example of a method (from the Counter class) with a nonvoid return type: public [i..t] getCount() {
[void][return][int] 12
Parameters
A method’s parameters are defined in a
comma-separated list enclosed in parentheses after the
name of the method. A parameter consists of two
parts, the parameter type and the parameter name …
All parameters in Java are passed [b..e], that is,
any time we pass a parameter to a method, a copy of that parameter is made for use within the method
body. So if we pass an int variable to a method, then
that variable’s integer value is copied.
[by value] 13
A [c..r] is a special kind of method that is used
to initialize a newly created instance of the class so
that it will be in a consistent and stable initial state.
This is typically achieved by initializing each
instance variable of the object (unless the default
value will suffice), …
[constructor] 14
A class can have many constructors, but each must have a different [s..e], that is,each must be distinguished by the type and number of the parameters it takes. If no constructors are explicitly defined, Java provides an implicit [d..t c..r] for the class, having zero arguments and leaving all instance variables initialized to their default values. However, if a class defines one or more...
[signature][default constructor] 14
The StringBuilder Class
An important trait of Java’s String class is that its instances are i..e; once an instance is created
and initialized, the value of that instance cannot be
changed … allows
for great efficiencies and optimizations within the
Java Virtual Machine.
[immutable] 18
To get around this obstacle, Java defines a [w..r
c..s] for each base type. An instance of each wrapper
type stores a single value of the corresponding base…
[wrapper class] 19
A common programming task is to keep track of an
ordered sequence of related values or objects. For
example, we may want a video game to keep track… Again, we would rather not
have to introduce 200 variables inour program just
because…
In such cases, we can save programming effort
by using an a..y, which is a sequenced collection of
variables all of the same type. Each variable, or c..l,
in an array has an i..x, which uniquely refers to the
value stored in that cell. The cells of an array a are
numbered 0,1,2,and so on. We illustrate an array…
[array][cell][index] 20