Java Easy Negligence Knowledge point

Source: Internet
Author: User

01
Basic data types
------------
1.byte
Bit
-128 ~ 127
1 bytes = 8bit
-----------------
|1| | | | | | | |
-----------------
Negative numbers are stored in the form: complement.
Positive number: Self

2^0
1:0000 0001
2:0000 0010
3:0000 0011
-1:0000 0001, 1111 1110, 1111 1111
-2:0000 0001, 1111 1110, 1111 1110

-128:1000 0000

Character set: GBK gb2312 UTF8 iso8859-1 ascii Unicode Big5


ASCII: American National Standards Agency. The corresponding is the 108 button.
gb2312: Simplified Chinese, one Chinese 2 byte, English or a byte
UTF-8: International unified Code, Chinese with 3byte, English or byte
UNICODE:JVM uses the character encoding, each character occupies 2byte, preceded by a two-byte header ( -2,-1)


char C = 12;
c = ' a ';
c = ' \u '
0-f: 1111 FF

Collection
------------------
Array: Retrieve fast, fixed length.
Reference.
List:
Deciding whether to include a particular element only determines the Equals method, and it has no relation to the object address and hashcode.
1.1) ArrayList
Data volume operation time consuming
100,000 write 860ms
100,000 reads 8,621ns
The inner package is an array, with capacity, capacity.
Array in memory address is contiguous, subscript is index, retrieval does not need to be compared,
You can navigate directly to the specified element by calculating the address.

1.2) LinkedList
Data volume operation time consuming
100,000 write 10ms
100,000 reads 1ms
The inside of the list is implemented by reference, and the inner class is node{e E; Node pre, node next};
Data concatenation is achieved using pallet references. The advantage is that the write operation is fast. The query is slow.


MAP:
Key-value,key is unique.

1.1) HashMap
Hash.
Array + linked list internal implementation.
Pallet nodes.
Node implements Map.entry<k,v> {
int hash;
K K;
V V;
Node<k,v> Next;
}
Map.put (...) {
...
Putval (hash (key), key, value, false, true);
}

Calculates a new hash.
The new key uses the high 16 and the low 16 to do the XOR operation. Hash internal algorithm
The purpose of the shift operation is to allow more features to participate in the calculation.
The purpose of the XOR calculation is to make the data more dispersed.
public int hash (key) {
int h;
return (key = = null)? 0: (H = key.hashcode ()) ^ (h >>> 16);
}

HashMap determine if key is the same, (P.hash = = Hash && (k = p.key) = = Key | | (Key! = null && key.equals (k))))

1. If the new hash is different, the key must be different, if the key is the same, but also to see whether the same object, if the same key,key, otherwise see equals ()
if (newhash1! = Newhash) {
Different
}
else{
if (Key1 = = Key2) {
Same
}
else{
Key1.equals (K2);
}
}

1.2) TreeMap
Binary tree structure, using contrast.

Set:
Do not repeat.
Hashset = = HashMap.
HashSet is implemented internally through HashMap, and value is populated with garbage values. Only the part of the key is used.

TreeSet: The TREEMAP implementation is used internally.

Thread-Safe collections
---------------
Vectors are equivalent to list.
Hashtable equivalent to HashMap

Collection Tool Class
----------------
Thread-Safe collections
Map = Collections.synchronizemap (m);

Design Patterns
----------------

Expert-level solutions for specific scenarios.
1.singleton
Single case
A class has and has only one instance.
Construct private
A reference that points to itself statically
Method.
1.1) A Hungry man
public class Garbage {

private static Garbage instance = new Garbage ();

Private Garbage () {
}

Public synchronized static Garbage getinstance () {
return instance;
}
}
1.2)
Package com.oldboy.java.gof;
/**
* Trash Box
*/
public class Garbage {

private static Garbage instance = NULL;

Private Garbage () {
}

/**
* Grain Size: Coarse
* @return
*/
public static Garbage getinstance () {
if (instance! = null) {
return instance;
}
Synchronized (Garbage.class) {
if (instance = = null) {
instance = new Garbage ();
}
}
return instance;
}
}

2.factory
Factory
2.1) Non-static factory
/**
* Non-static Factory mode
*/
public class TVFactory1 {
Public Tvset Producttvset () {
Tvset TV1 = new Tvset ();
Tv1.setbrand ("Panda");
Tv1.setsize (1000);
Tv1.setresolver (2000);
return TV1;
}
}
2.2) Static Factory
/**
* Static Factory mode
*/
public class TVFactory2 {
/**
*
*/
public static Tvset Producttvset () {
Tvset TV1 = new Tvset ();
Tv1.setbrand ("Panda");
Tv1.setsize (1000);
Tv1.setresolver (2000);
return TV1;
}
}
3.builder
Builder mode.

public static Class tvsetbuilder{

Tvset t = new Tvset ();
Public Tvsetbuilder Setbrand (String brand) {
T.setbrand (brand);
return this;
}
Public Tvsetbuilder setSize (int size) {
T.setsize (size);
return this;
}
Public Tvsetbuilder setresovler (int resovler) {
T.setresolver (Resovler);
return this;
}

Public Tvset Build () {
return t;
}

4.decorator
Decoration mode
Icing on
Class a{
}

Class Wrappeda extends a{
Private a A;
Public Wrappeda (a a) {
THIS.A = A;
}


public void aa () {
..
A.aa ();
...
return;
}
}
5.pooling
Pooled mode

6.prototype
Prototype mode

7. Adapter
Pre-implementation.
Public abstract class Windowadapter
Implements WindowListener, Windowstatelistener, Windowfocuslistener
{
public void windowopened (WindowEvent e) {}
public void windowclosing (WindowEvent e) {}

...
}


FileIO
-----------------
BYTE stream
Character Stream

Pojo
------------------
Plain old Java object, plain ancient Java objects.
Conforms to JavaBean design specifications, empty constructs, private properties, common Getter/setter.

= = differs from equals
------------------
1.== compares the memory address, that is, whether it is the same object.
2.equals method
Compares the contents of two objects in the same way, and the default implementation comparison is the memory address. This method needs to be rewritten and logically judged according to its own requirements.

Java Easy Negligence Knowledge point

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.