AP Lec1 Review
AP Lec1 Review
Review
Identifiers
Identifier: The name of a variable or other item
(class, method, object, etc.) defined in a program
A Java identifier must not start with a digit, and all the
characters must be letters, digits, or the underscore
symbol
Java identifiers can theoretically be of any length
Java is a case-sensitive language: Rate, rate, and RATE
are the names of three different variables
Identifiers
Keywords and Reserved words: Identifiers that have
a predefined meaning in Java
Do not use them to name anything else
public
class
void
static
Naming Conventions
Start the names of variables and methods with a
lowercase letter, indicate "word" boundaries with an
uppercase letter, and restrict the remaining
characters to digits and lowercase letters
topSpeed
bankRate1
timeOfArrival
MyClass
String
Variable Declarations
Every variable in a Java program must be declared before it is
used
A variable declaration tells the compiler what kind of data (type) will
be stored in the variable
The type of the variable is followed by one or more variable names
separated by commas, and terminated with a semicolon
Variables are typically declared just before they are used or at the start
of a block (indicated by an opening brace { )
Basic types in Java are called primitive types
int numberOfBeans;
double oneWeight, totalWeight;
Primitive Types
Equivalent To:
count += 2;
count = count + 2;
sum -= discount;
bonus *= 2;
bonus = bonus * 2;
time /=
rushFactor;
change %= 100;
time =
time / rushFactor;
change = change % 100;
amount *=
count1 + count2;
amount = amount *
(count1 + count2);
Assignment Compatibility
In general, the value of one type cannot be stored in
a variable of another type
int intVariable = 2.99; //Illegal
The above example results in a type mismatch because a
double value cannot be stored in an int variable
Precedence Rules
Be careful to make at least one of the operands a floatingpoint type if the fractional portion is needed
Type Casting
A type cast takes a value of one type and produces a value of
another type with an "equivalent" value
If n and m are integers to be divided, and the fractional portion of the
result must be preserved, at least one of the two must be type cast to
a floating-point type before the division operation is performed
double ans = n / (double)m;
Note that the desired type is placed inside parentheses immediately in
front of the variable to be cast
Note also that the type and value of the variable to be cast does not
change
Concatenation of Strings
Concatenation: Using the + operator on two strings in order
to connect them to form one longer string
If greeting is equal to "Hello ", and javaClass is equal to
"class", then greeting + javaClass is equal to "Hello
class"
String Methods
The String class contains many useful methods for stringprocessing applications
A String method is called by writing a String object, a dot, the
name of the method, and a pair of parentheses to enclose any
arguments
If a String method returns a value, then it can be placed anywhere
that a value of its type can be used
String greeting = "Hello";
int count = greeting.length();
System.out.println("Length is " +
greeting.length());
Always count from zero when referring to the position or index of a
character in a string
String Indexes
Escape Sequences
A backslash (\) immediately preceding a
character (i.e., without any space) denotes an
escape sequence or an escape character
The character following the backslash does not
have its usual meaning
Although it is formed using two symbols, it is
regarded as a single character
Escape Sequences
String Processing
A String object in Java is considered to be immutable, i.e.,
the characters it contains cannot be changed
There is another class in Java called StringBuffer that has
methods for editing its string objects
However, it is possible to change the value of a String
variable by using an assignment statement
String name = "Soprano";
name = "Anthony " + name;
Naming Constants
Instead of using "anonymous" numbers in a program, always
declare them as named constants, and use their name instead
public static final int INCHES_PER_FOOT = 12;
public static final double RATE = 0.14;
This prevents a value from being changed inadvertently
It has the added advantage that when a value must be modified, it
need only be changed in one place
Note the naming convention for constants: Use all uppercase letters,
and designate word boundaries with an underscore character
Comments
A line comment begins with the symbols //, and
causes the compiler to ignore the remainder of the
line
This type of comment is used for the code writer or for a
programmer who modifies the code
Source
Absolute Java, Savitch
Copyright 2010 Pearson Addison-Wesley. All rights reserved.