Basic concepts [operators such as arithmetic, relation, logic, bit, string, condition, priority] (3) - comprehensive detailed explanation (learning summary --- from entry to deepening)

Article directory

operator 

arithmetic operator

Assignment and its spread assignment operator

 relational operator

Logical Operators

bitwise operator

 string concatenation

conditional operator

 The problem with operator precedence

Data Type Conversion

automatic type conversion

cast

Scanner handles keyboard input


operator 

The basic purpose of computers is to perform mathematical operations, and Java provides a rich set of operators to manipulate variables.
 

Arithmetic Operators (Binary Operators) +,-,*,/,%
Arithmetic Operators (Unary Operators) ++,--
assignment operator =
spread operator +=,-=,*=,/=
relational operator >,<,>=,<=,==,!= instanceof
Logical Operators  &&,||,!,^
bitwise operator &,|,^,~ , >>,<<
conditional operator ? :
string concatenation +

arithmetic operator

 1. +, -, *, /, % are binary operators. % is the modulo operator , which is what we often call the remainder operation.
2. Among arithmetic operators, ++ (increment) and -- (decrement) are unary operators.

Operation rules of binary operators:
        Integer operations:
                 If one of the two operands is long , the result is also long .
                 When there is no long , the result is int . Even if the operands are all short or byte , the result is int .
        Floating-point arithmetic:
                 If one of the two operands is a double, the result is a double .
                 The result is a float only if both operands are floats.
        Modulo operation:
                 Its operands can be floating point numbers, and integers are generally used, and the result is "remainder", the sign of "remainder" is the same as the operation on the left, such as: 7%3=1, -7%3=-1, 7% -3=1 .

 [Example] Unary operators ++ and --

int a = 3;
int b = a++; //执行完后,b=3。先给b赋值,再自增。
System.out.println("a="+a+"\nb="+b);
a = 3;
b = ++a; //执行完后,b=4。a先自增,再给b赋值
System.out.println("a="+a+"\nb="+b);

Run the program, and the execution result is shown in Figure 2-3.
 

 Running effect diagram

Assignment and its spread assignment operator

operator Usage example equivalent expression
+=  a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b  a = a/b
%= a %= b  a = a%b

[Example 2-21] Spread operator
 

int a=3;
int b=4;
a+=b;//相当于a=a+b;
System.out.println("a="+a+"\nb="+b);
a=3;
a*=b+3;//相当于a=a*(b+3)
System.out.println("a="+a+"\nb="+b);

 Figure 2-4 Example 2-21 running effect diagram

 relational operator

Relational operators are used to perform comparison operations. The result of a relational operation is a boolean value: true/false ;

operator meaning example
== equal a==b
!=  not equal to  a!=b
less than a<b
> more than the a>b
>= greater than or equal to  a>=b
<= less than or equal to a<=b

Note
= is an assignment operator , and the real operator to judge whether two operands are equal is == .
==, != are all (basic and reference) data types can be used.
>, >=, <, <= are only for numeric types ( byte/short/int/long, float/double and char ).

Logical Operators
 

 The operands and results of logical operations are boolean values.

operator operator illustrate
and & As long as one is false, then false
short circuit with && As long as one is false, then false
or  | As long as one is true, then true
short circuit or || As long as one is true, then true
No reconciliation
XOR Same as false, different as true

Short-circuit and short-circuit or short-circuit. Computing from left to right, if the value of the logical expression can be determined only through the operand on the left of the operator
, the operand on the right of the operator will not continue to be calculated, improving efficiency.
 

[Example 2-22] Short-circuit AND and logic AND

//1>2 的结果为 false,那么整个表达式的结果即为 false,将不再计算 2>(3/0)
boolean c = 1>2 && 2>(3/0);
System.out.println(c);
//1>2 的结果为 false,那么整个表达式的结果即为 false,还要计算 2>(3/0),0 不能做除
数,//会输出异常信息
boolean d = 1>2 & 2>(3/0);
System.out.println(d);

bitwise operator

Bitwise operations refer to operations on binary bits.
 

bitwise operator  illustrate
~ reconciliation
& bitwise AND
| bitwise or
bitwise XOR
<< Left shift operator, shifting 1 bit to the left is equivalent to multiplying by 2
>> Right shift operator, shifting 1 bit to the right is equivalent to dividing by 2 and taking the quotient

【示例 2-23】左移运算和右移运算

int a = 3*2*2;
int b = 3<<2; //相当于:3*2*2;
int c = 12/2/2;
int d = 12>>2; //相当于 12/2/2;

新手雷区
 &和|既是逻辑运算符,也是位运算符。如果两侧操作数都是 boolean 类型,就作为逻辑运算符。如果两侧的操作数是整数类型,就是位运算符。
 不要把“^”当做数学运算“乘方”,是“位的异或”操作。

 字符串连接符

 “+”运算符两侧的操作数中只要有一个是字符串(String)类型,系统会自动将另一个操作数转换为字符串然后再进行连接。

【示例 2-24】连接符“+”
 

int a=12;
System.out.println("a="+a);//输出结果: a=12

条件运算符

String type = score<60?"不及格":"及格";

xboolean 类型表达式,先计算 x 的值,若为 true,则整个运算的结果为表达式 y
值,否则整个运算结果为表达式 z 的值。

【示例 2-25】条件运算符
 

int score = 80; 
int x = -100;
String type =score<60?"不及格":"及格"; 
int flag = x > 0 ? 1 : (x == 0 ? 0 : -1);
System.out.println("type= " + type);
System.out.println("flag= "+ flag);

运行结果如图 2-5 所示。
 

 运算符优先级的问题

运算符的优先级

优先级 运算符
1 ()  括号运算符
2 !、+(正号)、-(负号)  一元运算符
2 位逻辑运算符
2 ++、--  递增与递减运算符
3 *、/、% 算术运算符
4 +、- 算术运算符
5 <<、>>  位左移、右移运算符
6 >、>=、<、<= 关系运算符
7 ==、!= 关系运算符
8 & 位运算符、逻辑运算符
9 ^ 位运算符、逻辑运算符
10 | 位运算符、逻辑运算符
11 && 逻辑运算符
12 || 逻辑运算符
优先级  运算符
13 ? : 条件运算符
14 =、+=、-=、*=、/=、%= 赋值运算符、扩展运算符

小童建议
 大家不需要去刻意的记这些优先级,表达式里面优先使用小括号来组织!!
 逻辑与、逻辑或、逻辑非的优先级一定要熟悉!(逻辑非>逻辑与>逻辑或)。如:
a||b&&c 的运算结果是:a||(b&&c),而不是(a||b)&&c

数据类型的转换


我们讲解了八种基本数据类型,除了 boolean 类型之外的七种类型是可以自动转化的。

自动类型转换

自动类型转换指的是容量小的数据类型可以自动转换为容量大的数据类型。如图 2-6 所
示,黑色的实线表示无数据丢失的自动类型转换,而虚线表示在转换时可能会有精度的损失。

 可以将整型常量直接赋值给 byte、 short、 char 等类型变量,而不需要进行强制类型
转换,只要不超出其表数范围即可。

【示例 2-26】自动类型转换特例
 

short b = 12; //合法
short b = 1234567; //非法,1234567 超出了 short 的表数范围

强制类型转换


 

 (type) var

 强制类型转换,又称为造型(cast),用于强制转换数值的类型,可能损失精度。

【示例 2-27】强制类型转换
 

double x = 3.94; 
int nx = (int)x; //值为 3
char c = 'a';
int d = c+1;
System.out.println(nx);
System.out.println(d);
System.out.println((char)d);

运行结果如图 2-7 所示。
 

 当将一种类型强制转换成另一种类型,而又超出了目标类型的表数范围,就会被截断成
为一个完全不同的值。

【示例 2-28】强制类型转换特例

int x = 300;
byte bx = (byte)x; //值为 44

新手雷区
 不能在布尔类型和任何数值类型之间做强制类型转换

基本类型转化时常见错误和问题
 

 1. 操作比较大的数时,要留意是否溢出,尤其是整数操作时。
2. L 和 l 的问题:
        (1) 不要命名名字为 l 的变量,字母 l 容易和数字 1 混淆。
        (2) long 类型使用大写 L,不要用小写 l。

【示例】类型转换常见问题一

int money = 1000000000; //10亿
int years = 20;
//返回的total是负数,超过了int的范围
int total = money*years;
System.out.println("total="+total);
//返回的total仍然是负数。默认是int,因此结果会转成int值,再转成long。但是已经发生//了数据丢失
long total1 = money*years; 
System.out.println("total1="+total1);
//返回的total2正确:先将一个因子变成long,整个表达式发生提升。全部用long来计算。
long total2 = money*((long)years); 
System.out.println("total2="+total2);

运行结果如图所示。

 【示例】类型转换常见问题二

int l = 2; //分不清是L还是1,
long a = 23451l;//建议使用大写L
System.out.println(l+1);

Scanner 处理键盘输入

 Scanner 让程序和用户通过键盘交互

【示例】使用 Scanner 获取键盘输入

import java.util.Scanner;
public class Welcome2 {
        public static void main(String[ ] args) {
        Scanner scanner = new Scanner(System.in);
        // 将输入的一行付给 string1
        String string1 = scanner.nextLine();
        // 将输入单词到第一个空白符为止的字符串付给 string2
        String string2 = scanner.next();
        // 将输入的数字赋值给变量
        int a = scanner.nextInt();
        System.out.println("-----录入的信息如下-------");
        System.out.println(string1);
        System.out.println(string2);
        System.out.println(a * 10);
    }
}

运行结果如图 2-9 所示。

 图 2-9 示例 2-31 运行效果图

Guess you like

Origin blog.csdn.net/m0_58719994/article/details/132410988