day03
4. Escape character
Escape character | describe |
---|---|
\n | Newline character |
\t | Indent (TAB) |
\\ | Backslash |
\' | Single quotation mark |
\'' | Double quotation mark |
public class Demo1{ public static void main(String[] args){ /* Compilation failed. No escape characters were used */ //char a = '''; //System.out.print(a); // \n Newline character System.out.print("Today is/n"); System.out.print("Knock the code well/n"); System.out.print("days/n"); // \t Indent (TAB) System.out.println("2108 Learn today\t Escape character"); System.out.println("aaee\tcd"); System.out.println("aaee cd"); System.out.println("1.View data\t2.New data\t3.Modify data\t4.Delete data"); System.out.println("=================="); // \Backslash System.out.println("2021\\8\\25"); // \'single quote char a = '\''; System.out.println(a); // \Double quotes System.out.println("\""); //json } }
Reference data type (string)
type | Value range | Character encoding |
---|---|---|
String | Literal value between any '' | Unicode character sequence |
Literal value of String type:
String str1 = "Hello";
String str2 = "hello";
public class Demo2{ public static void main(String[] args){ //1. Declare and assign variable strings //byte short int long float double boolean char //Data of reference type title case String name = "Zhang San"; System.out.print("His name is:"); System.out.println(name); // -32768~32767 char s = 65; // 0~65535 //char a = s ; boolean flag = true; //false String word = ""; System.out.println("========="); System.out.println(word); System.out.println("========="); } }
Type conversion
Automatic type conversion:
- The two types are compatible
- The destination type is greater than the source type
Cast type:
- The two types are compatible
- The destination type is less than the source type
Cast rule:
- The integer length is sufficient and the data is complete
- Insufficient integer length, data truncation
- Decimal forced to integer, data truncation (missing precision)
- Character integer conversion, data integrity
- The value of boolean is true/false and cannot be converted with other types
public class Demo3{ public static void main(String[] args){ /* Automatic type conversion */ //byte short //Source type byte a = 1; //Target type short b = a; System.out.println(b); byte a2 = 100; int i = a2; System.out.println(i); System.out.println("========="); int num = 10; double d = num; System.out.println(d); /* Cast type */ //The destination type is less than the source type int i2 = 10; byte a3 = (byte)i2; System.out.println(a3); double d2 = 123.333; int i3 = (int)d2; System.out.println(i3); //abnormal double num1 = 123.333; int num2 = (byte)num1; System.out.println(num2); System.out.println("=========="); //Characters 0 ~ 65535 char a1 = 'A'; //Integer ASCII - 2147483648 ~ 2147483647 int num3 = a1; System.out.println(num3); int num4 = 97; char b1 = (char)num4; System.out.println(b1); System.out.println("=========="); //byte -128~127 short s = 129; byte b2 = (byte)s; System.out.println(b2); System.out.println("==========="); //0~65535 int i1 = -1; char c = (char)i1; System.out.println(c); } }
operator
Arithmetic operator:
Two operands are calculated
Operator | describe |
---|---|
+ | Addition and summation |
- | Subtraction and subtraction |
* | Multiplication and quadrature |
/ | Division and quotient |
% | Module and remainder |
/** operator */ public class Demo4{ public static void main(String[] args){ //Arithmetic operator double num1 = 10; double num2 = 20; System.out.println(num1+num2); System.out.println(num1-num2); System.out.println(num1*num2); // 0.5 -> int 0 System.out.println(num1/num2); System.out.println("============"); int num3 = 1234; System.out.println(num3/100.0); System.out.println(num3%100); System.out.println("============"); System.out.println(1235%2); double num4 = num1+num2; System.out.println(num4); } }
Arithmetic operator:
Unary operator (only one operand)
Operator | describe |
---|---|
++ | Increment, variable value + 1 |
– | Decrement, variable value - 1 |
public class Demo5{ public static void main(String[] args){ int num = 1; // num++ ---> num = num + 1 num++; System.out.println(num); //num-- ---> num = num - 1 num--; System.out.println(num); System.out.println("================"); int num2 = 1; //num2 + + followed by + + use / apply first for + 1 operation System.out.println(num2++); System.out.println(num2); num2 = 1; // Num2 + + Split num2 ++ //Operate println(num2+2) first //num2=num2+1 System.out.println(num2+++1); System.out.println("================"); // int num3 = 1; //First + 1, in use System.out.println(++num3); System.out.println(num3); num3 = 1; //num3 = num3+1 //num3 + and output System.out.println(++num3+1); System.out.println("================"); int num4 = 1; System.out.println(++num4+1+num4+++1); System.out.println(num4); System.out.println("============================"); //1234 int num5 = 1234; //Bit //Ten //Hundredth //Thousand bit // Num5 / 10 1234 / 10 = 123.4 because it is an integer of type int, 1234 / 10 = 123 decimal places are killed int ge = num5%10; int shi = num5/10%10; int bai = num5/100%10; int qian= num5/1000%10; System.out.println(qian); System.out.println(bai); System.out.println(shi); System.out.println(ge); int i = 1; System.out.println((i++)+1+(++i)+i+++i-(--i)+i+(i--)); System.out.println(i); } }
Assignment operator:
The right side of the equal sign is assigned to the left side of the equal sign
Operator | describe |
---|---|
= | Direct assignment |
+= | Assignment after summation |
-= | Assignment after difference |
*= | Assignment after quadrature |
/= | Assignment after quotient |
%= | Assignment after remainder |
public class Demo6{ public static void main(String[] args){ //Assignment Operators int num1 = 10; //num1 +=5 --> num1=num1+5; num1+=5; System.out.println(num1); //15 //num1-=10 --> num1=num1-10; num1-=10; System.out.println(num1); } }
Relational operator
Relational operators: comparing two operands
Operator | describe |
---|---|
> | greater than |
< | less than |
>= | Greater than or equal to |
<= | Less than or equal to |
== | be equal to |
!= | Not equal to |
public class Demo7{ public static void main(String[] args){ //Relational operators 1, > 2, < 3, > = 4, < = 5= 6,== int num1 = 10; int num2 = 5; System.out.println(num1>num2);//true System.out.println(num1<num2);//false System.out.println(5>5);// false System.out.println(5>=5);//true System.out.println(3<6); //true System.out.println(3<=3);//true System.out.println("================="); //==Mathematically equal System.out.println(3==3); // != Not equal to System.out.println(3!=3); System.out.println("================="); int num3 = 5; double num4 = 10.0; System.out.println(num3<num4); int num5 = 97; char c = 'a'; System.out.println(num5 == c); char c2 = 'male'; char c3 = 'male'; System.out.println(c2 == c3); System.out.println("================="); String sex = "male"; String sex2 = "male"; String sex3 = new String("male"); // ==In the basic data type, it is the comparison value, but in the reference type, it is the memory address System.out.println(sex3 == sex); //String comparison method equals //character string. Equals (string to compare) // Compare the values of two strings System.out.println(sex3.equals(sex)); //equals } }
Logical operators:
Logical comparison between operands or expressions of two boolean types
Operator | semantics | describe |
---|---|---|
&& | And (and) | Both operands are true at the same time, and the result is true |
|| | Or (or) | Two operands, one of which is true and the result is true |
! | Non (reverse) | True is false, false is true |
/** &&,|| And, or operator and &,| And, or operation */ public class Demo9{ public static void main(String[] args){ boolean f1 = 10>3;//true boolean f2 = 3<=3;//true //And&& /* &&also Only when the left and right sides are true, the result is true, and all other cases are false && As long as the result on the left of & & is false, judgment will not be executed on the right of &; */ System.out.println(f1 && f2); System.out.println(10>3 && 3<=3);//true System.out.println("=========&&=========="); System.out.println(10>3 && 3<=3);//true System.out.println(10>11 && 3<=3);//true System.out.println(10>3 && 4<=3);//false System.out.println(10>11 && 3<=3);//false // ||Or /* Or on the left and right sides, as long as one side is true, that is, true, and both sides are false, it is fasle As long as the left side of 𞓜 is true, the right side will not be executed */ System.out.println("=========||=========="); System.out.println(10>3 || 3<=3);//true System.out.println(10>11 || 3<=3);//true System.out.println(10>3 || 4<=3);//true System.out.println(10>11 || 4<=3);//false // &And operator /* Two sides are Boolean values or Boolean expressions Whether the left is false or not, the right is the execution judgment */ System.out.println(3>2 & 4< 10); //Perform & operation System.out.println(3&15); /* 3 Binary is 00000011 15 Binary is 00001111 &After the operation is 00000011 The result is 3 (and operation) */ //Perform | operation System.out.println(3|15); /* 3 Binary is 00000011 15 Binary is 00001111 |After the operation is 00001111 The result is 15 (| or operation) */ } }
/** !Inverse operator */ public class Demo11{ public static void main(String[] args){ /* !Yes, he will reverse the result Note 1: it can only operate on the results of boolean or boolean expressions, Otherwise, an error is reported Note 2: if the result of Boolean expression is negated, use () to obtain the result first and then negate it */ boolean flag = true; System.out.println(!flag); System.out.println(!(3>2) && 4< 10); } }
Logical operators are used in conjunction with relational operators
public class Demo10{ public static void main(String[] args){ int num = 1; System.out.println(num++ > 1 && ++num < 10); System.out.println(num); System.out.println("================="); int num2 = 1; System.out.println(num2++ > 0 || ++num2 < 1); System.out.println(num2); } }
ternary operator
Operator | semantics | describe |
---|---|---|
?: | Boolean expression? Result 1: result 2 | When the expression result is true, the result is 1. When the expression result is false, the result is 2 |
public class Demo12{ public static void main(String[] args){ int num = 120; System.out.println(num%2==0 ? "even numbers":"Odd number" ); } }
expression
Use operators to link variables or literals, and you can get a final result
Automatic type promotion
During arithmetic operation:
- The two operands are double, and the calculation result is promoted to double
- If there is no double in the operand, one is float, and the calculation result is promoted to float
- If there is no float in the operand, one is long, and the calculation result is promoted to long
- If there is no long in the operand, one is int, and the calculation result is promoted to int
- If there is no int in the operand, one is short or byte, and the calculation result is promoted to int
Special: when any type is added (+) to String, it is actually patchwork, and the result is automatically promoted to String
Console input
Import package syntax: import package name Class name
Order of use:
-
Import Java util. Scanner
-
Declare variables of type Scanner
Scanner input = new Scanner(System.in)
-
Use the corresponding method in the Scanner class (type sensitive):
. nextInt(); // Get integer
. nextDouble(); // Get decimal
. next(); // Get string
. next().charAt(0); // Get a single string
. next()Xxx(); Receive various types (except char)
eg: int i = input.nextInt();
double o = input.nextDouble();
String stu1 = input.nextInt();
String stu2 = input...next().charAt(0);;
The difference between next() and nextLine():
next() does not accept spaces and carriage returns, and takes spaces and carriage returns as termination symbols
nextLine() receives spaces and carriage returns
Note: if mismatched data is entered, Java. Net will be generated util. Inputmismatch exception input mismatch exception
summary
-
Variable:
A piece of storage space in computer memory, which is the basic unit for storing data
-
Data type:
Basic data type (8), reference data type (String, value, object)
-
Operator:
Arithmetic operator, assignment operator, relational operator, logical operator
-
Type conversion:
Automatic type conversion, forced type conversion
-
Type lift:
General type promotion of numbers and special type promotion of strings
-
Console input:
Import the toolkit, declare the Scanner, call the corresponding method, and receive the data entered on the console
Arithmetic operators, relational operators, logical operators, assignment operators, and () parentheses, their precedence
Arithmetic operators (+, -, *, /,%, + +, –)
Assignment operators (=, + =, - =, * =, / =,% =)
Relational operators (= =,! =, <, >, < =, > =)
Logical operators (&, & &, |, |,!)
() > arithmetic operators > relational operators > logical operators > assignment operators
() parentheses have the highest priority
The second is the arithmetic operator
The second is relational operators
Followed by logical operators
The second is the assignment operator
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-C19vGSnJ-1629934722498)(E: \ note \ yourself \ phase I \ picture \ priority. png)]
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-Bfcx9iOx-1629934722499)(E: \ note \ yourself \ phase I \ picture \ priority II. png)]
String equals string comparison
public class Demo8{ public static void main(String[] args){ //Strings are compared using equals //userName.equals(inputName) //passWord.equals(inputPwd) String userName = "admin"; String passWord = "123456"; String inputName = "Admin"; String inputPwd = "123456"; System.out.println(userName.equals(inputName)); System.out.println(passWord.equals(inputPwd)); if(userName.equals(inputName)){ System.out.println("The user name is entered correctly"); }else{ System.out.println("User name input error"); } if(passWord.equals(inputPwd)){ System.out.println("The password is entered correctly"); }else{ System.out.println("Password error"); } } }
Job:
public class Demo13{ public static void main(String[] args){ /* Judge whether the user is a lucky customer Enter the customer's four digit membership card number and calculate the sum of the numbers. The odd number is the lucky customer and the even number is not. Then output whether the user is the lucky customer with true / flash */ int card = 2108; //Calculate and obtain each digit int ge = card % 10; int shi = card/10%10; int bai = card /100%10; int qian = card /1000%10; //Calculation and int sum = ge+shi+bai+qian; String flag = (sum%2==0 ? "Not a lucky customer":"It's a lucky customer"); System.out.println(flag); /* A simple calculator + - * / */ int num1 = 10; int num2 = 20; System.out.println(num1 + num2); /* 3,Calculate the scores of 5 students, output their names and scores, and finally output whether the average score is even Zhang San 50, Li Si 60, Wang Wu 78, Zhao Liu 88, Chen Qi 90*/ String zs = "Zhang San"; int zcScore = 50; String ls = "Li Si"; int lsScore = 60; String ww = "Wang Wu"; int wwScore = 78; String zl = "Zhao Liu"; int zlScore = 88; String cq = "Chen Qi"; int cqScore = 90; System.out.println(zs+"What's your score:"+zcScore); System.out.println(ls+"What's your score:"+lsScore); System.out.println(ww+"What's your score:"+wwScore); System.out.println(zl+"What's your score:"+zlScore); System.out.println(cq+"What's your score:"+cqScore); //Calculate average score int sum1 = zcScore+lsScore+wwScore+zlScore+cqScore; int avg = sum1/5; //Use ternary operators to judge the results String flag1 = avg%2==0 ? "even numbers":"Odd number"; System.out.println(flag1); } //Arithmetic operators, relational operators, logical operators, assignment operators, and () parentheses, their precedence // () > Arithmetic operator > Relational operator > Logical operator > Assignment Operators }