Chapter 1
- Java
programs consist of pieces called classes. Classes include pieces called
methods that perform tasks and return information when the tasks are
completed.
- Most
Java programmers take advantage of the rich collections of existing
classes in the Java class libraries, which are also known as the Java APIs
(Application Programming Interfaces).
- Java
programs normally go through five phases – edit, compile, load, verify,
and execute.
- Phase
1 consists of editing a file with an editor. You type a program using the
editor, make the corrections and save the program on a secondary storage
device, such as your hard drive.
- Integrated
development environments (IDEs) provide tools
that support software development, including editors for writing and editing
programs and debuggers for locating logic errors.
- In
Phase 2, the programmer uses the command javac
to compile a program.
- If a
program compiles, the compiler produces a .class file that contains the
compiled program.
- The
Java compiler translates Java source code into bytecodes
that represent the tasks to be executed. Bytecodes
are executed by the Java Virtual Machine (JVM).
- In
Phase 3, loading, the class loader takes the .class files containing the
program’s bytecodes and transfers them to
primary memory.
- In
Phase 4, as the classes are loaded, the bytecode
verifier examines their bytecodes to ensure that
they are valid and do not violate Java’s restrictions.
- In
Phase 5, the JVM executes the program’s bytecodes.
- The
company that popularized personal computing was Apple.
- The
computer that made personal computing legitimate in business and industry
was the IBM Personal Computer.
- Computers
process data under the control sets of instructions called programs.
- The
six key logical units of the computer are the input unit, output unit,
memory unit, ALU, CPU, and Secondary Storage Unit.
- The
three types of languages discussed in chapter 1 are machine languages,
assembly languages, and high level languages.
- The
programs that translate high-level language programs into machine language
are called compilers.
- Multi-threading
allows a Java program to perform multiple activities in parallel.
- The
java command from the JDK executes a Java application.
- The javac command from the JDK compiles a Java program.
- A Java
program files must end with the .java file extension.
- When a
Java program is compiled, the file produced by the compiler ends with the
.class file extension.
- The
file produced by the Java compiler contains the bytecodes
that are executed by the Java Virtual Machine.
Chapter 2
- Javadoc comments are delimited by /** and */. Javadoc comments enable programmers to embed program
documentation directly in their programs. The javadoc
utility program generates HTML documentation based on Javadoc
comments.
- Every
program in Java consists of at least one class declaration that is defined
by the programmer (also known as a programmer-defined class or a
user-defined class).
- Keyword
class introduces a class declaration and is immediately followed by the
class name.
- A Java
class name is an identifier – a series of characters consisting of
letters, digits, underscores (_) and dollar signs ($) that does not begin
with a digit and does not contain spaces. Normally, and identifier that
does not begin with a capital letter is not the name of a Java class.
- A
public class declaration must be saved in a file with the same name as the
class followed by the “.java” file-name extension.
- Method
main is the starting point of every Java application and must begin with
public static void main( String args[] )
otherwise, the JVM will not execute the application.
- Methods
are able to perform tasks and return information when they complete their
tasks. Keyword void indicates that a method will perform a task but will
not return any information.
- A
backslash (\) in a string is an escape character. It indicates that a
“special character” is to be output. Java combines the next character with
the backslash to form an escape sequence. The escape sequence \n
represents the newline character, which positions the cursor on the next
line.
- Format
specifiers begin with a percent sign (%) and are
followed by a character that represents the data type. The format specifier %s is a placeholder for a string.
- An
import declaration helps the compiler locate a class that is used in a
program.
- A
Scanner (package java.util) enables a program to
read data for use in a program. The data can come from many sources, such
as a file on disk or the user at the keyboard. Before using a Scanner, the
program must create it and specify the source of the data.
- The
expression new Scanner( System.in
) creates a Scanner that reads from the keyboard. The standard input
object, System.in, enables Java applications to
read data typed by the user.
- Data
type int is used to declare variables that will
hold integer values. The range of values for an in is -2,147,483,648 to
+2,147,483,647.
- Types
float and double specify real numbers, and type char specifies character
data. Real numbers are numbers that contain decimal points, such as 3.4,
0.0 and -11.19. Variables of type char data represent individual
characters, such as an uppercase letter, a digit, a special character or
an escape sequence.
- Types
such as int, float, double and char are often
called primitive types or built-in types. Primitive type names are
keywords; thus, they must appear in all lowercase letters.
- Scanner
method nextInt obtains an integer for use in a
program.
- The
assignment operator, =, enables the program to give a value to a variable.
Operator = is called a binary operator because it has two operands. An
assignment statement uses an assignment operator to assign a value to a
variable.
- The
format specifier %d is a placeholder for an int value.
- If an
expression contains nested parentheses, the innermost set of parentheses
is evaluated first.
- A left
brace ({} begins the body of every method, and a right brace ()) ends the
body of every method.
- Every
statement ends with a semi-colon (;).
- The
(if) statement is used to make decisions.
- (//)
begins an end-of-line comment.
- Blank
lines, space characters, newline characters and tab characters are called
white space.
- Keywords
are reserved for use by Jave.
- Java
applications begin execution at method main.
- Methods
System.out.print, System.out.println,
and System.out.printf display information in the
command window.
- Comments
do not cause any action to be performed when the program executes. They
are used to document programs and improve their readability.
- All
variables must be given a type when they are declared.
- Java
is case sensitive, so the variables number and NuMbEr
are distinct.
- The
remainder operator can be used with noninteger
operands in Java.
- The
operators *,/, and % are on the same level of
precedence, and the operators + and – are on a lower level of precedence.
- int c, thisIsAVariable,
q76354, number;
This statement declares these variables as integers.
- System.out.print(“Enter an integer: “);
This statement prompts the user to enter an integer.
- Value
= input.nextInt();
This statement inputs an integer and assigns the result to int variable value.
- If (number != 7)
System.out.printf(
“The variable number is not equal to %d”, number);
This statement prints the line if the number is not equal to 7.
- System.out.printf(“This is a java program”);
- System.out.printf(“This is a java \nprogram”);
- System.out.printf(“%s\n%s”,
“This is a java”, “program”);
- if
(c<7)
System.out.println(
“c is less than 7”);
- if
(c>= 7)
System.out.println(“c
is equal or greater than 7”);
- A
scanner that reads values from the standard input: Scanner input = new
Scanner( System.in );
- System.out.print(“Enter first integer:”);
- x = input.nextInt();
- System.out.print(“Enter second integer:”);
- y = input.nextInt();
- System.out.print(“Enter third integer:”);
- z = input.nextInt();
- result
= x*y*z;
- System.out.printf(“Product is %d\n”, result);
Chapter 3
- Performing
a task in a program requires a method. Inside the method you put the
mechanisms that make the method do its tasks – that is, the method hides
the implementation details of the tasks that it performs.
- The
program unit that houses a method is called a class. A class may contain
one or more methods that are designed to perform the class’s tasks.
- A
class can be used to create an instance of the class called an object.
- Each
message sent to an object is known as a method call and tells a method of
the object to perform its task.
- Each
method can specify parameters that represent additional information the
method requires to perform its task correctly. A method call supplies
values – called arguments – for the method’s parameters.
- An
object has attributes that are carried with the object as it is used in a
program. These attributes are specified as part of the object’s class.
Attributes are specified in classes by fields.
- Each
class declaration that begins with the keyword public must be stored in a
file that has exactly the same name as the class and ends with the .java
file-name extension.
- Keyword
public is an access modifier.
- Every
class declaration contains keyword class followed immediately by the class’s
name.
- A
method declaration that begins with keyword public indicates that the
method is “available to the public” – that is, it can be called by other
classes declared outside the class declaration.
- Keyword
void indicates that a method will perform a task but will not return any
information when it completes its task.
- Any
class that contains public static void main( Sting.args[]) can be used to execute an application.
- To
call a method of an object, follow the variable name with a dot separator
(.), the method name and a set of parentheses containing the method’s
arguments.
- Scanner
method nextLine read characters until any
white-space character is encountered, then returns the characters as a
String.
- Class
String is in package java.lang, which is imported
implicitly into all source-code files.
- Variables
declared in the body of a particular method are known as local variables
and can be used only in that method.
- A
class normally consists of one or more methods that manipulate the
attributes (data) that belong to particular object of the class.
Attributes are represented as fields in a class declaration. Such
variables are called fields and are declared inside a class declaration
but outside the bodies of the class’s method declarations.
- When
each object of a class maintains its own copy of an attribute, the field
that represents the attribute is also known as an instance variable. Each
object (instance) of the class has a separate instance of the variable in
memory.
- Most
instance variable declarations are preceded with the private access
modifier. Variables or methods declared with access modifier private are
accessible only to methods of the class in which they are declared.
- Typed
in Java are divided into two categories – primitive types and reference
types (sometimes called nonprimitive types). The
primitive types are Boolean, byte, char, short, int,
long, float, and double. All other types are reference types, so classes,
which specify the types of objects are reference
types.
- Programs
use variables of reference types (called references) to store the location
of an object in the computer’s memory. Such variables refer to objects in
the program. The object that is referenced may contain many instance
variables and methods.
- A
reference to an object is required to invoke an object’s instance methods.
A primitive-type variable does not refer to an object an
therefore cannot be used to invoke a method.
- A
constructor can be used to initialize an object of a class when the object
is created.
- Constructors
can specify parameters but cannot specify return types.
- Floating-point
values that appear in source code are known as floating-point literals and
are type double by default.
- Scanner
method nextDouble returns a double value.
- The
format specifier %f is used to output values of
type float or double. A precision can be specified between % and f to
represent the number of decimal places that should be ouput
to the right of the decimal point in the floating-point number.
- A
house is to a blueprint as an object is to a class.
- Each
class declaration that begins with keyword public must be stored in a file
that has exactly the same name as the class and ends with the .java
file-name extension.
- Every
class declaration contains keyword class followed immediately by the
class’s name.
- Keyword
new creates an object of the class specified to the right of the keyword.
- Each
parameter must specify both a type and a name.
- By
default, classes that are compiled in the same directory are considered to
be in the same package, known as the default package.
- When
each object of a class maintains its own copy of an attribute, the field
that represents the attribute is also known as an instance.
- Java
provides two primitive types for storing floating-point numbers in memory:
float and double.
- Variables
of type double represent double-precision floating-point numbers.
- Scanner
method nextDouble returns a double value.
- Keyword
public is an access modifier.
- Return
type void indicates that a method will perform a task but will not return
any information when it completes its task.
- Scanner
method nextLine reads characters until a newline
character is encountered, then returns those characters as a String.
- Class
String is in package java.lang.
- An
import declaration is not required if you always refer to a class with its
fully qualified class name.
- A
floating-point number is a number with a decimal point, such as 7.33,
0.0975, or 1000.12345.
- Variables
of type float represent single-precision floating-point numbers.
- The
format specifier %f is used to output values of
type float or double.
- Types
in Java are divided into two categories – primitive types and reference
types.
- By
convention, method names begin with a lowercase first letter and all
subsequent words in the name begin with a capitol letter.
- An
import declaration is not required when one class in a package uses
another in the same package.
- Empty
parentheses following a method name in a method declaration indicate that
the method does not require any parameters to perform its task.
- Variables
or methods declared with access modifier private are accessible only to
methods of the class in which they are declared.
- A
primitive-type variable cannot be used to invoke a method – a reference to
an object is required to invoke the object’s methods.
- Variables
declared in the body of a particular method are called local variables and
can be used only in the method in which they are declared.
- Every
method’s body is delimited by left and right braces.
- Primitive-type
instance variables are initialized by default. Each local variable must
explicitly be assigned a value.
- Reference-type
instance variables are initialized by default to the value null.
- Any
class that contains public static void main( String.args[] ) can be used to execute an application.
- The
number of arguments in the method call must match the number of parameters
in the method declaration’s parameter list.
- Floating-type
values that appear in source code are known as floating-point literals and
are type double by default.
- A
local variable is declared in the body of a method and can be used only
from the point at which it is declared through the end of the method
declaration. A field is declared in a class, but not in the body of any of
the class’s methods. Every object (instance) of a class has a separate
copy of the class’s fields. Aldo fields are accessible to all methods of
the class.
- A
parameter represents additional information that a method requires to
perform its task. Each parameter required by a method is specified in the
method’s declaration. An argument is the actual value for a method
parameter. When a method is called, the argument values are passed to the
method so that it can perform its task.
Chapter 4
- Normally,
statements in a program are executed one after the other in the order in which
they are written. This process is called sequential execution.
- Various
Java statements enable the programmer to specify that the next statement
to execute is not necessarily the next one in sequence. This is called
transfer of control.
- Bohn
and Jacopini demonstrated that all programs
could be written in terms of only three control structures – the sequence
structure and the repetition structure.
- The
tern “control structures” comes from the field of computer science. The Java
Language Specification refers to “control structures” as “control
statements”.
- The
sequence structure is built into Java. Unless directed otherwise, the
computer executes Java statements one after the other in the order in
which they are written – that is, in sequence.
- Anywhere
a single action may be placed, several actions may be placed in sequence.
- The
while and for statements perform the action(s) in their bodies zero or
more times – if the loop-continuation condition is initially false, the
action(s) will not execute. The do..while
statement performs the action(s) in its body one or more times.
- The
words if, else, switch, while, do, and for are Java keywords. Keywords
cannot be used as identifiers, such as variable names.
- Single-entry/single-exit
control statements make it easy to build programs – we “attach” the
control statements to one another by connecting the exit point of one to
the entry point of the next. This is known as control-statement stacking.
- There
is only one other way in which control statements may be connected –
control-statement nesting – in which a control statement appears inside
another control statement.
- The if statement is a single-entry/single-exit control
statement.
- The
conditional operator (?:) can be used in place of
an if…else statement. This is Java’s only ternary operator – it takes
three operands. Together, the operands and the ?:
symbol form a conditional expression. The Java compiler always associates
an else with the immediately preceding if unless told to do otherwise by
the placement of braces. This behavior can lead to what is referred to as
the dangling-else problem.
- The
Java compiler always associates an else with the immediately preceding if
unless told to do otherwise by the placement of braces ({
and }). This behavior can lead to what is referred to as the
dangling-else problem.
- The if statement normally expects only one statement in
its body. To include several statements in the body of an if (or the body
of an else for an if…else statement), enclose the statements in braces ({ and }).
- A set
of statements contained within a pair of braces is called a block. A block
can be placed anywhere in a program that a single statement can be placed.
- Syntax
errors are caught by the compiler.
- A
logic error has its effect at execution time. A fatal logic error causes a
program to fail and terminate prematurely. A nonfatal logic error allows a
program to continue executing, but causes the program to produce incorrect
results.
- Just
as a block can be placed anywhere a single statement can be placed, you
can also use an empty statement, represented by placing a semicolon (;)
where a statement would normally be.
- The
while repetition statement allows the programmer to specify that a program
should repeat an action while some condition remains true.
- Counter-controlled
repetition uses a variable called a counter (or control variable) to
control the number of times a set of statements execute.
- In
sentinel-controlled repetition, a special value called a sentinel value
(also called a signal value, a dummy value or a flag value) is used to
indicate “end of entry”.
- To
perform floating-point calculation with integer values, cast on of the
integers to type double. Using a cast operator in this manner is called
explicit conversion.
- Case
operators are available for any type. The cast operator is formed by
placing parentheses around the name of a type. The operator is a unary
operator.
- The
+= operator adds the value of the expression on the right of the operator
to the value of the variable on the left of the operator and stores the
result in the variable on the left of the operator.
- Java
provides two unary operators for adding 1 to or subtracting 1 from the
value of a numeric variable. These are the unary increment operator, ++
and the unary decrement operator, --.
- All
programs can be written in terms of three types of control structures: Sequence, Selection, Repetition
- The if…else statement is
used to execute one action when a condition is true and another when that
condition is false.
- Repeating
a set of instructions a specific number of times is called counter-controlled
repetition.
- When
it is not known in advance how many times a set of statements will be
repeated, a sentinel
value can be used to terminate the repetition.
- The sequence structure
is built into Java – by default, statements execute in the order they
appear.
- Instance
variables of types char, byte, short, int, long,
float, and double are all give the value 0 by default.
- Java
is a strongly typed
language – it requires all variables to have a type.
- If
the increment operator is prefixed to a variable, the variable is
incremented by 1 first, then its new value is
used in the expression.
- A set
of statements contained within a pair of braces ({ and
}) is called a block.
- Java
provides the arithmetic compound assignment operators +=, -=, *=, /= and
%= for abbreviating assignment expressions.
- x++;
- ++x;
- x = x
+ 1;
- x +=
1;
- z =
x++ + y;
- if
(count > 10)
System.out.println(“Count
is greater than 10”);
- total
-= --x;
- q = q%divisor;
- q%=divisor;
- int sum, x;
- x =
1;
- sum =
0;
- sum
+= x;
- System.out.printf(“The sum is: %d\n”, sum);
- Public
class Calculate{
public static void main( String args[]){
int sum, x;
sum = 0;
x = 1;
while(x < 11)
{
sum += x++;
}
System.out.printf(“
The sum of the integers is %d\n”, sum);
}}
- while
(c <= 5)
{
product *= c;
++c;
}
Chapter 5
- Counter-controlled
repetition requires a control variable (or loop counter), the initial
value of the control variable, the increment (or decrement) by which the
control variable is modified each time through the loop (also known as
each iteration of the loop) and the loop-continuation condition that
determines whether looping should continue.
-