Self-study JAVA summary, self-learning java

Source: Internet
Author: User

Self-study JAVA summary, self-learning java

2. When defining constants, the C language is defined as const and the JAVA language is final.
3. When declaring member variables in JAVA, use static to define them.
4. The boolean type in JAVA only includes true and false, but in C, 0 is true and 0 is false.
5. in JAVA, the storage space of byte, short, int, and long is 1, 2, 4, and 8 bytes respectively, in C, its char, short, int, and long storage space are 1, 2, 4, and 4 bytes respectively. Here, long is int long. C language has the unsign type, but Java does not.
6. The long type must be represented by L after the value assignment in Java; otherwise, an error occurs. Or it will become the int type, but the length of the int type will be less than long. This rule is not found in C.
7. In JAVA, data types are sorted by capacity (the size of the number indicated) rather than by memory occupied.
8.int type data can be directly assigned to byte type variables. Byte type variables directly cut the int type data to the desired length, and the double type data cannot be directly assigned to the float type data because of the decimal point limit.
9. Logical and & logical or | (both the values at both ends must be calculated) Short-circuit and & short-circuit or | the second value cannot be calculated.
10. The value after switch should be int type. in Java, it can be char, byte, or short type, because it can automatically rotate to int type data.

Chapter 2
1. output the factorial sum of 1-10
2. Output an odd number within 100.
3 use the while and do-while loops to output 1-9
4. The output contains 5 or less values divided by 3.
5. Output prime numbers from 101 to 200

Chapter 3 object-oriented
Note: Object-oriented Thinking: 1. What are the attributes of those classes and objects and the relationship between Class 3 and Class 2. Appropriate methods should appear in appropriate classes.
Relationship between objects and classes: A class can be seen as a template of a class of objects, and an object can be seen as a specific instance of this class.
Relationships between classes (objects)
Association: The parameters of a class method are the objects of another class. The relationship is not very close (weakest)
Inheritance relationship: XX is a type of XX, which can be considered as an inheritance relationship encapsulation. (During design, it is very likely that a spanning tree will be generated, but it may also inherit from multiple different classes. C ++ has multiple inheritance (if the parent class contains duplicate member variables, it will be very troublesome to handle), but JAVA has inherited multiple interfaces ). Strong
Aggregation relationship: the relationship between the whole and the part (XX is a part of XX ). It can also be divided into clustering (number of roles) and combination (inseparable. Strong
Implementation relationship: When a subclass inherits the method of the parent class, it is implemented by the subclass. This is called implementation.
Polymorphism:

Static keywords:
The static method cannot directly access non-static variables and methods.
Objects can be viewed as attributes (member variables) and encapsulation bodies of methods.
Differences between Java and C:
C is process-oriented programming. Its Attributes and methods are separated, not aggregated. Java is object-oriented, and objects encapsulate attributes and methods together, then reuse, that is, Java is more reusable (methods and attributes), while process-oriented reuse is a method. In addition to reusability, scalability, maintenance, and replacement are more convenient than process-oriented.

JAVA syntax
Reference: all variable types except the basic types (4 types and 8 types) are referred to as reference types. The initial value is null.
Overload: The method name is the same, the parameter type or number of parameters are different, but different return values do not constitute overload.
Non-static members cannot be accessed in the static method, and static and other references should be placed in the data segment Area.

Package: Enter jar-cvf test. jar *. * to package the class file in the dos environment. After you copy the file to another user, you can use the file in the parent directory where the file is to be packaged.

Access modifier: private default (only accessible to classes in the same package) protected public (friend in C ++)
The modifier class contains the same package subclass anywhere.
Private YES
Default YES
Protected YES
Public YES

Inheritance:
A child can only have one father (extends), but he can have multiple interfaces ). Generally, children are larger than fathers.
Super inherits the methods of the parent class.
Constructor in inheritance:
The constructor of the base class must be called during subclass construction. Super. parent class constructor, this. Other constructor of subclass. If super is called, it must be written in the first line of the subclass constructor.
If the subclass does not show that the constructor of the parent class is called, the program automatically calls the constructor without parameters of the parent class and then executes the sub-method. If the subclass does not display a constructor that calls the parent class, and the parent class does not have a constructor that has no parameters, the compilation will fail.

Rewrite: The method name, parameter list, and return type of the inherited method must be consistent, and the access permission should be greater than or equal to the inherited method.

Hash encoding: Unique represents an object, and the location of the object can be found through hash encoding. Map <key, value>.

Equals: The equals method of an object is defined to be the same when two variables refer to the same object. When comparing two different objects, but in other aspects, You can override the equals method.

Object Transformation (the upward or downward transformation can be similar to the numerical type ):
For example, Animal a = new Cat ();. enjoy (); transformation below: Animal a = new Cat (); Cat c = (Cat) a; c. enjoy ();
Instanceof is followed by a class name to determine whether the object pointed to by the referenced variable belongs to this class or its subclass.

Note: when a parent class references a subclass object, it only sees that the subclass inherits the methods and variables of the parent class and cannot access the newly added content of its subclass object. It can generate scalability in the code.

Polymorphism (ultimate scalability)
Three conditions for using polymorphism: 1. There must be inheritance 2 there must be rewriting 3 parent class reference pointing to subclass object

Dynamic binding: In reality, new users call new users. It is used to determine the actual type of the referenced object during execution rather than during compilation. The method is called based on the actual caller. The new method is called by anyone.

Final: The final variable value cannot be changed, the final method cannot be overwritten, and the final class cannot be inherited.

Abstract class: abstract methods must be implemented by subclasses. The interface is also a special abstract class (the methods in it are all abstract methods, and the member variables are all static final types)

Chapter 4 Exception Handling
Location where an exception occurs: e. printStackTrace () traces the stack content when an exception event occurs. GetMessage () only prints exception information.
Throwable includes Error and Exception. An Error is a system Error and an Exception can be handled. In Exception, RuntimeException can be caught or not caught, and the remaining errors must be caught. However, if throws errors occur in the program, they must be caught.

Throws + exception is used after the method name, and throw + exception is used inside the Method

Finally executes the statements in finally, whether caught or not caught exceptions, so as to clear the resources.

Chapter 5 Array
In C, the range of the array must be specified, that is, the size, and its value must be placed on the stack space. Java cannot specify the size of the array, and its value is placed on the heap.
Space.
Each array has a length attribute. length is used.
System. exit (0 |-1);-1 indicates abnormal exit, and 0 indicates normal exit.

The Java sorting algorithm is involved here. Fast sorting, Bubble sorting, Hill sorting, heap sorting, and selection sorting.
Sort:
Sort by selection: sort the values in the array by selection method: sort the values in the array from small to large, and compare the first value with the subsequent value, when the smallest value is found, the position is exchanged and the next round of loop is performed until the end.
Modification makes the selection sorting more efficient: each cycle finds the minimum value of the unsorted sequence, and then exchanges it. (Redo it)

Bubble layout: From back to back, from bottom to top
Job: Bubble Sorting of integer data. The number of orders selected by date is 3 to 500. The last person is the first position.
Two-way loop linked list
Search: it is usually based on the sorted order.
Binary Search

Two-dimensional array: in JAVA, dimensions and dimensions arrays cannot be defined during static initialization. These arrays are automatically allocated and defined by JAVA.
In array arrayCopy, if you change the value of the copied array, the value of the copied array will also change.

Common classes:
Difference between String and StringBuffer: strings can be directly added after StringBuffer.
The String class indicates an unchangeable character sequence.
StringBuffer represents a variable character sequence.
Example:
String s1 = "Hello ";
String s2 = "World ";
// S1 + = s2; // HelloWorld memory analysis: In the data seg area, the memory size is Hello, another piece of memory is that World copies the values of the above two variables to the other memory range when executing the third sentence, and then points s1 to this memory area.

The append method of StringBuffer. Add. Reverse

The round rounding of the Math class is of the long type.

File class. io.
The path separator is in the windows backslash, Linux forward slash, or both use the/forward slash. Long TYPE to store the last modification time.

Recursion: The key is to find a method and find parameters and return values.

Enumeration: Some types must take one of some values. Enum

Chapter 2 Container (key) Set
Container: loads various other objects.

One graph is a class, with three knowledge points and six interfaces.
Figure: Container chart. Util
<Interface>
Collection. External Provision Method)
<Interface>
Set (no order
And it cannot be repeated) List (ordered and equals can be repeated) Map

HashSet rule list ArrayList HashMap

When calling methods such as remove and contains, container objects must compare whether the objects are equal. This involves the equals and hashCode methods of object types to implement custom object equality rules.
Note: equal objects should have equal hash codes. Hash code is very suitable for indexing.

Class: java. util. Collections provides some static methods to implement some common algorithms (such as sorting and binary search) based on List containers ).

Knowledge Point: enhanced for loop, generic, automatic package/unpackage
Interfaces: Collection, Iterator, Set, List, Comparable, and Map

Iterator performs traversal. Each container class that implements the Collection interface has an iterator method to return an object that implements the Iterator interface. The Iterator object becomes an Iterator for convenient traversal of elements in the container. The collection is traversed in a unified manner.

The remove Method of the iterator object is the only security method for deleting elements during iteration. That is to say, the iterator's remove method can only be seen by itself, but not by others.

Enhanced for loop (int I: array)
Disadvantages: array (which cannot easily access the lower tag value) and set (it is not convenient to delete the content in the Set compared with the use of Iterator ).

Set has no sequence and is not repeated.
Retain Intersection

The List is sequential and repeatable.
Each element in the container corresponds to an integer sequence number that records its position in the container. You can access the elements in the container according to the sequence number.

Each element in the List container corresponds to an integer sequence number that records its position in the container. You can access the elements in the container according to the sequence number, which is similar to array, but the List can change its size.
Array, Fast Read, fast change, slow Linked, Fast Read, slow Hash

ArrayList uses arrays as the underlying storage space and provides the List interface.
The linked List uses the linked List as the underlying storage space and provides the List interface.

Object set (int index, object element) returns the old value.

Map is used to store key-value pairs (Red/black tree)
Hashcode can be used to compare multiple elements, resulting in high efficiency. That is, to override the equals method, you must override the hashcode method.
The put method returns the original value.

Automatic packaging (automatically converting basic types to objects) and unpacking (automatically converting objects to basic types)

The generic type is the same as the type in <>. You do not need to use forced conversion when printing and using it.
You can use the generic type as long as the class is followed by <>. Otherwise, it cannot be used.
When defining a set, you can also define the object type in the set. You can specify the object type at the same time in the Iterator during Collection to increase the readability and Stability of the program.

Chapter 8 IO streams (one character and two bytes)
Data direction: input stream (byte stream: InputStream; audio stream: Reader), output stream (byte stream: OutputStream, audio stream: Writer ).

Byte stream and byte stream
By function: node stream (you can directly find the data source in the pipeline and read and write data (such as files and memory) from a specific data source (node )), processing stream (set-layer pipeline, that is, providing more powerful read/write functions for programs through data processing ).

When reading data, remember to close it. When writing data, remember to flush write all the data in the buffer to the destination, and then close it.
Experiments have proved that an integer variable must be defined outside, and the read data must be stored in that variable, and then written, at this time, the written files are in the same order as the files read.

Stream processing:
1. Buffer stream: With a buffer. It is mounted on the corresponding node stream and provides the buffer function for read and write data, which improves the Read and Write efficiency and adds some new methods. Buffer

2. Conversion stream: the byte stream is directly converted into a bytes stream for input or output. One readline method can read or write a row to increase the speed. InputStreamReader

3. Data Stream
DataInputStream is connected to InputStream node streams. It provides methods to access machine-independent JAVA raw data (such as int and double. Its constructor is DataInputStream (InputStream in)
First-write, first-read, first-out, and queue.
4. print stream (only output stream, no input stream, print)
Both PrintWriter and PrintReader belong to the output stream. They are for characters and bytes respectively, and will not throw an exception. It is used for output of multiple data types and has the automatic flush function.
5. Object stream
Serializable interface refers to the serialization and Mark interface, which does not need to be implemented.
ObjectOutputStream
Transient transparent modified member variables are not considered during serialization, that is, they are the default values when being written on the hard disk.
The externalizable interface is a subinterface of serializable. You can use this method to control reading or writing.

Chapter 3 threads
Thread: different execution paths in a program.
Process: static concept. The. exe file is a process. Process Execution means that the main method in the process starts to run.
You can create a Thread by creating a Thread object, use the run method to complete the operation, and use the start () method to start a Thread.

To start a Thread, you must call the start () method of the Thread class.

There are two ways to create a new thread:
1. Implement the Runnable interface.
2. Define a sub-class of a Thread and override its run method.

When starting a thread, try to inherit from the interface.

State Conversion of threads
Blocking status
| (Up)
Blocking events caused by Blocking Removal
| (Down) |
Create --> start () --> ready <--> schedule <--> Run Status --> stop

Sleep: static method of Thread (throwing an exception ). End the sub-thread call with a flag.
Jion: Merge thread. After the sub-thread is executed, continue the original thread.
Yield () gets out of the CPU, and the current thread enters the ready queue for scheduling.

The thread priority ranges from 1 to 10. The default value is 5.

Thread Synchronization
Synchronized (this) {} or public synchronized void add (String name) {} is used to lock the current object during the execution of this method.

One way to solve the deadlock: reduce the synchronization granularity, that is, lock the entire object, instead of locking the sub-objects in it.

Note: A thread accesses a method with a lock and locks the current object. The other thread cannot execute the locking method in this object, but can access this object without locking.

When a program accesses a resource in the database, it usually locks the "modified method", but does not lock the reading method.

Chapter 10 Network Programming Technology + Management + Communication
A good book on network programming, "TCP/IP details"
TCP: a connection-oriented protocol designed to provide reliable end-to-end byte stream communication over the unreliable Internet. TCP connections are byte streams rather than Packet streams. (TCP three-way handshake)

UDP: provides an application with a method to send encapsulated original IP datagram, and does not need to establish a connection during sending. It is an unreliable connection.

Socket is used to connect client ---- server to java.net
Two JAVA applications exchange data through a two-way network communication connection. one end of this two-way link is called a Socket.
The two types of Socket and ServerSocket defined in the java.net package are used to implement two-way connection between the client and server respectively. It is to achieve TCP, only TCP has C-S points, UDP does not.

The addressing information required for establishing a connection is the IP address and port number of the remote computer (65536 port numbers, that is, 65536 applications, used to distinguish different applications on the same machine, use more than 1024 RMB)

The port numbers can be divided into UDP and TCP

When using eclipse for testing, you need to use two eclipse for testing. If you are using dos, you need to open two windows, first open the server and then open the client.

UDP delivers fast information, but is not reliable. TCP transmission is slow but reliable.
UDP byteArrayOutputStream

Chapter 11 GUI programming (Database, HTML, CSS, java script, JSP, servelet, J2EE, spring, structs, hibernet, EJB)
AWT (Abstract Window Toolkit) is used for JAVA application GUI (Graphics User Interface graphical User Interface) Programming
Old Java graphics Development Kit. Javax. swing is a relatively new window development kit (AWT group of important images)

 

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.