Xiaobai's first day of getting started with Java

When you have some understanding of the Java language, you can start learning Java.

As your first day of Java learning, the content you need to master is as follows:

certainly! The premise is that we have installed the JDK and tested the writing, compiling and running of HelloWorld. Then we only need to choose an editor that is comfortable to us to focus on the following content.


1. Comments

In the actual work of programmers, a large amount of logical code will be written until the purpose of programming is completed.
If you don't have a lot of codes in your mind, you can imagine it as a big pile of HelloWorld. However, too many HelloWorlds will greatly increase your time and reduce your work efficiency when recalling its functions. So you can use comments to explain it, even if the time is too long, you can quickly reflect the actual function of its code.

So what exactly are annotations?
Like most programming languages, comments in Java will not appear in executable programs,
that is, the definition of the comments will be ignored during execution of the program.

1. Three kinds of comments

There are three types of comments in Java:

1.1 Single line comments

The most commonly used introduction is called a single-line comment. If we use the HelloWorld code as an example, such as:

public class HelloWorld{
    
    
	public static void main(String args[]){
    
    
		// 一个简单的输出语句
		System.out.println("Hello!!!~World");
	}
}

1.2 Multi-line comments

Of course, if you need to explain more content, you can also use multi-line comments, such as:

/*
public 是权限修饰符,用于控制该代码在其程序中被访问的权限

class 是类的关键字,在Java中,所有的代码都定义在一个类中

HelloWorld 是该类的名称,要注意的是该名称需与其文件名称一致

{} 相当于标准语法,Java是强类型语言,一定要注意语法规范

static void 暂时不需顾虑,面向对象章节会详细介绍

main 是主方法,该文件在被虚拟机执行时,虚拟机会根据main来执行器内部内容

System.out System系统下的out对象,然后通过.调用 println 方法

; 分号是逐行结束必须要写的规范
*/
public class HelloWorld{
    
    
	public static void main(String args[]){
    
    
		System.out.println("Hello!!!~World");
	}
}

1.3 Documentation comments

In fact, multi-line comments do not need to be written as clearly as above. It is enough to explain the program based on more than the content of single-line comments. If the explanation is as clear as above, documentation comments should be used because it can help you generate documentation.

Documentation comments start with /** and end with */. And the blank line of each sentence has a * as follows:

/**
 *public 是权限修饰符,用于控制该代码在其程序中被访问的权限

 *class 是类的关键字,在Java中,所有的代码都定义在一个类中

 *HelloWorld 是该类的名称,要注意的是该名称需与其文件名称一致

 *{} 相当于标准语法,Java是强类型语言,一定要注意语法规范

 *static void 暂时不需顾虑,面向对象章节会详细介绍

 *main 是主方法,该文件在被虚拟机执行时,虚拟机会根据main来执行器内部内容

 *System.out System系统下的out对象,然后通过.调用 println 方法

 *; 分号是逐行结束必须要写的规范
*/
public class HelloWorld{
    
    
	public static void main(String args[]){
    
    
		System.out.println("Hello!!!~World");
	}
}

Now that you have understood how to use the three annotations, we will not explain how to automatically generate documents for the time being,
because our learning motivation is to quickly figure out what the three annotations are.


2. Identifiers and keywords

The concepts of identifiers and keywords are inherently different and must not be confused.

2.1 Composition of identifier:

The identifier consists of: letters, numbers, _ (underscore), $ (dollar sign).
It cannot start with a number, and cannot be a keyword used by the Java language itself.
.Attention
! The Java language itself is strictly case-sensitive.

2.2 So what exactly is an identifier?

Keywords refer to officially defined names, which are names defined by the developers of the Java language itself when developing the language. For example, public and class are keywords.

So the identifier can be understood as the name we define during the coding process.

If, as a novice, you still don’t understand it very well, then we can write a code snippet to explain:

I have to mention an interesting episode here to help you digest and understand. After the Java language became popular in the Chinese market, support for Chinese languages ​​was added after JDK 1.7.

Let’s use Chinese definition identifiers to help digest:

//这里我们将类的标识符定义为了你好
public class 你好{
    
    
	public static void main(String args[]){
    
    
		System.out.println("Hello!!! ~World");
	}
}

3. Data type classification

Strictly speaking, any program is a data processing game. Therefore, there must be strict restrictions on the storage of data. These restrictions are reflected in the division of data types, that is, different data types can save different data contents.
.Note
: Java is a strongly typed language, which means that a type must be declared for each variable.

In the process of learning programming, you will always realize it later. Even if you don’t understand some concepts now, I must tell you:
Java’s data types are divided into two types: basic data types and reference data types!

Basic data types include : byte, short, int, long, float, double, char, and boolean.
The concept of reference data classes is : memory must be allocated during operations.

insert image description here

In other words, basic data types do not involve the allocation of memory space, while the use of reference data types requires developers to allocate space for them and then match the relationships.

In addition, Java's basic data types are mainly defined in numerical terms. The storage data ranges and default values ​​of these basic data types are as shown in the table:

No. type of data size/bit Representable data range Defaults
1 byte 8 -128 ~ 127 0
2 short (short integer) 16 -32768 ~ 32767 0
3 int (integer type) 32 -2147483648 ~ 2147483647 0
4 long(long integer type) 64 -9223372036854775808 ~ 9223372036854775807 0
5 float (single precision) 32 -3.4E38(-3.4 10^38) ~ 3.4E38(3.4 10^38) 0.0
6 double (double precision) 64 -1.7E308(-1.7 10^308) ~ 1.7E308(1.7 10^308) 0.0
7 char (character) 16 0 ~ 255 ‘\u0000’
8 boolean - true or false false

In addition to the data range, you must remember the contents of the above table, but it is also best to remember the byte value range!

The unit used for the size of the data type in the table is: bit, 1 byte equals 8 bits in the computer

After having a relative understanding of data types and data type divisions,
let's start to analyze them one by one!

3.1 Analysis of data types one by one:

3.2 Integer types:

3.2.1. int

Any numeric constant such as: 10, 30, 50, 90... belongs to the int data type in Java.
That is: all integer contents set in Java are int type data by default.

public class TestintDemo {
    
    
    public static void main(String args[]){
    
    
        //语法:数据类型 变量名称 = 变量;
        int num = 10;  // 10 是常量,常量的默认值是int
        int result = num * 2; //变量num的值乘以2,计算结果存于变量result当中
        System.out.println(result); // 输出打印 result
    }
}

If you don't understand the concept of variables and constants, let's put it aside for now, we will discuss it in the next knowledge point.

TestintDemo code snippet description:

This is the first time we embed code with some logic in the article, so in consideration of our friends in Novice Village, we will explain in detail:
This program first defines a num variable with a data type of int, and then assigns it a constant value: 10.
Then we defined the second variable: result, which is also an int data type, and then assigned a value to it that is not a fixed constant value, but a multiplication operation among the four arithmetic operations. The final output statement also outputs the result variable. Can you imagine what its value is?

We only demonstrate a simple multiplication operation here, of course! If the user needs, it can also be any one of the four arithmetic operations! (Addition +, Subtraction -, Multiplication*, Division)

Next, I would like to emphasize the importance of value range!

3.3 The importance of value range!

Think back to the table above! Yes, it’s the table that introduces data types!
What will happen if the program exceeds the value range?

Let's do a test using the int type:

public class TestintOutDemo {
    
    
    public static void main(String[] args) {
    
    
        int max = Integer.MAX_VALUE; //取最大值
        int min = Integer.MIN_VALUE; //取最小值

        //查看int类型取值范围的最大值和最小值
        System.out.println(max); //控制台的输出结果:2147483647
        System.out.println(min); //控制台的输出结果:-2147483648

        //给最大值做加法
        // 预计结果:2147483647 +1
        System.out.println("最大值+1后:"+(max+1));
        //给最小值做减法
        // 预计结果:-2147483648 -1
        System.out.println("最小值-1后:"+(min-1));
    }
}

Let's take a look at the results of running the program:
insert image description here

This is how the same thing?
After the maximum value +1, it becomes a negative number, and after the minimum value -1, it actually becomes a positive number! ?

If the calculation exceeds the value range of the data type itself, a loop operation will occur. If the maximum value continues to increase, it will become the minimum value, and then it will continue to loop to the next minimum value. Otherwise, the minimum value will become the maximum value minus 1. value, this phenomenon is called overflow. In computers, binary is the basic unit, and int data occupies a total length of 32 bits, which means that the first bit is the sign bit, and the remaining 31 bits are data bits
.
Of course, when the data is already the maximum value stored in the data type, if the +1 operation is continued, the sign bit will be changed, and eventually this data overflow problem will occur.

Generally speaking, for this kind of problem, you only need to choose a reasonable data type. For example, if you want to store age-related data, you can use the int type. The value range of the int type is enough to store age. After all, the age will not exceed the int type. Value range? Have you not…

Or you can also transform. Don't forget that there are four integer types: byte short int long!

3.4 What is transformation?

There are four integer types: byte short int long.
Each of them has its own size of memory space. Their sizes are arranged according to the size between them, from small to large: byte short int long. From big to small: long int short byte.
Switching the memory space that occupies a smaller area to the larger one is called automatic conversion, and switching the memory space that occupies a larger area to the smaller one is called forced conversion.

So how do we transfer this demo?

3.2.2 long (automatic type conversion description)

We can allow the int type to implement automatic type conversion to the long type that occupies a larger byte space.

public class TestintTolongDemo {
    
    
    public static void main(String[] args) {
    
    
        int max = Integer.MAX_VALUE; //取出最大值
        int min = Integer.MIN_VALUE; //取出最小值

        System.out.println("输出向上转型后的最大值:"+(max+1L));
        System.out.println("输出向上转型后的最小值:"+(min-1L));
    }
}

How to implement the transformation:

During the conversion process, if one of the values ​​involved in the operation is converted to a larger type, the data type of the result will also be automatically converted to a larger type, instead of the default int of the integer type.

The transformation method can be easily implemented by adding specific statements:

like:

int num1 = 10; //int类型
int num2 = 20; //int类型
System.out.println((long)num1 + num2); //long 可以加给参与运算的任意一个变量

For example:
if there are not two variables involved in the operation: for example, a constant and a variable

int num1 = 10;
System.out.println(num1 + 20L); //可以直接给常量加上L,将其转型为不是默认int的long
System.out.println((long)num1 + 20); //或者还是将变量的存储方转为long

3.5 Description of forced type conversion

We already know how to change the memory space occupied by the smaller party to the larger party.
Next, we will explain how to change the memory space occupied by the larger party to the smaller one.

long num = 1000;
int x = (int)num; //强制类型转换的语法为:
		//在转型方的前面加上优先级运算符(),然后在符号内填写所转换的目标数据类型
System.out.println(x);

Although the program supports us to do forced type conversion, we must pay attention to the problem of data loss caused by program overflow!

3.6 A more reasonable type conversion diagram:

insert image description here

3.2.3 byte

There are two other data types in the integer type that have not been introduced, that is byte and short!

The value range of byte data type: -128 ~ 127 This should be kept in mind!

public class TestbyteDemo {
    
    
    public static void main(String[] args) {
    
    
        byte num = (byte) 130; //由于:默认的int整数130 超过了byte数据范围,
        					//需要做强制类型转换
        					
        System.out.println(num); //程序运行结果为:-126
                                //强制类型转换造成了数据丢失
    }
}

I choose an example that exceeds the value range to tell you a concept:

Java improves automatic type conversion for byte types

public class TestbyteDemo {
    
    
    public static void main(String[] args) {
    
    
        byte num = 127; //127默认为int类型,这里没有做强制类型转换!
        			//因为:没有超过byte数据类型的取值范围!
        System.out.println(num); 
                                
    }
}

3.6 About default values ​​of data types

We know that each data type has its default value, but before the jdk1. Compilation error!

example:

public class TestintDemo{
    
    
	static int num;
	public static void main(String args[]){
    
    
		System.out.println(num);
	}
}

but:

public class TestintDemo{
    
    
	public static void main(String args[]){
    
    
		int num;
		num = 0; //但是此编码形式在 jdk 1.4 及之前会报错
				// 必须写为:int num = 0;
		System.out.println(num);
	}
}

certainly. This node is just a supplementary explanation!

3.7 Floating point types

The floating-point type is the decimal type in mathematics. The default type of the integer is int, and the default type of the decimal is double ('this type is the most widely used for storing data').
There are two data types representing decimal types in java, one is float and the other is double, and the storage data range of double is exactly twice that of float, so double is called double precision value.

3.7.1 double

Let's first look at how to use double to define decimals.

public class TestdoubleDemo {
    
    
    public static void main(String[] args) {
    
    
        double num = 10.2;
        System.out.println(num * 2); //这里包含一个自动类型转换
            //因为 double的内存空间所占大于int数据类型,
            //固较小的int类型会自动转换为较大的double类型
    }
}

The execution result of the look program:
insert image description here

3.7.2 float

Since the default type of the decimal is the double type, if you want to use the float type, you need to do a forced type conversion. There are two conversion methods: variables use the letter "F" or "f"; constants add parentheses before the constant value, and fill in float in the parentheses.

public class TestfloatDemo {
    
    
    public static void main(String[] args) {
    
    
        float num1 = 10.2F;
        float num2 = 10.2f;
        float num3 = (float)10.2;
        System.out.println(num1 + num2 + num3);
    }
}

The final execution result of the program should be: 30.6, let's run it to see:
insert image description here

It’s actually not the 30.6 I expected! ?
This is a BUG caused by Java itself. This problem cannot be solved by calculation alone. It needs to be solved by using the BigDecimal class or Math, which we will learn later.

3.8 Only double or float can save decimals

int x = 9;
int y = 5;
System.out.println(x / y);

The running result of this code automatically omits decimals!
insert image description here

It is defined using two int types. If you want to change the result, you can transform either one.

int x = 9;
int y = 5;
System.out.println(x / (double)y);
// System.out.println(x / (float)y);

Let’s take a look at the running results after the program transformation:
insert image description here

3.9 Character type

3.9.1 char

Byte belongs to byte. According to the traditional concept: one character is equal to two bytes (one char is equal to two bytes).
In the following sample code we will demonstrate the conversion of char and int variables.
In the computer world, everything is represented in the form of encoding, and Java uses the UNICODE encoding format.

The unicode encoding format includes part of the ASCII code, and the range of English letters is as follows:

  • Capital letters: 65 ~ 90
  • Lower case letters: 97 ~ 122

The encoding range difference between uppercase and lowercase letters is 32.
In Java, the use of "double quotes" represents strings, while characters are represented using 'single quotes'.

look Code:

public class TestcharDemo {
    
    
    public static void main(String[] args) {
    
    
        char a = 'A';
        int num = a;
        System.out.println("测试的字符为:"+a);
        System.out.println("该测试字符所对应码点为:"+num);
    }
}

Code point:

The code value corresponding to a character in an encoding is called a code point.

After writing the code and adding the concept of code points, it’s time to take a look at the execution results of the program:
insert image description here

3.10 Common coding ranges

‘A’(65)~ ‘Z’(90);
‘a’(97)~‘z’(122);
‘0’(48)~‘9’(57);

3.11 Use encoding range to convert letter cases

public class TestcharToBig {
    
    
    public static void main(String[] args) {
    
    
        char letter = 'A';
        int num = letter + 32;
        letter = (char) num;
        System.out.println(letter);
    }
}

//程序的最后输出结果一定为字符:'a'

3.12 Boolean type

The word Boole comes from the name of a mathematician: George Boole.
The Boolean type is a logical result, and it mainly stores two types of data, true and false.

Use of boolean values:

public class TestbooleanDemo {
    
    
    public static void main(String[] args) {
    
    
        boolean flag = false;
		//利用if语句判断条件是否成立
			//if语句会在后续内容中进行介绍
        if (!flag){
    
    
            System.out.println("Hello!!!~World");
        }
    }
}

Be sure to remember that boolean is often used for logical judgments and it only has two values! : true, false.
There is no conversion between integer values ​​and boolean values.
In many other programming languages, such as: C++, values ​​or even pointers can replace boolean values, a value of 0 is equivalent to the Boolean value false, and a non-zero value is equivalent to the true value of boolean. In Java, it is not allowed to use 0 or 1 instead of Boolean values.


4. Variables and Constants

Like all programming languages, Java uses variables to store values. A constant is a quantity whose value does not change.

Until now, we have not explicitly stated the concepts of variables and constants.
In this section, we explain in detail:

4.1 Declare variables

Its syntax is:

//声明变量
数据类型 变量名称;

//声明并赋值
数据类型 变量名称 = 常量值;

// 你甚至可以同时声明多个变量
数据类型 变量名称1,变量名称2,变量名称3;

4.2 Variable initialization

If you use uninitialized variables directly, the compiler will report an error!

int num;
System.out.println(num);

Variable initialization is the best solution to this problem!
The so-called initialization is actually...

int num = 0; //赋值....赋初始值
System.out.println(num); //然后就可以正常使用了!

And there are two ways to initialize it. One is to write it directly on one line as above.
The second way:

int num; //这种写法只是在声明

//使用时做初始化操作
num = 0; //这样写是在赋值
System.out.println(num);

//而第一种写法是声明并赋值

4.3 Variables and constants

In the above content, we just stated the declaration and use of variables. So what exactly are constants and variables?

Constants :

You can understand it as a fixed value, whether it is an integer or a floating point number. As long as it is a fixed value, it is called a constant.

variable:

Variables need to be declared first. Such as int num; Although there is no assignment yet, if you assign a value such as:
int num = 10; here is to declare a variable, the variable is of type int, and its name is num.
Then a constant 10 is assigned. In fact, a value is stored in the variable, and this value is a constant.

But in fact, the use of constants is like this:

public class TestDemo{
    
    
	final static int num = 0;
	//经 final 修饰符修饰后,只能赋值一次且值永远不可改变。
}

5. Operators

When the three words operator appeared in my head, my first reaction was the concept of operator in mathematics. Just like the four arithmetic operations are always inseparable from '+', '-', '*', '/', '%'. In computer programming languages, there is also the concept of operators.
What is even more surprising is that there are categories of operators in programming languages, including: relational operators, mathematical operators, ternary operators, logical operators, and bitwise operators.

Let’s analyze them one by one:

Let’s start with relational operators!

5.1 Relational operators

The main function is to compare the size relationship of the data. The returned result is boolean data (only true and false values). Commonly used relational operators are: greater than > greater than or equal to > = less than <
less than
or equal
to <
= equal
to = =
Not equal to !=

Code demo:

package TestOperator;

public class TestRelation {
    
    
    public static void main(String[] args) {
    
    
        System.out.println("大于:"+ (3 > 1));
        System.out.println("大于等于:"+(3 >= 1));
        System.out.println("小于:"+ (3 < 1));
        System.out.println("小于等于:"+(3 <= 1));
        System.out.println("等等于:"+ (3 == 1));
        System.out.println("不等于:"+(3 != 1));
    }
}

We know that relational operators have only two result values: true or false, and the results of the program can be imagined
insert image description here

5.2 Mathematical operators

Mathematical operators are also often used in actual development. Their contents mainly include: four arithmetic operations, modulus, auto-increment, etc.

5.2.1 Four arithmetic operation code examples

Four arithmetic operations: addition, subtraction, multiplication and division

int numA = 10;
int numB = 20;

System.out.println("加法计算:" +(numA + numB));
System.out.println("减法计算:" +(numA - numB));
System.out.println("乘法计算:" +(numA * numB));
System.out.println("除法计算:" +(numA / numB));

Let’s take a look at the test results of the four arithmetic operations:
insert image description here
Oh! The result of the trigger calculation is actually zero! ?
0.5, which is not the correct result value, is because two int types are calculated, and the decimal part is discarded.

You can transform it on input, like:

System.out.println("除法计算:" +((double)numA / numB));

insert image description here

This time, there is no problem with the result!

But what should I do if there is a remainder in the division calculation?

If there is an inexhaustible remainder in a division equation, we can use the modulo operator to get the remainder.

like:

int numA = 10;
int numB = 3;

System.out.ptintln(numA % numB);

insert image description here

If you can't feel the magic of the modulo operator, how about using it to determine odd and even numbers?

5.2.2 Use the modulo operator to determine odd and even numbers

int num = 10;

if(num % 2 ==0){
    
    
	System.out.println("您选择的数字是一个偶数!");
}else{
    
    
	System.out.println("您选择了一个奇数~");
}

The if branch statement used here will be introduced in detail later!
Let's take a look at the execution results of the program:
insert image description here

5.3 Simplifying operators

Simplified operators are to simplify user writing, including: *=, /=, +=, -=, %=, ++, –

5.3.1 Simplified operators of the four arithmetic operations

//设置变量
int numA = 10;

System.out.println("乘等于:"+(numA*=2));

The usage methods are similar. We only demonstrate simplified operators for multiplication in the code. After reading the execution results, jump directly to the more important increment and decrement operators.

insert image description here

5.3.2 Increment and decrement operators

++ and -- are called self-increasing and self-decreasing. Depending on where this operator is set, the order of execution is also different.

Take the auto-increment operator as an example:

We set the increment operator after the variable:

int num =1;
num++;
System.out.println(num);

Here a variable is set, then the variable is incremented, and finally the variable is printed. The result is 2 because it is incremented by 1.
insert image description here

But do you know how and when the variable is incremented?
I'll change the code to explain it better:

int num =1;

System.out.println(num++);
System.out.println(num);

The initial value of the variable is set to 1. Although the ++ operation is defined in the first print data, the value is not ++ when outputting, because the ++ operation is also in the statement when ++ is written behind the variable This is done after the execution is complete. You can take a look at the execution results of the program.

Program result execution diagram:
insert image description here

What if the operator is written before the variable?

We are testing with the self-decrement operator:

int num = 5;
--num;
System.out.println(num);

The program execution result is:
insert image description here

Then when did it complete the self-decreasing action?

//同上述自增测试思路相同
int num = 5;

System.out.println(--num);
System.out.println(num);

insert image description here

It turns out that the decrement action has been completed when the statement is executed.

5.4 Ternary operator

The ternary operator is also called the ternary operator. It is a form of assignment. When executing this operator, the result of a Boolean expression can be assigned.

Basic syntax:

数据类型 变量 = 布尔表达 ? 设置满足条件所执行的内容 : 设置不满足条件时所执行的内容;

Specific examples are as follows:

//定义变量,存储整数
int numA = 10;
//定义变量,存储第二个整数
int numB = 20;

//定义三目运算符,条件设置为 两个变量的大小比较
	//如果第一个变量的值大于第二个变量,则输出第一个变量存储的值
	//否则,与其相反。切记还要定义变量进行接收。
int max = numA > numB ? numA : numB;
//输出存储三元运算符表达式的变量
System.out.println(max);

The result is shown as:
insert image description here

5.5 Logical operators

There are three types of logical operations:
and (multiple conditions are satisfied together)
or (multiple conditions are only satisfied by one)
not (using the "!" operation, you can achieve the effect of negating the value)

5.5.1 Using '!'

The effect of negating the value, for example: when the result is true, the result is false. When the result is false, you get a true.

boolean flag = true; //第一个标志为true值
System.out.println(!flag); //输出时利用 '!' 进行取反

Program execution result:
insert image description here

5.5.2 Plain '&' and short-circuit '&&'

The difference in grammatical form between ordinary AND and short-circuit AND is that ordinary AND requires writing one &, while short-circuit AND requires writing two &&. The real difference in function is demonstrated in the following code example.

ordinary&:

if ((1 == 2) & (10 / 0 == 0)){
    
    
    System.out.println("HelloWorld!");
}

Program execution result:
insert image description here

An error message appeared after the program was executed. We know that in mathematics, zero cannot be used as a divisor because it is meaningless. more importantly! : We used ordinary AND operations. When the direct result of the first condition is false, there is no short-circuit effect, but the subsequent conditional statements continue to be executed. Use normal and return the result only after all conditional statements set are executed! Yes, this is ordinary and!
.That
is: when a condition value is false, its result is already false. However, it does not have a short-circuit effect. The result is returned only after all the set conditional statements are executed.

Work with short-circuit effects is the responsibility of the short-circuit and:

if ((1 == 2) && (10 / 0 == 0)){
    
    
     System.out.println("HelloWorld!");
}

insert image description here

Although no results are output, no error message is reported. Because when the first result is false, the result is destined to be false, and subsequent statements will be short-circuited.

5.5.3 or

The OR operation is to judge several conditions together. If one of the result values ​​returns true, the result is true. False will be returned only when all results are false.
Or there are two kinds of single |and double||

Normal or:

if ((1 == 1) | (10 / 0 == 0)){
    
    
	System.out.println("HelloWorld!");
}

Results of the:
insert image description here

Although the function of ordinary or is: one is true, the result is true. When the conditions are all false, the result is false, but an error occurs in the program. As you can imagine, the effect is the same as ordinary and does not have a short-circuit effect.

On the other hand, short circuit or:

if ((1 == 1) || (10 / 0 == 0)){
    
    
	System.out.println("HelloWorld!");
}

insert image description here

so amazing! ! Actually output HelloWorld!

It seems that both short circuit and short circuit or have a short circuit effect, while normal and ordinary or do not have a short circuit effect.
Although the function of short-circuit AND is that the result is true only when all conditions are true.
The short-circuit or effect is that when one of all conditions is true, the result is returned as true. If both are false, it will return false.

5.6 bit operators

Bit operators operate on underlying binary data. Bit operators include: &, |, ^, >>, <<, >>>

Since the content here touches the bottom level, if you want to tell it clearly in words, you will need to read a lot of books to convey it clearly.
So here is just an example.

If the requirement allows you to calculate the value of 2 * 2 * 2 as quickly as possible. what will you do?
Directly Sysmte.out.println(2 * 2 * 2); ? No!!! Then the computer needs to first convert the decimal 2 to binary for calculation, and then convert the result to decimal for output! Don't forget that the computer world is a binary language.
If you want a faster way, you can use bitwise operators

int num = 2;
System.out.println(num <<2); 

This code uses the computer's bit-by-bit data storage mechanism to directly manipulate the bits and move the data on the bits by 2 to the left. The result is: 8. The efficiency is high because it eliminates the conversion between bases, directly moves the data in bits, and then outputs the result.

Program execution result:
insert image description here

Guess you like

Origin blog.csdn.net/tianlei_/article/details/132032614