Programming foundation of Java Foundation

Time:2020-2-9

Basic data type

Java is a strongly typed language. The data stored in Java is of type, and its type must be determined at compile time.

The basic data type variable stores the data itself, while the reference type variable stores the spatial address of the data.

Programming foundation of Java Foundation

Basic type conversion

Automatic type conversion

When a value or variable with a small table number range is directly assigned to another variable with a large table number range, the system will perform automatic type conversion, otherwise forced conversion is required.

Cast

A cast may have an overflow that results in a loss of data precision.

Character string

Java does not have a built-in string type, but provides a standard class library string.

Feature: immutable string

Advantage: the compiler can share strings (only string constants are shared, and the results of + or substring operations are not shared)

operator

Self increasing and decreasing

int a = 5;
Int b = + + A; // a automatically increases first, and then assigns a value to B
System.out.println(a); // 6
System.out.println(b); // 6
int a = 5;
Int b = a + +; // A is assigned to B first, and then increased automatically
System.out.println(a); // 6
System.out.println(b); // 5

Conditional judgement

Switch (expression){
    case value1:
        statement1;
        break;
    case value2:
        statement2;
        break;
    case valuen:
        statementn;
        break;
    default:
        Code executed by default;
}

When the value of the expression after the switch is the same as the value after the case statement, execute from this position until the break statement or the end of the switch statement block is encountered; if there is no matching case statement, execute the code of the default block.

  • The value after case can be a constant value or a constant expression, but it cannot be a variable or an expression with a variable

  • You can combine the case statements with the same function

  • The default block can appear anywhere or be omitted

loop

While

while (condition) {
    statement;
}

do-While

do {
    statement;
} while (condition);

for

For (loop variable initialization; loop condition; loop variable change){
    statement;
}

Cyclic jump

  • break

  • continue

array

Arrays are used to store fixed size elements of the same type. Once the initialization of the array is completed, the space occupied by the array in memory will be fixed, so the length of the array cannot be changed. Even if the elements of an array are cleared, the space occupied by it will still be reserved.

Array itself is also a reference type, which can store both basic data types and reference types, as long as the stored array elements have the same type.

dynamic initialization

When performing dynamic initialization, the system will automatically assign initial values to array elements according to the declared array type. If it is a reference type, the initial value of the array element is null.

int[] scores;
Scores = New Int [5]; // declare an integer array of length 5

//The above two steps can be combined into one step
int[] scores = new int[5];

initiate static

Displays the initial value for each element specified

int[] scores;
scores = new int[] {1,2,3,4};

//The above two steps can be combined into one step
Int [] scores = {1,2,3,4}; // method 2

Traversing array

  • For loop traversal array

  • Foreach traversal array: foreach is not a keyword in Java, but a special simplified version of for statement. More convenient when traversing arrays and collections

int[] scores = {1,2,3,4};

for (int score : scores) {
    System.out.println(score);
}

The essence of array

The array is divided into two parts: one is the array reference, that is, the array reference variable defined in the code; the other is the actual array object, which runs in the heap memory and cannot be accessed directly. It can only be accessed through the reference variable.

Two-dimensional array

In essence, a two-dimensional array is a one-dimensional array, but its array elements are also references. References stored in array elements point to one-dimensional arrays.

String [][] str1 = {new String[3], new String[]{"hello"}};

Schematic diagram of the above code:

Programming foundation of Java Foundation

Welcome to my public address.

Programming foundation of Java Foundation