- Real-world objects contain state and behavior.
- A software object's state is stored in fields.
- A software object's behavior is exposed through methods.
- Hiding internal data from the outside world, and accessing it only through publicly exposed methods is known as data encapsulation.
- A blueprint for a software object is called a class.
- Common behavior can be defined in a superclass and inherited into a subclass using the extends keyword.
- A collection of methods with no implementation is called an interface.
- A namespace that organizes classes and interfaces by functionality is called a package.
- The term API stands for Application Programming Interface.
- The term "instance variable" is another name for non-static field.
- The term "class variable" is another name for static field.
- A local variable stores temporary state; it is declared inside a method.
- A variable declared within the opening and closing parenthesis of a method is called a parameter.
- What are the eight primitive data types supported by the Java programming language? byte, short, int, long, float, double, boolean, char
- Character strings are represented by the class java.lang.String.
- An array is a container object that holds a fixed number of values of a single type.
- Consider the following code snippet:
arrayOfInts[j] > arrayOfInts[j+1]
Question: What operators does the code contain?
Answer: >, + - Consider the following code snippet:
int i = 10;
int n = i++%5;
a. Question: What are the values of i and n after the code is executed?
Answer: i is 11, and n is 0.
b. Question: What are the final values of i and n if instead of using the postfix increment operator (i++), you use the prefix version (++i))?
Answer: i is 11, and n is 1. - Question: To invert the value of a boolean, which operator would you use?
Answer: The logical complement operator "!". - Question: Which operator is used to compare two values, = or == ?
Answer: The == operator is used for comparison, and = is used for assignment. - Question: Explain the following code sample: result = someCondition ? value1 : value2;
Answer: This code should be read as: "If someCondition is true, assign the value of value1 to result. Otherwise, assign the value of value2 to result."
- Operators may be used in building expressions, which compute values.
- Expressions are the core components of statements.
- Statements may be grouped into blocks.
- The following code snippet is an example of a compound expression. 1 * 2 * 3
- Statements are roughly equivalent to sentences in natural languages, but instead of ending with a period, a statement ends with a semicolon
- A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed.
- The most basic control flow statement supported by the Java programming language is the if-then statement.
- The switch statement allows for any number of possible execution paths.
- The do-while statement is similar to the while statement, but evaluates its expression at the bottom of the loop.
- Question: How do you write an infinite loop using the for statement?
Answer:
for ( ; ; ) {
}
- Question: How do you write an infinite loop using the while statement? Answer:
while (true) {
}
- Consider the following class:
public class IdentifyMyParts {
public static int x = 7;
public int y = 3;
}
a. Question: What are the class variables?
Answer: x
b. Question: What are the instance variables?
Answer: y
c. Question: What is the output from the following code:
IdentifyMyParts a = new IdentifyMyParts();
IdentifyMyParts b = new IdentifyMyParts();
a.y = 5;
b.y = 6;
a.x = 1;
b.x = 2;
System.out.println("a.y = " + a.y);
System.out.println("b.y = " + b.y);
System.out.println("a.x = " + a.x);
System.out.println("b.x = " + b.x);
System.out.println("IdentifyMyParts.x = " + IdentifyMyParts.x);
Answer: Here is the output:
a.y = 5
b.y = 6
a.x = 2
b.x = 2
IdentifyMyParts.x = 2
Because x is defined as a public static int in the class IdentifyMyParts, every reference to x will have the value that was last assigned because x is a static variable (and therefore a class variable) shared across all instances of the class. That is, there is only one x: when the value of x changes in any instance it affects the value of x for all instances of IdentifyMyParts.