Since the beginning of school, I have been slack in learning Java, so I decided to take advantage of the National Day holidays before and after Java from basic to generic, set up once (learn while practicing, have details).
Note: Some chapters may basically be excerpts from Liao Xuefeng's tutorials Home page - LiaoxueFeng.com, the official website of Liao Xuefeng Fragments, then review with yourself in the future, are reproduced, standing on huge shoulders, will also complement your understanding!O(8745;) O~
The method specified by the Java entry program must be static, the method name must be main, and the parameters in parentheses must be String arrays
//The command line parameter type is a String[] array;
//Command line parameters are received by the JVM by the user and passed to the main method;
//How to parse command line parameters needs to be implemented by the program itself
Examples of defining floating point numbers:
float f1 = 3.14f; float f2 = 3.14e38f; // 3.14x10^38 in scientific notation double d = 1.79e308; double d2 = -1.79e308; double d3 = 4.9e-324; // 4.9x10^-324 in scientific notation
For float types, the f suffix is required.
Floating-point numbers can represent a very large range, with float types representing a maximum of 3.4x1038 and double s representing a maximum of 1.79x10308.
The Java language does not specify the storage of boolean types, since theoretically only 1 bit is required to store boolean types, but usually the JVM internally represents the boolean as a 4-byte integer.
var keyword
Sometimes the name of a type is too long to write. For example:
StringBuilder sb = new StringBuilder();
At this point, if you want to omit the variable type, you can use the var keyword:
var sb = new StringBuilder();
The compiler automatically infers from the assignment statement that the type of variable sb is StringBuilder. For the compiler, the statement:
var sb = new StringBuilder();
In fact, it automatically becomes:
StringBuilder sb = new StringBuilder();
Therefore, defining variables with var is only a matter of minimizing the type of variable.
Priority of relational operators
From high to low:
- !
- >,>=,<,<=
- ==,!=
- &&
- ||
If an expression of a Boolean operation can determine the result ahead of time, subsequent calculations are no longer performed and return the result directly.
Because the result of false && x is always false, whether x is true or false, and operation, after determining the first value as false, does not proceed with the calculation, but returns false directly.
Similarly, for a || operation, as long as the first value is determined to be true, subsequent calculations will not proceed, but will return true directly:
boolean result = true || (5 / 0 > 0); // true
Ternary operation b?X: y must be followed by the same type, and ternary operations are also short circuiting operations, calculating only x or Y
Execute String s = "hello";The JVM virtual machine first creates the string "hello" and then points the string variable s to it:
s │ ▼ ┌───┬───────────┬───┐ │ │ "hello" │ │ └───┴───────────┴───┘
Next, execute s = world;When the JVM virtual machine creates the string "world" first, and then points the string variable s to it:
s ──────────────┐ │ ▼ ┌───┬───────────┬───┬───────────┬───┐ │ │ "hello" │ │ "world" │ │ └───┴───────────┴───┴───────────┴───┘
The original string "hello" is still there, but we can't access it through variable s.
Therefore, string immutability means that the content of the string is immutable.
null value
A variable of reference type can point to a null value, which means it does not exist, that is, it does not point to any object. For example:
String s1 = null; // s1 is null String s2; // No initial value, s2 is also null String s3 = s1; // s3 is also null String s4 = ""; // s4 points to an empty string, not null
Note that to distinguish null from empty string', an empty string is a valid string object and does not equal null
Initialization of arrays
int[] ns = new int[5];
You can also specify initialized elements directly when defining arrays so that instead of writing out the size, the compiler automatically calculates the size
int[] ns = new int[] { 68, 79, 91, 85, 62 }; System.out.println(ns.length); // The compiler automatically reckons that the array size is 5
It can be further abbreviated as:
int[] ns = { 68, 79, 91, 85, 62 };
Note that the array is a reference type and the size of the array is not variable. Let's look at the following code:
// The results of five classmates: int[] ns; ns = new int[] { 68, 79, 91, 85, 62 }; System.out.println(ns.length); // 5 ns = new int[] { 1, 2, 3 }; System.out.println(ns.length); // 3
For the array ns, execute ns = new int[] {68, 79, 91, 85, 62};It points to an array of five elements:
ns │ ▼ ┌───┬───┬───┬───┬───┬───┬───┐ │ │68 │79 │91 │85 │62 │ │ └───┴───┴───┴───┴───┴───┴───┘
Execute ns = new int[] {1, 2, 3};It points to a new array of three elements:
ns ──────────────────────┐ │ ▼ ┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐ │ │68 │79 │91 │85 │62 │ │ 1 │ 2 │ 3 │ │ └───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘
However, the original arrays of five elements have not changed, they can only be referenced by the variable ns.
String Array
If an array element is not a basic type but a reference type, what is the difference between modifying an array element?
Strings are reference types, so let's first define an array of strings:
String[] names = { "ABC", "XYZ", "zoo" };
For an array variable name of type String[], it actually contains three elements, but each element points to a string object:
┌─────────────────────────┐ names │ ┌─────────────────────┼───────────┐ │ │ │ │ │ ▼ │ │ ▼ ▼ ┌───┬───┬─┴─┬─┴─┬───┬───────┬───┬───────┬───┬───────┬───┐ │ │░░░│░░░│░░░│ │ "ABC" │ │ "XYZ" │ │ "zoo" │ │ └───┴─┬─┴───┴───┴───┴───────┴───┴───────┴───┴───────┴───┘ │ ▲ └─────────────────┘
Assigning names[1], such as names[1] = "cat";The results are as follows:
┌─────────────────────────────────────────────────┐ names │ ┌─────────────────────────────────┐ │ │ │ │ │ │ ▼ │ │ ▼ ▼ ┌───┬───┬─┴─┬─┴─┬───┬───────┬───┬───────┬───┬───────┬───┬───────┬───┐ │ │░░░│░░░│░░░│ │ "ABC" │ │ "XYZ" │ │ "zoo" │ │ "cat" │ │ └───┴─┬─┴───┴───┴───┴───────┴───┴───────┴───┴───────┴───┴───────┴───┘ │ ▲ └─────────────────┘
Notice here that the string "XYZ" pointed to by names[1] hasn't changed. Instead, the reference to names[1] has been changed from pointing to "XYZ" to pointing to "cat". As a result, the string "XYZ" is no longer accessible through names[1].
Traversal of arrays
int[] ns = { 1, 4, 9, 16, 25 };
for (int n : ns) {
System.out.println(n);
}
int[] ns = { 1, 4, 9, 16, 25 };
for (int n : ns) {
System.out.println(n);
}
In the for (int n: ns) loop, the variable n takes the elements of the NS array directly, not the index.
Obviously the for each loop is more concise. However, the for each loop cannot get an index of the array, so which for loop to use depends on what we need.
Printing using a for each loop can also be cumbersome. Fortunately, the Java Standard Library provides Arrays.toString(), which allows you to quickly print array contents.
int[] ns = { 1, 1, 2, 3, 5, 8 };
System.out.println(Arrays.toString(ns));
Sorting an array actually modifies the array itself. For example, the array before sorting is:
int[] ns = { 9, 3, 6, 5 };
In memory, this integer array is represented as follows:
┌───┬───┬───┬───┐ ns───>│ 9 │ 3 │ 6 │ 5 │ └───┴───┴───┴───┘
When we call Arrays.sort(ns);After that, the integer array becomes in memory:
┌───┬───┬───┬───┐ ns───>│ 3 │ 5 │ 6 │ 9 │ └───┴───┴───┴───┘
That is, the contents of the array to which the variable ns points have been changed.
If you sort an array of strings, for example:
String[] ns = { "banana", "apple", "pear" };
Before sorting, this array is represented in memory as follows:
┌──────────────────────────────────┐ ┌───┼──────────────────────┐ │ │ │ ▼ ▼ ┌───┬─┴─┬─┴─┬───┬────────┬───┬───────┬───┬──────┬───┐ ns ─────>│░░░│░░░│░░░│ │"banana"│ │"apple"│ │"pear"│ │ └─┬─┴───┴───┴───┴────────┴───┴───────┴───┴──────┴───┘ │ ▲ └─────────────────┘
Call Arrays.sort(ns);After sorting, this array is represented in memory as follows:
┌──────────────────────────────────┐ ┌───┼──────────┐ │ │ │ ▼ ▼ ┌───┬─┴─┬─┴─┬───┬────────┬───┬───────┬───┬──────┬───┐ ns ─────>│░░░│░░░│░░░│ │"banana"│ │"apple"│ │"pear"│ │ └─┬─┴───┴───┴───┴────────┴───┴───────┴───┴──────┴───┘ │ ▲ └──────────────────────────────┘
The original three strings do not change in memory, but each element of the ns array points to a change.
2-D Array
Each element of the array does not have to be the same length, for example, an ns array can be defined as follows:
int[][] ns = { { 1, 2, 3, 4 }, { 5, 6 }, { 7, 8, 9 } };
This two-dimensional array is structured in memory as follows:
┌───┬───┬───┬───┐ ┌───┐ ┌──>│ 1 │ 2 │ 3 │ 4 │ ns ─────>│░░░│──┘ └───┴───┴───┴───┘ ├───┤ ┌───┬───┐ │░░░│─────>│ 5 │ 6 │ ├───┤ └───┴───┘ │░░░│──┐ ┌───┬───┬───┐ └───┘ └──>│ 7 │ 8 │ 9 │ └───┴───┴───┘
To print a two-dimensional array, you can use two nested for loops:
for (int[] arr : ns) { for (int n : arr) { System.out.print(n); System.out.print(', '); } System.out.println(); }
Or use Array.deepToString () from the Java standard library:
int[][] ns = { { 1, 2, 3, 4 }, { 5, 6, 7}, { 9, 10, 11, 12 } }; System.out.println(Arrays.deepToString(ns));
3-D Array
A three-dimensional array is an array of two-dimensional arrays. You can define a three-dimensional array as follows:
int[][][] ns = { { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }, { {10, 11}, {12, 13} }, { {14, 15, 16}, {17, 18} } };
It is structured in memory as follows:
┌───┬───┬───┐ ┌───┐ ┌──>│ 1 │ 2 │ 3 │ ┌──>│░░░│──┘ └───┴───┴───┘ │ ├───┤ ┌───┬───┬───┐ │ │░░░│─────>│ 4 │ 5 │ 6 │ │ ├───┤ └───┴───┴───┘ │ │░░░│──┐ ┌───┬───┬───┐ ┌───┐ │ └───┘ └──>│ 7 │ 8 │ 9 │ ns ────>│░░░│──┘ └───┴───┴───┘ ├───┤ ┌───┐ ┌───┬───┐ │░░░│─────>│░░░│─────>│10 │11 │ ├───┤ ├───┤ └───┴───┘ │░░░│──┐ │░░░│──┐ ┌───┬───┐ └───┘ │ └───┘ └──>│12 │13 │ │ └───┴───┘ │ ┌───┐ ┌───┬───┬───┐ └──>│░░░│─────>│14 │15 │16 │ ├───┤ └───┴───┴───┘ │░░░│──┐ ┌───┬───┐ └───┘ └──>│17 │18 │ └───┴───┘
If we want to access an element of a three-dimensional array, such as ns[2][0][1], we just need to follow the location to find the corresponding final element 15.
In theory, we can define any N-dimensional array, but in practice, higher-dimensional arrays are rarely used, in addition to two-dimensional arrays, which can sometimes be used.