JAVA basic syntax

Types, variables, and operators

type

Java can be divided into primitive type and class type.

Basic data type

It can be divided into integer, byte, floating point number, character and Boolean

package package_03;

public class Test1 {
    public static void main(String[] args){

        /**
         * print For general output, precision format conversion and line feed output cannot be preserved
         * printf It is often used for format conversion, but it should be noted that it is not newline output, but only for precision conversion
         * println It is a newline output and cannot be used for format conversion
         *
         * printf()It's jdk1 API is available only after 5. f means format, that is, format
         * Used in system Out is to format the output text and display it in text mode
         */
        //Byte (1 byte) short (2 bytes) int (4 bytes) long (8 bytes) range
        //%d decimal integer format output
        System.out.printf("byte The value range of is:%d ~ %d%n",Byte.MIN_VALUE,Byte.MAX_VALUE);
        System.out.printf("short The value range of is:%d ~ %d%n",Short.MIN_VALUE,Short.MAX_VALUE);
        System.out.printf("int The value range of is:%d ~ %d%n",Integer.MIN_VALUE,Integer.MAX_VALUE);
        System.out.printf("long The value range of is:%d ~ %d%n",Long.MIN_VALUE,Long.MAX_VALUE);
        //float (4 bytes) double (8 bytes)
        System.out.printf("float The value range of is:%d ~ %d%n",Float.MIN_EXPONENT,Float.MAX_EXPONENT);
        System.out.printf("double The value range of is:%d ~ %d%n",Double.MIN_EXPONENT,Double.MAX_EXPONENT);
        //Unicode that can be represented by char (2 bytes)
        //Use integer Tohexstring (Arg. Hashcode()) to get the output result
        System.out.printf("char The value range of is:%h ~ %h%n",Character.MIN_VALUE,Character.MAX_VALUE);
        //Boolean
        System.out.printf("Boolean The value range of is:%b ~ %b%n",Boolean.TRUE,Boolean.FALSE);


    }
}

**[Tip]: * * if you know more format control characters about the thread, you can refer to:
https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html

operator

**

  • Arithmetic operation
  • Comparison and conditional operation
  • Logical operation
  • Bit operation
  • Increment and decrement operation
    **
package package_03;

public class Test1 {
    public static void main(String[] args){

        //Bitwise operators & (and - and), | (or - or), ^ (XOR), ~ (complement)
        System.out.println("&And operation:");
        System.out.printf("0 & 0 %5d%n",0 & 0);
        System.out.printf("0 & 1 %5d%n",0 & 1);
        System.out.printf("1 & 0 %5d%n",1 & 0);
        System.out.printf("1 & 1 %5d%n",1 & 1);

        System.out.println("|And operation:");
        System.out.printf("0 | 0 %5d%n",0 | 0);
        System.out.printf("0 | 1 %5d%n",0 | 1);
        System.out.printf("1 | 0 %5d%n",1 | 0);
        System.out.printf("1 | 1 %5d%n",1 | 1);

        System.out.println("XOR Operation:");
        System.out.printf("0 ^ 0 %5d%n",0 ^ 0);
        System.out.printf("0 ^ 1 %5d%n",0 ^ 1);
        System.out.printf("1 ^ 0 %5d%n",1 ^ 0);
        System.out.printf("1 ^ 1 %5d%n",1 ^ 1);
    }
}

Process control

if... else conditional
//Mode 1
if(Conditional formula){
	Description block;
}else{
	Description block;
}

//Mode II
if(Conditional formula I){
	Description block;
}else{
	if(Conditional formula II){
	Description block;
	}else{
	Description block;
	}
}
switch conditional

JDK1. After 7, switch can be used to compare integers, characters, strings and enums

//1, First look at the parentheses of switch, in which the variable or expression to obtain the value is placed
switch(Variable or expression){//Value must be integer, character, string, Enum (Enum)

	//After obtaining the value, start the integer, character, string and Enum comparison set in case
	//If it matches, execute the following description sentence and know that break leaves the switch block. If there is no match, execute the default code block.
	case Integer, character, string, or enumeration( Enum):
		Descriptive sentences;
		break;
	case Enumeration, character string, or( Enum):
		Descriptive sentences;
		break;
	default:
		Descriptive sentences;
}
for loop
for(Initial formula; The execution result must be Boolean; Repetitive){
}
while Loop

The difference between while and do... While
1. The expression of loop structure is different
The expression of the while loop structure is: while (expression) {loop body}.
The expression of do while loop structure is: do {loop body;} While (conditional expression).

2. Different judgment methods during execution
When the while loop is executed, it will enter the loop only when the conditions are met. After entering the loop, execute all the statements in the loop until the conditions are not met, and then jump out of the loop.
The do while loop will run once first. After the first do loop, check whether the value of the conditional expression is true after execution. If the value is not true, the loop will exit.

3. Different execution times
The while loop is executed after judgment. If the judgment condition is not tenable, the intermediate loop body can not be executed.
The do while loop is executed first and then judged. The number of executions is at least once. After one execution, judge whether the condition is true. If it is not true, jump out of the loop and continue to run the loop body.

4. The order in which the end loop body is executed is different
At the end of the while loop, the loop body is also in the middle loop body and executed in the middle loop body. The conditions for whether the loop body continues to run are also in the loop body.
Do while loop is to add the end loop body to the middle loop body, and execute the end loop body when executing the middle loop body. The condition of whether the loop body continues to run is in the end loop body.

break,continue

Both break and continue are used to control the loop structure, mainly to stop the loop.
1.break
break is used to completely end a loop, jump out of the loop body and execute the statement after the loop.

2.continue
Continue is a bit similar to break. The difference is that continue only terminates this cycle, and then executes the following cycle. Break completely terminates the cycle.
It can be understood that continue is to skip the remaining statements in the current cycle and execute the next cycle.

Keywords: Java

Added by theironchef on Mon, 07 Feb 2022 13:30:22 +0200