Comments, identifiers, keywords
notes
-
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 our code writers to see.
-
Writing notes is a very good habit. You must pay attention to standardization when writing code.
-
There are three types of annotations in Java:
-
Single line annotation: single line text can be annotated, and the structure is: "/ / + content"
//Output a Hello world! System.out.println("Hello World!");
-
Multiline annotation: you can annotate a paragraph of text with the structure of: "/ * * /"
/* multiline comment * 123 * 123 * 123 * 123 */
-
Document comment: Java Doc, structure: "/ * * /"
/** * @Description HelloWorld * @Author Gao Xu, Hunan University * */
-
identifier
- keyword
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (IMG vepzfcic-163586433915) (C: \ users \ Huawei \ appdata \ roaming \ typora \ user images \ image-20211023134015164. PNG)]
- All components of Java require names, class names, variable names, and method names to be called identifiers.
public String setTeacher(String TeacherName){ String teacherName = TeacherName; return teacherName; } /* Variable name -- > teachername Method name -- > setteacher*/
-
Identifier considerations
- All identifiers should start with letters (a-z or a-z), dollar sign "$", and underscore ""
- The first character can be followed by any combination of letters (a-z or a-z), dollar sign "$", underscore "" or numbers
- Keywords cannot be used as variable or method names
- Identifiers are case sensitive
String Man = "Gx"; String man = "Gx"; //These two variables are completely different because Java identifiers are case sensitive
- Examples of legal identifiers: age, $salary_ value,__ 1_value
- Examples of illegal identifiers: 123abc, - salary, #abc
public static void main(String[] args) { String teacher = "Gx" System.out.println(teacher); }
- It can be named in Chinese, but it is generally not recommended to use it in this way, nor is it recommended to use pinyin. It's very Low!
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. example: Java,c++
-
Weakly typed language
-
Java data types fall into two categories
-
primitive type
-
Integer type: byte takes up 1 byte, range: - 128 ~ 127
short takes up 2 bytes, range: - 32768 ~ 32767
int takes 4 bytes, range: - 2147483648 ~ 2147483647
long takes up 8 bytes, range: - 9223372036854775808 ~ 9223372036854775807
//Integer type byte num1 = 10; short num2 = 30; int num3 = 500; //Most commonly used long num4 = 30L; //The long type needs an L after the number //Floating Point Types float num5 = 50.1F; //For float type, add an F after the number, otherwise an error will be reported, so you can change it to double type double num6 = 3.1415926; //Character type char name = 'A'; //String, string is not a keyword, it is a class String name1 = "gaox"; //boolean, boolean boolean flag = true; boolean flag = false;
-
Floating point type: single precision float takes up 4 bytes and double precision double takes up 8 bytes
-
Character type char: 2 bytes
-
Boolean type: 1 bit, with only true and false values
-
-
Supplementary: what is byte?
- Bit: it is the smallest unit of data storage in the computer. 11001100 is an eight bit binary number
- byte: it is the basic unit of data processing in the computer. It is customarily expressed in uppercase B
- 1B (byte) = 8bit (bit)
- Characters: letters, numbers, words, and symbols used in computers
// 1 bit means 1 bit // 1byte represents a byte 1B = 8bit 1024B = 1KB 1024KB = 1M 1024M = 1G 1024GB = 1TB
Thinking: what is the difference between 32-bit and 64 bit computers?
A: a 32-bit operating system can only use a 32-bit CPU, while a 64 bit operating system can use either a 64 bit CPU or a 32-bit CPU; Their addressing capabilities are also different. 32-bit computers have 4GB of memory and 64 bit computers have 128GB + memory
- reference type
- class
- Interface
- array
-
Type conversion
-
Because Java is a strongly typed language, type conversion is required for some operations.
Low high
byte, short, char —> int —> long —> float —> double
-
In the operation, different types of data are first transformed into the same type, and then the operation is carried out.
-
Forced type conversion: the format is: (type) variable name; Application: from high to low
int i = 34; byte b = (byte)i; //Cast type
-
Automatic type conversion: automatic conversion; Usage: from low to high
int i = 34; double b = i; //Automatic conversion
-
Attention
-
Boolean values cannot be converted
-
Cannot convert an object type to an unrelated type
-
When converting high capacity to low capacity, force conversion is used
-
There may be memory overflow or accuracy problems during conversion
int money = 1000000000; int years = 20; int total = money * years; //The result is - 1474836480, which overflows during calculation long total1 = money * years; //The result is - 1474836480. Both money and years are int, so an error occurred before the conversion //Correct conversion mode long total2 = money * ((long)years); //The result is 2000000000
-
Variable, constant
variable
-
Variables are variables that can be changed, opening up a space in memory
-
Java is a strongly typed language, and every variable must be named for its type
-
Java variable is the most basic storage unit in a program. Its elements include variable name, variable type and scope
type varname [= value]; //Format: data type variable name = value;
-
matters needing attention:
- Each variable has a type, which can be either a basic type (int, double...) or a reference type (String,...)
- Variable name must be a legal identifier
- Variable names are a complete statement, so each name must end with a semicolon
pubilc class Demo1 { //Class variable static keyword static double salary = 2500.00; //Instance variable: subordinate to the object. If initialized, its corresponding default value will be returned //int defaults to 0; The default value of double is 0.00 //Boolean defaults to false //The default values are NULL except for the basic type String name; int age; public static void main(String[] args){ //Local variables; Must be named and initialized int i = 10; //Create an object of class Demo1 demo = new Demo1(); demo.age = 10; demo.name = "Gx"; } }
Supplement: variable naming convention
- For all variables, method names, class names and requirements, see the meaning of names
- Class member variables: initial lowercase and hump principle: monthSalary
- 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()
constant
-
Constant: a value that cannot be changed after initialization!!
-
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.
-
Constant names generally use uppercase characters
//Format is final data type constant name = value; final double PI = 3.14;
operator
The Java language supports the following operators:
- Arithmetic operators: +, -, *, /,%, + +, –
- Assignment operator:=
- Relational operators: >, <, > =, < =, = == Instanceof (for object-oriented)
- Logical operators: & &, |, |,!
- Bitwise operators: &, |, ^, ~, > >, <, > > (understand!!! >)
- Conditional operators:?,: (understand!!! >)
- Extended assignment operators: + =, - =, * =, / = (understand!!! >)
b = a++; //After a + + executes this line of code, assign a value to b first, and then increase it by itself b = ++a; //++A increases itself before executing this line of code, and then assigns a value to b //Many operations can be operated by some tool classes Math class a && b; //Both are true, and the result is true a || b; //If one of the two is true, the result is true /** Bitwise Operators a = 0011 1100 b = 0000 1101 */ a & b = 0000 1100 //In the same bit comparison, both are 1, and the result is 1 a | b = 0011 1101 //In the same bit comparison, one bit is 1 and the result is 1 a ^ b = 0011 0001 //For the same bit comparison, the same is 0 and the difference is 1 (XOR) ~b = 1111 0010 //Just reverse it 2 << 3 = 16; //Shift left is equivalent to * 2 8 >> 2 = 2; //Shift right equivalent to / 2 //String connector+ int a = 10; int b = 10; System.out.println("" + a + b); //Output 1020, preceded by "" and added as a string System.out.println(a + b + ""); //Output 20, with "" in the rear, add directly according to int //Ternary operator // x ? y : z int score = 50; String type = score < 60 ? "fail," : "pass"; System.out.println(type); //Failed output
Package mechanism, Java Doc
Package mechanism
-
In order to better organize classes, Java provides a package mechanism to distinguish the namespace of class names.
-
The syntax format of the package statement is:
package pkg1[. pkg2[. pkg3......]];
-
Generally, the company domain name is inverted as the package name; com.gxstudy.blog
Java Doc
-
The Java doc command is used to generate its own API documents
-
parameter information
- @Author author name
- @Version version number
- @since you need the earliest jdk version
- @param parameter name
- @Return return value
- @throws exception thrown