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

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

《迪哥Java教程》#12 Java里的变量

Java variables tutorial video - 012

我们需要在程序中存储值 这些值可以随着程序的输入
We need to store values in our programs. These values can change based on the
而改变 由于值是变量
input from the program. And since the values can change
所以需要一个符号去表示它们 我们称之为“变量”
we need a symbol to represent them. We call that symbol a variable.
本节课是我要为你们演示的Java变量教程
So what I wanna show you with this Java variables tutorial,
它是介绍程序中的变量是什么并且如何使用的教程
is what are variables in programming and how to use variables in a Java program.
本节课 我们会学习到什么是变量
In this lesson, we’re going to look at
怎样创建它们 其实我们已经演示过了但是
what is a variable, and how do we create them. We’ve done this before but
没有强调过 同时我们会介绍一个非常重要的概念
we’ve never called it out. We’lll also introduce a very important concept the
这节课里它会频繁的出现
you’ll see often in this course,
不可变值 我们也会介绍常量
immutability. We’ll also look at constants,
以及它与变量和不可变变量的区别 最后
how they differ from variables and immutable variables. And finally
我们会学习转换变量类型 那么让我们开始
we’ll learn about casting variables to other types. So let’s start with
学习变量 我们已经见识过了变量
what is a variable. We’ve already seen variables
虽然我们并没有通过名字调用它们 当我们定义基本数据类型时
although we weren’t calling them by that name. When we define primitive values we
总是用类型 名称 值
always use the pattern
这种模式 名字就是Java中的变量
type, name, and value. The name is a variable in Java.
就像代数中一样 变量是值的占位符
Just like in algebra, a variable is a placeholder for the value.
本例中X是变量 看起来我们
In this case X is our variable. Just looking at it
知道X代表1
we know X represents one. If
如果1加X是43 我们会知道
1 + X was 43, we’d know that X
X表示42 你可以把一个变量当成是一个邮箱
represented 42. You can think of a variable as a mailbox.
假设一个邮箱只存档一个邮件 每个邮箱有一个编号
Pretend a mailbox can hold one piece a mail. Every box has a number.
Java中这个邮箱的地址被称为引用
In Java, a mailbox address is called the reference.
基本上很少需要你知道引用的实际值
You’ll rarely ever need to know the actual reference value.
这是因为通常我们提供一个变量名以便访问 你必须知道你所创建的
That’s because we give variables a name. You just need to know the name of the
变量的名字
variable you create.
一个变量可以被创建来处理任何基本类型
A variable can be created to hold any primitive. That isn’t to say
“变量”可以“持有任何基本类型” 当你定义某一类型的变量
“a variable” can “hold any primitive”. When you define the variable with a type,
就意味着在程序的生命周期中它都是那个类型
that means but the life for the program variable is of
所以一旦创建int
that type. So once created an int,
它就一直是int 你可以创建一个持有其他引用的变量
it is always an int. You can have a variable hold other references.
当我们介绍到Java的类型时会介绍到这点
We’ll cover that later on when we cover other Java types. Here
这里我们把integer变量起名为i double变量
we have an integer named ‘i’, a double
命名为d 布尔值命名为b
variable named ‘d’, and a boolean variable named ‘b’.
所以变量声明的第一部分是类型
So the first part of any variable declaration is the type.
本例中 int即类型
In this case, int is the type.
符号名称代表变量 现在
The name of symbol is for the variable. Now
这个i不是一个合适的名称 你需要给变量起个描述性的名字
‘i’ is not a very good name. You want to give your variables a descriptive name,
它能明确的
so it’s clear
指出这个值表示什么 大部分的IDE可以自动完成变量名
what the value represents. Most IDEs auto complete your variable names,
所以你不需要用短名称来保存类型
so you’re not saving any typing by using short names. So
所以我的变量i可能的意思是组件的个数或者天数
my ‘i’ variable here really probably should be something like countOfWidgets numberOfDays
或者
or
我的特别号码 只要是对你是清晰明确的就好
mySpecialNumber. Just something that’s clear and obvious to you.
如你在上节课上看到的 你可以初始化变量并赋值
As you saw in previous lessons, you can initialize your variable to a value
像这样
like this.
这是可选的 你可以稍后再赋值
This is optional. You can provide a value later,
但你应该在变量初始化以后再使用它
but you should never use a variable until you know that you have initialized it.
大部分场景下 变量会被自动初始化为零
For most cases, a variable will be initialized to 0
某些场景稍后会介绍到
automatically. In some cases which will cover later
在初始化前使用变量是错误的
it’s an error to use the variable before initializing it.
如果创建变量时你已经知道了它的值 可以直接赋值
If you know the value when you’re creating your variable, go ahead and add it.
你也可以把另一个变量的值赋值给一个变量
You can also set a variable to the value of another variable.
就像这样 好的
That looks like this. Ok
另外一件事 如果你知道一个变量在它的生命周期内永远都不会变化
Another thing to do is, if you know your variable will never change will life of the variable,
你要把它修饰为“final”的 就像游戏谁会成为百万富翁一样
you want to make it ‘final’. It’s like the game show Who Wants to Be a Millionaire.
当你增加了“final”这个关键字 就说明这是你定义的最终的值
When you add the keyword ‘final’ you’re saying this is your final value.
永远不会改变 当一个变量不能改变
It’s not going to change. When a variable can’t change,
被称为不可变(immutable) 你要记住这个词
it said to be ‘immutable’. You’ll want to remember that word.
本课中会频繁出现 不可变意味着
It’s gonna come often come up often in this class. Immutable means
值一旦被赋予就不能改变 正常的变量你可以按需修改
value cannot change once it’s set. Normal variables can change is often as you need.
所以你可能想知道
So you’re probably wondering
本例中值在哪 嗯当你创建一个不可变变量时
where’s the value in this example? Well you can define it like we have already.
你可以按以前的方式来定义变量 然后分配一个你想要的值
You can assign it to the value you want when you create the immutable variable.
在代码里 你可以先定义final变量 之后再来赋值
You can also define a final variable and the delay the variable assignment to later in the code.
这节课中 当你看到代码块 比如这里的蓝色部分
In this course when you see a block like the blue in here,
它意味着这还还有许多Java代码
that means there’s a lot more Java code here,
但选中的代码和主题无关
but the exact code is not relevant to the discussion.
这个例子中 我们会在变量定义许久以后
So in this example we’re setting the variable value
再给它赋值 但
much later after the definition. Still
只可以赋值一次 让我们在代码里试验下
you get only said it once. Let’s try this in code.
我们来定义一个变量命名为“handCount”
We’re going to define a variable called handCount.
我们有两只手 现在让我们来看看改变值以后发生了什么
We have two hands. Now look what happens when we try to change the value.
出现了一个不能给final变量
we get the error cannot assign a value
赋值的错误
to the final variable handCount.
现在我们也许不能提前知道手的数量
Now we may not know the number of hands in advance,
但一旦我们这么做了以后它就不能被修改了 我们可以修改代码来声明一个变量
but once we do it shouldn’t change. We can modify the code to declare the variable
然后我们在中间加入一个临时的变量
then we’ll add another dummy variable in between. Now we’ll assign
现在把hanCount赋值为2 有效
two to the handCount. And that works!
现在 在下一行再次修改handCount
Now I try to change handCount again on the next line
再次得到一个错误 你想要为每一个你创建的
will get the error again. You’ll want to use final
已知不会变化变量加上final
with every variable the you create that you know
这个方法可以让Java对代码进行优化
will not change. This allows Java to make some optimizations
可能让你的程序更快 有时有种情况是
and possibly make your application much faster. Sometimes there’s a case where
你知道变量永远不会发生变化
you know the variable will never ever change.
比如常量PI 它的值一直都是3.1415…… 在程序中永远
Like the constant pi. Its always going to be 3.1415… It’s not gonna change
不会变化 这时你应该把它用于常量
in your program ever. For this you’ll want to use a constant.
如果Java用关键字比如Const可能会更好
Now it would have been nice if Java used a keyword like
但他们没那么做
Const, but they didn’t. They
他们保留了这个关键字 但没有做任何事情
reserved the keyword but it doesn’t do anything.
不 声明一个常量你需要使用带上关键字final的static
No, to declare a constant you need to use the keyword ‘static’
static final 意味着
with the keyword ‘final’. ‘static final’ means
Java中的常量 static实际上意味着编译时赋值
constant in Java. ‘static’ really means set
但在本例中 认为static final
at compile-time, but in this case, think of ‘static final’
就是常量的意思 要注意的一件事
as meaning constant. One thing to note
当你创建一个static final的常量时 它必须在方法外边创建
when you create a ‘static final’ constant, it must be created outside of a method.
一会我们将更加详细的讲解 但
It’s something we’ll go into a little more detail later, but
你可以在方法内创建一个常量 如果你已经知道变量的值
you can create a constant inside a method. If you know your variable value
永远不会改变
will never change
告诉Java 它是个常量 与不可变变量的区别在于
ever, tell Java it’s a constant. The difference between an immutable variable
在编译时你知道它是个常量
and constant is, you know the value of the constant at compile-time.
而不可变变量直到运行时才知道
An immutable variable isn’t known until runtime.
我之前说 一旦变量类型确定就不能改变类型了
Now I said earlier, once you give a variable type you can’t change it.
因为Java是静态类型语言
That’s because Java is a statically typed language.
这意味着编译时值是有类型的 你无法修改
This means the value is typed at compile-time. You cannot change it,
如果你想按照不同类型的方式来处理变量 你不得不明确的告诉Java
and if you want it treated as a different type, you have to explicitly tell Java to
要这样做
do so.
静态类型是相对于动态类型来说的
The opposite of statically typed is dynamically typed.
也就是说对于动态类型 运行时变量的类型是可变的 例子语言是
That means a variable can change type at runtime. An example language would be
Perl 而Java选择成为静态语言的原因是
Perl. The reason Java chose to be statically typed
编译时你可能会发现许多愚蠢的错误
is because you can detect many stupid errors
我说愚蠢是因为你永远也不会想要改变
at compile-time. I say stupid because you never want to change the type of a
变量的类型
variable.
如果你改变类型 很可能会打印一个错误
If you change it, that’s likely a typo error.
有一些情况 你会想要修改类型
There are cases where you want to change the type.
比如 有一个short值 但你想把它用作
For example, you have a short but
long值 你可以告诉Java “嘿
you trying to use it somewhere that expects a long. You can tell Java “Hey!
假装这个integer值是一个long型的” 通过一种称之为类型转换的机制 当你要转换范围小一些的类型
pretend this integer is a long” using something called casting. When you go from a smaller type
到范围大一些类型时
to a bigger type
你做了一个扩大范围的转换 这对数字类型总是有效
you’re making a widening conversion. This always works for the number types
如果你是从箭头里的类型开始 向右移动改变类型
so if you start with the type somewhere in these arrows, and move to a type to the right
Java不会有任何问题 但相反方向是不同的
Java’s going to do it without complaint. The other direction is a different story.
比如 有一个long型 然后用short赋值
For example if you start with a long, and then try to put it into a short,
如果数字足够小 Java会报出一个编译错误这没关系
It doesn’t matter if the number is small enough Java is going to give you a compile error.
我们会看上节课里出现过的有损转换 从大到小
You get the lossy conversion error we saw in previous lessons. Going from bigger to smaller
被称为收窄转换 来看代码
is called a narrowing conversion. Let’s look at this in code.
创建一个long型值给它起名为“meaningOfLife”
We’ll create a long value and call it meaningOfLife.
赋值为42 再创建一个short类型
will assign the value 42 and we’ll create a short
起名为“shortVersion” 试着把值赋给
called shortVersion and try to give it the value
“meaningOfLife” Java会报错 没法编译
meaningOfLife. And Java complains. We can’t compile.
那该怎么办 当你知道那是可以的时候 你可以告诉Java
So what do we do? When you know it’s okay, you can tell Java
嘿 老兄!我知道我们在做什么 我们在类型转换呢
“Hey man! I know what I’m doing here”. We do that with a cast
要转换一个变量 我们把目标类型放到括号里
To cast a variable, we put the type we want in parentheses.
所以在这个例子中 我们把long转换成一个short
So in this case, we’re casting our long to a short.
让我们来修正代码
Let’s go fix our code.
这样编译是无法通过的 但是如果我们添加short类型的转换
So this won’t compile. But if we add cast a short
就可以了 我们在告诉Java
it works. We’re telling Java
一切尽在掌握中 也意味着Java会给予你
we’re in control. Now this also means Java is giving you enough
足够的绳子 让你去上吊 如果你把非常大的数字放到非常小的short里 让我们来看看发生了什么
rope to hang yourself. Let’s see what happens if we try to put a really big number into a short.
运行程序 得到了我们并不希望看到
And we run it and we get a result that
的结果 一般来说Java会接受我们已知在做什么时所做的事
really isn’t what we wanted. Basically Java accepted that we know what we’re doing.
然后尝试填满它 虽然不对 但取出了需要的比特出来
and tried to stuff it in there anyway. I didn’t work out. So we get the bits that did fit.
另一件事要告诉你们的是记住整型
Here’s one other thing to show you. Remember that integer types
单精度浮点类型的存储方式是不同的 即使它们都是32位的
and floating point types are stored differently. Even though they both into 32-bits
它们表示的是不同的 并且具有不同的取值范围
they are represented differently and have different ranges.
如果有一个大的integer
so if we take a big integer like
比如1234567890
1234567890
这是有效的 Java并不会有任何抗议
it works. Java won’t even protest.
那么 让我们打印下两者的不同 答案应该是零
So let’s print out the difference between the two. The answer should be zero.
我们得到的结果是-46 一个教训
and we have -46. Lesson learned.
注意integer和float的转换
Be careful with converting between integer and floating-point numbers.
那么 总结一下变量 我们看了如何定义变量
So that wraps up variables. We’ve looked at how to define them,
怎么让它们不可变 以及怎么让它们成为常量
how we can make them immutable, and how we can make them a constant.
嘿 感谢收看本视频 如果你想知道你学到了多少
Hey thanks for watching the video. There is a quick quiz for this on DeegeU.com
DeegeU.com上有个快速测试
if you’d like to gauge how much you learned.
如果你喜欢本系列视频 请为我点赞
If you like the videos you are seeing, please let me know by liking the video and
并点击YouTube上DeegeU频道的订阅按钮
hitting the subscribe button to 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.
官网首页上有一个投票
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 in the next video!

发表评论

译制信息
视频概述

介绍变量 常量 不可变变量以及它们的用法

听录译者

收集自网络

翻译译者

谷子

审核员

知易行难

视频来源

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

相关推荐