Han Shunping's third day of JAVA introduction
1 variable
1) why are variables needed
Variable is the basic unit of the query program. The variable is equivalent to the representation of a data storage space in memory. The variable can be regarded as the house number of a room. Through the house number, we can find the room, and through the variable, we can access the variable value.
Variables have three basic elements: type + name + value
The basic steps of using variables: declaring variables, assigning values, and using.
Example:
public class Var01{ //Write main method public static void main(String[] args) { //Declare variable int a; a = 100; System.out.println(a); //It can also be used like this int b = 800; System.out.ptintln(b); } }
2) variable quick start
Example 2
//Define variables public class Var02{ //Write main method public static void main(String[] args) { //Record person's information int age = 30; double score = 88.9; char gender = 'male'; String name = "king"; //Output information, shortcut keys System.out.println("The person's information is as follows:"); System.out.println(name); System.out.println(age); System.out.println(score); System.out.println(gender); } }
3) precautions for variable use
① variable represents a storage area in memory (different variable types take up different space sizes)
② this area has its own name (variable name) and type (data type).
③ variables must be declared before use.
④ the data in this area can be changed continuously within the agreed type range.
⑤ variables cannot have the same name within the same scope.
⑥ variable = variable name + value + data type.
4) use of plus sign
① when both sides are numerical values, add.
② when one of the left and right sides is a string, do splicing operation.
2 data type
Each kind of data defines a clear data type, and different sizes of memory space (bytes) are allocated in memory.
① java data types are divided into two categories: basic data types and reference types.
② there are eight basic data types (byte,short,int,long,float,double,char,boolean)
③ reference type (class, interface, array)
1) integer type
① each integer type of Java has a fixed range and field length, which is not affected by the specific OS (operating system), so as to ensure the portability of Java programs.
② Java integer constants are of type int by default, and declaration of long constants must be followed by 'l' or 'l'.
③ variables in Java programs are often declared as int, and long is used unless it is not enough to represent a large number.
④ bit: the smallest storage unit in the computer, byte: the basic storage unit in the computer, 1byte=8bit.
//Integer type details ② //Java's integer constant is of type int by default. Declaration of a long constant must be followed by 'l' or 'l'. public class VarDetail{ //Write main method public static void main(String[] args) { // int n1 = 1;// 4 bytes //int n2 = 1L;// There may be a loss from int to long. } }
2) floating point type
Floating point number is stored in the machine in the form of floating point number = sign bit + exponential bit + trailing digit.
The mantissa may be lost, resulting in a loss of accuracy (decimals are approximate).
Floating point type usage details:
① similar to integers, Java floating-point types also have fixed ranges and field lengths, which are not affected by specific OS.
② the floating-point constant of Java is double by default and declared as a floating constant, followed by 'f' or 'f'.
③ floating point constant has two forms: decimal form: 5.12, 512.0f,. 512 (must have decimal point) scientific counting form: 5.12e2, 5.12E-2.
④ in general, double type should be used because it is more accurate.
⑤ floating point number usage trap: comparison between 2.7 and 8.1/3.
public class FloatDetail{ //Write main method public static void main(String[] args) { //The floating-point constant of Java is double by default. It is declared as a floating constant and must be followed by 'f' or 'f'. //float num = 1.1;// Will report an error float num = 1.1F;//correct double num1 = 1.1;//correct double num2 = 1.1F;//correct //Floating point constants can be expressed in two forms: decimal form: 5.12, 512.0f,. 512 (there must be a decimal point) //Form of scientific counting method: 5.12e2 [], 5.12E-2 [] double num3 = .123;//0 can be omitted, equivalent to 0.123 double num4 = 5.12e2;//512.0 double num5 = 5.12E-2;//0.0512 //In general, the double type should be used because it is more accurate. double num6 = 2.1234567851; float num7 = 2.1234567851F; System.out.println(num6);//2.1234567851 System.out.println(num7);//2.1234567 //Floating point use trap: comparison between 2.7 and 8.1/3. double num8 = 2.7; double num9 = 8.1/3; System.out.println(num8);//Output 2.7 System.out.println(num9);//Output decimals close to 2.7, not 2.7 decimals. //When using, we should be careful when we judge whether the operation result is decimal. //It should be judged within a certain accuracy range based on the absolute value of the difference between two numbers. if(Math.abs(num8 - num9) < 0.000001){ System.out.println("The difference is very small, to my specified range, it is considered equal"); } } }
2) floating point type
The character type can represent a single character. The character type is char and char is two bytes (Chinese characters can be stored).
//Use of char public class charDetail{ //Write main method public static void main(String[] args) { char c1 = 'a'; char c2 = '\t'; char c3 = 'Han'; char c4 = 97;//Note: the character type can store a number directly System.out.println(c1); System.out.println(c2); System.out.println(c3); System.out.println(c4);//Output the characters represented by 97. So the output is a } }
Character type usage details
① a character constant is a single character enclosed by (').
② escape characters are also allowed in Java. If char = '/ n'/ n indicates a newline character
③ in Java, the essence of char is an integer. When outputting, char is the character corresponding to unicode code.
④ you can directly assign an integer to char. When outputting, it will be output according to the corresponding unicode characters.
⑤ char is computable, equivalent to an integer, because it has the corresponding unicode code.
//char usage details public class charDetail01{ //Write main method public static void main(String[] args) { //In Java, char is essentially an integer. When outputting, it is the character corresponding to unicode code. //To output the corresponding number, you can (int) character. char c1 = 97; System.out.println(c1);//Output character a char c2 = 'a';//Output the number corresponding to 'a' System.out.println((int)c2);//97 char c3 = 'week'; System.out.println((int)c3);//21608 //char is computable, equivalent to an integer, because it has the corresponding unicode code. System.out.println('a' + 10);//Output 107 } }
The nature of character types
① when the character type is stored in the computer, the code value corresponding to the character needs to be found, such as' a '
Storage: 'a' = > code value 97 = > binary = > storage
Read: binary = > 97 = > 'a' = > display
② the correspondence between characters and code values is determined through the character coding table.
3) boolean type
Basic introduction
① boolean type: Boolean. Only true and false values are allowed.
② boolean type occupies one byte
③ applicable to logic operation, generally used for program flow control:
if conditional control statement, while loop control statement, do while loop control statement and for loop control statement.
//Use of bool public class bool{ //Write main method public static void main(String[] args) { //Demonstrate the case of judging whether the result is passed //Define a boolean variable boolean isPass = true; if (isPass == true){ System.out.println("Pass the exam, Congratulations"); }else{ System.out.println("If you don't pass the exam, you must do it next time"); } } }