JavaSE
prelude
learning method
record
Write more code, take more notes and write more articles
Communication, thinking, skills
Share, ask (what's wrong), think (why)
insist
Blog = = effective memory
Summary and thinking
Writing organization ability
Learning and summarizing ability
Logical thinking ability
Help others and make friends
Rome wasn't built in a day
Blog platform - wordpress, typecho
Preparatory course
- io device
i – > input input
o – > output · output
- dos command
Environmental installation
Configure environment variables
1. Add JAVA_HOME – > value of variable = installation path (parent directory of bin)
2. Configure path – >% Java_ HOME%\bin
%JAVA_HOME%\jre\bin
cmd – > whether the Java - version test is successful
Hello World
public class Hello{ public static void main(String[] args) System.out.print("Hello World!"); }
Compile – > javac
Run – > java
The file name is consistent with the class name
Program operation mechanism
IDEA installation
IDE – > integrated development environment
introduction
All identifiers should be in case, $_ start
Naming rules
Package name: lowercase
Class name: hump. All words are capitalized
Variable name and method name: small hump lastNameAge
Constant name: upper case, middle plus_
data type
Basic data type
Integer type
int
byte
short
long–>l
Floating point type
float–>f
double
Character type
char
String – > is not a keyword, but a class
Boolean type
Reference data type
class
Interface
array
expand
//Integer extended binary 0b decimal octal 0 hex 0x int i=10; int i2=010;//8 int i3=0x10;//16 0~9 A~F //Floating point extension //Rounding error divisor of float finite discrete // Avoid comparing floating-point numbers BigDecimal math tool class //double float f=0.1f; double d=1.0/10; System.out.println(f); System.out.println(d); System.out.println(f==d); //0.1 //0.1 //false float d1=212114343134431f; float d2=d1+1; System.out.println(d1==d2); //true //Character class expansion char c1='a'; char c2='in'; System.out.println((int)c1); System.out.println((int)c2); //All characters are numbers in nature //Encoding Unicode 2 bytes 65536 2 16 //U0000 UFFFF char c3='\u0061'; System.out.println(c3); //Escape character // \n -- line break \ t -- tab String sa = new String("hello world"); String sb = new String("hello world"); System.out.println(sa==sb); String sc = "hello world"; String sd = "hello world"; System.out.println(sc==sd); //Boolean extension boolean flag = true; //if (flag==true) is the same as if (flag) //Less is More! The code should be concise and easy to read
Data type conversion
byte,short,char–>int–>long–>float–>double
Different data types are converted to the same type before operation
Force conversion
Variable from low to high (data type)
Automatic conversion
Automatic conversion
variable
Local variable – > in method
Instance variable – > outside the method and inside the class, it belongs to the object
public class Day02 { String name; int age; public static void main(String[] args) { Day02 day02 = new Day02(); System.out.println(day02.age); } } //If the numeric type is not initialized, the default value is 0.0 //The Boolean default is false //All but the basic types are null
Class variable stasic
constant
final constant name = numeric
operator
++a operation before assignment
a + + assignment before operation
Logical operator
And & &--- > are both true, and the result is true;
Or |--- > one of the two variables is true, and the result is true;
Not! --- > If it is true, it becomes false, and vice versa;
//Short circuit operation int a = 5; boolean d = (a<4)&&(a++<4); System.out.println(d); System.out.println(a);
Bit operation
//Bit operation /* A = 0011 1100 B = 0000 1101 If both are 1, it is 1, otherwise it is 0, a & B = 0000 1100 One 1 is 1 a|b = 0011 1101 0 for the same, 1 for the different, a ^ B = 0011 0001 Reverse ~ B = 1111 0010 */
Shift left < < = = * 2
Move right > > = = / 2
Base system
In order to understand what is binary, let's talk about decimal first.
We usually use hexadecimal, as the name suggests, full 10 into 1,
That is, when the low value is enough to 10, add 1 to the high value, and the standard becomes 0.
Binary is the same. When counting, full 2 will enter 1,
When the low position is full of 2, it is in the high position + 1, and the low position becomes 0.
String connector+
If a string appears on one side of the + side, other types will be converted to string type connection
Note: operation order
int a = 10; int b = 20; System.out.println(""+a+b); System.out.println(a+b+""); 1020 30
Conditional operator
? : ----> x ? y : z
If x is true, the result is y, otherwise z
Priority questions ()
import package
com.baidu.www
Process control
Scanner
if selection structure
- if
- if......else
- if......else if......else
Select one of them to execute, and then jump out of the program. However, the basic structure of Java is sequential structure
if nested structure
switch multiple selection structure
switch(expression) case value:...... break; case value:...... break; case value:...... break; default:......
Cyclic structure
-
while
[the transfer of external chain pictures fails. The source station may have an anti-theft chain mechanism. It is recommended to save the pictures and upload them directly (img-mq5tptfs-1613715044162) (note picture / while description. png)]
-
do...while
The do... While loop is similar to the while loop. The difference is that the do... While loop is cycled at least once (recirculation is performed first, while the reverse is true. Recirculation is judged first)
-
Enhanced for loop
int[] numbers = {12,55,53,34,88,22}; for (int n:numbers){ System.out.println(n); } //Assign the value of numbers to n
-
for
Example 1:
//The for loop outputs the number between 1-1000 that can be divided by 5, and 3 are displayed in each line for (int i = 1 ; i <= 1000; i++) { if (i % 5 == 0) { System.out.print(i+" "); } if (i%(5*3)==0) { System.out.println(); } }
Example 2:
//Print 99 multiplication table for (int i = 1; i <= 9; i++) { for (int j = 1; j <= i; j++) { System.out.print(i+"*"+j+"="+(i*j) + "\t"); } System.out.println(); } 1*1=1 2*1=2 2*2=4 3*1=3 3*2=6 3*3=9 4*1=4 4*2=8 4*3=12 4*4=16 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 //Own error demonstration for (int i = 1; i <= 1; i++) { int y = 1; int sum = y * i; System.out.println(i+"*"+y+"="+sum); } for (int i = 1; i <= 2; i++) { int e = 2; int sum = e * i; System.out.print(i+"*"+e+"="+sum+" "); } System.out.println(); for (int i = 1; i <= 3; i++) { int s = 3; int sum = s * i; System.out.print(i+"*"+s+"="+sum+" "); } System.out.println(); for (int i = 1; i <= 4; i++) { int si = 4; int sum = si * i; System.out.print(i+"*"+si+"="+sum+" "); } System.out.println(); for (int i = 1; i <= 5; i++) { int wu = 5; int sum = wu * i; System.out.print(i+"*"+wu+"="+sum+" "); } System.out.println(); for (int i = 1; i <= 6; i++) { int l = 6; int sum = l * i; System.out.print(i+"*"+l+"="+sum+" "); } System.out.println(); for (int i = 1; i <= 7; i++) { int q = 7; int sum = q * i; System.out.print(i+"*"+q+"="+sum+" "); } System.out.println(); for (int i = 1; i <= 8; i++) { int b = 8; int sum = b * i; System.out.print(i+"*"+b+"="+sum+" "); } System.out.println(); for (int i = 1; i <= 9; i++) { int j = 9; int sum = j * i; System.out.print(i+"*"+j+"="+sum+" "); } System.out.println();
-
break/continue
break force exit loop body
continue exits the current loop and executes the next loop
practice:
//Print triangles for (int i = 1; i <= 5; i++) { for (int j = 5; j >= i; j--) { System.out.print(" "); } for (int j = 1; j <= i; j++) { System.out.print("*"); } for (int j = 1; j < i; j++) { System.out.print("*"); } System.out.println(); }
method
retun can end the method
heavy load
Variable parameter
There can only be one, which must be put at the end
practice:
//Self written calculator public class Demo04 { public double add(double a,double b){ double sum = a + b; return sum; } public double sub(double a,double b){ double sum = a - b; return sum; } public double mul(double a,double b){ double sum = a * b; return sum; } public double div(double a,double b){ double sum = a / b; return sum; } public static void main(String[] args) { Demo04 mythod = new Demo04(); Scanner scanner = new Scanner(System.in); for (int i = 1;; i++){ System.out.println("Please enter the number to calculate"); double a = scanner.nextDouble(); double b = scanner.nextDouble(); System.out.println("Input calculation method"); String method = scanner.next(); switch (method) { case "+": System.out.println(mythod.add(a, b)); break; case "-": System.out.println(mythod.sub(a, b)); break; case "*": System.out.println(mythod.mul(a, b)); break; case "/": System.out.println(mythod.div(a, b)); break; } } } }
array
//Declaration array int[] num01; //Create array array = new int[10] //Declare and create an array int[] array = new int[length]; //assignment int[] num = {0,1,2,3,4,5,6,7,8,9}; num[1] = 1;
Array principle
ArrayIndexOutOfBoundsException array index out of bounds exception
The length is definite
The type must be the same in the array
The elements in the array can be of any type
Array variables belong to the reference type. Arrays can be regarded as objects. Each element in the array is equivalent to the member variable of the object
The array itself is an object, and the object in Java is in the heap. Therefore, whether the original type or other object types are saved, the array object itself is in the heap
Use of arrays
public class Demo03 { public static void main(String[] args) { int[] array = {1,2,3,4,5,6}; //printArry(array); int [] reverse = reverse(array); printArry(reverse); // for (int i : array) { // System.out.println(i); // } } //Print array elements public static void printArry(int[] arrays){ for (int i = 0; i < arrays.length; i++) { System.out.println(arrays[i]); } } ⭐⭐//Invert array as return value public static int[] reverse(int[] arrays){ int[] result = new int[arrays.length]; //Reverse operation for (int i = 0,j = result.length-1; i < arrays.length; i++,j--) { result[j] = arrays[i]; } return result; } }
Multidimensional array
//Loop nested output two-dimensional array int[][] array = {{1,2},{2,3},{3,4},{4,5}}; for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { System.out.print(array[i][j]+" "); } } //Output two-dimensional array with enhanced for loop int[][] array1 = new int[11][11]; array1[1][2] = 1; array1[2][3] = 2; System.out.println("Output original checkerboard array"); for (int[] ints : array1) { for (int anInt : ints) { System.out.print(anInt+"\t"); } System.out.println(); }
Array class
Bubble sorting
One of the eight sorts
//Bubble sorting public static int[] sort(int[] array) { int temp = 0; //The outer loop determines how many times the program runs for (int i = 0; i < array.length-1; i++) { //Inner loop, compare the size of two numbers for (int j = 0; j < array.length-1; j++) { if (array[j+1]<array[j]){ temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } return array; }
Sparse array
public class Demo08 { public static void main(String[] args) { int[][] array1 = new int[11][11]; array1[1][2] = 1; array1[2][3] = 2; System.out.println("Output original checkerboard array"); for (int[] ints : array1) { for (int anInt : ints) { System.out.print(anInt+"\t"); } System.out.println(); } System.out.println("================================="); //Convert to sparse array save //Get the number of valid values int sum = 0; for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { if (array1[i][j]!=0){ sum++; } } } System.out.println("altogether"+sum+"Valid values"); //Create a sparse array int[][] array2 = new int[sum+1][3]; array2[0][0] = 11; array2[0][1] = 11; array2[0][2] = sum; //Traverse the two-dimensional array and assign the value to the sparse array int count = 0; for (int i = 0; i < array1.length; i++) { for (int j = 0; j < array1[i].length; j++) { if (array1[i][j]!=0){ count++; array2[count][0] = i; array2[count][1] = j; array2[count][2] = array1[i][j]; } } } //Output sparse array System.out.println("Output sparse array"); for (int i = 0; i < array2.length; i++) { System.out.println(array2[i][0]+"\t" +array2[i][1]+"\t" +array2[i][2]+"\t"); } //Restore sparse array //Read sparse array int[][] array3 =new int[array2[0][0]][array2[0][1]]; //Restore value for (int i = 1; i < array2.length; i++) { array3[array2[i][0]][array2[i][1]] = array2[i][2]; } System.out.println("=================================="); for (int[] ints : array3) { for (int anInt : ints) { System.out.print(anInt+"\t"); } System.out.println(); } } }
Memory
-
Heap:
new objects and arrays
It can be shared by all threads and will not store other object references
-
Stack
Store basic variable types (including specific values of basic types)
The variable of the reference object (the specific address where the reference is stored in the heap)
-
Method area
Can be shared by all threads
Contains all class and static variables
Object oriented programming OOP
Essence: organize code in the form of classes and encapsulate data in the form of objects
Abstract = common ground
Note: there are only properties and methods in the class
※ characteristics:
1. Encapsulate the internal data operation details of the private "high cohesion and low coupling" ---- > class, and only expose a small number of methods for external use
Property private get/set
2. The essence of inheriting extensions is to abstract a certain batch of buildings, so as to realize better modeling of the world (abstraction again)
Inheritance is a relationship between classes (in addition to dependency, composition, aggregation, etc.). In Java, there is only single inheritance without multiple inheritance (a subclass can only have one parent class)
Under public, after the subclass inherits the parent class, there will be all the methods of the parent class (private can also be used, and the public method get/set needs to be set)
CTRL + H to view the inheritance tree
Object class ----- > all classes in Java inherit object class from each other or indirectly
super
Method rewrite
3. Polymorphism
From the perspective of code operation, there are classes first and then objects. Classes are the template of objects
Objects, concrete things. Class, abstract, an abstraction of an object.
constructor
-
Even if a class doesn't write anything, it will have a method = = constructor
-
Must be the same as the name of the class
-
There must be no return value and void cannot be written
-
Used to initialize values