Chapter 4: Class Flashcards
What’s the diff between local variable and instance variable? What about global variable?
Local variable lives inside of {} (methond body or code block)
instance variable: inside of a class
There is no global variable in Java but there is for C/C++.
Explain: pass by value
In Java, variables are passed by value, meaning that the value of the variable is COPIED and passed into a parameter.
How to do testing in Java? What is bottom-up testing? What if a method called has not been implemented?
Tool: JUnit, allow to run method or code without writing main()
each method should be unit tested.
Bottom-up testing: always test methods being called first.
Use a stub if a method is not ready yet.
Explain: Encapsulation, abstraction, and API
Abstraction and encapsulation have areas in common: they both require separating “implementation details” from “well-defined interface”
API stands for application programming interface: it is the documentation of how to use a class
Abstraction reduces complexity and encapsulation also improve privacy and security
Access modifiers: name them, and explain their function
public: can be used anywhere
protected: can be used by child class and other unrelated classes in the same package
default (package): can be used by classes in the same package
private: can only be used within the class definition
Explain: Overloading
Overloading a method name: same name but different parameter list, essentially diff methods so can have different return type and access modifier
add(double, int) and add(int, double)
What’s this called? is it legal? Any issue related to it?
Method overloading
completely legal
Auto-type conversion can cause issue: add(1,2)
Always think as if you are a compiler, see if you know which method to invoke
Explain the use of StringTokenizer
Example: wordFactory = new StringTokenizer(String, delimiter);
wordFactory.hasMoreTokens();
wordFactory.nextToken();
wordFactory.countTokens();
What are static variables and methods? How to use them?
Only one copy per class. All members can access static variables but it is better be used via ClassName.staticVariable;
Special:
import static java.util.Math.*;
we don’t need to write Math.abs(); anymore
just: abs();
Draw the memory analysis for a class method execution being invoked from the main()
Stack(stack frames) Heap Method area
compare none objects like: null and the result of getClass(object)
use “==” because they are not objects
do not use equals()
Explain: deep copy and shallow copy
Deep copy: a copy that is completely independent from the original object
you can do direct assignment with immutable objects
How to make a package?
at the very beginning:
package com.letcs.calculator;
what is javadoc? how to use javadoc?
Auto-extract all API for a class or an entire package
use /**
*/
to comment
run in the cli: javadoc -d directory package_name
@tags:
@param; @return; @throws; @deprecated (dropped, no longer in use)
@see other class; @ author; @version;
How to use a variable number of parameters?
method(type … name)
vararg specification
… : is called an ellipsis