Method overload (@ overload)
Premise:
- Overloaded methods exist in the same class [premise]
- Overloaded methods must have the same method name
- The parameter list of overloaded methods must be different
- The return types of overloaded methods can be different
public class LoadMethodDemo { public static void main(String[] args) { add(20); } public static int add(){ System.out.println("add -> 100"); return 100; } public static void add(int i){ System.out.println(i); } }
The advantage is to simplify the api - easy to call
Learn to use APIs
By learning java.lang.Math - math tool class - provides a lot of ways about mathematical calculation
package tech.aistar.day03; /** * This class is used to demonstrate: java.lang.Math * * @author: success * @date: 2021/7/19 8:59 morning */ public class MathDemo { public static void main(String[] args) { //absolute value int n = Math.abs(-10); System.out.println(n); //Seeking random number pseudo-random //static double random();// Random decimals between [0.0,1.0] System.out.println(Math.random()); //Random integer between [1100] int result = (int) (Math.random()*100+1);//[1,100] System.out.println(result); //Random integers between [3,5] int m = (int) (Math.random()*3+3); System.out.println(m); //Find the maximum / minimum of the two numbers System.out.println(Math.max(10,20));//20 //Fill in the blanks in the written examination- //Returns the maximum (closest to positive infinity) double value less than or equal to the parameter, which is equal to a mathematical integer. //floor method System.out.println(Math.floor(3.5));//3.0 System.out.println(Math.floor(4.0));//4.0 System.out.println(Math.floor(-3.5));//-4.0 //Returns the nearest long in the parameter, where long is rounded to positive infinity. //Math.floor(a + 1/2) System.out.println(Math.round(-3.5)); //Root opening System.out.println(Math.sqrt(9));//3.0 //Power System.out.println((int)Math.pow(2,3));//8 } }
sentence
ternary operator
ternary operator
-
Conditional expression? Result1: result2
If the expression is true, the result is result1, otherwise the result is result2
-
Support unlimited nesting - but not recommended, poor readability
m? (expression? result1:result2):result2
if...else...
-
if statements are used separately
if(condition){//establish //code... }
-
{} scenes that can be omitted
if(condition) //code... If if{}There is only one line of code in,that{}Can be omitted,But be sure to indent
-
if... else... Use
if(condition){ //Conditions established }else{ //The condition is not tenable } //If the statement still has only one line, {} can be omitted if(condition) //code else //code..
-
Multi conditional branch if... else if... else... If... else
Note that conditions are mutually exclusive
-
if(condition){ //Conditions established }else if(condition){ //The condition is not tenable }else if(condition){ //code }else{ //code.. }
-
if... else statements can be nested indefinitely
if(condition){ //Conditions established if(condition){ }else{ } }else if(condition){ //The condition is not tenable if(condition){ //Conditions established }else if(condition){ //The condition is not tenable }else if(condition){ //code }else{ //code.. } }else if(condition){ //code }else{ //code.. }
exercises
-
Buy milk tea at half price for the second cup. The unit price is 10 yuan. Ask for the total price
10 5 10 5 10
- Find the maximum number of days in a month of a year
switch...case
Not as flexible as if... else
Syntax:
switch(variable)/ /byte short int char String enum[Enumeration type] //Packing type Byte Short Integer Character case Value 1: //code [break]; case Value 2: //code [break]; case Value 3: //code [break]; [default: //code.. [break];] }
-
When the variable in the switch bracket can match the value after a case, it will enter the corresponding case to execute the program inside
-
When entering a case block for execution, the switch block will jump out only when the break statement is encountered. If the break statement is not encountered, the program will
Automatically enter the next case block for execution
-
The default statement can be omitted or placed casually. It is recommended to put it at the end. If the variable in switch brackets does not match any value after case
When, it will enter default
Circular statement
while
- It belongs to post loop - judge whether the loop condition is true first. If it is true, enter the loop body, otherwise the loop will not be executed
- Usage scenario: when you don't know how many times to loop, but know the conditions for loop exit - the while loop is preferred
Syntax:
While (condition){
//Loop body
}
package tech.aistar.day03; /** * This class is used to demonstrate: while loop * * @author: success * @date: 2021/7/19 10:53 morning */ public class WhileDemo { public static void main(String[] args) { //while can replace for //'a'-'z' char c = 'a'; while(c <= 'z'){ System.out.println(c); c++; } System.out.println("====="); //'A' - 'Z' int n = 65; while(n<=90){//Conditions for exit System.out.println((char)n); n++; } //1-10 int m = 1; //Dead cycle while(true){ //There must be a statement to break the loop inside the loop - break System.out.println(m); if(m==10) // Conditions for loop exit break; m++; } } }
practice
-
Find the greatest common divisor of two numbers
thinking:20 12 -> 4 20 % 12 = 8 12 % 8 = 4 8 % 4 = 0
-
Decimal to binary
for loop
It belongs to post cycle
-
Single layer for
for(①Expression 1;②Expression 2;③Expression 3){ //④ Circulatory body } ①Expression 1 - Initialization of variables during a loop.such as int i = 0; - It will only be executed once. ②Expression 2 - Conditions for loop exit.such as i<=10 - At least once ③Expression 3 - During the cycle,Variation of variable factors.i++ - It may not be implemented,Possible implementation Execution sequence①-②[establish]-④-③-②[establish]-④-③.....
Weird writing
for(;;){ //Dead loop - break a statement that breaks a loop } The three expressions can be omitted arbitrarily,Or somewhere else
Compound form of for loop
for(Define several variables at the same time;condition;expression){ //Loop body }
-
Nested for loops, the number of nested layers is not recommended to exceed three. The more nested, the lower the performance
2-1. The inner and outer layers are independent - the selected use of the inner for can be executed separately. The variable factors of the outer for loop are not used
2-2. Inner and outer correlation - the inner for uses the variable factors of the outer for loop
-
Exercise - print all three digits. Three digits consist of numbers 1,2,3,4. But there can be no duplicates
Change one line for every 4 printed
package tech.aistar.day03; /** * This class is used to demonstrate: * Exercise - print all three digits. Three digits consist of numbers 1,2,3,4. But there can be no duplicates * * Change one line for every 4 printed * * @author: success * @date: 2021/7/19 2:26 afternoon */ public class ForExerciseDemo { public static void main(String[] args) { //Define a counter int count = 0; for (int x = 1; x <=4 ; x++) { for (int y = 1; y <=4; y++) { for (int z = 1; z <=4; z++) { // System.out.print(x+""+y+z); if(x!=y & x!=z & y!=z){ System.out.print(x*100+y*10+z+"\t"); count++; // if(count % 4==0){ // System.out.println(); // } if(count == 4){ System.out.println(); count = 0; } } } } } } }
do...while...
It belongs to pre - loop - whether the loop condition is established or not, it is preferred to enter the loop body for execution once
Then judge the conditions and decide whether to enter again next time
do{
//Loop body
}While (condition);
break statement
- break can jump out of the swtich block
- Break can break the cycle of its layer
continue Statement
Appears in the loop body
Skip this cycle and continue to the next cycle
array
- The birth of array - in order to solve the disadvantage that a single variable can only store a single value
- Array itself also belongs to data structure [array, stack, heap, tree [red black tree], linked list, graph...]
- Array is an object type. Array is a heap area in JVM memory, and basic types are stored in the stack area of JVM memory
- An array must be a contiguous space in memory
- Once the size [length, number of data stored in the array] of the array is determined, it cannot be changed
grammar
1. Element type[] Variable name = new Element type[size];//Size the size of the array 2. Element type variable name[] = new Element type[size];// An element type is a data type[8 Basic data types,And object type] effect - The element type determines the type of data that can be stored in this array //What is the data type of arr? Int [] - > integer array // "Define an int array of length 3" int[] arr = new int[3];
Assignment method
If an array is defined but no assignment is performed, the system will assign a default value by default
The default value depends on the element type
byte,short,int,long -> 0
float,double -> 0.0
boolean -> false
Char - > space
Object type - > null
-
First define the array, and then assign values one by one through subscripts
int[] arr = new int[3]; //The index of the array ranges from [0, array length - 1] //If the subscript is not in this range, the console throws a java.lang.ArrayIndexOutOfBoundsException array subscript out of bounds exception arr[0] = 10; arr[1] = 10; arr[2] = 10;
- Assign values while defining the array
int[] arr = {3,4,5};
- Assign values while defining the array
int[] arr = new int[]{3,4,5};
- Assignment by ordinary for
int[] arr = new int[3]; for(int i=0;i<arr.length;i++){ arr[i] = (int)(Math.random()*100+1); }
Traversal of array
-
Value one by one through subscript
System.out.println(arr[subscript]);
-
Array provides the attribute length - gets the length of the array
Traversal through a normal for loop
for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); }
-
Use the syntax of the enhanced for loop provided by jdk5.x
Is a read-only for
int[] arr2 = {3,4,5}; for(Element type element variable:Array variable name){ System.out.println(element); } for(int e:arr2){//e here is either a subscript or a real element System.out.println(e); }
Expand - array memory
//Open up a new space 01 in the Jvm heap through the new keyword to store this object {10,20,30} //Then the memory address of the object in heap space is assigned to the reference variable arr1 [stored on the stack] //"Conclusion" - the essence of arr1 is not an object, but a reference variable //"Conclusion" - the relationship between references and objects // The memory address of the object in the heap space is stored in the reference variable // "Balloon theory" - a reference variable can only point to one object at the same time // -"An object can be pointed to by multiple references at the same time" int[] arr1 = new int[]{10,20,30}; //Open up a new space 02 in the Jvm heap through the new keyword to store this object {10,20,30} int[] arr2 = {10,20,30}; arr1 = arr2;//Both arr1 and arr2 point to the array object created for the second time //The object created for the first time becomes a garbage object in memory - GC [garbage collection thread collects and releases memory] //"Garbage object" - an object that does not have any references to it
Parameter transfer mode of extension method
- Basic types are passed by value
- Object type [reference type] is passed by address
Summary: in Java, there is only value passing and no address passing. The special type is String [although it is an object type, it has the characteristics of basic type passing]