4. constant
Constant: refers to the fixed data in the Java program.
classification
type | meaning | Data examples |
---|---|---|
integer constant | integer | Positive and negative integers |
Decimal constant | decimal | Positive and negative decimals |
character constants | Enclosed in single quotation marks, there is only one character and cannot be empty | 'word', 'a', '1' |
string constant | Enclosed in double quotation marks, it can have multiple characters and can be empty | "String", "123", "abc", "“ |
Null constant | There is only one value | true,false |
Boolean Literals | There are only two values | Null (not printable directly) |
5. Variable and data type (variable)
5.1 data type
5.1.1 classification
Basic data types: integer, floating point, character, Boolean
Reference data types: string, array, interface, enumeration, annotation
1) Basic data type:
Integer, floating point, character, Boolean
[the external link 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-bwEfmzyq-1645851443972)(C:\Users\fortrun\Desktop\Notes \ JavaSE in the first stage \ basic data type. png)]
The default type in Java: integer type is int and floating point type is double
2) Character type
The range of integers that a single byte can represent is: - 128 ~ 127
float defaults to 7 significant digits
double defaults to 15 significant digits
ASCII coding table: all English letters, numbers and symbols are corresponding to the decimal system, so the world's first coding table ASCII is produced(
American Standard Code for Information Interchange.
character | numerical value |
---|---|
0 | 48 |
9 | 57 |
A | 65 |
Z | 90 |
a | 97 |
z | 122 |
**Unicode code table: * * universal code. It is also a comparison between numbers and symbols. The beginning 0-127 is exactly the same as ASCII, but it contains more characters from 128.
(Unicode is a universal fixed length character set in the world, and all characters are 16 bits)
3) Escape character
\"--------"
\'--------'
\\--------\
\t -------- tab
\n -------- line feed
5.1.2 data type conversion
1) Automatic conversion type (implicit)
Features: the code does not need special processing and is completed automatically.
Rule: data range from small to large (byte < short < char < int < long < float < double)
//On the left is the long type. On the left is the int integer by default. The left and right are different //Indicates that the int constant 100 is assigned to the long variable for storage //Automatic type conversion occurred long num=100; //Similarly, in accordance with the rule from small to large, automatic type conversion is carried out double num1=1.55F; /* byte<short<char<int<long<float<double */ float num2=1.52L;
2) Cast type (explicit)
Features: the code needs special format processing and cannot be completed automatically.
Format: type with small range variable name with small range = (type with small range) data with large range;
format
Format: type with small range variable name with small range = (Type with small range) Originally a large range of data;
//Compilation failure will occur //On the left is an int integer variable, and on the right is a floating-point variable of double type by default //The value range of double type on the right is larger than that of int integer, so automatic type conversion cannot be performed int i=1.5; //Cast type int i=(int) 1.5; //Type int on the left and type long on the right //Assign long type to int byte<short<char<int<long<float<double //From large to small, automatic data type conversion cannot occur int num=(int) 100L; int num=(int)3.99; //All decimal places will be discarded and output 3
matters needing attention
- Cast is generally not recommended because it may cause precision loss and data overflow.
- byte/short/char these three types can be used for mathematical operations, such as addition "+"
- byte/short/char will be promoted to int type before calculation.
- Data type conversion cannot occur for boolean type
byte<short<char<int<long<float<double
- Floating point data calculation may have errors. If you want to realize accurate operation, you can use Java math. BigDecimal type
System.out.printf(0.1+0.2); //0.3000 0000 0000 0000 4
5.2 variables
5.2.1 concept
Variable: the amount of content that can be changed during program operation.
5.2.2 format:
Data type variable name = data value
//First declare a variable and then assign a value int num1; num1=5; //Or one step int num2=10;
5.2.3 precautions:
-
If you create multiple variables, the names between variables cannot be repeated.
-
For float and long types, don't throw away the letter suffixes F and L.
//Define single precision variable float f=1.5F; //Define long integer variables long l=1.123456789L;
-
If you use a variable of type byte or short, the data value on the right cannot exceed the range of the type on the left.
-
There are variables for assignment, which cannot be used directly; It must be assigned before it can be used.
-
Variable usage cannot exceed the scope.
Scope: from the line defining the variable to the end of the curly bracket to which it belongs.
-
You can create multiple variables with one statement, but it is generally not recommended.
6. Operator
6.1 arithmetic operators
operator | meaning |
---|---|
+ | Addition operation |
- | Subtraction operation |
* | Multiplication |
/ | Division operation (quotient only, no remainder) |
% | Modular operation (only remainder, no quotient) |
++ -- | Self increasing and self decreasing operation |
matters needing attention:
-
If two integers are divided, only the integer part will be retained and the decimal part will be discarded
System.out.println(5/2); //The result is 2, and the decimal part will be discarded
-
If you want to keep the decimal part of the operation result, use the cast type to convert one of the operands to the double type for the operation
System.out.println((double)5/2); //2.5 System.out.println(5/(double)2); //2.5 System.out.println((double)5/(double)2); //2.5
-
Neither 0 nor 0.0 can be divisor
-
byte+byte=int: (note)
byte b1=10; //b1=b1+2; byte+int=int; For incompatible types, conversion from int type to byte may cause precision loss //b1=b1+(byte)2; byte+byte=int; Due to the optimization of the compiler, the operation result is automatically converted to int type b1=(byte)(b1+2) //Cast type b1+=2; //Really equivalent to b1=(byte)(b1+2) //+=Self contained strong rotation
1) Self increasing and self decreasing operation (can only be used for variables, not constants)
Independent operation: + + i is no different from i + (the same principle of self subtraction)
Hybrid operation:
Variable i + +: first assign the value of a to b, and then add 1 to variable a. The final result is a=2, b=1 * * (use first and then add)**
public static void main (String[] args) { int a=1; int b=a++; System.out.println(a);//2 System.out.println(b);//1 }
Variable + + i: add 1 to variable a, and then assign a to b. The final result is a=2, b=2 * * (add first and then use)**
public static void main (String[] args) { int a=1; int b=++a; System.out.println(a);//2 System.out.println(b);//2 }
2) The plus sign "+" in the four operations has three common uses:
-
For numerical values, that is addition.
-
For character char type, char will be promoted to int before calculation.
Comparison table between char type characters and int type numbers: ASCII and Unicode
-
For the String string (capitalized, not keyword), the plus sign represents the String concatenation operation.
When any data type is connected with a string, the result will become a string
String str2 = "Java"; // String + int --> String System.out.println(str2 + 20); //Java20
6.2 assignment operator
operator | meaning |
---|---|
= | be equal to |
+= | Acceleration (with forced rotation) |
-= | Subtraction |
*= | Wait by |
/= | Divide and so on |
%= | Mold taking, etc |
a+=1-------->a=a+1 b-=2-------->b=b-2 c*=3-------->c=c*3 d/=4-------->d=d/4 e%=5-------->e=e%5
6.3 comparison operators
operator | meaning |
---|---|
== | Equal to [two equal signs are equal, and one equal sign represents assignment] |
>= | Greater than or equal to |
<= | Less than or equal to |
> | greater than |
< | less than |
!= | Not equal to |
matters needing attention:
- The result of the comparison operator must be a boolean value. If it is true, it is false
- If you make multiple judgments, you can't write in succession.
(in mathematics, for example, 1 < x < 3 [not allowed] in the program.)
6.4 logical operators
operator | meaning | remarks |
---|---|---|
&& | And (and) | If it is all true, it is true, otherwise it is false |
|| | Or (or) | At least one true is true; It's all false. It's false |
! | Non (negative) | Originally true, it becomes false; It used to be false, but it became true |
matters needing attention:
-
Logical operators can only be used with boolean values.
-
And, or need to have a boolean value on the left and right respectively, but the reverse can only have a unique boolean value.
-
And, or two operators. If there are multiple conditions, they can be written continuously.
Two conditions: condition a & & condition B
Multiple conditions: condition a & & condition B & & condition C
6.5 ternary operators
-
Format:
Data type variable name = expression ? Result 1 : Result 2;
-
Calculation method
-
technological process:
First, judge whether the expression is true or false
If it is true, assign the result 1 on the left to the variable
If it is false, assign the result 2 on the right to the variable
(choose one of the two)
int num=1>2 ? 10:20; System.out.println(num);//20; int a=10; int b=20; int max=a<b ? a:b; System.out.println(max);//20 int result = 3 > 4 ? 2.5 : 10; // Wrong writing!
matters needing attention:
- Both expression A and expression B must meet the requirements of the data type on the left.
- The result of the ternary operator must be used.
6.6 shift operator (understand)
-
< < shift left operator is used to move the binary bits of data to the left, supplemented by 0 on the right
-
>>Shift right operator is used to move the binary bits of data to the right, and the left is supplemented by sign bits
-
>>>Represents a logical shift right operator (unsigned shift right), which is used to move the binary bits of data to the right, and 0 is used on the left
Supplement.byte b1=13; //The binary of 13 is: 0000 1101 = > the result of shifting one bit left is 0001 1010 (26) System.out.println(b1<<1); //Moving one bit to the left is equivalent to multiplying by 2 //Shift right: 0000 1101 = > the result of shifting right one bit is 0000 0110 (6) System.out.println(b1>>1) //Moving one bit to the right is equivalent to dividing by 2 byte b2=(byte)(byte<<1);//To use strong rotation
6.7 bitwise operators (understand)
-
&Indicates a bitwise and operator, which performs and operation according to binary bits. The same 1 is 1, and 0 is 0
0000 1011 & 0000 1101 = 0000 1001
-
|Represents a bitwise OR operator, which performs or operations according to binary bits. 1 is 1, and the same 0 is 0
0000 1011 | 0000 1101 = 0000 1111
-
~Indicates the bitwise negation operator. Negation is carried out according to binary bits. 1 is 0 and 0 is 1
0000 1011 ~ = 1111 0100
-
^Indicates the bitwise XOR operator, which performs XOR operation according to binary bits. The same is 0, but the difference is 1
0000 1011 ^ 0000 1101 = 0000 0110
6.8 operator priority
[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-itl10xiL-1645851443974)(C:\Users\fortrun\Desktop\Notes \ priority of JavaSE \ operator in the first stage. png)]