elements of grammar
Note: it mainly records the features of Java but not C + +
1. Notes
Single line and multiple lines are no different from C + +
Document notes:
/** *@(Note Title 1) specific content *@(Note Title 2) specific content */
2. Keywords
java can name identifiers in Chinese
3. Data type
Like C + +, java is also a strongly typed language
3.1 basic types
Integer type | meaning |
---|---|
byte | Occupy 1 byte: - 128 ~ 127 |
short | Occupy 2 bytes: - 32768 ~ 32767 |
int | Occupy 4 bytes: - 2147483648 ~ 2147483647 |
long | Accounting for 8 bytes: - 9223372036854775808 ~ 9223372036854775807 |
No long long type
int n1=10; long n2=10L; //Add an L to calibrate that 10 is a long type, otherwise an error will be reported int i1=10; //decimal system int i2=010; //Octal 0 int i3=0x10; //Hex 0x int x=10_00_00 //Underline is not output when printing x. This method is mainly to see a big number more clearly
Floating point type | meaning |
---|---|
float | 4 bytes |
double | 8 bytes |
long f1=1.1F; //Add an F to calibrate that 1.1 is a float type, otherwise an error will be reported double f2=1.1;
Character type | meaning |
---|---|
char | 2 bytes, Unicode encoding (U0000~UFFFF) |
Boolean type | meaning |
---|---|
boolean (not bool) | Take one place. There are only two values: true and false |
3.2 reference type
Class, interface, array
4. Type conversion
(type)Variable name //Cast high -- > low long a=1000000000; int b=10; long x=a*b; //x will output 10000000000, and B will automatically convert upward during calculation //If int a is declared, a*b will not generate type conversion, the calculation result will overflow, and x will obtain the overflow value
Note: 1. boolean cannot be converted.
2. You cannot convert an object type to an unrelated type.
3. When converting a high-capacity type (such as long) to a low-capacity type (such as int), perform a cast. Conversely, automatic conversion is performed.
4. There may be memory overflow or accuracy problems during conversion.
5. Variables
If the instance variable is not initialized, the general default values are in the form of 0, 0.0, null and false
Class variable: decorated with static
public class Demo{ static double salary=2500; double age=25; public static void main(String[] args){ Demo demo=new Demo(); System.out.print(demo.age); //Instance variables must be called through objects System.out.print(salary); //Class variables are not called through objects } }
constant
java constants are decorated with final
Variable naming convention
- All variables, methods and class names: see the meaning of the name
- Class member variables: lowercase first letter and hump principle: monthSalary except the first word, the first letter of the following word shall be capitalized monthCommonBonus
- Local variables: initial lowercase and hump principle
- Constants: uppercase letters and underscores: MAX_VALUE
- Class name: initial capitalization and hump principle: Man, LinkList
- Method name: initial lowercase and hump principle
6. Operator
java has one more instanceof, which is the same as C + +
Instance is used to test whether the object on its left is the instance of the class on its right, and return the boolean data type.
boolean result = object instanceof class
public static void main(String[] args) { Object testObject = new ArrayList(); displayObjectClass(testObject); } public static void displayObjectClass(Object o) { if (o instanceof Vector) System.out.println("Object is java.util.Vector Class"); else if (o instanceof ArrayList) System.out.println("Object is java.util.ArrayList Class"); else System.out.println("Object is " + o.getClass() + " Class"); } } //The output is "the object is an instance of the java.util.ArrayList class"
If the object is an object of a class (or direct subclass, indirect subclass, or the same inheritance tree), instanceof will return true.
null is false when compared with any type with instanceof.
//A special case int a=10,b=10; System.out.print(""+a+b); //Output 20 System.out.print(a+b+""); //Output 1010 //From left to right, judge whether the "+" sign is a string connection or an arithmetic addition
More mathematical operations can call the Math class
7. Package mechanism
package
Similar to C + + namespace. It can also be regarded as a folder. You can avoid naming conflicts
package pkg1[.pkg2[.pkg3...]]
Class A is placed in the directory Mitchell.tools, so when writing class A, you must add a sentence at the beginning
package Mitchell.tools;
To edit the HelloWorld class, you must add a sentence at the beginning
package Mitchell;
import package
The import statement must be placed under the package statement
import package1[.package2].(classname | *); //*Represents all classes under related packages
8.JavaDoc
Purpose: generate your own API documents
basic content
Available parameters | meaning |
---|---|
@author | Author name |
@version | Version number |
@since | Indicates the earliest jdk version required |
@param | Parameter name |
@return | Return value |
@throws | Exception thrown |
Open file as directory
Current file directory
Method of generating document
Method 1 command line
Method 2 idea built-in tools
effect
Web effect