annotation

Single-line comment: Comment only the current line, starting with // and ending with the line

/ / outputCopy the code

Multi-line comment: Comment a paragraph of text starting with /* and ending with */

/* output output */Copy the code

Documentation comments: Used to produce API documentation that works with JavaDoc.

/** @description HelloWorld class * @author Diamond piao**/Copy the code

identifier

  • All identifiers should start with A letter (A-z or a-z), A dollar character ($), or an underscore (_).
  • The first character can be followed by any combination of letters (A-Z or A-Z), dollar signs ($), underscores (_), or digits.
  • You cannot use keywords as variable names or method names.
  • Identifiers are case sensitive.

The data type

Public static void main(String[] args) {// byte system.out.println (" basic type: byte binary: "+ byt.size); System.out.println(" Wrapper class: java.lang.byte "); System.out.println(" min: byte.min_value =" + byte.min_value); System.out.println(" Max: byte.max_value =" + byte.max_value); System.out.println(); // short system.out.println (" basic type: short binary: "+ short. System.out.println(" Wrapper class: java.lang.short "); System.out.println(" min: Short.MIN_VALUE=" + Short.MIN_VALUE); System.out.println(" Max: Short.MAX_VALUE=" + Short.MAX_VALUE); System.out.println(); // int system.out. println(" Basic type: int binary number: "+ integer.size); System.out.println(" Wrapper class: java.lang.integer "); System.out.println(" Min: integer.min_value =" + integer.min_value); System.out.println(" Max: integer.max_value =" + integer.max_value); System.out.println(); // long system.out.println (" Base type: long binary number: "+ long.size); System.out.println(" Wrapper class: java.lang.long "); System.out.println(" min: long.min_value =" + long.min_value); System.out.println(" Max: long.max_value =" + long.max_value); System.out.println(); // float system.out. println(" Basic type: float binary number: "+ float. System.out.println(" Wrapper class: java.lang.float "); System.out.println(" min: Float.MIN_VALUE=" + Float.MIN_VALUE); Println (" Max: Float.MAX_VALUE=" + Float.MAX_VALUE); System.out.println(); // double system.out. println(" base type: double: "+ double-.size); System.out.println(" Wrapper class: java.lang.double "); Println (" min: Double.MIN_VALUE=" + Double.MIN_VALUE); Println (" Max: Double.MAX_VALUE=" + Double.MAX_VALUE); System.out.println(); // char system.out. println(" basic type: char binary: "+ character.size); System.out.println(" Wrapper class: java.lang.Character"); Println (" min: character.min_value =" + (int) character.min_value); // Print character.min_value to the console as a numeric value instead of a Character. System.out.println(" maximum: character.max_value =" + (int) character.max_value); }Copy the code

ASCIIS code

1 letter (case insensitive) = 1 byte space

1 Chinese character = 2 bytes of space

1 ASCII code = 1 byte

Utf-8 encoding

1 English character = 1 byte

English punctuation = 1 byte

1 Chinese (including traditional) = 3 bytes

Chinese punctuation = 3 bytes

Unicode

1 English character = 2 bytes

English punctuation = 2 bytes

1 Chinese (including traditional) = 2 bytes

Chinese punctuation = 2 bytes

1bit means 1bit,

1Byte indicates a byte 1B=8b.

1024B=1KB

1024KB=1M

1024M=1G. 

Floating point number precautions

  1. The default is double.
  2. Floating point numbers have rounding errors and many numbers cannot be represented accurately. If you need to perform precise numerical calculations that do not generate rounding errors, you need to use the BigDecimal class.
  3. Avoid floating point numbers in comparisons.

Type conversion

  1. Boolean types cannot be cast.

  2. An object type cannot be converted to an object of an unrelated class.

  3. Casts must be used when converting a large type to a small type.

  4. Overflow or loss of accuracy may occur during conversion,

  5. Floating-point to integer conversions are achieved by dropping decimals, not by rounding.

Automatic type conversion: Data types with small capacity can be automatically converted to data types with large capacity.

Syntax for casting: (type)var. Type in operator () indicates the target data type to which the value var is to be converted. The condition is that the converted data types must be compatible.

variable

Java is a strongly typed language, and every variable must have its type declared.

Java variables are the most basic storage unit in a program. Their elements include variable name, variable type and scope.

A variable must be declared before it is used. Only after the variable is declared, a storage unit of corresponding length can be allocated to it. The declaration format is as follows:

Type varName[=value] [{,varName[=value]}]; // Data type variable name = value; Multiple variables of the same type can be declared separated by commas.Copy the code

Each variable has a type, which can be a primitive or a reference type.

The variable name must be a valid identifier.

Variable declarations are a complete statement, so each declaration must end with a semicolon.

Variable scope

  • Class variable (static variable: static variable) : a variable that is independent of a method.
  • Instance variable (member variable: member variable) : a variable independent of a method, but without the static modifier.
  • Lacal variables: Variables in a method of a class.

A local variable

  • A variable defined inside a method or statement block. The life cycle starts at the declared location and ends at “} “.
  • Must be declared and initialized before use
  • Local variables do not have default values, so local variables must be initialized before they can be used.

The instance variables

  • A variable defined outside a method and inside a class.
  • The life cycle of a subordinate object is always with the object.
  • If it is not initialized by itself, it is automatically initialized to the default value of the type.

A static variable

  • Use the static definition.
  • Subordinate to a class, the life cycle follows the class from load to unload.
  • If it is not initialized by itself, it is automatically initialized to the default value of the type.

constant

  • Constant: The value cannot be changed after initialize! Values that do not change.
  • A constant can be understood as a special variable whose value is set and cannot be changed during program execution.
  • Constant names generally use uppercase characters.
  • Using constants in programs improves the maintainability of code.

Naming conventions for variables

  1. All variables, methods, and class names: they are known by their names
  2. Class member variables: lowercase and hump principle: monthSalary
  3. Local variables: lowercase and the hump rule
  4. Constants: uppercase and underscore: MAX_VALUE
  5. Class name: Uppercase and hump principle: Man, GoodMan
  6. Method name: Lowercase and hump Rules: run(), runRun()

The operator

  • Arithmetic operators: +, -, *, /, %, ++, —
  • The assignment operator =
  • Relational operators: >, <, >=, <=, ==,! = instanceof
  • Logical operators, &&, | |,!
  • Operator: &, | ^. ~, > >, < <, > > >
  • Conditional operators? :
  • Extended assignment operators :+=, -=, *=, /=

Integer arithmetic

If either operand is Long, the result is also Long.

In the absence of long, the result is int. Even if the operands are all shot,byte, the result is int.

Floating point arithmetic

If either operand is double, the result is double.

The result is float only if both operands are float.

Modulus operation

  • Negative % negative = negative
  • Negative % positive = negative
  • Positive % negative = positive

Unary operator

The increment (++) and decrement (–) operator is a special kind of arithmetic operator in which two operands are required to perform the operation. The increment and decrement operator is a single operand, with both prefix and suffix.

public static void main(String[] args) { int a = 3; int b = a++; // After executing,b=3. I'm going to assign b and then I'm going to increment it. int c = ++a; // After executing,c=5. Increment first, then assign b}Copy the code

Logical operator

Logic and: && and &, logic or: | | and |, logic not:!

Logic and logic or by means of a short circuit. You compute it from left to right, and you don’t compute it anymore if you determine the value. The result is true only when both operands are true, but when the first operation is false, the result must be false, and the second operation is not judged.

Boolean and return false as long as either of them is false.

Return true as long as any of the logic is true;

Ternary operator

Syntax format:

x ? y : z 
Copy the code

Where x is a Boolean expression, the value of x is calculated first. If true, the result of the whole triadic operation is the value of the expression Y; otherwise, the result of the whole operation is the value of the expression Z

The role of package

  • Groups classes or interfaces with similar or related functions into the same package to facilitate the search and use of classes.
  • Like folders, packages are stored as tree directories. The class name in the same package is different, and the class name in different packages can be the same. When two classes with the same class name in different packages are called at the same time, the package name should be added to distinguish them. Therefore, packages can avoid name conflicts.
  • Packages also restrict access so that classes that have package access can access classes in a package.
  • Java uses packages to prevent naming conflicts, access control, and provide searching and locating classes, interfaces, enumerations, annotations, and so on.

The syntax format for a package statement is:

Package pkg1[.pkg2 [.pkg3...]];Copy the code

Create a package

  • When creating a package, you need to give the package a proper name. Later, if another source file contains the class, interface, enumeration, or annotation type provided by the package, the package declaration must be placed at the beginning of the source file.
  • The package declaration should be in the first line of the source file, and each source file can have only one package declaration to which each type in the file applies.
  • If a source file does not use package declarations, its classes, functions, enumerations, comments, and so on will be placed in an unnamed package.
  • Generally, company domain name inversion is used as the package name;

The import keyword

In order to be able to use the members of a package, we need to explicitly import the package in a Java program. Use the “import” statement to accomplish this function.

Import statements in Java source files should come after package statements and before all class definitions, either none or more, with the syntax:

The import package1 [. Package2...]. .(classname|*);Copy the code

JavaDoc

Javadoc is a Sun technology that extracts class, method, member, and other comments from a program’s source code to form an API help document that matches the source code. That is, as long as the program is annotated with a specific set of tags, the development documentation of the program can be formed through Javadoc at the same time after the program is written. The javadoc command is used to generate API documents by entering javadoc + file name. Java in the target file directory on the command line.

Start with /* and end with /.

  • @ the author the author name
  • @ version version number
  • @since specifies the earliest JDK version you want to use
  • @ param parameter name
  • @return Indicates the return value
  • Throws exceptions