Home > Java > javaTutorial > A brief summary of java basic knowledge

A brief summary of java basic knowledge

黄舟
Release: 2016-12-21 11:45:35
Original
1280 people have browsed it

A summary of basic knowledge of ava

In an independent original program, there can only be one public class, but there can be many non-public classes.
In addition, if no class in a Java program is public, then the file name of the Java program can be named whatever you want.

Documentation comments end with a single slash followed by two stars (/**), followed by a star followed by a single slash (*/). Content commented in this way is interpreted as formal documentation of the program and can be included in documentation generated by tools such as javadoc to describe the program's hierarchy and methods.

Java treats the data type of integer constants as int type. Therefore, if a constant larger than 2147483647 is used in the program, an error will occur during compilation. To solve this problem, just add a capital letter after the integer constant. "L" can be used, which means that the constant is an integer constant of type long.
The code for the maximum value is MAX_VALUE and the minimum value is MIN_VALUE. If you want to get the maximum or minimum value of a certain type, just add the full name of the class to which they belong before these codes.

f page feed \ backslash
b go back one space ' single quote
r return " double quote

Data type variables have been defined in the program. If you want to use another data type to express it, Java
Will automatically perform data type conversion when the following conditions are met:
1. The data type before conversion is compatible with the type after conversion
2. The representation range of the data type after conversion is larger than the type before conversion

If a value outside the representable range of the variable is assigned to this variable, this conversion is called a narrowing conversion. Since the accuracy of the data may be lost during the conversion process, Java will not automatically perform these types of conversions. , at this time, mandatory conversion must be done.

+3; // means positive 3
~a; // means taking the complement of a

Priority operator class associativity
1 () bracket operator From left to right
1 [] Square bracket operator from left to right
2 !, + (positive sign), - (negative sign) Unary operator from right to left
2 ~ Bit logical operator from right to left
2 ++, -- Increment and decrement operators from right to left
3 *, /, % Arithmetic operators from left to right
4 +, - Arithmetic operators from left to right
5 <<, > > Bit left shift and right shift operators from left to right
6 >, >=, <, <= Relational operators from left to right
7 ==, != Relational operators from left to right
8 & (bit operator AND) Bit logical operators from left to right
9 ^ (bit operator symbol XOR) Bit logical operators from left to right
10 | (bit operator symbol OR) Bit logical operators from left to Right
11 && Logical operator from left to right
12 || Logical operator from left to right
13 ?: Conditional operator from right to left
14 = Assignment operator from right to left

1, occupied bytes Types that occupy fewer bytes are converted to types that occupy more bytes.
2. Character types are converted to int types.
3. Int types are converted to float types. 4. If the type of an operand in the expression is. double, the other operand number will also be converted to double
type.
5. The Boolean type cannot be converted to other types.

The selected value in the switch statement can only be characters or constants.

1. The first time. When entering the for loop, assign a starting value to the loop control variable.
2. Check whether to continue executing the loop based on the content of the judgment condition. When the judgment condition value is true (true)
, continue to execute the statements in the loop body; judge. When the condition value is false (false), the loop will be jumped out and other statements will be executed. 3. After the statements in the loop body are executed, the loop control variable will change the value of the loop control variable according to the increase or decrease requirements. , then return to step 2 to re-judge whether to continue executing the loop.

System.arrayCopy(source,0,dest,0,x): The meaning of the statement is: copy the x elements of the source array starting from subscript 0 to the target array, starting from the position corresponding to subscript 0 of the target array Start accessing.
Arrays.sort(array name) is an operation of sorting an array
It is worth mentioning that Java allows the number of elements in each row of a two-dimensional array to be different, which is different from ordinary programming languages.
"Anonymous objects", as the name suggests, are objects without explicit declaration. Readers can also simply understand
as an object that is only used once, that is, no specific object name refers to it.

There are two ways to compare objects, they are the "==" operator and the equals() method. The "==" operator is used to compare whether the memory address values ​​​​of two objects are equal, equals() The method is used to compare whether the contents of two objects are consistent.
this represents the current object, and
the so-called current object refers to the object that calls methods or properties in the class.
If you want to use a certain constructor method to call another constructor method in the program, you can use this to achieve it. The specific
calling form is as follows:
this() ;
Among all methods in a class, only the constructor is called first, so calling the constructor using this must and can only be placed on the first line of the constructor. A class can use static code blocks that are not included in any method body. , when the class is loaded, the static code block is executed, and only executed once. Static code blocks are often used to initialize class attributes. You can watch more java video tutorials for a better understanding of this part.

Although the constructor is private, it does not necessarily mean that this class cannot produce instantiated objects. It just means that the location where the instantiated objects are produced has changed, that is, instantiated objects can only be produced in this class.

When declared, internal classes will destroy the structure of the program, and readers are often not recommended to use them during development.

You can also use static to declare inner classes. The inner classes declared with static become external classes, but the inner classes declared with static cannot access non-static external class attributes.
Inner classes can also be called from outside the outer class by creating objects, as long as the inner class is declared as public
Can


As mentioned in the first part of the java video, Java supports three forms of annotations. The first two are // and /*… */.
The third way is called documentation comments. It ends with the "/**"Start with"*/" flag. Documentation comments provide the functionality to embed
program information into the program. Developers can use the javadoc tool to extract the information and then convert it into HTML files.


When instantiating a subclass object, it will default to calling the constructor method in the parent class first, and then
call the corresponding constructor method in this class.
Using super to call the constructor in the parent class can only be placed on the first line of the program.


After declaring the constructor method in the abstract
class, it must be explicitly called in the subclass
If a class does not
use the extends keyword to clearly indicate that it inherits another class, then this class will inherit the Object class by default.


Data members in the interface must be initialized, and all data members are constants.
2. All methods in the interface must be declared as abstract. That is to say, the interface cannot maintain a general method like an abstract class, but must all be "abstract methods".


Interfaces, like general classes, also have data members and methods, but data members must be assigned initial values, and
this value cannot be changed anymore


Interfaces are a mechanism for Java to implement multiple inheritance. A class Only one parent class can be inherited, but if a class needs to inherit multiple abstract methods, it is obviously impossible to implement, so the concept of interface emerged. A class can only inherit one parent class, but it can implement multiple interfaces.
Similarly, the extension (or inheritance) of the interface is also implemented through the keyword extends. Interestingly,
an interface can inherit multiple interfaces
The parent class uses its own class to instantiate its own object
, but it does not know who its own subclass is, so errors will definitely occur during conversion


You can use instanceof to determine whether a class implements an interface, or you can use it to determine whether an instance object belongs to a class.


As mentioned before, Object is the parent class of all classes, and the toString() method needs to be overridden.
If readers check the JDK manual, they will find that there is an equals method in the Object class. This method is used Compare
whether the objects are equal, and this method must be overridden


The interface cannot be instantiated
directly, because there is no constructor method in the interface, but it can be implemented through the subclass of the interface
based on the concept of object polymorphism Instantiation


Exceptions can be divided into two major categories: java.lang.Exception class and java.lang.Error class. Both classes inherit
from the java.lang.Throwable class.


If you use throws to throw an exception in a class method, you must explicitly use try-catch to catch it where
it is called. The


jar command is a very useful command provided in Java, which can be used to compress a large number of classes (.class files)
and then save them as .jar files.



class class name extends Thread // Extend the subclass from the Thread class
{
modifier run(){ // Override the run() method in the Thread class
// Programs processed by threads;
}
}
It can be started with start()
It can also be implemented through implements Runnable
There is no start() method in the Runnable interface, so a class that implements the Runnable interface must also use the start() method in the Thread class to start multiple threads .
The Thread class implements the Runnable interface, which means that the Thread class is also a subclass of the
Runnable interface.


After a class inherits the Thread class, no matter how many times the object of this class calls the start() method, only one thread will be running.


new Thread(t).start();//where t is a class that implements the Runnable interface
After multiple consecutive calls, multiple threads sharing data will be generated


Any thread generally has five states , that is, create, ready, run, block, and terminate.
Two Thread objects are allowed to have the same name, but for the sake of clarity this should be avoided.
If the program does not specify a name for the thread, the system will automatically assign a name to the thread.


You can use the isAlive() method to test whether the thread has been started and is still starting.


If a thread object
calls the setDaemon(true) method before starting (calling the start() method), this thread becomes a background thread.
When only the background thread is running in the process, the process will end.
join() method is used to force a certain thread to run (forcibly run the following thread after running)


You can find a static method named sleep(long millis) in the Thread class. This method is used
Thread sleeping.


When a thread is running, another thread can call the interrupt() method of the corresponding Thread object to
interrupt it.


You can also use the Thread object to call the isInterrupted() method to check the interruption status of each thread.


Once the interrupt state is marked, when the thread reaches interruptible blocking (some interrupts cannot be interrupted)
, it will throw an exception and end the current operation, and then reset the state.


synchronized(object)
{
Code that needs to be synchronized;
}
It should be noted that the synchronization monitor can generally be acted by any object, as long as it is unique and constant,
It is usually recommended to use a share that may be accessed concurrently The resource acts as a synchronization monitor.


In addition to synchronizing code blocks, you can also synchronize functions. Just add the synchronized keyword before the definition of the function that needs to be synchronized.


The wait() method causes the current thread to wait and release the lock on the synchronizer.
The notify() method wakes up a single thread waiting on the synchronizer. The choice of wake-up is arbitrary.
notifyAll() wakes up all waiting threads.


There are many ways to control the thread life cycle, such as: suspend method, resume method and stop method.
Although stop can avoid the occurrence of deadlock, it also has other shortcomings: if a thread is operating a shared data segment and is "stopped" before the operation is completed, it will lead to incomplete data.


Figure 9-11 The life cycle of a thread


The method of ending a thread by controlling the loop condition in the run method is recommended for readers
, and it is also the most commonly used method in practice. The


StringBuffer class is used for strings whose content can be changed. Various other types of data can be added and inserted into the string, and the original content in the string can also be transposed.


In actual development, if you need to frequently change the content of a string, you need to consider using the StringBuffer class to implement it. Because its content can be changed, the execution performance will be higher than that of the String class.


exit(int status) method to terminate the virtual machine in advance. If an abnormal situation occurs and you want to terminate the operation of the virtual machine, pass a non-zero value as a parameter. The


CurrentTimeMillis method returns the time in milliseconds since 0:00:00 on January 1, 1970 to the present, which is a large value of type long.


The Runtime class encapsulates the running process of the Java command itself, and many of its methods are duplicated with those in System
. You cannot directly create a Runtime instance, but you can obtain a reference to the running Runtime object through the static method Runtime.getRuntime.


The getInputStream() method can get an input stream. The input stream obtained by the getInputStream()
method on the client's Socket object is actually the data stream sent back from the server. The getOutputStream() method gets an
output stream. The output stream returned by the getOutputStream() method on the client Socket object is the data stream that will be sent to the
server.


Sockets have two main modes of operation: connection-oriented and connectionless.


Connectionless operations use the datagram protocol. A datagram is an independent unit that contains all the information for this delivery.


Connection-oriented operations use the TCP protocol. A socket in this mode must establish a connection with the destination socket before sending data. Once the connection is established, sockets can use a stream interface

The above is a brief summary of Java basic knowledge. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template