java Flashcards
What is JIT?
Just in time compiler. Improves java performance by compiling bytecode to native machine code at run time.
What is reflection?
Allows a program to examine itself and manipulate internal properties. Ex: a java class can obtain the names of all its members and display them. Junit4 uses this to call test methods annotated with @Test
Relational vs non-relational database. Pros and cons
Relational: has specific relationships between data, structured, consistent but its slower and not scalable.
Non-relational: More free form, faster, scalable, but its harder to query and has lower data integrity
Database normalization
Process of organizing data in a database.
1st normal form:
Eliminate repeating groups in individual tables.
Create a separate table for each set of related data.
Identify each set of related data with a primary key.
2nd normal form:
Create separate tables for sets of values that apply to multiple records.
Relate these tables with a foreign key.
3rd normal form:
Eliminate fields that don’t depend on the key
What are the solid principles?
Single-responsibility principle: A class should have only one job
Open-closed principle: Objects should be open for extension but closed for modification. Class should be
extendable without modifying the class itself
Liskov substitution principle: Every subclass or derived class should be substitutable for their base or parent class
Interface segregation principle: A class should never be forced to implement an interface that it doesn’t use, or clients shouldn’t be forced to depend on methods they do not use.
Dependency inversion principle: Entities must depend on abstractions, not on concretions. It states that the high-level module must not depend on the low-level module, but they should depend on abstractions. Allows for decoupling
Vector vs arraylist
Vectors are synchronized, arraylists are not. Thread locks are needed if multiple edit an arraylist. Or Collections.synchronizedList
Both have internal arrays that expand when needed. When expanding: vectors double the array while arraylists increase the array by 50%.
What makes a string immutable?
You can’t change the object itself but you can change the reference to the object.
Big O notation
Best to worst: O(1), O(logn), O(n), O(nlogn), O(n^2)
O(1): constant time, no loops, accessing an array by index
O(logn): logarithmic, binary search, start in middle and search both ways
O(n): linear, one loop through all elements
O(nlogn): log-linear, Quicksort, Mergesort, and Heapsort, breaks down into chunks, divine and conquer
O(n^2): quadratic, nested loops, exponent increases for each nested loop
Applet
Small programs embedded into web pages
JVM
Java virtual machine: Virtual computer that allows the running of java programs
Compiler
Converts java source code into binary program consisting of byte codes
Interpreter
Inspects and deciphers byte codes and then executes it
Packages
Related sets of classes, based on directory path
Instance variable
Variables declared within a class but outside any method. Usually at the top
Class variable
Fields that have the static modifier.
Instance method
Methods that need to be called from a declaration of an object
Class method
Methods that don’t need to be called from a declaration of an object. Static methods
Encapsulation
Hiding items of data and methods within an object. Usually by declaring them private
Aggregation vs composition
Aggregation: ‘has-a’ relationship. Class with a reference to another class child can exist without the parent. ie: a car has wheels but you can take the wheels off.
class Wheel {}
class Car {
List<Wheel> wheels;
}</Wheel>
Composition: ‘part-of’ relationship. Class inside of a class child cannot exist without the parent. ie: a room is part of a building. You cant have a room without a building.
class Building {
class Room {}
}
Overriding
A subclass has the same method as the parent class but different functionality. A type of polymorphism.
Overloading
Same method name but different parameters
Polymorphism
Method to behave different depending on the type of object that calls it.
Conditions to use polymorphism
The method call for a derived class object must be through a variable of a base class type
The method called must also be a member of the base class
The method signature must be the same in the base and derived classes
The method return type must be the same in the base and derived classes
The method access specifier must be no more restrictive in the derived class than in the base
Only applies to methods, not variables
Interface vs abstract class
Consider using abstract classes if any of these statements apply to your situation:
You want to share code among several closely related classes.
You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.
Consider using interfaces if any of these statements apply to your situation:
You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.
You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.
You want to take advantage of multiple inheritance of type.
interface can have default methods that allow implementation at the interface level
- as parameter wont accept List