A personal memo that I haven't put together with the intention of letting others read it.
Primitive Type
- There is no unsigned numeric type
- Use Double.compare instead of == for floating point to compare NaN or +0.0 to -0.0
Inheritance
Inheritance example:
interface Foo {
void bar();
}
class Foo extends Bar implements Foo {
...
}
Multiple inheritance
- Unlike C ++, multiple classes cannot be inherited
operator
- Unlike C ++, operator overloading is not possible
Override of equals
- Implement to meet the following rules:
- Reflexive: x.equals (x) is true
- Symmetric: x.equals (y) is also true only if y.equals (x) is true
- Transitive: If x.equals (y) and y.equals (z) are true, then x.equals (z) is also true
- Consistent: The result of x.equals (y) is unchanged if the information is not changed.
- Override hashCode
final, finally, finalize
final
- Class: Inheritable
- Method: Cannot be overridden
- Variable: Cannot be changed
finally
- Normally, finally is executed no matter what you do, such as return, continue, break and exception.
- Not executed if thread or VM dies suddenly
- Not executed by System # exit or Runtime # exit
finalize
- Methods to run before the JVM GCs the object
- Java 9 is deprecated so you don't have to remember
Modifier
abstract
- Class: Has an abstract method. I can't instantiate.
- Interfaces: All interfaces are implicitly abstract, so you don't have to write an abstract.
- Method: A method that has no definition. The class must also be abstract.
protected
- Can be assigned only to methods and variables
- Can be referenced from the same class or subclass
private
- If you make the constructor private, you cannot instantiate the class outside the class.
- Used to create only from static public methods in the Factory Method pattern.
- Inheritance becomes impossible
static
- Method: Become a class method
- Variable: Become a class variable. Become one in the class and access by class name
volatile
- Due to the multi-thread environment, it prevents the cache of field values between threads and prevents leaders such as assignment processing.
- If there is a dependency on the update, volatile is not enough and you have to use the synchronized block.
class
External class
- A class that has an inner class
- ** There is no static in the outer class **
- ** Private cannot be written in external class **
- By default, it can be accessed only from within the same package, and if it is made public, it can be used from another package.
- Public classes need to have the same filename
Inner class
- A class defined by nesting in an external class. Can be used from classes that are not external classes
- You can access the members of the outer class by instantiating the inner class from the object of the outer class.
- ** Non-static inner classes cannot be instantiated from a static context **
Local class
- Class defined in the method of the external class
- Since it is a kind of inner class, static fields and methods cannot be created.
static inner class
- Behavior is completely different from the inner class
- Instantiate from an outer class, not from an object in the outer class
- Of course, you cannot access non-static members of the outer class. Can be accessed if static
String
- ==: Strings should not be compared with ==
- +: Java string concatenation uses StringBuilder internally, so concatenating Strings with + is slow.
- length (): It only returns the number of Unicode code units, so it doesn't work as expected including surrogate pairs. use codePointCount
- String # split takes a regular expression as a String instead of a Pattern
Integer
- Use factory methods such as Integer.valueOf (int) instead of constructors. Because the instance may be reused.
Deque
Double-ended queue. Since it implements Queue interface, it can be used as Queue, but Stack is a class, so it is not implemented.
On the other hand, Stack is recommended in the Stack documentation, such as Use Deque over Stack. You should use Deque when you need a wind interface. Stack implements Vector, which allows index access that seems unnecessary.
In general, LinkedList has $ O (1) $ for all operations, and an implementation like ArrayDeque has an additional worst calculation time of $ O (n) $, but Time Complexity for all operations is $ O in Amortized Analysis. (1) It becomes $.
Class that reserves a continuous area of the heap
- ByteArrayOutputStream
- Buffered* (BufferedInputStream)
- ArrayList
- String
- StringBuilder
Be careful not to use too much memory
Legacy class
- Vector => ArrayList
- Stack => LinkedList
- HashMap => Hashtable
- StringBuffer => StringBuffer
Comparison
There are two main ways to implement arbitrary class comparisons.
Comparable.compareTo
class Foo implements Comparable<Foo> {
int key;
public Foo(int key) {
this.key = key;
}
@Override
public int compareTo(Foo f){
return this.key - f.key;
}
}
Collections.sort(fooList);
Collections.sort(fooList, Collections.reverseOrder());
Comparator.compare
class FooComparator implements Comparator<Foo> {
@Override
public int compare(Foo a, Foo b) {
return a.key - b.key;
}
}
Collections.sort(fooList, new FooComparator());
thread
Example of usage
class Foo implements Runnable {
public void run() {
while (true) System.out.print(".");
}
}
Foo foo = new Foo();
Thread thread = new Thread(foo);
thread.start();
Classes that are not thread-safe
It is necessary to handle with synchronized etc. when there is access in parallel
reference
There are the following types of references:
- Strong: Ordinary reference
- Weak: References that are GCed when the String reference is gone
- Soft: Unlike Weak, it does not always disappear, it only disappears when memory is really needed. This needs to be cleaned up before the GC gets an OutOfMemoryError
- Phantom: Use to schedule finalize yourself https://qiita.com/yoshi389111/items/66448f9a013eee4be035
https://docs.oracle.com/javase/jp/8/docs/api/java/lang/ref/Reference.html
Generics
- Both classes and methods can be generic
- Primitive Type cannot be a parameter
- You can define bounded type parameters by writing
? Super Xxx
or? Extends Xxx
- Parameter Type cannot be instantiated
- Parameter Type cannot be used for static methods and variables
Other things I want to find out later
- object reflection:
Class.forName
etc.
- class loader
- dependency injection