Week 2 Flashcards
Method - Describe boolean equals(Object o)
indicates whether some other object is “equal to” this one
Method - Describe Object clone()
creates and returns a copy of the object
Method - Describe void finalize()
called by the garbage collector on an object when garbage collection determines that there are no more references to the object
Method - Describe Class< ? > getClass()
returns the runtime class of the object
Method - Describe int hashCode()
returns a hash code value for the object
Method - Describe void notify()
wakes up a single thread that is waiting on the object’s monitor
Method - Describe void notifyAll()
wakes up all threads that are waiting on the object’s monitor
Method - Describe String toString()
returns a string representation of the object
automatically called if you print an Object. Usually, this is overridden to provide human-readable output
Method - Describe void wait()
causes the current thread to wait until another thread invokes the notify() or the notifyAll() method for the object
Method - Describe void wait(long timeout)
causes the current thread to wait until either another thread invokes the notify() or the notifyAll() method for the object, or a specified amount of time has elapsed
Method - Describe void wait(long timeout, int nanos)
causes the current thread to wait until another thread invokes the notify() or the notifyAll() method for the object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed
What is an Error?
Some examples?
An Error represents something that went so wrong with your application that you shouldn’t attempt to recover from it
EX - ExceptionInInitializerError, OutOfMemoryError, StackOverflowError
Define hash code
a number that puts instances of a class into a finite number of categories
try/catch/finally block rules (5)
- multiple catch blocks are allowed
- multi-catch blocks (catching more than one exception in a given block)are allowed; exception types are separated by |
- a finally block is optional
- a try/finally block only IS allowed, but a try block by itself is not
- a finally block will always execute, unless System.exit() is called
What are unchecked exceptions?
Examples?
an exception that is not required to be handled or declared
EX: RuntimeException;
- ArithmeticException (for illegal math problems);
- IndexOutOfBoundsException (if you reference an index that’s > the length of an array);
- NullPointerException (if you attempt to perform an operation on a reference variable that points to a null value)
Define annotations
special constructs that use the @ symbol. provide metadata about the source code to the compiler and JVM
Explain Test Driven Development (TDD)
- process of writing unit tests you want your code to pass
Steps
- write a unit test
- run the test ==> test will fail
- fix the test by writting application code
- retest until the test passes
- repeat
Define Unit Testing
testing of individual software components in isolation from the rest of the system; writing unit tests which execute the code we want to inspect
JUnit Annotations
Java framework for unit testing
static Assert methods (5)
- assertTrue()
- assertFalse()
- assertEquals()
- assertNotEquals()
- assertThat()
JUnit Assertions
verifies that the state of the application meets what is expected
What annotation would you use to prevent a test from running?
@Ignore (use sparingly!)
Testing Best Practices (7)
- dependency injection - design pattern in which an object receives other objects that it depends on
- write testable code
- use mocking libraries for dependencies
- measure your code coverage
- externalize test data when possible (read test datat from external file)
- try to use only 1 assert statement per test (helps pinpoint defects when debugging)
- write deterministic tests (“flkay tests”)
4 types of variable scopes in Java?
- instance/object scope - variable is attached to individual objects created from the class; when modified, it has no effect on other, distinct objects of the same class
- class/static scope -variables reside on the class definition itself. This means that when objects update a class-scoped variable, the change is reflected across all instances of the class
- method scope - scope of a variable declared within a method block, whether static or instance; do not exist after method finishes execution
- block scope - only exist within the specific control flow block (for/while/do-while loops/if/else-if/else blocks, switch cases, regular blocks declared by {}; no longer available after block ends
return statements
used for returning a value when the execution of the block is completed. The return statement inside a loop will cause the loop to break and further statements will be ignored by the compiler
Explain stack vs heap?
Heap is where objects are stored in memory. Stack is where local variable references are kept - a new stack FRAME is created for each method invocation
Non-accessmodifiers (6)?
- static
- final
- abstract
- default
- synchronized
- transient
obscure keywords :volatile, native, strictfp
What makes a class immutable (6)?
- Declare the class as final so it can’t be extended.
- Make all fields private so that direct access is not allowed.
- Don’t provide setter methods for variables.
- Make all mutable fields final so that it’s value can be assigned only once.
- Initialize all the fields via a constructor performing deep copy.
- Perform cloning of objects in the getter methods to return a copy rather than returning the actual object reference.