Java Final Review Crash (2)

Java Final Review Crash (2)


Java Basic Syntax


After configuring the Java development environment and downloading the Java development tools (Eclipse, IDEA, etc.), you can write Java code. Because this article is to sort out the Java system from scratch, it is necessary to start with basic concepts.

type of data

In Java, there are only four types of data types:

  • Integer type : byte, short, int, long
    byte is also byte, 1 byte = 8 bits, the default value of byte is 0;
    short occupies two bytes, that is, 16 bits, 1 short = 16 bits, its default value The value is also 0;
    int occupies four bytes, which is 32 bits, 1 int = 32 bits, the default value is 0;
    long occupies eight bytes, which is 64 bits, 1 long = 64 bits, the default value is OL;
    So the byte size space occupied by the integer type is long > int > short > byte
  • Floating
    point There are two data types for floating point: float and double
    float is a single-precision floating-point type, occupying 4 bits, 1 float = 32 bits, the default value is 0.0f;
    double is a double-precision floating-point type, occupying 8 bits , 1 double = 64 bits, the default value is 0.0d;
  • Character
    type The character type is char, the char type is a single 16-bit Unicode character, the minimum value is \u0000 (ie 0), the maximum value is \uffff (ie 65535), the char data type can store any character, such as char a = 'A'.
  • Boolean
    Boolean refers to boolean. Boolean has only two values, true or false, which only means 1 bit. The default value is false. The above x bits all refer to the occupancy in the memory.

insert image description here

Basic grammar

  • Case-sensitive: Java is a case-sensitive language. For example, Hello is different from hello, which is actually Java's string representation.
  • Class name: For all classes, the first letter should be capitalized, such as MyFirstClass
  • Package name: The package name should be lowercase as much as possible, such as my.first.package
  • Method name: The first letter of the method name needs to be lowercase, and each letter of the following word needs to be uppercase, such as myFirstMethod()

operator

Operators are not only in Java, but also in other languages. Operators are some special symbols, which are mainly used for mathematical functions, some types of assignment statements and logical comparisons. Let’s take Java as an example to see operators.

assignment operator

The assignment operator is represented by the operator =, which means to copy the value on the right of the = sign to the left. The value on the right can be any constant, variable or expression, but the value on the left must be an explicit, defined variable. For example int a = 4.
But for objects, what is copied is not the value of the object, but the reference of the object, so if you copy one object to another object, you are actually assigning the reference of one object to another object.

Arithmetic operators

Arithmetic operators are similar to numerical calculations in mathematics. The main
insert image description here
thing to note about arithmetic operators is the priority issue . When there are multiple operators in an expression, the priority order of the operators determines the calculation order. The simple rule is to multiply and divide first and then add and subtract. () has the highest priority. There is no need to remember all the priority orders. If you are uncertain, you can use () directly .

Self-increment and self-decrement operators

This is not explained in words, the explanation is better to see the example directly

int a = 5;
b = ++a;
c= a++;

Comparison operator

Comparison operators are used to compare between variables in a program, between variables and arguments, and between other types of information.
The result of the comparison operator is a boolean. When the relationship corresponding to the operator is established, the result of the operation is true, otherwise it is false. There are 6 comparison operators, which are usually used in conditional statements as the basis for judgment.
insert image description here
Logical operators

There are three main logical operators, and, or, not
insert image description here
The following is the true/false symbol table corresponding to the logical operators
insert image description here

Bitwise operators

Bitwise operators are used to operate on each bit in the integer primitive type , that is, a binary bit. Bitwise operators perform Boolean algebra on the corresponding bits in the two arguments and produce a result.
insert image description here

If both sides of the comparison are numbers, the comparison becomes a bitwise operation.

Bitwise AND: Bitwise AND operation (AND), the result is 1 if both operands are 1, otherwise the result is 0. You need to first convert both sides of the comparison into binary and then compare each bit

Bitwise OR: Perform bitwise OR (OR), as long as one of the two bits is 1, the result is 1, otherwise it is 0.

Bitwise NOT: Bitwise XOR operation (XoR), if the bit is 0, the result is 1, if the bit is 1, the result is 0.

Bitwise XOR: Perform bitwise negation (NOT), if the bits of the two operands are the same, the result is 0, and if they are different, the result is 1.

shift operator

The shift operator is used to shift the operand in a certain direction (left or right) by the specified number of bits.
insert image description here
Ternary operator

The ternary operator is an operator similar to if...else.... The syntax is: conditional expression? expression1: expression2 . The position in front of the question mark is the condition of the judgment. The judgment result is a Boolean type. When it is true, expression 1 is called, and when it is false, expression 2 is called.


Java executes control flow


The control flow in Java is actually the same as C. In Java, flow control involves if-else, while, do-while, for, return, break, and the selection statement switch . The following is an analysis of this

Conditional statements

Conditional statements can execute different statements based on different conditions. Including if conditional statements and switch multi-branch statements.

if conditional statement

The if statement can independently judge the result of the expression, indicating the execution result of the expression, for example

int a = 10;
if(a > 10){
    
    
	return true;
}
return false;

if...else conditional statement

The if statement can also be used in conjunction with else, which usually means that if a certain condition is met, one kind of processing is performed, otherwise another kind of processing is performed .

int a = 10;
int b = 11;
if(a >= b){
    
    
	system.out.println("a >- b");
}else{
    
    
	System.out. println("a < b");
}

The expression inside the () after the if must be of type boolean. If true, execute the compound statement after the if; if false, execute the compound statement after the else.

if...else if multi-branch statement

The if...else in the above is the judgment of a single branch and two branches. If there are multiple judgment conditions, you need to use if...else if

int x = 40;
if(x > 60) {
    
    
	system.out.println("x的值大于60"");
}else if (x > 30) {
    
    
	system.out.println("x的值大于30但小于60");
}else if (x > 0) {
    
    
	system.out.println("x的值大于0但小于30");
}else {
    
    
	system.out.println("x的值小于等于0");
}

switch multi-branch statement

A more elegant way than the if...else if statement is to use the switch multi-branch statement, an example of which is as follows

switch (week) {
    
    
	case 1:
		System. out.println( "Monday");
		break;
	case 2:
		System.out. println("Tuesday");
		break ;
	case 3:
		system. out.println("Wednesday" );
		break;
	case 4:
		system.out. println("Thursday");
		break;
	case 5:
		system. out. println( "Friday");
		break;
	case 6:
		System. out.println("Saturday");
		break;
	case 7:
		system. out.println( "Sunday");
		break;
	default:
		system. out.println("No Else");
		break ;
}


loop statement

A loop statement is to repeatedly execute an expression operation under certain conditions until the requirements of the loop statement are met. The loop statements used are mainly for, do...while(), while

while loop statement

The while loop statement loops by using a condition to control whether to continue executing the statement repeatedly. The format of the while loop statement is as follows

while(布尔值){
    
    
	表达式
}

Its meaning is, when (boolean value) is true, execute the following expression, when the boolean value is false, end the loop, the boolean value is actually an expression, such as

int a - 10;
while(a > 5){
    
    
	a--;
}

do...while loop

The only difference between a while and a do…while loop is that the do…while statement executes at least once, even if the first time the expression is false. In a while loop, if the first time the condition is false, then the statement in it will not be executed at all. In practice, while is more widely used than do...while. Its general form is as follows

int b - 10;
// do. .-while循环语句
do {
    
    
	system. out.println("b -- " + b);
	b--;
} while(b == 1);

for loop statement

The for loop is the way we often use the loop, this form will be initialized before the first iteration. It is of the form

for(初始化;布尔表达式;步进){
    
    }

Boolean expressions are tested before each iteration. If the result obtained is false, the code following the for statement will be executed; each time the loop ends, the next loop will be executed according to the step value.

comma operator

One thing that cannot be ignored here is the comma operator. The only use of the comma operator in Java is the for loop control statement. In the initialization part of an expression, a series of comma-separated statements can be used; with the comma operator, multiple variables can be defined within a for statement, but they must have the same type

for(int i = 1;j = i + 10;i < 5;i++,j = j * 2){
    
    }

for-each statement

In Java JDK 1.5, a more concise and convenient method for traversing arrays and collections, the for-each statement, is also introduced. The example is as follows

int array[] = {
    
    7,8,9};

for (int arr : array) {
    
    
	System.out.println(arr);
}

jump statement

In the Java language, there are three jump statements: break, continue and return

break statement

We have seen the break statement in switch. It is used to terminate the loop. In fact, the break statement is used in the for, while, do...while loop statement to forcefully exit the current loop, such as

for(int i = 0;i < 10;i++){
    
    
	if(i == 5){
    
    
		break;
	}
}

continue statement

Continue can also be placed in a loop statement, which has the opposite effect to the break statement. Its function is to execute the next loop instead of exiting the current loop. The above example is mainly used.

for(int i = 0;i < 10;i++){
    
    
	System. out.printl(" i = " + i );
	if(i == 5){
    
    
		System.out.printl("continue ... ");
		continue;
	}
}

return statement

The return statement returns from a method and transfers control to the statement that called it.

public void getName() {
    
    
	return name;
}

Guess you like

Origin blog.csdn.net/ws15168689087/article/details/123099097