I variable
- What is the variable: it is the variable quantity! Manipulate data in memory through variables
- Java is a strongly typed language. Every variable must declare its type
- Java variable is the most basic storage unit in a program. Its elements include variable type, variable name and scope
Variable scope
- Class variable
For variables written in the class, the keyword static should be added - Instance variable
It is written in the middle of the class, which is similar to the class variable. There is no static keyword - local variable
Written in the method, you must declare and initialize the value
be careful
- Each variable has a type, which can be either a basic type or a reference type
- Variable names must be legal identifiers
- Variable declaration is a complete statement, so each declaration must end with a semicolon
public class variable { //Class variable static static double salary = 2500; //Instance variable: subordinate object; If you do not initialize yourself, the default value of this type is 0.0 //Boolean: false by default //The default value is null except for the basic type String name; int age; //main method public static void main(String[] args) { //Local variables: values must be declared and initialized int i = 10; System.out.println(i); //Variable type variable name = assignment variable demoname = new variable(); System.out.println(demoname); System.out.println(demoname.name); System.out.println(demoname.age); //Class variable static System.out.println(salary); } public void add() { } }
II constant
- Constant: the value cannot be changed after initialization! A value that does not change
- The so-called constant can be understood as a special variable. After its value is set, it is not allowed to be changed during the operation of the program
- Keyword final
- Constant names generally use uppercase characters
public class constant { //Modifier, there is no order static final double PI = 3.14; public static void main(String[] args) { System.out.println(PI);//3.14 } }
Naming conventions for variables
- All variables, methods and class names: see the meaning of the name
- Class member variables: lowercase initial and hump principle: monthSalary except for the lowercase initial of the first word, the following words are capitalized
- Local variables: initial lowercase and hump principle Consistent with class member variables
- Constants: uppercase letters and underscores MAX_VALUE
- Class name: initial capitalization and hump principle Man,GoodMan
- Method name: initial lowercase and hump principle run(),runRun();
III operator
- Arithmetic operators: +, -, *, /,% (remainder, modulo operation), + +, –
- Assignment operator:=
- Relational operators: >, <, > =, < =, = =,! =, instanceof?
- Logical operators: & &, ||! (and or not)
- Conditional operator:?:
- Extended assignment operators: + =, - =, * =/=
- Bitwise operators: &, |, ^, ~, > >, <, > > (just understand!!!)
Focus on the first four
Arithmetic operator
Type conversion
package operator; public class Operator { public static void main(String[] args) { //Binary operator int a = 10; int b = 20; System.out.println(a + b); System.out.println(a - b); System.out.println(a * b); System.out.println(a / (double) b);//0.5 long l = 1358641315L; int i = 654; short s = 54; byte by = 4; System.out.println(l + i + s + by);//Long System.out.println(i + s + by);//Int System.out.println(s + by);//Int } }
Relational operator
package operator; public class Demo Relational operator { public static void main(String[] args) { //Results returned by relational operators: correct, wrong, Boolean //if int a = 10; int b = 20; //Modulo operation int c = 21; System.out.println(c % a);//1 System.out.println(a > b);//false System.out.println(a < b);//true System.out.println(a == b);//false System.out.println(a != b);//true } }
Self increasing and self decreasing operation power operation
Power Math tool class
package operator; public class Demo Self increasing and self decreasing { public static void main(String[] args) { //++-- self increasing and self decreasing unary operators int a = 3; int b = a++;//After executing this line of code, assign a value to b first, and then increase it by itself //a=a+1 System.out.println(a); //a=a+1 int c = ++a;//After this line of code is executed, it will be self incremented first, and then assign a value to b System.out.println(a); System.out.println(b); System.out.println(c); //Power operation 2 ^ 3 = 2 * 2 * 2 = 8 //We can use many operation classes! double pow = Math.pow(2, 3); double pow1 = Math.pow(3, 3); System.out.println(pow); System.out.println(pow1); } }
Logical operator
Or and non
package operator; //Logical operator public class Demo { public static void main(String[] args) { //And (and) or (or) not (negative) boolean a = true; boolean b = false; //Logic and operation: if both variables are true, the result will be true System.out.println("a && b=" + (a && b)); //Logical or operation: if one of the two variables is true, the result is true System.out.println("a || b=" + (a || b)); //If true, it becomes false; if false, it becomes true System.out.println("!(a && b)=" + (!(a && b))); //Short circuit operation int c = 5; boolean d = (c < 4) && (c++ < 4); System.out.println(d);//false System.out.println(c);//5 boolean e = (c++ < 4) && (c < 4); System.out.println(e);//false System.out.println(c);//6 } }
Bit operation interview questions
Move left and right (learn the principle of computer combination)
package operator; //Bit operation public class Demo Bit operation { public static void main(String[] args) { /* A = 0011 1100 B = 0000 1101 A&B = 0000 1100 A|B = 0011 1101 A^B = 0011 0001 ~B = 1111 0010 Interview questions: 2 * 8 = 16 2*2*2*2 Bit operation efficiency is very high. It directly deals with the bottom layer and binary, which is applied in many bottom algorithms << Shift left * 2 >> Shift right / 2 */ System.out.println(2 << 3);//16 /* analysis 0000 0000 0 0000 0001 1 0000 0010 2 0000 0011 3 0000 0100 4 0000 1000 8 0001 0000 16 (It can be expanded in depth to understand the principle of computer composition) */ } }
Extended assignment operator surface test questions
Interview question: string connection
The difference between a string at the top and not at the top
package operator; //Extended assignment operator public class Demo Extended assignment operation { public static void main(String[] args) { int a = 10; int b = 20; a += b;//a = a+b a -= b;//a = a-b System.out.println(a);//10 //Interview questions System.out.println(a + b);//30 //String connector +, string System.out.println("" + a + b);//1020 System.out.println(a+b+"");//30 } }
Ternary operator
package operator; //Ternary operator public class Demo Ternary operator { public static void main(String[] args) { //x ? y : z //If x==true, the result is y; otherwise, the result is Z score int score = 80; int score2 = 50; String type = score < 60 ? "fail," : "pass"; String type2 = score2 < 60 ? "fail," : "pass"; System.out.println(type);//pass System.out.println(type2);//fail, } }
Operation priority
Parentheses ()