java knowledge points
type of data
Data types are divided into basic types and reference types. The difference between basic data types and reference types is that basic data types are allocated on the stack, while reference types are allocated on the heap.
Stack concept
? . . . .
basic type
- byte
- short
- int
- long
- chart
- float
- double
- boolean
Reference type
Apart from the basic types, the rest are reference types, which are stored in the heap. (Stack difference)
String
Characters and strings
Characters are basic types, use ''
representations, strings are reference types, use ""
representations, and can store characters of any length.
Array
int[] arr = new int[5];// 未初始化
// int[] ns = new int[] { 68, 79, 91, 85, 62 }; // 创建时候直接初始化
// int[] ns = { 68, 79, 91, 85, 62 }; // 简写
System.out.println(arr);
int len = 0;
for(; len < arr.length; len++) {
System.out.println(arr[len]);
}
- Create an array using the new keyword, the timing of initialization is optional
- Not initialized, the system will assign default values
- Array access through index, error will be reported if the array length is exceeded
variable
Create variable
Type name variable name [= value], can also be named after initialization
String a;
a = "s";
Scope
Process control
Logical Operators
>
<
==
&
|
....
Flow control statement
Conditional statements
- if
- switch
loop statement
- while
- for
public class HelloWorld {
public static void main(String[] args) {
int i = 10;
// 循环
// while(i > 0) {
// System.out.println(i);
// i -= 1;
// }
// for(; i > 0; i--) {
// System.out.println(i);
// }
for(int b = 3; b > 0; b--) {
System.out.println(b);
}
// 条件
if (i > 10) {
System.out.println(i);
} else {
System.out.println("x");
}
switch (i) {
case 10:
System.out.println(i);
break;
default:
System.out.println("x");
break;
}
}
}
break和continue
class
Keyword class
Properties and methods
Modifier
- public
- private
- static
- protected
- final
- abstract
Construction method
Same as the class name, it will be called only when the object is instantiated, no return value, no default constructor will be specified, multiple constructors can be specified, use overloading to determine the call based on the parameters
Method overloading
In a class, we can define multiple methods. If there are a series of methods, their functions are similar, only the parameters are different, then you can make this group of method names into the same name method, and judge the execution according to the parameters
inherit
java only supports single inheritance
interface
Only public or abstract methods, or public static final fields
package
Maintain namespace