20210208JAVA basic grammar

1 comment

Writing comments is a very good habit.
Single-line comments//
Multi-line comments
/*
Text
/
Document comments/
*
*Document comments
*/
Some interesting code comments

2Identifiers and keywords

Insert picture description here
All identifiers should start with a letter (AZ or az), dollar sign $, or underscore _.
After the first letter, any combination of letters , dollar signs , underscores, or numbers
can be used. Keywords cannot be used as variable names or method names.

Examples of legal identifiers: age, $salary, _value, __1_value
Examples of illegal identifiers: 123abc, -salary, #abc
Java identifiers are case-sensitive and
can be named in Chinese, but generally not recommended or recommended Pinyin, very LOW

3 data types

Java is a strongly typed language: the use of variables is required to strictly comply with regulations, and all variables must be defined before they can be used.
Strongly typed languages ​​are safe and slow.
VBs and JAVAs are weakly typed languages.

String string
int number

Java data type classification

Primitive typePrimitive type

Integer types: byte (one byte); Short 2 bytes; int four bytes; Long 8 bytes
floating decimal is: float 4 bytes; Double 8 bytes long
character : Char occupies 2 bytes. //About 2^16?
Boolean type: occupies 1 bit, its value is only true, false

Reference type

Class
interface
array

byte

Bit: the smallest unit of computer internal data storage, 11001100 is an 8-bit binary number
Byte byte: the basic unit of computer data processing, it is customary to use a capital letter B to represent
1B (byte) = 8bit
characters: letters and numbers used in computers , Word and symbol
1bit: 1 bit
1B (byte)=8b (bit)
1024B=1KB=2^10B
1024KB=1M=2^20B
1024M=1G=2^30B

4 Expansion of data types and explanation of interview questions

It’s best to avoid using floating-point numbers to compare.
All characters are essentially numbers.

5 type conversion

Since Java is a strongly typed language, type conversion is needed when performing some operations.
Capacity: low…………………………high
byte, short, char->int->long->float-> In double (decimal priority is greater than integer)
operation, different types of data are converted to the same type, and then calculated

6 variables, constants, scope

6.1 Variables

A variable is a memory space.
Java is a strongly typed language, and each variable must declare its type.
Java variable is the most basic storage unit in the program. Its elements include variable name , variable type and scope
type varName [=value] [{, varName[=value]}]
//Data type variable name=value, you can declare multiple variables of the same type separated by commas.
note:

  1. Every variable has a type, and the type can be a basic type or a reference type.
  2. The variable name must be a legal identifier
  3. A variable declaration is a complete statement, so every declaration must end with a semicolon

6.2 Scope of variables

Class variable: write at the end of the class, keyword Static
instance variable: write in the middle of the class, no Static
local variable: write in the method

6.3 Constant

Constant: The value cannot be changed after initializing (initialize)! Unchanging value The
so-called constant can be understood as a special variable. After its value is set, it is not allowed to be changed during the running of the program.
Final constant name = constant value;
final double PI = 3.14;
constant names are generally capitalized character

6.4 Naming conventions for variables

  1. All variables, methods, and class names: see the name know what it means
  2. Class member variables: first letter lowercase and camel case principle: monthSalary Except for the first word, the first letter of the following words is capitalized lastname-lastName
  3. Local variables: lowercase initials and camel case
  4. Constant: uppercase letters and underscore: MAX_VALUE
  5. Class name: initial capitalization and camel case principle: Man, GoodMan
  6. Method name: lowercase initials and camel case principle: run(),runRun()

7 basic operators

Arithmetic operators: ± /,% (modulo operation: take the remainder), ++,-
assignment operators: =, a=10 (assign 10 to a)
Relational operators: >,<,>=,<=, ==,!= (not equal to), instanceof
logical operators: && and, || or,! Non-
bit operator: &,|,^,~,>>,<<,>>> (understand!!!)
Conditional operator:? :
Extended assignment operator: +=, -=,
=, /=

priority()

8Increase and decrease operators, first acquaintance with the Math class

9 logical operators, bit operators, conditional operators

&& short-circuit operation

10 Three original operators and summary

11 pack mechanism

In order to organize classes better, Java provides a package mechanism, which is used to distinguish the namespace of the class name.
The syntax format of the package statement is:
package pkg1[.pkg2[.pkg3…]]
com.kuang.operator
generally uses the company domain name inversion as Package name
In order to be able to use a member of a package, we need to explicitly import the package in the Java program. Use the import statement to complete this function
import package1[.package2…].(classname|*);

12JavaDoc generates documentation

The JavaDoc command is used to generate your own API documentation
@author Author name
@version Version number
@since Indicate the jdk version that needs to be used earliest
@param Parameter name
@return Return value situation
@throws Exception thrown situation

Guess you like

Origin blog.csdn.net/foundliving/article/details/113797688