Java Basic Grammar Notes

Source: Internet
Author: User
Tags arithmetic operators bitwise

In a separate primitive program, there can be only one public class, but there are many non-public classes.

In addition, if no class is public in a Java program, the Java program's file name can be arbitrarily named.


Document comments, which start with a single slash and two star markers (/**), and end with a single star marker plus a slash (*/). The content that is annotated in this way is interpreted as a formal document of the program and can be included in a document generated by a tool such as Javadoc to illustrate the hierarchy of the program and its methods.


Java treats the data type of an integer constant as int, so if a constant of more than 2147483647 is used in the program, the compile-time error will occur

To solve this problem, simply add an uppercase "L" after the integer constant, 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 take the maximum or minimum value of a type, simply precede the code with the full name of the class to which they belong.


\f page \ \ Backslash

\b Backward one-piece \ ' single quotation mark

\ r homing \ "double quotes


A variable of the data type is already defined in the program, and if you want to represent it with a different data type, Java

The conversion of the data type is done automatically when the following conditions are true:

1. The data type before conversion is compatible with the converted type.

2. The converted data type has a larger representation range than the type before conversion


If you assign a value to a variable that is beyond the scope of the variable, this conversion is called a narrowing conversion. Because the accuracy of the data may be lost during the conversion, Java does not automatically do these types of conversions, and a mandatory conversion is required.


+3; Indicates positive 3

~a; To show the complement of a


Precedence operator Class-binding

1 () bracket operator from left to right

1 [] square brackets operator from left to right

2!, + (plus sign),-(minus) unary operators from right to left

2 ~ Bitwise logical operators 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 <<, >> left shift, right shift operator left to right

6 >, >=, <, <= relational operators from left to right

7 = =,! = relational operator from left to right

8 & (bitwise operator and) bitwise logical operators from left to right

9 ^ (bitwise OP symbol XOR) bitwise logical operator from left to right

10 | (bitwise operation SYMBOL OR) bit logical operator left to right

&& logical operators from left to right

12 | | Logical operators from left to right

13?: conditional operator from right to left

14 = assignment operator from right to left


1. A type with less bytes is converted into a type that consumes more bytes.

2. The character type is converted to the int type.

3, the int type is converted to float type.

4. If the type of an operand in the expression is double, the other operation number is also converted to double

Type.

5. Boolean types cannot be converted to other types.


The selection value in the switch statement can be either a character or a constant.


1. When entering for loop for the first time, assign a starting value to the loop control variable.

2, according to the content of the judging conditions to check whether to continue to execute the loop, when the judgment condition value is true (true)

, the statement in the body of the loop is resumed, and when the condition value is False (false), it jumps out of the

Loop to execute other statements.

3. After executing the statements in the body of the loop, the loop control variable will change the loop according to the requirement of increment or decrement.

Control the value of the variable, and then go back to step 2 to re-determine whether the loop continues execution.


System.arraycopy (SOURCE,0,DEST,0,X): The statement means: Copy the source array starting with subscript 0

x element to the target array, starting at the position corresponding to the subscript 0 of the target array.

Arrays.sort (array name) array-ordered operation

It is worth mentioning that Java allows the number of elements in each row of a two-dimensional array to be different, which is the general procedure

The language is different.

An "anonymous object", as the name implies, is an object that has no explicit declaration. The reader can also simply understand

For objects that are used only once, that is, there is no specific object name to reference it.


There are two ways to compare objects, which are "= =" operators with the Equals () method, "= ="

operator is used to compare the memory address values of two objects for equality, the Equals () method is used to compare the two objects within a

are consistent.

This represents the current object, while the

The so-called current object refers to the object that invokes the method or property in the class.

If you want to use a constructor method to invoke another constructor method in the program, you can use this to implement the specific

The invocation form is as follows:

This ();

All methods in a class

, only the constructor method is called first, so the constructor method with this call must also be placed

In the first line of the construction method

A class can use a static block of code that is not contained in any method body, and when the class is loaded, the static code

Blocks are executed and executed only once, and static code blocks are often used to initialize class properties.


Construction Method Although

is private, but it doesn't necessarily mean that this class cannot produce an instantiated object, just the bit that produces the instantiated object.

The instantiation object can only be produced in this class.


When the inner class declares, it destroys the program's knot

The reader is often not recommended for use in development.


You can also use static

Declares an inner class, the inner class declared with static becomes an outer class, but an inner class declared with static cannot be visited

Ask for non-static external class properties.

Inner classes can also be called from outside classes by creating objects, as long as the inner class is declared as public

Can


As already mentioned in the first part of this book, Java supports three types of annotations. The first two types are//and/* ... */.

The third way is called a document comment. It begins with "/**" and ends with a "*/" sign. Document annotations provide the

The functionality of the program information embedded in the program. Developers can use the Javadoc tool to remove information and then convert

As an HTML file.


The subclass object, when instantiated, defaults to calling the constructor method in the parent class before

Call the appropriate constructor method in this class.

Using super to call a constructor in the parent class can only be placed on the first line of the program.


In the abstract

When a constructor method is declared in a class, it must be explicitly called in the subclass

If a class does not

There is an explicit identifier for inheriting another class using the extends keyword, then the class inherits the Object class by default.


The data members in the interface must be initialized, and the data members are constants.

2. The methods in the interface must all be declared abstract, that is, the interface can not be like an abstract class to maintain a

Methods, and must all be "abstract methods".


interfaces, like general classes, have data members and methods, but data members must assign initial values, and

This value will no longer be changed


Interfaces are a mechanism for Java to implement multiple inheritance, and a class can inherit only one parent class, but if you need a

If a class inherits multiple abstract methods, it is obviously impossible to implement, so there is the concept of an interface. A class is only

You can inherit a parent class, but you can implement multiple interfaces.

Similarly, the extension (or inheritance) of an interface is implemented by the keyword extends. The interesting thing is that

An interface can inherit multiple interfaces

The parent class instantiates its own pair with its own class.

Like, but it does not know who is its own subclass, that must be wrong when converting


You can use instanceof to determine whether a class implements an interface, or it can be used to judge a real

Example object belongs to a class.


As mentioned earlier, Object is the parent class of all classes, where the ToString () method is required to be replicated,

If the reader goes to the JDK manual, it is found that there is a equals method in the Object class that is used to compare

objects are equal, and this method must be replicated


Interfaces cannot be instantiated directly

, because there is no construction method in the interface, but it can be based on the concept of object polymorphism, through the sub-class of the interface

Instantiate it


Exceptions can be divided into two main categories: The Java.lang.Exception class and the Java.lang.Error class. All of the two classes inherit

From the Java.lang.Throwable class.


If an exception is thrown with throws in the method of the class, the

The place to call it must be explicitly captured with Try-catch.


The jar command is a very useful command available in Java that can be used on a large number of classes (. class files)

To compress, and then save as a. jar file.



Class name extends thread//extend subclass from thread class

{

Modifier run () {//Replication Thread class in the Run () method

A thread-processing program;

}

}

Use Start () to start

can also be achieved by implements Runnable

There is no start () method in the Runnable interface, so a class implements the

The Runnable interface must also start multithreading with the Start () method in the Thread class.

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, the object of this class is invoked regardless of the

How many times the start () method, the result is that only one thread is running.


New Thread (t). Start ();//Where T is a class that implements the Runnable interface

This causes multiple threads to share data after successive calls


Any thread typically has five states, namely, create, ready, run, block, and terminate.

Allow two Thread objects to have the same name, but in order to be clear, you should try to avoid the occurrence of such a situation.

If the program does not specify a name for the thread, the system automatically assigns a name to the thread


The IsAlive () method can be used to test whether a thread has started and is still starting.


If a thread pairs

The Setdaemon (True) method is called before starting (calling the start () method), and the thread becomes a post-

Server thread.

The process ends when only the background thread is running in the process.

The join () method is used to force a thread to run (after it is forced to run and then run later)


In the Thread class, you can find a static method named Sleep (Long Millis), which is used to

The sleep of the thread.


When one thread is running, another thread can invoke 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 interrupt state of each thread.


Once the interrupt state is played, the thread executes to interruptible blocking (some interrupts cannot be interrupted)

, an exception is thrown and the current operation is ended, and then the state is reset.


Synchronized (object)

{

Code that needs to be synchronized;

}

It is important to note that the synchronization monitor can generally be used by any object, as long as it is the only constant.

It is generally recommended to use a shared resource that may be concurrently accessed to act as a synchronization monitor.


In addition to synchronizing blocks of code, you can synchronize functions as long as the functions that need to be synchronized

Add the Synchronized keyword before you define it.


The wait () method causes the current thread to wait and release the lock on the Synchronizer.

The Notify () method wakes a single thread waiting on the Synchronizer, and the selection for wake is arbitrary.

Notifyall () wakes up all the waiting threads.


There are many ways to control the thread life cycle, such as the Suspend method, the Resume method, and the Stop method.

While stop prevents deadlocks from occurring, there are other deficiencies: if a thread is working on a common

Enjoy data segment, the operation process is "stop" if it is not completed, it will result in the incompleteness of the data.


Figure 9-11 the thread life cycle


The way to end a thread by controlling the loop condition in the Run method is to recommend reading

, which is also the most used method in practice.


The StringBuffer class is used for strings that can be changed by the content, adding and inserting data from various other types

Into a string, you can also transpose the original content in the string.


In real-world development, if you need to change the contents of a string frequently, you need to consider using the StringBuffer class real

Now, because its content can be changed, execution performance will be higher than the String class.


Exit (int status) method to terminate the operation of the virtual machine prematurely. To terminate a virtual event for an exception

Machine, passing a non-0 value as a parameter.


The Currenttimemillis method returns from January 1, 1970 0:0 0 seconds to present in milliseconds as a single

Bit of time, which is a long type of large value.


The runtime class encapsulates the running process of the Java command itself, many of which are related to methods in the System

Repeat. You cannot create a Runtime instance directly, but you can use the static method Runtime.getruntime to obtain a positive

A reference to the runtime object that is running.


The getInputStream () method can get an input stream, getInputStream () on the client's Socket object

The input stream that the method obtains is actually the data stream that is sent back from the server side. The Getoutputstream () method gets a

Output stream, the output stream returned by the Getoutputstream () method on the client Socket object is going to be sent to

Server-side data flow.


Sockets has two main modes of operation: connection-oriented and non-connected.


A non-connected operation uses the datagram protocol. A datagram is a separate unit that contains all the

Information on this delivery.


Connection-oriented operations use the TCP protocol. A socket in this mode must be preceded by the data sent to the

The socket of the destination gets a connection. Once the connection is established, sockets can use a stream interface













Java Basic Grammar Notes

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.