Dabai became the sixth day of the Java software siege lion (floating point, Boolean, type conversion, arithmetic operators, relational operators, logical operators)

type of data

1. Floating point

float single precision [4 bytes]
double double precision [8 bytes, higher precision]

In the Java language, all floating-point literals [3.0] are handled as double types by default.
To handle the literal value as a float type, you need to add F/f after the literal value

Note:
Double and float are approximate values ​​when stored in the computer's internal binary storage.
In the real world, some numbers are infinite loops, such as: 3.3333333...
Computer resources are limited, and storing unlimited data with limited resources can only store approximate values.

2. Boolean

About Boolean data type: boolean

In the Java language, the boolean type has only two values: true, false, and no other values.
Unlike the C language, 0 and 1 can represent false and true.

In the bottom storage, the boolean type occupies 1 byte, because in actual storage, the false bottom layer is 0, and the true bottom layer is 1.

The Boolean type is very needed in actual development, and it is often used in logical operations and conditional control statements .

3. Type conversion

About the mutual conversion between basic data types:

Conversion rules:

1 Among the eight basic data types, all the remaining 7 types except for the Boolean type can be converted to each other.

2 The conversion from small capacity to large capacity is called automatic type conversion, and the capacity is sorted from small to large:
byte <short, char <int <long <float <double
Note: No matter how many bytes are occupied by any floating point type, it is better than an integer type large capacity.
Char and short can represent the same number of types, but char can take a larger positive integer.

3 The conversion of large capacity to small capacity is called forced type conversion. The type conversion symbol needs to be strengthened before the program can be compiled and passed, but the accuracy may be lost during the runtime, so use it with caution.

4 When the face value of the integer number does not exceed the value range of byte, short, and char, it can be directly assigned to variables of type byte, short, and char.

5 When the byte, short, and char are mixed, they are converted to int type before the operation.

6 Mixed operation of multiple data types, first convert to the type with the largest capacity before performing operation.

Operator

4. Arithmetic operators

About the operators in Java programming: arithmetic operators

+ Sum
- Subtract
* product
/ Quotient
% Take the remainder【Modulo】
++ Add 1
- - Decrement 1

Summary: The ++ operator can appear before the variable or after the variable. As long as the ++ operation ends, the value in the variable will definitely increase by 1 whether it is before or after the variable.

++a: Increment by 1 first, in the operation.
a++: Calculate first, then increment by 1.

5. Relational operators

> more than the
>= greater or equal to
< Less than
<= Less than or equal to
== equal
!= not equal to

= Is the assignment operator;
== is the relational operator;
the result of the relational operator must be of Boolean type.

public class OperatorTest
{
    
    
	public static void main(String[] args){
    
    
		int a=10;
		int b=10;

		System.out.println(a>b); //false
		System.out.println(a>=b); //ture
		System.out.println(a<b); //false
		System.out.println(a<=b); //true
		System.out.println(a==b); //true
		System.out.println(a!=b); //false
	}
}

6. Logical operators

Logical Operators Explanation
& Logical AND (the operators on both sides are true, the result is true)
Logical negation (negation, !false is true, !true is false, this is a unary operator)
^ Logical exclusive OR (as long as the operators on both sides are different, the result is true)
&& Short-circuit AND (the execution result of the first expression is false, a short-circuit AND will occur)

| Logical OR (As long as one of the operators on both sides is true, the result is true)
|| Short-circuit OR (The execution result of the first expression is true, short-circuit OR will occur)

1 The logical operator requires that the operators on both sides are of Boolean type, and the final operation result of the logical operator is also a Boolean type.

2 The short-circuit and sum logic is the same as the final operation result, except that there is a short-circuit phenomenon.
3 The final operation result of short-circuit OR and logical OR is the same, but short-circuit or short-circuit phenomenon exists.

** 逻辑与和短路与的区别** 
public class Test01
{
    
    
	public static void main(String[] args){
    
    
		int x=10;
		int y=8;
		System.out.println(x < y& ++x < y); //进行了++x的操作
		System.out.println(x); //x=11
	}
}

public class Test02
{
    
    
	public static void main(String[] args){
    
    
		int x=10;
		int y=8
		System.out.println(x < y& ++x < y); //因为x<y是false,直接短路,后面的操作就不在执行
		System.out.println(x); //x=10
		}
}

From a certain perspective, short-circuit and smarter. Since the following expression may not be executed, the execution efficiency is higher. This method is used more in actual development. Short-circuit AND is used more than logical AND. Short circuit is more commonly used.

However, in some special logic services, all operators on both sides of the operator must be executed. In this case, logical AND must be used instead of short-circuit AND. Using short-circuit AND may cause the expression on the right side to not be executed.

Guess you like

Origin blog.csdn.net/qq2632246528/article/details/112465397