Be Prepared Ch 1 - 2.3 Flashcards
Why shouldn’t you use a for each loop sometimes?
You cant modify stuff when using a for each loop. as the for each loop is making a copy of the stuff. Rather use a regular for loop
Free responses will typically be less than _____ lines of code
12
Appropriate Casting
Int a,b;
Double ratio;
…
Ratio = (double)a/b; // or a/(double) b;
//but not ratio = (double) (a/b) – this is a cast applied too late!
There are two forms fo the ++ and – operators in Java. What are they and how are they different?
The prefix form increments (or decrements) the variable before its value is used in the rest of the expression; the postfix form increments (or decrements) it afterwards.
While(i<=n) sum+= i++; is the same as
While (I <= n)
{
Sum+= I;//i
i++;
}
== and != compare the _________ of the objects, not the ______ To compare the ____ use the equals or compareTo method
addresses; values; values
== or != must be used when you need to compare an object to ____
null
“Everything” in Java not explicitly set to something is initialized to a ____ value.
zero;
For references (anything that holds an object) that is null.
For int/short/byte that is a 0.
For float/double that is a 0.0
For booleans that is a false.
Everything in Java, except _____, not explicitly set to something is initialized to a zero value.
Local Variables;
You need to explicitly initialize any local variables (by either initialization or assignment)
assignment
throwing away the old value of a variable and replacing it with a new one
initialization:
it’s a special kind of assignment: the first. Before initialization objects have null value and primitive types have default values such as 0 or false. Can be done in conjunction with declaration.
declaration:
a _____ states the type of a variable, along with its name. A variable can be declared only once. It is used by the compiler to help programmers avoid mistakes such as assigning string values to integer variables. Before reading or assigning a variable, that variable must have been declared.
De Morgan’s Laws
!(a&&b) is the same as !a||!b
!(a||b) is the same as !a&&!b
Short-circuit evaluation
For the java logical operators, && and ||, if the value of the first operand is suffiecient to dertemine the result, the the second operand is not evaluated.
Bit-wise logical operators
Not in ap exam. But can cause synatx errors when comparing bits, or bolleans . .. Do not comply with short-ciruit evaluation when used in that way
For-each loop
Int[] scores = [87,85,76];
For (int score : scores)
System.out.println(score+” “);
Note: if you need access to indices, use a for or while loop
a “break;” statement takes you out of the inner loop but not out of the ____.
outer loop
Strings are _____
immutable
no string methods can change the string. An assignment statement str1 = str2; copies the reference from str2 into rts1, so they both refer to the same memory location.
A literal string
is a string of characters within double quotes. May include “escape sequences” \n, ", \ etc.
Ex: “Hello” or “Hello\n”
String Methods
Length
Equals
compareTo
Substring()
Substring(int start)
indexOf(String find)
indexOf(String find, int fromIndex)
compareTo -
returns a positive number if str1 is greater than str2 (lexicograhpically), zero if they are equal, anda negative number if str1 is less than str2
indexOf(String find)
Returns the index within this string of the first occurrence of the specified substring.
returns -1 if find is not found in str
indexOf(String find, int fromIndex)
Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
returns -1 if find is not found in str
If you have a string method called process(String msg, String delim)
, how does process change msg?
It doesnt.
Rhyme = “…something.\n …bob…bob..\n..”
String rhyme2 = process(rhyme, “\n”); << does not change rhyme!
Process receives and works with a copy of a reference to the original string (see Be Prepared Section 3.3) the method can reassign the copy, as it does here, but the original reference still refers to the same string. This consideration, combined with the immutability of strings, assures us that rhyme remains unchanegd after the call process(rhyme)