notes
When the amount of code is small, we can understand it ourselves, but once the project structure is complex, comments are necessary.
Annotations are not executed, but are annotation text for programmers to see.
The following are three comments for Java:
- Single line comments: / / contents
- Multiline comments: / * contents*/
- Document comments: / * * contents*/
identifier
- keyword
Here are some common keywords
All identifiers should start with a letter, dollar sign ($), or underscore ()
The first letter can be followed by any combination of letters, dollar characters, underscores, or numbers
Keywords cannot be used as variable or method names
Identifiers are case sensitive (String Man and String Man are two completely different variables)
Data type (eight basic types + three reference types)
Java is a strongly typed language: it is required that the use of variables should strictly comply with the regulations, and all variables must be defined before use
Basic type
- Integer type
byte:1 byte
short:2 bytes
int:4 bytes
long:8 bytes (long num = 30L;//long type needs an L after the number)
- Floating point type (finite, discrete, rounding error, approximate, not equal, etc.)
It is best to completely avoid using floating-point numbers for comparison
float:4 bytes (float num = 50.1F;//float type needs an F after the number)
float num = 10 / / is also OK
double:8 bytes
- Character type
char:2 bytes (variable can only be assigned one character and constrained by single quotation marks ('))
char name = 'A';// correct
char name = 'AB';// error
- boolean type
Occupy one byte (true or false)
boolean flag = true;
boolean flag = false;
reference type
- class
- Interface
- array
Cast type
public class datatype{ public static void main(String[] args){ char a = 'a'; System.out.println(a); //a System.out.println((int)a); //97 } }
Escape character
//The escape character is \ *, the common ones are \ t and \ n public class HelloWorld { public static void main(String[] args) { System.out.println("Hello\nWorld"); } } //The result is Hello World public class HelloWorld { public static void main(String[] args) { System.out.println("Hello\\nWorld"); } } //The result is Hello\nWorld
Type conversion
From low to high
byte < short < char < int < long < float <double
In the operation, different types of data are first converted to the same type, and then calculated
public class HelloWorld { public static void main(String[] args) { int i = 12; //byte b = i; An error will be reported. Forced conversion is required from high to low byte b = (byte)i;//No error will be reported short num = 10; int n = num;//Low to high automatic conversion } }
variable
- A variable is a variable
- Each variable must declare its type
- The statement after declaration or assignment shall be added;
public class HelloWorld{ //Class variable static static double salary = 2500; //Attributes: Variables //Instance variable: no initialization value is required outside the method in the class String name; int age; boolean bool; //main method public static void main(String[] args){ //Local variable: the value must be declared and initialized, and its life cycle is not easy to use if it only goes out in the main function int i = 10; System.out.println(i); //Variable type variable name = new HellowWorld(); HelloWorld helloWorld = new HelloWorld(); System.out.println(helloWorld.age);//The output is 0, and the default is 0 System.out.println(helloWorld.name);//The output is null, and the default value is null System.out.println(helloWorld.age + helloWorld.name); System.out.println(helloWorld.bool);//The output Boolean value is false by default System.out.println(salary);//Class variable direct output } //Other methods public void add(){ } }
constant
- The value cannot be changed after initialization! Value that will not change.
- Constant names generally use uppercase characters.
- Keywords: final(final constant name = value;)
public class datatype { static final double PI = 3.14; public static void main(String[] args) { System.out.println(PI); } }