Java basic syntax
notes
Comments will not be executed
1. Single line notes: notes one line of text//
2. Multiline comment: comment a paragraph of text / **/
3. Document comments: / * **/
identifier
1. Identifier: the name used in user programming. Class name, variable name and method name are all called identifiers
2. Naming rules:
1.The identifier consists of numbers, letters, underscores and dollar characters 2.Cannot start with a number 3.Keywords cannot be used 4.Case sensitive
3. standard:
1.See the name and know the meaning 2.Chinese is not recommended 3.Use legal abbreviations 4.Either it's all English, or it's all Pinyin
data type
1. Strongly typed language: the use of variables is required to strictly comply with the regulations. All variables must be defined before use,
High security
2.Java data types fall into two categories:
Basic type
Reference type
//integer int a=10_0000_0000; //JDK7 new feature, so the underline will not be output byte num1=23; short num2=89; int num3=190; long num4=467L;//L must be added after Long type (try to capitalize) //Floating point number float num7=8.909F;//F must be added after float type double num8=90.897856342357678786454; //Boolean type boolean flag=false; //character char name='1'; //String is not a keyword but a class
Type conversion
1.Java is a strongly typed language, so type conversion is required when performing operations
2. Automatic conversion
low----------------------------------------->high byte,short,char->int->long->float->double //When four different types of integer are added, a long will be automatically converted to a long. If there is no long, it will be all int, regardless of whether there is an int type in the middle of those numbers
3. Cast (high - > low)
int i=128; byte b =(byte) i; //Force conversion System.out.println(i); //128 System.out.println(b); //-128 memory overflow because the maximum byte is 127 //Memory overflow problem int money=10_0000_0000; int years=20; long total=money*years; //-1474836480, because before converting to long, the default multiplication of two ints is int, which has made an error System.out.println(total); long total1=money*(long)years;//First convert a variable, and then convert it as a whole System.out.println(total1);//20000000000
Data type expansion
Base system
//Integer extension: hexadecimal //Binary: 0b octal: 0 decimal hex: 0x int a=0b1010; int b=072; int c=0x98; int d=78;
Precision loss
//Floating point extension: precision loss //The float finite rounding error is approximately close to but not equal to //Therefore, it is necessary to completely avoid floating-point type comparison (BigDecimal class can be used) float f=0.1f; double d=0.1; System.out.println(f);//0.1 System.out.println(d);//0.1 System.out.println(f==d); //false float f1=232332323232323232f; float f2=f1+1; System.out.println(f1==f2);//true
//Char expansion //The essence of all characters is still numbers //Encoding: a character in the Unicode table corresponds to a number 0-65536 (the 16th power of 2, because the 1 character is 2 bytes) //Indication range: u0000-uffff '\ u0061' char c3='\u0061'; //a System.out.println(c3); char c1='a'; char c2='in'; System.out.println(c1); //a System.out.println(c2);//in System.out.println((int)c1); //Cast 97 System.out.println((int)c2);//20013
Escape character
//Escape character \ t: tab \ n: line feed \ a: ring \ \: \ ":" \ r: Enter System.out.println("hello\tworld");//hello world System.out.println("hello\"world");//hello"world
//boolean extension boolean flag=true; if(flag){ } //The writing above is equivalent to if(flag==true){ }
byte
1.1Byte (byte) = 8bit (bit)
2.bit is the smallest storage unit of computer internal data
3.1B=8b
1024B=1KB
1024KB=1MB
1024MB=1G
character
Characters refer to letters, numbers, words and symbols used in computers
Computer 32-bit and 64 bit
1. They support different memories. 32-bit operating systems support up to 4G of memory, but 64 bit systems can support 4G, 8g, 16G, 32G, 64G, 128G, 256G, etc.
2. They support different software. 64 bit systems support 32-bit and 64 bit software, and 32-bit systems only support 32-bit software.
3. The ability to process data is also different. Theoretically, a 64 bit system will be twice as fast as a 32-bit system. And in terms of system volume, 64 bit system is much larger than 32-bit system.
variable
Data type variable name = value; (values that can be changed during program operation)
1. Each variable has a type, which can be a basic type or a reference type.
2. Variable name must be a legal identifier.
3. Variable declaration is a complete statement, so each declaration must end with a semicolon.
Scope of variable
1. Static variable (class variable)
2. Instance variable
3. local variable
public class demo4 { //3. Class variable (static): add a static keyword before the method inside the class static double salary=2500; //2. Instance variable: it is subordinate to the object (inside the class and outside the method). If it is not initialized by itself, this will happen Default 0.0 for types //Boolean default: false the default values are null except for the basic type String name; int age; //main method public static void main(String[] args) { //1. Local variables: must declare and initialize values int i=10; System.out.println(i); //10 //2. The instance variable uses variable type variable name = new variable type (); demo4 demo44=new demo4(); System.out.println(demo44.name);//null System.out.println(demo44.age);//0 //3. Class variables can be output directly System.out.println(salary);//2500.0 } }
Naming conventions for variables
1. Class member variable, local variable, method name: initial lowercase and hump principle
2. Class name: initial capitalization and hump principle
constant
1. The value that cannot be changed during program operation after initialization can be understood as a special variable
2. Add the final keyword before the variable (the modifier does not distinguish between before and after, and when it is with static, it does not distinguish between before and after)
3. Constant names generally use uppercase letters and underscores
operator
Arithmetic operators: + - * /% + –
++ -- Self increment before: use before adding (time difference) Auto increase after: add before use int a1=90; int b1=a1++; /* 90 a1++ 91 90 */ int c1=++a1; /* 91 ++a1 92 92 */ System.out.println(a1); //92 System.out.println(b1); //90 System.out.println(c1); //92 / Whole for whole, small for small Whole small into small 10/3=3 10.0/3.0=3.3 10.0/3=3.3
Assignment operator: = + = - = * = / =%=
Relational operator: > > = < < = == instanceof
Logical operator: & & |! (short circuit operation)
System.out.println(true&&false); //False is false System.out.println(true||false);//True is true System.out.println(!(true&&false));//True is false, false is true, true
Bitwise operators: & ^ ~ > < > > < > >
& | ^ ~ a=0b00111100; b=0b00001101; a&b=0000 1100 0 if there is 0 a|b=0011 1101 If there is 1, it is 1 a^b=0011 0001 Only 0 and 1 are 1 ~b=1111 0010 0 Is 1,1 Is 0 (Fast operation:~a=a+1 ~13=-14) >> << >>>(The fastest calculation) 0000 0000 0 0000 0001 1 0000 0010 2 1 Moving one bit to the left multiplies by two therefore<<One will take two 2<<3(2*2 The third power of) >>One is divided by two 0000 0011 3 0000 0100 4 0000 1000 8 0001 0000 16
Conditional operator:?:
int score=90; String type=score > 60 ?"pass":"fail,"; System.out.println(type);//pass
String connector:+
int a =10; int b =30; System.out.println(""+a+b);//1030 string before +: string connector System.out.println(a+b+"");//40 strings are added at the end to become connectors
//For many operations, we will use tool classes to perform operations System.out.println(Math.pow(2,3));//8.0 power operation
priority
General formula: a period in parentheses, single, double and three eye re assignment
Binocular formula: "multiply and divide surplus", then "add and subtract", and then "shift", "big wait, small wait", "wait, wait"
"* / %" "+ -" "<< >>" "> >= < <=" "== !="
Bitwise and, exclusive or, logical and and or (one quotation mark and one level)
"&" "^" "|" "&&" "||"
Combination formula: single eye, three eyes and assignment, combined from right to left
package mechanism
1. In order to better organize classes, Java provides a package mechanism to distinguish the namespace of class names
2. Generally, the company domain name is inverted as the package name com baidu. www
3. Guide Package: impos *: wildcard
JavaDoc
1.javadoc command is used to generate API documents of its own classes. It is a tool for processing comments and extracting comments
2. parameter information
@ author author author name
@ version version number
@ since indicates the earliest jdk version required
@ param parameter name
@ return return return value
@ throws exception thrown
public class Demo6 { /** * @author guojiu * @param args * @throws Exception Documentation Comments */ public static void main(String[] args) throws Exception{ } }
3. Generated by command line operation
javadoc parameter XXX java
① open the class in the folder
② open this folder with cmd (enter cmd before the path)
③ enter javadoc parameter XXX java
④ some things will be automatically generated in the folder and open index html
⑤ the Doc below the class is the API document generated by extracting the comments just now
4. Use IDEA to generate javadoc documents
Tools->Generate JavaDoc
Original video: https://www.bilibili.com/video/BV12J41137hu