java basic syntax
1.Comments, identifiers, keywords 2.data type 3.Type conversion 4.Variable, constant
1. Note:
We usually write code. When the amount of code is relatively small, we can still understand what we write, but once the project structure becomes complex, we need to use comments.
Comments are not executed. They are for those of us who write code
Writing notes is a very good habit
You must pay attention to specification when writing code.
Single-Line Comments // multiline comment /* */ Documentation Comments /** * */
public class hello { public static void main(String[] args) { //Single-Line Comments //Output a Hello World! System.out.println("Hello world!"); /*multiline comment */ /* I'm a multiline comment I'm a multiline comment I am a multiline comment (Ctrl+D is to copy the previous line) */ //JavaDoc: document comments / * **/ /** * @Description Hello World * @Author Crazy God says Java_ Liu Ting **/ } }
2. Identifier:
All components of Java need names. Class names, variable names, and method names are all called identifiers
Identifier considerations
All identifiers should be in letters $(Dollar sign),_(Underline) start The first letter can be followed by a letter $,_ Number or any combination of characters Keywords cannot be used as variable or method names Identifier case sensitive It can be named in Chinese, but it is not recommended to use it, that is, it is also recommended to use pinyin Low
Legal identifier: letter, dollar sign $, underscore_
public class Demo01 { public static void main(String[] args) { // Case sensitive String hello = "fs"; String Hello = "fs"; //legitimate String Ahello = "fs"; String hhello = "fs"; String $hello = "fs"; String _hello = "fs"; //wrongful // String 1hello = "fs"; // String #hello = "fs"; // String *hello = "fs"; } }
3. Keywords:
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
Weakly typed language
JavaScript,Python...
Java data types fall into two categories
Basic type( primitive type),There are 8 basic types, all of which are reference types Reference type( reference type)
public class Demo02 { public static void main(String[] args) { //Eight basic data types //integer int num1 = 10000000; //No more than 2.1 billion, most commonly used byte num2 = 20; //Cannot exceed 127 short num3 = 30; //3 billion long num4 = 30L; //Plus L //decimal float num5 = 50.1F; double num6 = 3.1216864565757; //character char name = 'country'; String namea = "Front sound"; //String is not a keyword. Is a class //Boolean boolean flag1 = true; boolean flag2 = false; } }
extend
//Integer extension // Binary 0b decimal octal 0 hex 0x int i1 = 10; int i2 = 010; //Octal 0 ~ 7 (0-7) 010 (8) int i3 = 0x10; //Hex 0x 0 ~ 9 (0-9) a ~ f (10-15) 0x10 (16)
//Floating point extension: //Interview question: how do banking letters mean money? BigDecimal (math tool class) //float double is problematic. It is best to avoid using floating point numbers for comparison float f = 0.1f; //0.1 double d = 1.0/10; //0.1 System.out.println(f==d); //false //Floating point digits are limited, rounding error, approx //It is best to avoid using floating-point numbers for comparison float f1 = 23131313131f; float f2 = f1+1; System.out.println(f1==f2); //true
//Character extension: are all characters or numbers char c1 = 'a'; char c2 = 'in'; System.out.println(c1); //a System.out.println((int)c1);//Cast, 97 System.out.println(c2); //in System.out.println((int)c2);//Cast, 20013 //Encoding Unicode table (97=a,65=A) 2 bytes 0-65536 //U000~UFFFF hex (u0061=a, equivalent to 97 of decimal) System.out.println('\u0061'); //a '\' is an escape character
//Escape character // \t tab // \n line feed // . . . System.out.println("Hello\tWorld");
//Object from memory analysis String sa = new String("Hello world"); String sb = new String("Hello world"); System.out.println(sa == sb); //false object String sc = "Hello world"; String sd = "Hello world"; System.out.println(sc == sd); //true
//Boolean extension boolean flag = true; if(flag==true){} //Novice if(flag){} //An old hand writes less is more like this (the code should be concise and easy to read)
5. Bytes:
position(bit): It is the smallest unit of data storage in the computer. 11001100 is an eight bit binary number byte(byte, B): It is the basic unit of data processing in the computer. It is customary to use uppercase B express 1B(byte) = 8bit,1 Byte equals 8 bits Characters: letters, numbers, words and symbols used in computers
1 bit means 1 bit
1Byte represents a byte 1B=8b
1024B = 1KB, 1024KB = 1M, 1024M = 1G
6. Type conversion:
Because Java is a strongly typed language, type conversion is required for some operations.
High capacity – > low:
In the operation, different types of data are converted to the same type before operation.
- Cast, (type) variable name, capacity from high to low
- Automatic conversion, capacity from low to high
//Cast (type) variable name high -- low //Automatic conversion low high int i = 128; byte b = (byte)i; //Cast memory overflow - 128 ~ 127 double d =i; //Automatic conversion System.out.println(i); //128 System.out.println(b); //-128 System.out.println(d); //128.0 /* Note: 1.Boolean values cannot be converted 2.Cannot convert an object type to an unrelated type 3.Force conversion when converting a high container to a low capacity 4.There may be a memory overflow or a precision problem * */ System.out.println((int)23.7); //23 lost accuracy char c = 'a'; int n = c+1; System.out.println(n); //98 System.out.println((char)n); //b
//When the operands are large, pay attention to the overflow problem //JDK7 new feature, numbers can be separated by underscores int money = 10_0000_0000; //1 billion, the underline will not be printed System.out.println(money); //1000000000 int years = 20; int total = money*years; //Large data, overflow System.out.println(total); //-1474836480 long total2 = money*years; //The default is int, and there is an overflow problem before conversion System.out.println(total2); //-1474836480 long total3 = money*(long)years; //First turn a number to Long System.out.println(total3); //20000000000
7. Variable:
What is the variable: it is the variable! Java Is a strongly typed language. Each 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:
Each variable has a type, which can be either a basic type or a reference type.
Variable name must be a legal identifier.
Variable declaration is a complete statement, so each declaration must end with a semicolon.
Variable scope
-
Class variable
In the class, it can be used in the whole class, which must be preceded by static;"static int a = 666;"
-
Instance variable
In a class, it belongs to an object; “ int age;//0" If you do not initialize yourself, the default value of this type is 0,0.0 Boolean: default is false;Except for the basic type, the default values are null;
-
local variable
In a method, you must declare and initialize values when using; “ int cba = 666;"
Naming conventions for variables
All variables, methods and class names: see the meaning of the name Class member variables: initial lowercase+Hump principle: lastName Local variables: initial lowercase+Hump principle Constants: uppercase letters and underscores: MAX_VALUE Class name: initial capital+Hump principle: Man,GoodMan Method name: initial lowercase+Hump principle: run(),fastRun()
7. Constant:
-
Constant: the value cannot be changed after initialization! It is a value that will not change.
-
The so-called constant can be understood as a special variable. After its value is set, it is not allowed to be changed during program operation.
final Constant name = Value; final double PI = 3.14;
-
Constant names generally use uppercase characters.
8. Operator:
Arithmetic operator:+, -, *, /, %, ++, - Assignment Operators = Relational operator:>,<, >=, <=, ==, != instanceof Logical operator:&&, |,! Bitwise Operators :&,|, ^, ~,>, <<, >>>understand! ! ! ) Conditional operator? : Extended assignment operator:+=, -=, *-=,/log.cn.onllowv. _wind
Arithmetic operator
int a=10; int b=20; System.out.println(a/b); //0 System.out.println((double)a/b); //0.5 long c=12300000000; System.out.println(a+b); //int System.out.println(a+c); //long automatically converts the data type with large capacity in the formula
Self increasing and self decreasing operator
// ++Self increasing -- self decreasing unary operator int a = 3; int b = a++; //b=a,a=a+1, i.e. B = 3, a = 4 int c = ++a; //a=a+1,c=a increases automatically first, that is, a = 5, C = 5 System.out.println(a); //5 System.out.println(b); //3 System.out.println(c); //5
//Power operation 2 ^ 3 2 * 2 * 2 = 8 double pow = Math.pow(2,3); // (base, index) double type System.out.println(pow); //8.0
//Extension: written test question I = 5 S = (I + + + + + + + I) + (I -- + (- I) s =? int i=5; int s=(i++)+(++i)+(i--)+(--i); System.out.println(s); //24
Logical operator
&& Logic and operation: both variables are true and the result is true || Logic and operation: one of the two variables is true and the result is true ! Take the opposite, true becomes false, false becomes true
// And (snd) or (or) not (negative) boolean a = true; boolean b = false; System.out.println(a&&b); // false System.out.println(a||b); // true System.out.println(!(a&&b)); // true int c=5; boolean d = (c<5)&&(c++<5); //The first value is false, and no judgment will be made later System.out.println(d); //false System.out.println(c); //5 c + + not executed
Bit operation
/* A = 0011 1100 B = 0000 1101 A&B 0000 1101 Bitwise AND A|B 0011 1101 Bitwise OR A^B 0011 0001 XOR ~B 1111 0010 wrong Interview question: how is 2 * 8 the fastest? 2<<3 <<Shift left * 2, high efficiency!! >>Shift right / 2 */ System.out.println(2<<3); // 16
Assignment Operators
int a = 10; int b = 20; a+=b; // a = a+b a-=b; // a = a-b System.out.println(a); //10 //String connector +, convert to string type, and then splice. Note!! System.out.println(""+a+b); //1020 System.out.println(a+b+""); //30 perform the operation first, and then convert it to String splicing System.out.println(a+b+"str"); //30str
Ternary operator
// x ? y : z //If x is true, the result is y, otherwise z //if(x) y; else z; int score = 80; String type = score<60?"pass":"fail,"; System.out.println(type); //pass
9. Package mechanism:
-
In order to better organize classes, Java provides a package mechanism due to the namespace of class names
-
Syntax format of package:
package pkg1[.pkg2[.pkg3...]];
-
Generally, the company domain name is inverted as the package name; com.fs.www
-
In order to be able to use the members of a package, you need to import the package in your Java program
-
Reference: Alibaba Java development manual
10. JavaDoc generated document:
-
The javadoc command is used to generate its own API documents
-
parameter information
- @Author author name
- @Version version number
- @since indicates the earliest jdk version used
- @param parameter name
- @Return return value
- @throws exception thrown
-
API documentation: https://docs.oracle.com/javase/8/docs/api/
/** * @author fs * @version 1.0 * @since 1.8 */ public class Demo05 { String name; /** * @author fs * @param name * @return * @throws Exception */ public String test(String name) throws Exception{ return name; } }
Generate doc document
Open the folder where a class is located cmd command line Input: javadoc -encoding UTF-8 -charset UTF-8 Doc(Class name).java Automatically generates information about this class API Document, check the folder and find some more files open index.html((first page) view document comments
Video learning address: https://www.bilibili.com/video/BV12J41137hu?p=21