Zero-basic learning java: variation (1), Basic java
I. Concepts of variables:
(1) A storage area in the memory
(2) The region has its own name (variable name) and type (data type)
(3) Each variable in Java must be declared first and then used
(4) data in the region can change continuously within the same type.
Note the following when using variables:
(1) scope of the variable: valid for one pair {}
(2) initialization value
Defines the format of the variable: data type variable name = initialization value
2. Classification of variables-by Data Type
For each data type, a specific data type is defined, and memory space of different sizes is allocated in the memory.
1Integer type: byte, short, int, long
Each integer type in Java has a fixed table number range and field length, which is not affected by the specific OS to ensure the portability of java programs.
The default integer constant of java is int type. After declaring a long type constant, add 'L' or 'l'
2Floating point type: float and double
(1) similar to the integer type, the Java floating point type also has a fixed table number range and field length, and is not affected by the specific OS.
(2) The default float type constant of Java is double type, and the float type constant must be added with 'f' or 'F '.
(3) floating point constants have two forms:
Decimal number format: for example, 5.12 512.0f. 512 (a decimal point is required)
Scientific Notation: for example, 5.12e2 512E2 100E-2
3Character Type: char
Char data is used to indicate "characters" (2 bytes) in the general sense)
Three manifestations of constant type:
(1) A character constant is a single character enclosed by single quotes (''), covering all written characters in the world. For example, char c1 = 'a'; char c2 = '中'; char c3 = '9 ';
(2) The Escape Character '\' Can Be Used in Java to convert the subsequent characters into special character constants. For example, char c3 = '\ n'; //' \ n' indicates a linefeed.
(3) Unicode values are directly used to represent the character type constant '\ uxxxx '. XXXX represents a hexadecimal integer. For example, \ u000a indicates \ n.
(4) the char type can be computed. Because it should have Unicode code
4, Character encoding
(1) ASCII code
In a computer, all data is represented in binary. Each binary bit has two states: 0 and 1. Therefore, 8 binary bits can combine 256 states, which is called a byte ). A single byte can be used to represent 256 different States. Each State corresponds to one symbol, that is, 256 symbols, from 0000000 to 11111111.
ASCII code: In the 1960s s, the United States developed a set of character codes to define the relationship between English characters and binary characters. This is called an ASCII code. The ASCII code consists of A total of 128 characters. For example, the SPACE is 32 (Binary 00100000), and the uppercase letter A is 65 (Binary 01000001 ). These 128 symbols (including 32 control symbols that cannot be printed) only occupy the last seven digits of one byte, and the first one digit is set to 0.
(2) UnicodeEncoding
Garbled: there are multiple encoding methods in the world. The same binary number can be interpreted as different symbols. Therefore, to open a text file, you must know its encoding method. Otherwise, garbled characters may occur when an incorrect encoding method is used.
Unicode: An encoding that includes all symbols in the world. Each symbol is uniquely encoded and Unicode is not garbled.
Unicode disadvantages: Unicode is only a collection of symbols. It only specifies the binary code of the symbols, but does not specify how the binary code should be stored: Unicode and ASCII cannot be distinguished: the computer cannot distinguish between three bytes to indicate one or three symbols respectively.
(3) UTF-8:
UTF-8 is the most widely used Unicode implementation method on the Internet.
UTF-8 is a variable length encoding method. It can use 1-6 bytes to represent a symbol and change the length of the byte according to different symbols.
UTF-8 encoding rules:
For a single-byte UTF-8 encoding, the maximum bit of this byte is 0, and the remaining 7 digits are used to encode characters (equivalent to ASCII code ).
For multi-byte UTF-8 encoding, if the encoding contains n Bytes, then the first n bit of the first byte is 1, the first byte of the n + 1 is 0, the remainder of this byte is used to encode characters. The maximum two bytes after the first byte are "10", and the remaining six digits are used to encode the characters.
5Boolean Type: boolean
The boolean type is suitable for logical operations and is generally used for program flow control.
If condition control statement
While loop control statement
Do-while loop control statement;
For loop control statement
Boolean data can only be set to true or false without null.
It is not allowed to replace false and true with zero or non-zero integers, which is different from the C language.
Case
// Variable: Basic Data Type (8 Data Types) vs reference data type (class, interface, array)
// 1. Format of variable definition in java: data type variable name = initialization value
ClassTestretriable {
PublicstaticvoidMain (String [] args ){
// 2. variables must be defined first and then used
IntMyInt1 = 10;
DoubleD = 12.3;
System. out. println (myInt1 );
System. out. println (myInt1 + d );
// I1 is out of its scope and cannot be used.
// System. out. println (i1 );
// 3. Integer: byte (-128 ~ + 127) short int (default type) long
ByteB1 = 12;
// Byte b2 = 128;
ShortS1 = 128;
IntI1 = 12;
// Defines a long variable. "L" or "l" is added at the end of the value"
LongL1 = listen 4123351336925l;
System. out. println (l1 );
// 4. float (numeric with decimal point): float double (default type)
DoubleD1 = 12.3;
// Declare float data of float type. "F" or "f" must be added at the end"
FloatF1 = 12.3F;
System. out. println (f1 );
// 5. bytes (= two bytes): char can only represent one character (English, Chinese, punctuation, Japanese ,...)
CharC1 = 'a ';
// Char c2 = 'AB ';
String str = "AB ";
CharC3 = '中 ';
String str1 = "China ";
// Escape characters
CharC4 = '\ T ';
CharC5 = '\ n ';
System. out. println ("abc" + c5 + "def ");
// Understand
CharC6 = '\ u1234 ';
System. out. println (c6 );
// 6. boolean type: boolean can only be set to true or false. The value cannot be null.
BooleanBool1 =True;
If(Bool1 ){
System. out. println ("Today is Friday ");
}Else{
System. out. println ("Today is Saturday! ");
}
}
PublicvoidMethod1 (){
IntI1 = 10;
System. out. println (i1 );
}
}
Shang xuanxiang frees you from having to learn JAVA without worrying about IT!