未登录,请登录后再发表信息
最新评论 (0)
播放视频

《迪哥Java教程》#15 Java里的运算符2

The Java operators tutorial video (Part 2) - 015

上节课我们重点学习了数学运算符
In the last lesson, we focused mostly on math operators.
数据很重要
Numbers are important,
但我们需要构建更庞大更复杂的逻辑运算操作
but we need to construct larger and more complex logic operations,
比如 我的形状是不是一个圆形
like hey is my shape a circle
或者这颜色是不是红色
or is the color red?
我们还需要进行位操作
We also need to perform bit manipulation.
这就是我们这节课要讲的内容
And that is what we are going to cover in this lesson!
这节课的目标是让我们对Java运算符理解透彻
The goal for this lesson is to finish up our understanding of Java operators.
我们需要涵盖相等运算符
We need to cover the equality operators,
逻辑运算符
logical operators,
位运算符
bit operators.
最后 我们会看一看运算符的优先级
And then we’ll take a final look at operator precedence.
相等运算符和关系运算符是用来检查两个值是如何关联的
Equality and relational operators check to see how two values are related.
这些运算符都会返回一个布尔值
These operators all return a boolean value.
记住 布尔值要么是true 要么是false
Remember a boolean value is either true or false.
所以这些运算结果都是true或false
So each of these evaluates to either true or false.
我们使用两个等号“==”用来判断两个值是否相等
To determine if two values are equal, we use two equals symbols for equality.
它和赋值运算符不同
This is different than the assignment operator.
赋值运算符只有一个等号“=”
That uses only one equal sign,
我们常会犯这样的错误 把两个等号“==” 写成一个等号“=”
and it’s a common mistake to use one equal sign when you mean two.
幸运的是 Java会在你尝试编译时报错
Luckily Java will complain about it when you try to compile.
等我们写代码的时候我会给你们演示
I’ll show you that when we get to the code.
在当前例子中 isEqual会返回false
In this example, isEqual would return false.
我们还可以测试两个值是否不相等
We can also test if two values are not equal.
不相等通过使用“!=”来表示
That uses the exclamation point with the equals sign.
我们把它读作A不等于B
We read that as A is not equal to B.
这两个值不相等 测试会返回true
They are not equal, the test returns true.
“!=”和“==”是相反的
This is the opposite of the equality operator.
表达式“1!=2”会返回true
one not equal to two would return true.
我们用”>”和”<"来测试严格不等式
We use the greater than and less than symbols to test strict inequalities.
严格不等式的意思是
A strict inequality says,
A是否严格大于B
is A strictly greater than B.
答案只能是true或false
The answer is always a true or false.
如果A等于B 那么严格不等式的结果就是false
So if A = = B, then the strict inequality is false.
举个例子
For this example,
“1>2”的结果会是false
1 greater than 2 will be false,
而“1<2”的结果会是true
while 1 less than 2 will be true.
如果两个值都是1 那么1>1就是false
If both values were 1, greater than 1 is false.
非严格不等的意思是
The not so strict version is,
A是否大于等于B或者小于等于B
is A greater than or equal to B or is A less than or equal to B?
在这种情况下 相等也是返回true的
In this case, we’re allowing equals to return true as well.
所以如果A、B相等 那么返回true
If A equals B, then the operator returns true.
我们用”>=”、”<="来表示非严格不等运算符
We use an equals symbol with the greater than or less than symbol for these operators.
下面我们来看如何给相等和关系运算表达式赋布尔值
Lets look at assigning booleans to equality and relational operators.
如果我们令a=3
So if we set A equals to 3
b=4
and B equals to four,
我们来看各运算符的运算结果
lets see what happens for each operator.
“=”的结果是“false””!=”的结果是“true”
Equals is false.Not equals is true.
“>”的结果是”false” “>=”的结果是“false”
Greater than is false.Greater than or equal is false.
“<"的结果是"true""<="的结果是"true"
Less than is true, and less than or equal is true.
现在我们设a和b都是4 再运行一遍
Now lets set A and B equals to four and run it again.
在这个案例中”=”的结果是”true”
“!=”的结果是“false”
In this caseEquals is true.
Not equals is false.
“>”的结果是”false””>=”的结果是”true”
Greater than is false.Greater than or equal is true.
因为两个值是相等的 所以本次测试结果是”true”
This is because the two values are equal, so that counts as true in the test.
“<"的结果是"false" "<="的结果是”true"
Less than is false, and less than or equal is true.
现在看一下我前面讲的误用“=”
Now I said earlier that it was a common mistake to use a single equals.
让我们看看如果我删掉第一个测试中的一个“=” 会发生什么
Lets see what happens if I delete one of the equals signs in the first test.
它甚至都无法编译
It won’t even compile.
编辑器报错“int不能转化为boolean”
It complains with “int cannot be converted to a boolean”.
这里进行的是我们上节课讲的赋值链
That’s because it actually is trying to do the assignment chaining we saw in the last lesson.
把b的值赋给a 然后尝试把a的值赋给布尔值
It’s setting A equal to B, then trying to assign it to the boolean value.
a的值不是布尔值 所以会报错
The int is not a boolean, so we get an error.
接下来讲条件运算符
Next up are conditional operators.
条件运算符是在布尔值上执行逻辑运算
Conditional operators perform logical operations on boolean values.
你们可能知道这两个运算符是AND和OR
You may know these as AND and OR.
”&&“是我们要看的第一个运算符
And is the first one we’ll look at.
它读起来像是一个英语短句 “a and b”
It reads like an English statement. A and B.
如果a是”true” b也是”true”
If A is true and B is true,
“a&&b”结果为”true”
A and B is true.
如果其中一个是“false”
If either are false,
那么“a&&b”为”false”
then A and B is false.
a和b都必须是”true”时结果才为”true”
Both A and B must be true for the result to be true.
”||“读起来也像是一个英语短句 “a or b”
Or also reads like an English statement. A or B.
如果a是”true”或b是“true”
If A is true or if B is true,
那么“a||b”是”true”
then A or B is true.
当a和b都是”false” 的时候
If A and B are both false,
“a||b”才是“false”
then A or B is false.
只有a和b其中一个为“true”的时候 结果才是“true”
Only one, either A or B, needs to be true for the result to be true.
最后还有一元运算符
Finally there is one unary operator
一元运算符我们在上节课没有涉及到 我们称它为“NOT”
we didn’t cover in the last lesson. It’s called “not”.
所以如果b是“true“ “!b”就是”false”
So if B is true, not B is false.
如果b是”false” “!b”就是”true”
If B is false, not B is true.
基本上NOT就是把布尔值的符号给翻转了
Basically the not just flips the sign of the boolean value.
我们一般在布尔值的前面加上“!”
We use an exclamation point in front of a boolean value
或者在表达式前面加”!“
or expression for nots.
只要你看到“! ”的时候
When ever you see the exclamation point,
你就可以把它转换成“not”
you can substitute the word “not”.
下面来看看布尔运算符的使用
Lets look at the boolean operators.
假设a和b都是true
Assume A is true and B is true.
接下来将展示“AND” “OR”和“NOT”的运算结果
Here’s what AND, OR and NOT look like.
我们把b改为“false”
Now lets make B false
然后看看它们分别会有什么结果
and see what they look like.
布尔表达式可以把这些都串起来
Now the power with boolean expressions is we can chain several of these together.
我们也可以使用相等和关系表达式
We can also use equality and relational expressions.
所以假设a是“true” b是“4>3”
So assume A is true and B is 4 > 3.
让我们来试一下
Let’s play with that a bit.
注意这可能会有点长
Note these can get long.
先来看相等运算符
The equality operators will be evaluated first,
然后看“AND” 接着是“OR”
then &&, then ||.
你不该把它们这么串在一起
You really don’t want to string them together like this however.
很难一眼看出来哪个运算符优先
It’s not intuitive to know which operators have precedence.
要明确计算这些运算符的顺序
You want to be explicit about the order statements are evaluated.
应该用括号括起来
To do that, use parentheses.
我们仔细看这下个
We’ll look at this closer in a minute.
最后我们讲位运算符
Finally we have the bit operators.
位运算用于改变整数的单个二进制位
These operators are for changing the individual bits of a integer number.
记住整型包括byte short int long
Remember integers include bytes, shorts, int and longs.
我们要看的第一组位运算符是“按位运算符”
The first group of bit operators we’ll look at are the bitwise operators.
位运算符有“与”
The bitwise operators are for AND,
“或”和“异或”
OR and exclusive or (XOR).
两个int型值按位运算会返回int或long型结果
These take two integer types, and return an integer or a long.
这一点很重要
That’s important.
如果对两个byte型或short型进行按位运算
If you use these operations on two bytes, or two shorts,
结果会是int型
the result will be an int.
所有按位运算的结果都是int或long型
The result is always an int or a long,
除非你把结果向下转换了
unless you cast it back down.
按位运算符和我们前面学的逻辑运算符类似
The bitwise operators are similar to the boolean operators we saw before.
实际上你们可以在按位运算中把二进制位上的
In fact, you can mentally think about these operations as 1 is true, and 0 is false
1想成true 把0想成false
as you work through each bit in the number.
我们把一个二进制位变为1我们称为“设置” 变为0就是“清除”
We call a bit that has a 1 as set, and a 0 as clear.
按位操作用一个“&”符号表示“与”
The bitwise operators use a single ampersand for
一个”|”表示“或”
and, a single bar for or,
一个“^”表示新的运算叫做“异或”
and a tilde for a new operation called exclusive or.
我们假设有两个二进制位
Lets assume we have two bits
进行按位与操作
we want to AND.
我们假设第一位是1 第二个位也是1
We’re saying if the first bit is 1 and the second bit is 1
那么按位与结果是1
then the result from an AND is 1.
如果是一个1和一个0 按位的结果是0
If we had a 1 and a 0, the result is 0.
两个都是1时 位与结果为1
ANDs want both to be a 1,
否则结果为0 可以把这个运算扩展到二进制数的每一个位上
otherwise it’s a zero. We can extend this for every bit in an byte.
如果a和b各自的8个二进制位按位与计算
If we AND each of the 8 bits in a and b,
会得到下面的结果
we’ll get the following result.
只有a和b的对应的位上都是1结果才是1
We get set bits only in the positions where the a and b bits were also set.
按位或不一样
ORs are different.
对于按位或运算
For an OR we are saying
如果第一个位是1或者第二位是1
if the first bit is a one or the second bit is a one,
那么按位或的结果是1 如果两位都是1
then the result of the OR is a 1.If they are both a 1
结果还是1
it’s still one
按位或操作 除非两位都是0
For ORs, the answer is a 1,
否则结果就是1
unless both initial bits are a 0.
同样的我们可以把这个运算用到整数的所有位上
Again this can be extended to a whole integer.
这里如果我们把这8位都进行按位或运算 会得到下面的结果
Here if we OR each of the 8 bits in a and b, we’ll get the following result.
a和b的对应位上只要有一个1 结果就是1
We get set bits only in positions where there is a set bit in a or b.
异或
Exclusive or
拼作XOR 使用“^”表示
is spelled XOR and uses the carrot symbol.
异或的运算 除了两位都是1的结果不同 其他都和或一样
This behaves like an or, except if both are a 1.
异或运算时如果两位都是1 结果是0
In this case if both are a 1, the result is 0.
它像是高端的位运算 仅此一个
It’s like the Highlander bit operator. There can be only one.
这是独有的 我就是这么记住的
It’s exclusive. That’s how I remember it.
这里如果我们给a和b的8位分别进行异或运算
Here if we XOR each of the 8 bits in a and b,
我们会得到下面的结果
we’ll get the following result.
只有在a或b为1时异或的结果才为1
We get set bits only in the positions where a bit was set in only a or only b
而不是a和b全部为1
but not both.
有一个我们在上节课没有讲到的一元操作是按位取反操作not
One unary operation we skipped in the last lesson is the bitwise unary not.
我跳过了 因为我想把所有的位操作符放在一起来讲
We skipped it, because I wanted to keep all the bit manipulating operations together.
not操作符会将整数的所有位翻转
The unary not flips all the bits in your integer.
如果一位是1 它就会变成0 反之亦然
So if the bit is 1 it becomes 0, and vice versa.
如果我们对a的8位数都取反
Here if we NOT each of the 8 bits in a,
所有的位 都会翻转
all the bits get flipped.
1变成0 0变成1
Set bits become cleared bits, and cleared bits become set bits.
我们看一些执行取反操作的代码 希望能让你们对这个问题更明白
Lets look at some code for this, and hopefully it will become clear.
我们的操作对象是整型 我让第一个数的二进制位都是1
We’ll work with integers. We’ll set our first number to all 1s.
第二个数的二进制位是一半1 一半0
The second number will be half ones, and half zeros.
如果我们对它们执行与操作
If we AND the numbers,
结果是 一半1 一半0
we’ll get back a number with half ones, half zeros.
基本可以说是返回了相同的数
Basically we got the same number back.
如果我们对它们执行或操作 那么返回的结果的二进制位都是1
If we OR the numbers, we’ll get all the bits set.
这是因为第一个数的所有的二进制位都是1
This is because all the bits are set in the first number.
异或操作相对来说会更有意思一点
Things are a bit more interesting for XOR.
当我们运行这段代码的时候
When we run this,
我们会得到和第二个数字相反的结果
we get the reverse of the second number.
看一下如果第一个数是42
Look at what happens if we set the first number to 42,
第二个数是24 会发生什么呢
and the second number to 24.
对它们当做异或操作 结果是50
We XOR the numbers and get 50.
我们再次对50和第二个数做异或
We then XOR 50 with the second number again,
返回的结果是42
we get back to 42.
你可以用任意两个数做异或
You can do this with any two numbers.
为了完整性 这里显示的是取反运算符是如何翻转二进制位的
And for completeness, here’s how we flip the bits using the not.
这些运算符是用来移动二进制位的
These operators are used for bit shifts.
那个是什么意思呢
What does that mean?
它的意思是 我们根据箭头的方向
It means we move all the bits to the left or the right
将所有的二进制位都像左或像右移
depending on the direction of the arrows.
这些是用来进行二进制位转换的运算符 符号的方向代表这二进制位移动的方向
These are the operators used for bit shifts.The direction the symbols point determine the direction of the shift.
如果是像右移
If we are shifting right,
这个数中最重要的那一位决定着转换进来的是1还是0
the most significant digit in the number determines what is shifted in.
如果最高位是1 那么转换进来的也是1
So if the highest digit is 1, we shift 1 into the number.
如果是0 那么换进来的就是0
If its a zero, we shift zero.
原因是我们要保持这个数字的正负形
The reason for this, is we are keeping the number either positive or negative.
就想你们记的那样 最高位表述的是数字的正负
As you remember, the highest bit determines the sign of the number.
这是在计算机如何存储负数那一课中 学到的
We saw that in the how computers store negative numbers lesson.
有的时候 我们总是想要将0换进来
Sometimes we always want to shift a zero into the number,
为了达到这个目的 我们可以使用无符号右移
so for that we use the unsigned shift right.
它由三个大写符号组成”>>>”
That has three greater than symbols.
用这个运算符 换进来的只会是0
A zero is always shifted in.
我们再看一下它们的代码
Again, let’s play with these in code.
我们取一个数 将它左移
We’ll take a number, and shift it to the left.
这里要注意的是 结果恰好是原来的数的两倍
The cool thing to note here is, the result is just the previous number times two.
当你执行左移运算的时候 每移动一位 都相当于乘2
So when you shift left, each shift is a power of two.
如果我们移动两位
If we shifted by 2,
数就变成原来的4倍
the number is multiplied by four.
右移运算恰恰和左移相反
Shifting right is just going in the opposite direction.
我们分别要用一个正数和一个负数来举例
Let’s do one negative number, and one positive number.
你们看 这个运算不会影响数字的正负
As you can see, the shifting keeps the sign.
如果我们再次执行无符号位移
If we do it again with the unsigned shift,
你们会看到负数变成了正数
you see the sign changes for the negative number.
那是因为我们将0换进来 让它变成了正数
That’s because we shifted a zero in, making it positive.
如果想学更多的位运算
If you want to see more bit operations,
可以了解一下计算字节中的比特位的4中方法
check out 4 ways to count bits in a byte.
尽管你要编写的程序类型才是决定
Manipulating bits is a common interview question,
你使用比特位的频率的关键
although the types of programs you work on
不过面试中还是会经常考比特位的操作
will determine how often you work with bits.
最后我们要讨论的是运算符的优先级
The last thing we wanted to discuss was operator precedence.
我们已经学过了所有的java运算符了
We’ve covered all the Java operators individually.
你们可以将许多的单个的操作符链接在一起 形成更大更复杂的操作符
You can chain many of these into larger more complex operations.
要知道的是 java会以一个独特的顺序来执行运算符
The catch is Java will perform operations in a specific order.
并不完全是从左到右
It’s not always left to right.
这是运算符优先级的列表
Here’s the list of operator precedence.
这张表要这么看
The way to read this chart is,
java会从这个表上面的运算符开始向下执行
Java will start from the top, and work it’s way down.
如果运算符的优先级相同
If operators are on the same line,
就会从左到右依次执行
it will work from left to right.
再次提示 应该用括号来让运算的优先级变得更加明确
Again, you want to use parentheses to make it explicit.
不用括号 直接将多个运算符链接成长的运算符 不是一个好的方法
It’s not a good practice to chain long operations without parentheses.
如果你的表达式真的很长
If your expression is really long,
你可能想要把它像这样断成一个一个的短的表达式
you might want to break it up into smaller statements like this.
这样 当你在计算的时候就不会含糊不清了
That way there’s no ambiguity in what you’re trying to calculate.
括号里的永远是第一个执行
Parentheses always goes first.
括号在不同的地方就是不同的操作符了
And that wraps it up for the different operators.
现在我们有能力对数字进行比特位级别的操作了
We now have the power to control our numbers down to the bit level.
我们可以用逻辑运算符组成复杂的布尔表达式
We can form complex boolean expressions using the logical operators,
知道如何去测试是否相等
and we know how to test for equality.
现在可以开始使用这些运算符写更大的程序了
We can now start using these operators to build bigger programs.
确实有用的程序
Programs that actually do something.
自己试试写个java程序来运行这些操作符吧
You’ll want to play with these in your own Java programs.
看到代码执行的结果后 会更容易理解这些操作符
It’s easier to understand when you see it happen in code.
如果还有任何疑问 请在下方的评论处或者在DeegeU.com网站上留言
If you have any questions, leave them in the comments below or on DeegeU.com.
下节课再见
See you in the next lesson!
非常感谢大家观看这个视频
Hey thanks for watching the video.
在DeegeU.com网站上有一个小调查
There is a quick quiz for this on DeegeU.com
如果你想要评估一下自己学会了多少 可以做一下
if you’d like to gauge how much you learned.
如果你喜欢现在看的视频的话
If you like the videos are seeing,
请点击喜欢这个视频或者在youtube上进入 DeegeU频道点击订阅 让我知道你们喜欢
please let me know by liking the video and hitting the subscribe button to the DeegeU channel on YouTube.
我会非常感激
I’d really appreciate that.
如有任何不解或者疑问 请在下方评论处或者DeegeU.com网站留言
If you have concerns or questions please leave them in the comments below or on DeegeU.com.
DeegeU.com网站的第一页有一个调查问卷
There’s a poll on the front page of DeegeU.com
你们可以填一下问卷让我知道下次讲什么
so you can let me know what topic is covered next.
非常感谢大家的观看 下次见 拜拜
Thanks for watching and see you next video.

发表评论

译制信息
视频概述

本视频讲了java运算符的使用已经优先级

听录译者

收集自网络

翻译译者

ID385818

审核员

审核员1024

视频来源

https://www.youtube.com/watch?v=QfxolfMq-_I

相关推荐