1, Comments, identifiers, keywords
1. comments
-
We usually write code. When the amount of code is relatively small, we can still understand the code we write, but once the project structure is complex, we need to use comments!
-
Comments will not be executed by the program. They are for people who write our own code to see and understand
-
Writing notes is a very good habit
-
There are three types of annotations in Java:
- Line comment
- Multiline comment (block comment)
- Document comments (JavaDoc)
-
Find nice comments: search for interesting code comments
-
Three types of annotated code
-
Single line comment: only one line can be commented//
public class Hello { public static void main(String[] args) { //Single-Line Comments //Output hello sentence! System.out.println("hello"); } }
-
Multiline annotation: you can annotate a paragraph of text, / **/
public class Hello { public static void main(String[] args) { //Single-Line Comments //Output hello sentence! //multiline comment /* This is a multiline comment This is a multiline comment This is a multiline comment */ System.out.println("hello");
-
JavaDoc: document comments, / * **/
public class Hello { public static void main(String[] args) { //Documentation Comments /** * @descroption hello * @Author zhang */ System.out.println("hello"); } }
-
comments: comments
Annotations: annotations
Bold: Bold
Italic: italic
Description: description
Author: Author
2. Identifier
- Keywords in Java
- All components of Java need names. Class names, variable names and method names are called identifiers. (everyone has his own name, and so does the program. As programmers, we are God's creator. We have to name the program we create, which also reflects that the program comes from life!)
- Identifier considerations
- All identifiers should start with a letter (A-Z or A-Z), dollar sign ($), or 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
- Examples of legal identifiers: age, $salary_ value,__ 1_value
- Examples of illegal identifiers: 123abc, - salary, #abc
- It can be named in Chinese, but generally it is not recommended how to use it or Pinyin. In principle, the naming should see the meaning of the name
- The file name is consistent with the class name, and the class name is capitalized (the default specification)
2, Data type
1. Language type (security and performance)
- Strongly typed language: it is required that the use of variables should strictly comply with the regulations. All variables must be defined before they can be used (high security and low performance)
- Weakly typed language: it is required that the use of variables does not strictly comply with the regulations, and all variables do not need to be defined before use (low security and high performance)
2.Java data types are divided into two categories
-
Primitive type
-
Single and double quotation marks: 'middle' (only one character or word, 'middle' or 'A'; " zahng "(can have multiple characters)
-
Judge the value type and size
-
Eight basic type codes:
public class Demo2 { //Eight basic types //integer byte b1 = 10; short b2 = 100; int b3 = 1000; long b4 = 1000000L;//Long type should be followed by an L to distinguish it from the above. //Floating point number float c1 = 1.1665F;//float type should be followed by an F to distinguish it from double. double c2 = 1.45466456; //Character type char d1 = 'A';//Only one word or one character can be written //String is not a basic type, but a reference type String e1 = "zhang";//Multiple characters can be written //boolean: boolean value, whose values are only true and false boolean f1 = true; }
-
-
reference type: all reference types except basic data types!
3. Variables
-
What is a variable: it is a variable
-
java is a strongly typed language. Every variable must declare its type
-
Java variable is the most basic storage unit in a program. Its three elements include variable name, type and scope
type varName = value //Data type variable name = value; you can declare multiple variables of the same type separated by commas!
-
matters needing attention:
- Each variable has a type, which can be either a basic data type or a reference type (String is a reference type)
- Variable name must be a legal identifier
- Variable declaration is a complete statement, so each declaration must end with a semicolon (;)
4. What are bytes
- Bit: it is the smallest unit of data storage in the computer. 11101110 is an eight bit binary number, which is traditionally represented by a lowercase b.
- byte: it is the basic unit of data processing in the computer. It is customarily expressed in capital B
- 1B (byte) = 8 bit s, that is, if represented in binary, 1 byte = bit to the (8-1) power of 2
- Characters: letters, numbers, words, and characters used in computers
- Unit conversion
- 1 bit means 1 bit
- 1byte means one byte, 1B = 8b (the 8th power of binary) [the power of 8-1]
- 1024B = 1KB
- 1024KB = 1M
- 1024 M = 1G
- The difference between computer 32 and 64 bit
- Downward compatibility, 64 bit computers can be installed with either 64 bit operating system or 32-bit operating system, otherwise not!
- The higher the number of bits, the stronger the computer memory addressing, and the faster the computer runs
- At the same time, 64 bit computers can support 128G memory at most, and 32-bit computers can support 4G memory at most. Therefore, if you want to add memory modules, 64 bit computers can theoretically be added to 128G at most
3, 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
- Decimal priority is greater than integer
- In the operation, different types of data are first converted to the same type, and then the operation is performed
- High to low conversion requires forced conversion, and low to high conversion does not
Cast type: high-----low int-----byte Automatic conversion: low------high int-------double
- Forced type conversion (add conversion type in parentheses) [format: (type) variable name]
public class Demo9 { public static void main(String[] args) { int s1 = 128; byte s2 = (byte)s1;//out of memory System.out.println(s2); } } The result is: -128
- Automatic type conversion (implicit type conversion)
public class Demo10 { public static void main(String[] args) { int s1 = 128; double s2 = s1; System.out.println(s2); } } The result is: 128.0
- Common errors
- 1. The result of conversion is negative because it exceeds the maximum positive range of byte to avoid memory overflow
public class Demo9 { public static void main(String[] args) { int s1 = 128; byte s2 = (byte)s1;//out of memory System.out.println(s2); } } The result is: -128
-
-
2. boolean value cannot be converted. boolean is capitalized by bits, while other data types are in bytes!!!
-
3. Object type cannot be converted to irrelevant type (person cannot be converted to a Book)
-
4. Force conversion when converting high capacity to low capacity
-
There may be memory overflow or precision problem during conversion! (when floating-point number is converted to int, for example, 23.7 becomes 23 and - 4.6 becomes - 4, precision is lost)
-
char type
public class Demo11 { public static void main(String[] args) { //Character conversion char s1 = 'a'; int s2 = s1 +1; System.out.println(s2);//Automatic conversion System.out.println((char) s2);//Force conversion } } The result is 98 b
-
When operating large numbers, pay attention to the overflow problem. JDK7 has a new feature that numbers can be separated by underscores
public class Demo12 { public static void main(String[] args) { //When the number is large, pay attention to the overflow problem //JDK7 new feature, numbers can be separated by underscores int s1 = 1_0000_0000; int s2 = 100; int s3 = s1 * s2;//Overflow during calculation long s4 = s1 * s2;//When calculating, it is calculated according to the default int, and there is an error before it is converted to long type System.out.println(s3); System.out.println(s4); } } The result is: 451611456564151 451611456564151
-
Solve the above problems
public class Demo12 { public static void main(String[] args) { //When the number is large, pay attention to the overflow problem //JDK7 new feature, numbers can be separated by underscores int s1 = 1_0000_0000; int s2 = 100; int s3 = s1 * s2;//Overflow during calculation long s4 = s1 * s2;//When calculating, it is calculated according to the default int, and there is an error before it is converted to long type long s5 = s1 * (long)s2;//When calculating, first convert s2 from int to long, and there will be no problem with the result System.out.println(s3); System.out.println(s4); System.out.println(s5); } }
-
4, Variable, constant
1. Variables
-
What is a variable: it is a variable
-
java is a strongly typed language. Every variable must declare its type
-
Java variable is the most basic storage unit in a program. Its three elements include variable name, type and scope
type varName = value //Data type variable name = value; You can declare multiple variables of the same type separated by commas!
-
matters needing attention:
- Each variable has a type, which can be either a basic data type or a reference type (String is a reference type)
- Variable name must be a legal identifier
- Variable declaration is a complete statement, so each declaration must end with a semicolon (;)
2. Variable scope
-
Class variable
-
Instance variable
-
local variable
public class Demo13 { //Class variable static int s1 = 10;//Class variables must be decorated with static //Instance variable int s2 = 123;//static is not modified as an instance variable //main method public static void main(String[] args) { //local variable int s3 = 4546;//Variables defined in methods, local variables } }
-
Explain variables in detail
public class Demo14 { //Class variable static int s3 = 464; static { //Code blocks, defining attributes, are loaded when the class is loaded } //Instance variable: subordinate object (current class) //If you do not initialize yourself, there is a default value //Basic data type: integer, default value: 0, floating point number: 0.0, character: u0000 (not displayed), boolean: false //The default values are null except for the basic data type int age; String name; char address; double cat; boolean pet; //main method public static void main(String[] args) { //Local variable: valid in the current method, and the scope is the current main method //Must declare and initial values int s1 = 11; System.out.println(s1); //Invocation of instance variables Demo14 s2 = new Demo14(); System.out.println(s2.age); System.out.println(s2.name); System.out.println(s2.cat); System.out.println(s2.address); System.out.println(s2.pet); //Class variable System.out.println(s3);//Can be called directly } //add method public void add(){ } } The result is: 11 0 null 0.0 //Default value for char false
3. Constant
- constant: the value cannot be changed after initialization!! Values that will not be changed.
- The so-called constant can be understood as a special variable. After its value is set, it is not allowed to be modified during program operation
- Format: final constant name = value; final double PI = 3.1415926, variable names are capitalized by default
- Constant names usually use 1 uppercase character!!
- Modifier, no order exists
public class Demo15 { static public int S = 14; public static void main(String[] args) { System.out.println(S); } } The output result is: 14
4. Naming specification of variables
- All variables, methods and class names: see the meaning of the name
- Class member variables: initial lowercase and hump principle: timeLate
- Local variables: initial lowercase and hump principle
- Constants: uppercase letters and underscores: MAX_VALUE
- Class name: initial capitalization and hump principle: Demo, Hello
- Method name: initial lowercase and hump principle: add(), run()
5, Operator
- operator
- cast: conversion
- Self increasing, self decreasing, unary operator = = + +, –==
public class Demo1 { public static void main(String[] args) { //Self increasing and self decreasing, + +-- int s1 = 2; int s2 = s1++;//Assign first and then add int s3 = ++s1;//Add before assign System.out.println(s1); System.out.println(s2); System.out.println(s3); } } The result is: 4 2 4
- Logical operation
public class Demo2 { public static void main(String[] args) { //Logical operation //Short circuit operation int s1 = 6; boolean s2 = (s1<5)&&(s1++<5);//If the front is false, the following s1 + + will not execute!! System.out.println(s2); System.out.println(s1); } } The result is: false 6
- Bit operation
public class Demo3 { public static void main(String[] args) { //Bit operation 0 1 /* A =0011 1100 B =0000 1101 A&B: 0000 1100 If both are 1, it is 1 A|B: 0011 1101 As long as one of the two is 1, it is 1 A^B(XOR); 0011 0001 the two numbers are the same as 0, and the difference is 1 ~B:1111 0010 Reverse 2*8 = 16 2*2*2*2 << Shift left >> Shift right 0000 0001 1 0001 0000 16 */ System.out.println(2<<3);//The above example System.out.println(256>>2); System.out.println(256>>>2);//Same as > > } } The result is: 16 64 64
- Ternary operator
public class Demo4 { public static void main(String[] args) { //Ternary operator //x (statement)? y : s //If x==true, y is returned; otherwise, s is returned int s1 = 1; int s2 = 1<2 ? 10 : 5; System.out.println(s2); } } The result is: 10
- Priority: () high priority, it is recommended to use more!!!
6, Package mechanism, javaDoc
1. Package
- In order to better organize classes, Java provides a package mechanism to distinguish the namespace of class names
- The format of the package statement is: package pkg1;
package java.lang
- Generally, the company domain name is inverted as the package name; www.baidu.com com.baidu.www
- In order to use the members of a package, we need to import the package into the Java program and use the "import" statement to complete this function!
- import package
import base.Demo14; import base.* //Import all * indicates wildcards
- Word: refactor refactoring
2.JavaDoc
- JavaDoc commands are used to claim their API documents
- It can be added to classes or methods (automatically generating something)
package operator; /** * @author zhang * @version 1.0 */ public class Demo4 { String name; /** * * @param args * @throws Exception */ public static void main(String[] args) throws Exception{ //Ternary operator //x (statement)? y : s //If x==true, y is returned; otherwise, s is returned int s1 = 1; int s2 = 1<2 ? 10 : 5; System.out.println(s2); } }
-
parameter information
- @Author author name
- @Version version number
- @since indicates the earliest JDK version used
- @param parameter name
- @Return return value
- @throws exception thrown
-
Generate JavaDoc document
-
javadoc -encoding UTF-8 -charset UTF-8 Demo4.java
-
-
Generate document
-
-
-
IDEA generate help document
-
zh_CN //Generate Chinese documents -encoding UTF-8 -charset UTF-8 //Set encoding set
Generate JavaDoc help document!