ADM-201 dump PMP dumps pdf SSCP exam materials CBAP exam sample questions

《迪哥Java教程》#6 Java里的Byte类型 – 译学馆
未登录,请登录后再发表信息
最新评论 (0)
播放视频

《迪哥Java教程》#6 Java里的Byte类型

Java Byte Tutorial - 006

计算机圈内有个笑话 说More than a bit意思是“有那么一点儿”
A computer byte is more than just a computer joke about something
而字节(byte)就不止是“那么一点儿”了
more than a bit.
在这节课中 我们将要深入了解第一个Java基本数据类型——字节类型
In this lesson we’ll take a deeper look at the first Java primitive-the byte.
这节课的目标就是理解
The goal for this lesson is to understand
什么是Java中的字节 有多大
what’s a byte in Java, how big is it
并且我们是如何在Java中创建字节的
and how do we create bytes in Java.
你可能认为这意味着 任何数都在2的8次方以内
You might think this means any number that is 2 to the power eight
你这样想也是情有可原的 因为这是惯性思维
you’d be forgiven because that’s rational thought.
然而 这些数字都是二进制补码
However these numbers are two’s complement numbers.
这意味着包括负数 你只能存储负的2的7次方
That means this includes the negative numbers.You can store only 2 the power 7
到正的2的7次方之间的数字
positive and negative digits.
0记为正数 这代表一个字节可以表示
0 counts as a positive number. This means a byte can represent a number from
-128到127之间的数字 超出这个范围的数字
-128 to 127. Anything outside that range
就没法放到一个字节里 要在Java中创建一个字节
it’s not gonna fit in a byte. To create a byte Java.
我们需要使用“byte”关键字 给它一个名字 并赋给它一个初始值
we use the keyword “byte”, give it a name, and optionally give it an initial value.
我们可以赋一个十进制的值 二进制的值或十六进制的值
We can give the value as a decimal value, a binary value,or hexadecimal value.
屏幕上绿色部分是关于如何定义字节类型的示例
The green examples are how you define a byte in Java.
而红色的语句是一个编译时会报错的示例
The red line is an example that will not compile.
因为128超出了一个字节的范围 如果你现在用的是Java7之前的版本
128 is too large for a byte. Now if you’re working with the
将不能使用二进制文字版本
version of Java before version 7, you can’t use the binary literal version.
你需要像屏幕中这样做(加上语句Byte.paraseByte) 我们可以直接使用二进制
You need to do something like this. We’ll use literals, because
是因为我们这节课是使用Java8
this course is using Java 8.
首先打开应用
So let’s go open up our application that we created before.
我们将添加语句来创建一些字节
And inside it we’ll add some lines to create some bytes.
这里没有新的东西 每个字节都赋值42
There’s no surprise to any of these. Each of the bytes creates the number 42.
第一个使用十进制
The first one uses a decimal literal,
第二个是二进制 第三个是十六进制
the second one uses the binary literal, and the third one is a hexadecimal literal.
然后运行 他们都打印出42
When we run it, they all print out 42.
它之所以打印出42的十进制
The reason why it prints out 42
而不是以二进制或者十六进制的形式输出
and not the binary form or hexadecimal
是因为我们没有格式化输出 只要求返回数字
form is that we’re not formatting our outputs. We’re just saying give us back the number,
无论我们最初怎么定义它的 这数字就是42
That number is 42 regardless of how we originally defined it.
我之前说过一个字节只能存储-127到127
Now I had said earlier that a byte can only go from
那么如果我们把超出这个范围的数字放进去会怎么样呢
-127 to 127. What happens if we try to put something
我们把这里的数字改为420
in bigger anyway. So if we change it to 420,
它立马报错了
it’ll immediately give us an error.
不可兼容类型:从int型转为byte型可能发生丢失
Incompatible types possible loss a conversion from int
这说明了420是int类型
to a byte. Basically it’s saying that 420’s type is an int
并且将420放入byte中就会报错
and is trying to stuff it in a byte and it won’t work. Even if we try to compile
即使我们编译它
this,
也会遇到相同的错误
you’ll get the same error when I compile. To get rid of the error
将数字改为42就能解决这个错误了
back to byte forty-two and
嘿 感谢观看这一期视频
we’re good. Hey! Thanks for watching the video.
如果你想测试你这节课学的怎么样
There is a quick quiz for this on DeegeU.com if you’d like to gauge how much you
你可以到DeegeU.com上完成一个小测试
learned.
如果你喜欢这一期的视频 请顶这个视频然后
If you like the videos you are seeing please let me know by liking the video and
点击YouTube DeegeU频道上的订阅按钮来让我知道
hitting the subscribe button for the DeegeU channel on YouTube. I’d
非常感谢 如果有任何疑问请在下方评论
really appreciate that. If you have concerns or questions please leave them
或者在DeegeU.com的评论区留言
in the comments below or on DeegeU.com.
在DeegeU.com的首页有一个投票活动
There’s a poll on the front page of DeegeU.com
可以让我知道下个视频中你想学习的内容是什么
so you can also let me know what topic is covered next.
感谢收看 下期再见
Thanks for watching and see you in the next video!

发表评论

译制信息
视频概述

迪哥Java让每一个人都学会编程 这一期讲的是byte类型的使用

听录译者

收集自网络

翻译译者

闷蛋(ಥ﹏ಥ)

审核员

知易行难

视频来源

https://www.youtube.com/watch?v=Ji4geHitwuA

相关推荐