1. Notes
Comments will not be executed. They are for those who write code. Writing comments is a good habit. We must pay attention to standardization when writing code at ordinary times.
-
There are three types of annotations in Java
A single line annotation can only annotate one line of text//
Multiline annotation can annotate a paragraph of text / **/
Document comments / * * / JavaDoc
2. Identifier
-
keyword
-
All components of Java need names. Class names, variable names, and method names are called identifiers.
3. Precautions for identifier
- All identifiers should be in letters (A-Z or A-Z), dollar sign ($), or underscore () Start.
- The first character can be followed by a letter (A-Z or A-Z), a dollar sign ($), or an underscore () Any combination of characters or numbers.
- Keywords cannot be used as variable or method names.
- Identifiers are * * case sensitive * *.
- It can be named in Chinese, which is generally not recommended.
4. Data type
- Strongly typed language
- It is required that the use of variables should strictly comply with the regulations, and all variables must be defined before they can be used.
-
Java data type
Basic type
public class Demo02 { public static void main(String[] args) { //Eight basic data types //integer int num1 = 10; //Most commonly used byte num2 = 20; short num3 =30; long num4 = 30L; //Long type should be followed by an L //Decimals: floating point numbers float num5 = 50.1F; //For Lfloat type, add an F after the number double num6 = 3.141592658; //character char name ='country'; //String, string is not a keyword, class //String namea = "Iron Man"; //Boolean: Yes No boolean flag = true; //boolean flag = false; } }
Reference type: class interface array
5. Data type expansion
public class Dome03 { public static void main(String[] args) { //Integer expansion: binary 0b decimal octal 0 hexadecimal 0x int i = 10; int i2 = 010; //Octal 0 int i3 = 0x10; //Hex 0x 0 ~ 9 A ~ F 16 System.out.println(i); System.out.println(i2); System.out.println(i3); System.out.println("======================================"); //=========================================== //Floating point expansion? How to express banking business? money //BigDecimal uses mathematical tool classes for comparison //=========================================== //float finite discrete rounding error is approximately close to but not equal to //double //It is best to use floating point numbers for comparison float f = 0.1f; //0.1 double d = 1.0/10; //0.1 System.out.println(f==d); //false float d1 = 231313131313131313f; float d2 = d1+1; System.out.println(d1==d2); //true //================================================= //Character expansion? //================================================= char c1='a'; char c2='in'; System.out.println(c1); System.out.println((int)c1);//Force conversion System.out.println(c2); System.out.println((int)c2); //Force conversion //All characters are still numbers in nature //Encoding Unicode table: (97 = a 65 = a) 2 bytes 0 - 65536 //U0000 UFFFF char c3 = '\u0061'; System.out.println(c3); // a //Escape character // \t tab // \n line feed System.out.println("Hello\nWorld"); //Boolean extension boolean flag = true; if (flag==false){} //Novice if (flag){} //an old hand } }
6. Type conversion
public class Demo04 { public static void main(String[] args) { int i = 128; double b =i; //Cast (type) variable name high -- low //Automatic conversion low high System.out.println(i); System.out.println(b); /* Note: 1.Boolean values cannot be converted 2.Cannot convert an object type to an unrelated type 3.When converting high capacity to low capacity, force conversion 4.There may be memory overflow or accuracy problems during conversion */ System.out.println("==========================="); System.out.println((int)23.7); //23 System.out.println((int)-65.56f); //-65 System.out.println("==========================="); char c = 'a'; int d = c+1; System.out.println(d); System.out.println((char)d); } }
public class Demo05 { public static void main(String[] args) { //When operating large numbers, pay attention to the overflow problem //JDK7 new feature, numbers can be separated by underscores int money = 10_0000_0000; int years = 20; int total = money*years; //-1474836480, overflow during calculation long total2 = money*years; //The default is int. something went wrong before the conversion? long total3 = money*((long)years); //First convert a number to Long System.out.println(total3); } }
7. Variables and constants
-
A variable is a variable!
-
Java is a strongly typed language, and every variable must declare its type.
-
Java variable is the most basic storage unit in a program. Its elements include variable name, variable type and scope.
-
matters needing attention
1. Each variable has a type, which can be either a basic type or a reference type.
2. The variable name must be a legal identifier.
3. Variable declaration is a complete statement, so each declaration must end with a semicolon.
public class Demo07 { //Class variable static static double salary = 2500; //Attributes: Variables //Instance variable: subordinate object; If you do not initialize yourself, the default value of this type is 0.0 //Boolean: false by default //Except for the basic type, the other default values are null; String name; int age; //main method public static void main(String[] args) { //Local variables: values must be declared and initialized int i=10; System.out.println(i); //Variable type variable name = new Demo07(); Demo07 demo07 = new Demo07(); System.out.println(demo07.age); System.out.println(demo07.name); //Class variable static } //Other methods public void add(){ } }
- Constant: the value cannot be changed after initialization! Value that will not change.
- Constant names generally use uppercase characters.
final Constant name=Value; final double PI=3.14
8. Variable naming specification
- All variables, methods and class names: see the meaning of the name
- Class member variables: first letter lowercase and hump principle: monthSalary except the first word, the following words are capitalized
- Local variables: initial lowercase and hump principle
- Constants: uppercase letters and underscores: MAX_VALUE
- Class name: initial capitalization and hump principle: Man, GoodMan
- Method name: initial lowercase and hump principle: run(), runRun()
9. Basic operators
- Arithmetic operators: +, -, *, /,%, + +, –
- Assignment operator:=
- Relational operators: >, <, > =, < == instanceof
- Logical operators: & &, ||,!
- Bitwise operators: &, |, ^, ~, > >, <, > > >
- Conditional operator:?:
- Extension operators: + =, - =, * =/=
10. Package mechanism
- In order to better organize classes, Java provides a package mechanism to distinguish the namespace of class names.
- Generally, the inverted company domain name is used as the package name.
- In order to use the members of a package, we need to explicitly import the package in the Java program, and use the "import" statement to complete this function.
11.JavaDoc
-
The javadoc command is used to generate its own API documents
-
parameter information
- @Author author name
- @Version version number
- @since indicates that the earliest version of jdk is required
- @param parameter name
- @Return return value
- @throws exception thrown
-
How to open JavaDoc document with IDEA
- First open idea, click Tools, and then click Generate JavaDoc
- Configure parameters