Java
Basic
————————————————————————————————————————————————————————————JVM (Java Virtual Machine)
It's the virtual machine that runs Java bytecodes.
translate Java Bytecode into the machine language for a particular computer;
execute the corresponding machine language instructions.
————————————————————————————————————————————————————————————JRE (Java Runtime Environment)
It provides the libraries, the Java Virtual Machine, and other components to run applets and applications written in the Java programming language.
JDK (Java Development Kit)
It's a superset of the JRE, and contains everything that is in the JRE, plus tools such as the compilers and debuggers necessary for developing applets and applications.
————————————————————————————————————————————————————————————Object Reflection
It's a feature in Java that provides a way to get reflective information about Java classes and objects, and perform operations such as:
Getting information about the methods and fields present inside the class at runtime.
Creating a new instance of a class.
Getting and setting the object fields directly by getting filed reference, regardless of what the access modifier is.
————————————————————————————————————————————————————————————Access Modifier
Default --- Data members are accessible only within the same package.
Private --- Data members are accessible only within the same class in which they declared.
Protected --- Data members are accessible within the same package or sub classes in different packages.
Public --- Data members are accessible from any where.
————————————————————————————————————————————————————————————Private constructor
What's this?
Say class A has private constructor, then only its subclass B can call A's constructor, and if A is an inner class of class C, then other inner classes of C can also call A's private constructor.
why do so?
You don't want any objects of your class to be created at all.
You only want objects to be created internally.
Uses?
You want to implement a singleton. A singleton is a design pattern that allows only one instance of your class to be created, and this can be accomplished by using a private constructor.
It occurs when the class only contains static members. When a class contains only static members, those members can be accessed using only the class name - no instance of the class need to be created.
————————————————————————————————————————————————————————————Four fundamental OOP concepts
Abstraction
Encapsulation
Inheritance
Polymorphism
————————————————————————————————————————————————————————————Abstraction
The process of hiding the implementation details from the user, only the functionality will be provided to the user.
————————————————————————————————————————————————————————————Encapsulation
A mechanism of wrapping the data (variables) and code acting on the data together as a single unit. The fields of a class will be hidden from other classes, and can be accessed only through the methods of their current class, such as setter and getter.
Declare the variables of a class as private
Provide public setter and getter methods to modify and view the variables values
Benefits
the fields of a class can be made read-only or write-only
a class can have total control over what is stored in its fields
————————————————————————————————————————————————————————————Inheritance
The process where one class acquires the properties (methods and fields) of another.
super: similar to this keyword
Used to differentiate the members of superclass from the members of subclass, if they have same names
Used to invoke the superclass constructor from subclass
IS-A Relationship: object A is a type of object B.
instanceof
HAS-A Relationship: determines whether a certain class has a certain thing.
By having separate classes, we do not have to put the entire code that belongs to one class, which makes it possible to reuse the classes in multiple applications.
Abstract Class
A class which contains the abstract keyword in its declaration.
may or may not contain abstract methods; if a class contains at least one abstract method, then must be declared abstract
can not be instantiated
To use an abstract class, you have to inherit it from another class, provide implementations to the abstract methods in it
If you inherit an abstract class, you have to provide implementations to all the abstract methods in it.
Non abstract class: concrete class (normal class) Abstract Method: if you want a class to contain a particular method but you want the actual implementation of that method to be determined by child classes, you can declare the method in the parent class as an abstract.
only contains signature, no method body. E.X., public abstract double computePay();
————————————————————————————————————————————————————————————Polymorphism
The ability of an object to take on many forms.
Oracle Definition: Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.
Most common use: when a parent class reference is used to refer a child class object.
To be continued...
————————————————————————————————————————————————————————————Java doesn't support multiple inheritance.
The designers of Java considered multiple inheritance to be too complex, and not in line with the goal of keeping Java simple.
Java's alternative to multiple inheritance - interfaces
————————————————————————————————————————————————————————————Interfaces vs Abstract:
A class must be declared abstract when it has one or more abstract methods.
Interface is not a class, while abstract class is. An interface is essentially a type that can be satisfied by any class that implements the interface.
Abstract classes are meant to be inherited from, and when one class inherits from another it means that there is a strong relationship between the 2 classes. With an interface on the other hand, the relationship between the interface itself and the class implementing the interface is not necessarily strong.
Java does not allow multiple inheritance. However, a class can implement multiple interfaces.
An abstract class may provide some methods with definitions. An abstract class can also have constructors and instance variables as well. An interface, however, can not provide any method definitions.
If you want to declare non-public members, good to use abstract class. In an interface, all methods must be public.
If you think you will need to add methods in the future, then an abstract class is a better choice. Because if you add new method headings to an interface, then all of the classes that implement the interface will have to be changed to implement the new methods. That can be quite hassle. Interfaces are a good choice when you think that the API will not change for a while.
————————————————————————————————————————————————————————————Object class
Every class in Java is directly or indirectly derived from the Object class.
toString()
hashCode()
equals(Object obj)
getClass()
finalize()
clone()
wait()
notify()
notifyAll()
————————————————————————————————————————————————————————————Immutable
Once the constructor of an object has completed its execution, that instance can not be altered any more.
————————————————————————————————————————————————————————————Throw
Used to explicitly throw an exception from a method or any block of code.
Throws
Used in the signature of method to indicate that this method might throw one of the listed type exceptions.
————————————————————————————————————————————————————————————Collection
Java Collection framework provides many interfaces (Set, List, Queue, Deque etc.) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet etc).
————————————————————————————————————————————————————————————ArrayList vs LinkedList
ArrayList: implements it with a dynamically re-sizing array.
LinkedList: implements it with a doubly-linked list.
————————————————————————————————————————————————————————————HashMap vs HashTable
HashTable is synchronized, while HashMap is not.
HashTable does not allow NULL keys or values, while HashMap allows one key as NULL and any number of NULL values.
————————————————————————————————————————————————————————————HashMap vs LinkedHashMap vs TreeMap
All offer a key->value map and a way to iterate through the keys. The most important distinction between these classes is the time guarantees and the ordering of the keys.
HashMap offers O(1) lookup and insertion. If you iterate through the keys, though, the ordering of the keys is essentially arbitrary. It's implemented by an array of linked lists.
TreeMap offers O(log N) lookup and insertion. Keys are ordered, so if you need to iterate through the keys in sorted order, you can. This means that keys must implement the Comparable interface. TreeMap is implemented by a Red-Black Tree.
LinkedHashMap offers O(1) lookup and insertion. Keys are ordered by their insertion order. It's implemented by doubly-linked buckets.
————————————————————————————————————————————————————————————Iterator vs ListIterator
Target: Iterator - List and Set; ListIterator - List
Directons: Iterator - One; ListIterator - Two
Index: Iterator - cannot obtain; ListIterator - can obtain indexes at any point of time
Concurrency: Iterator - cannot add elements while traversing; ListIterator - can
Replacement: Iterator - cannot; ListIterator - can
————————————————————————————————————————————————————————————Overloading
It occurs when two or more methods in the same class have the exact same name but different parameters. Different parameters can be different number of parameters or different types of parameters.Overloading happens at compile time.
Overriding
If a derived class requires a different definition for an inherited method, then that method can be redefined in the derived class. Overriding happens at run time.
————————————————————————————————————————————————————————————Final
Class: indicates that the class can not be used as a base class to derive any class from it.
Method: indicates that the method may not be overridden in a derived class.
Variable: indicates that the variable can not be changed.
————————————————————————————————————————————————————————————Finally
Used in association with a try/catch block and guarantees that a section of code will be executed, even if an exception is thrown. The finally block will be executed after the try and catch blocks, but before control transfers back to its origin.
Note
the code in the "finally block" will be executed even if there is a return statement somewhere in the "try block". And it will run before the return statement in the "try block".
However, there are cases when "finally block" will not be called after return.
System.exit() is called first.
JVM crash
Return value in the "finally block" will override any return value/exception in the try/catch block. Thus, a return value in the "finally block" is a bad idea.
Some cases in which the finally block will not be executed, such as the following:
If the virtual machine exits during try/catch block execution.
If the thread which is executing during the try/catch block gets killed.
———————————————————————————————————————————————————————————— Finalize
Not a reserved keyword. It's a method that the Garbage Collector always calls just before the deletion/destroying the object which is eligible for Garbage Collection, so as to perform clean-up activity. Clean-up activity means closing the resources associated with that object like Database Connection, Network Connection or we can say resource de-allocation. Once finalize method completes immediately Garbage Collector destroy that object.
———————————————————————————————————————————————————————————— Static
A static member is a member of a class that isn't associated with an instance of a class. Instead, the member belongs to the class itself. So you can access the static member without first creating a class instance.
Static field: shared by all the objects, and it has only one copy in the memory.
Static method: you can’t access a non-static method or field from a static method because the static method doesn’t have an instance of the class to use to reference instance methods or fields.
————————————————————————————————————————————————————————————Container
Containers provide an interface between a component and the low-level platform-specific functionality that supports the component.
4 Types:
Applet container - manages applets that run on the client.
Application client container - manages the execution of application components that run on the client.
Enterprise JavaBeans (EJB) container - manages the execution of enterprise beans for J2EE applications.
Web container - manages the execution of servlet components for J2EE applications.
————————————————————————————————————————————————————————————Serializable class
a class is serializable if and when it implements the Serializable interface, which is an interface residing in the java.io.package. If a class is serializable, it means that any object of that class can be converted into a sequence of bits so that it can be written to some storage medium (like a file), or even transmitted across a network.
Transient variable: Suppose that there's a particular object data member (like a password) that we may not want to get saved when an object is serialized. Then, what we can do is declare that data member to be "transient". A transient variable is not a part of the persistent state of a serialized object.
————————————————————————————————————————————————————————————Mod Operator 取余
遵循的公式: (a / b) * b + c = a 所以: a mod b = c = a - (a / b) * b, / 为整除符号 例:-2 mod 3 = -2
————————————————————————————————————————————————————————————逻辑右移 vs. 算术右移
Operation
Description
>>>
Logical right shift (符号位补0,带符号位一起移)
>>
Arithmetic right shift (符号位不变,不带符号位移)
———————————————————————————————————————————————————————————— Objects.hash(...)
重写hashCode function时,可以使用 Objects.hash(...),参数可为任意primitive variables
————————————————————————————————————————————————————————————Two arrays are covariant, and two generics are invariant.
———————————————————————————————————————————————————————————— String.split(pattern)
str = "boo:and:foo"
str.split(":") => {"boo", "and", "foo"}
str.split("o") => {"b", "", ":and:f"}
split 会自动去除split后数组中的 trailing 空字符串。
————————————————————————————————————————————————————————————
Last updated