hi my name is mosh and I'm gonna be your instructor in this Java course in this 嗨，我叫莫什，我将成为您这门Java课程的讲师
course you're gonna learn everything you need to get started programming in Java 当然，您将学到开始使用Java进行编程所需的一切
we'll start off by installing all the necessary tools to build Java 我们将首先安装所有必要的工具来构建Java
applications then you're gonna learn about the basics of Java you'll learn 应用程序，然后您将学习将要学习的Java基础知识
how Java code gets executed you'll learn how to build simple algorithms and 如何执行Java代码，您将学习如何构建简单的算法，以及
throughout this course I'm gonna share with you lots of tips and shortcuts from 在整个课程中，我将与您分享许多技巧和捷径
my years of experience I'll teach you how to write good code like a 我多年的经验，我会教你如何像
professional developer so we'll end up watching this course you will have a 专业的开发人员，因此我们将最终观看本课程，您将获得一个
solid foundation in Java and be ready to learn about advanced Java features I've 扎实的Java基础，并准备学习我拥有的高级Java功能
designed this course for anyone who wants to learn Java if you're a beginner 如果您是初学者，则为想要学习Java的任何人设计了本课程
don't worry I'll make Java super simple and hold your hands through this entire 不用担心，我将使Java变得超级简单，并在整个过程中全力以赴
course you're not too old or too young you'll write your first Java program in 当然，您年龄不要太小或太小，您将用编写第一个Java程序。
minutes my name is mosh I'm a software engineer 分钟，我叫莫什，我是软件工程师
with two decades of experience and I've taught over 3 million people how to code 拥有二十年的经验，我已经教了超过300万人如何编码
and how to become professional software engineers I have a coding school at code 以及如何成为专业的软件工程师我在代码学院学习
with mass comm where you can find plenty of courses that help you take her coding 与大众交流，在那里您可以找到很多课程来帮助您进行编码
skills to the next level I hope you'll stick around and learn this beautiful 技能提升到新的水平，我希望您能继续学习并学习这一美丽
and powerful programming language and now award from this video sponsor as 和强大的编程语言，现在由该视频赞助商授予
someone who runs an online business I cannot stress enough the importance of 经营在线业务的人我没有足够强调
staying safe online which is why I was so excited when dashlane reached out to 保持在线安全，这就是为什么dashlane伸出援手让我如此兴奋的原因
me if you don't know - Lane is the password manager and VPN recommended by 我，如果您不知道-Lane是密码管理器和VPN推荐的
Apple and Google and it's a fantastic safeguard for keeping your information 苹果和谷歌，这是保留您的信息的绝佳保障
secure it's completely free to use for your first device so head over to - Ling 确保您的第一台设备完全免费使用，因此请转到-Ling
comm / marché - give it a go if you want to upgrade to the premium to get VPN or comm /marché-如果您想升级到高级版以获取VPN或
dark web monitoring you can use the promo code mosh to get 10% off sign up 暗网监控，您可以使用促销代码mosh享受10％的折扣
for - 9 today and keep yourself safe online now back to the course 的-今天9点，让自己安全上网现在回到课程
in this java tutorial we're going to download and install the necessary tools 在本Java教程中，我们将下载并安装必要的工具
to build java applications so open up your browser and search for jdk download 构建Java应用程序，因此打开浏览器并搜索jdk下载
jdk is short for Java development kit and it's basically a software jdk是Java开发工具包的缩写，基本上是一种软件
development environment for building Java applications it has a compiler it 用于构建Java应用程序的开发环境，它具有编译器
has a bunch of code that we can reuse it has a Java Runtime environment at a 有一堆我们可以重复使用的代码，在
bunch of other stuff so over here you can see this page on Oracle com Java se 一堆其他东西，因此在这里您可以在Oracle com Java se上看到此页面
which is short for Java standard edition click on this now over here click on Java标准版的缩写，现在单击此处，请单击
this icon now on this page we can see Java development kit for various 这个图标现在在此页面上，我们可以看到各种Java开发工具包
platforms like Linux Mac OS and Windows here I'm on a Mac so I'm gonna download 像Linux Mac OS和Windows这样的平台我在Mac上，所以我要下载
this dmg file over here now before we do this first we need to accept the license 这个dmg文件现在在这里，在我们首先执行此操作之前，我们需要接受许可
agreement all right now let's download the dmg let me open this we're gonna say 现在同意所有协议，让我们下载dmg，让我打开它，我们要说
this package let's double click this and here we see this installation wizard 这个包让我们双击它，在这里我们看到这个安装向导
it's super easy just click continue and install you have to enter your 这非常简单，只需单击“继续”并安装，您必须输入
computer's password and then alright done beautiful so we can move this to 电脑的密码，然后一切正常，所以我们可以将其移至
trash now the next piece of software we need is a code editor there are so many 现在我们需要的下一个软件是代码编辑器
cool editors for building Java applications the popular ones are 很棒的用于构建Java应用程序的编辑器
NetBeans Eclipse and IntelliJ in this Java course I'm gonna use IntelliJ but NetBeans Eclipse和IntelliJ在本Java课程中，我将使用IntelliJ，但是
if you have a favorite editor feel free to use that to take this course that's 如果您有喜欢的编辑，可以随时使用它来学习本课程
perfectly fine so let's search for IntelliJ download all right you can see 很好，让我们搜索IntelliJ下载，就可以看到
download IntelliJ IDEA click on this link over here download the community 下载IntelliJ IDEA单击此处的链接下载社区
edition which is absolutely free and it's more than enough for this course so 这是完全免费的版本，对于本课程来说已经足够了
download all right now let's drag and drop this onto the Applications folder 立即下载所有内容，让我们将其拖放到Applications文件夹中
beautiful alright we've installed all the necessary tools to build Java 不错，我们已经安装了所有必要的工具来构建Java
applications so next we're gonna look at the anatomy of a Java program 应用程序，所以接下来我们要看一下Java程序的剖析
in this java tutorial we're gonna look at the anatomy of java programs the 在本Java教程中，我们将研究Java程序的剖析
smallest building block in java programs are functions Java程序中最小的构建块是函数
if function is a block of code that performs a task as a metaphor think of 如果函数是一个代码块，可以隐喻的方式执行任务
the buttons on the remote control of your TV each button performs a task 电视遥控器上的按钮，每个按钮执行一项任务
functions in programming languages are exactly the same for example we can have 编程语言中的函数完全相同，例如我们可以拥有
a function for sending emails to people we can have a function for converting 向人们发送电子邮件的功能我们可以具有转换功能
someone's weight in pounds to kilograms we can have a function for validating 某人的体重（以磅为单位），我们可以使用一个验证功能
users input and so on now let's see how we can code a function in Java we start 用户输入等等，现在让我们看看如何开始使用Java编写函数
by specifying the return type of that function some functions return a value 通过指定该函数的返回类型，某些函数返回一个值
like a number at day time and so on other functions don't return anything so 像白天的数字，等等，其他函数不会返回任何东西
the return type of this functions is void void is a reserved keyword in Java 此函数的返回类型为void void是Java中的保留关键字
and that's why I've coded that in blue here now after the return type we have 这就是为什么在返回类型之后，我现在在这里用蓝色编码的原因
the name of our function so here we should give our function a proper 函数的名称，所以在这里我们应该给函数一个适当的名称
descriptive name like send email this name clearly identifies the purpose of 描述性名称，例如发送电子邮件，此名称清楚地标识了
this function okay now after the name we have a pair of parentheses and inside 这个功能好了，现在名字后面有一对圆括号
these parentheses we add the parameters for this function we use these 这些括号中，我们为此函数添加了参数
parameters to pass values to our function for example our send email 参数以将值传递给我们的函数，例如我们的发送电子邮件
function should have parameters like who is the receiver what is the subject of 函数应该具有参数，例如谁是接收者，对象是什么
this email what is the content of this email and so on now in this tutorial 该电子邮件的内容是什么，以此类推，现在在本教程中
we're not gonna worry about parameters we'll look at them in the future 我们不用担心参数，以后我们会再看它们
now after the parentheses we had a pair of curly braces and inside these braces 现在在括号后面，我们有一对花括号，在这些括号内
we write the actual Java code now one thing I want you to pay attention to 我们现在编写实际的Java代码，我希望您注意一件事
here is that in Java we put the left brace on the same line where we define 这是在Java中，我们将左括号放在我们定义的同一行上
our function in other programming languages like C sharp it's more 我们在其他编程语言（例如C Sharp）中的功能
conventional to put the left brace on a new line but we don't do that in Java so 常规将左括号放在新行上，但我们在Java中不这样做，因此
we put the left brace on the same line where we define our function now every 我们将左花括号放在同一行，我们现在定义函数
Java program should have at least one function and that function is called Java程序应至少具有一个功能，并且该功能称为
main so main is the entry point to our programs whenever we execute a Java main所以main是每当我们执行Java时程序的入口点
program the main function gets called and the code inside this 程序调用主函数，并在其中的代码
function gets executed okay now these functions don't exist on their own they 函数现在可以执行了，这些函数不再单独存在
should always belong to a class so a class is a container for one or more 应该始终属于一个类，因此一个类是一个或多个容器
related functions basically we use these classes to organize our code just like 相关功能，基本上，我们使用这些类来组织代码，就像
how products are organized in a supermarket in a supermarket we have 我们在超市中如何在超市组织产品
various sections like vegetables fruits cleaning products and so on each section 各个部分，例如蔬菜水果清洁产品等等
contains related products by the same token a class in java contains related 通过相同的令牌包含相关产品java中的类包含相关产品
functions now every Java program should have at least one class that contains 函数现在每个Java程序都应该至少包含一个包含以下内容的类
the main function can you guess the name of that class its main so this is how we main函数，您可以猜出该类的名称吗？main这就是我们
define a class in Java we start with a class keyword then we give our class a 在Java中定义一个类，我们从一个class关键字开始，然后给我们的类一个
proper descriptive name and then we add a pair of curly braces now the functions 适当的描述性名称，然后我们在函数中添加一对花括号
that we define in between these curly braces belong to this class and more 我们在这些花括号之间定义的类别属于此类
accurately we refer to them as methods so a method is a function that is part 准确地说，我们将它们称为方法，因此方法是一部分的功能
of a class in some programming languages like Python we can have a function that 某些编程语言（如Python）中的类，我们可以有一个函数
exists outside of a class so we call it a function but when a function belongs 存在于类之外，因此我们将其称为函数，但是当函数属于
to a class we refer to it as a method of that class okay now in Java all these 到一个类，我们现在在Java中将它称为该类的方法
classes and methods should have an access modifier an access modifier is a 类和方法应具有访问修饰符，访问修饰符是
special keyword that determines if other classes and methods in this program can 确定此程序中其他类和方法是否可以使用的特殊关键字
access these classes and methods we have various access modifiers like public 访问这些类和方法，我们有各种访问修饰符，例如public
private and so on now most of the time we use the public access modifier so we 私有等等，现在大多数时候我们使用公共访问修饰符，所以我们
put that in front of our class and Method declarations so this is the basic 把它放在我们的类和方法声明前面，所以这是基本的
structure of a Java program at a minimum we have a main class and inside this Java程序的结构至少要有一个主类，并且在其中
main class we have the main method now you might be curious why we have a 我们现在有main方法，所以您可能很好奇为什么我们有一个main方法
capital m in the name of this class because in Java we use different 此类的名称为大写m，因为在Java中我们使用了不同的名称
conventions for naming our classes and our methods to name our classes we use 命名我们的类的约定以及我们用来命名我们的类的方法
the Pascal naming convention and that basically means the first letter of Pascal命名约定，这基本上意味着
every word should be uppercase in contrast to name our methods we use the 相对于命名我们使用的方法，每个单词都应大写
camel naming convention and that means the first letter of every word should be 骆驼命名约定，这意味着每个单词的首字母应为
operate case except the first wart so that is 除了第一个疣的情况下操作案例
why we have a capital m in the name of this class alright now that you 为什么我们在这个班级的名字上有一个大写的m
understand the anatomy of a Java program let's create a new Java project and see 了解Java程序的结构，让我们创建一个新的Java项目并查看
all these building blocks in action in this Java tutorial you're gonna learn 您将在本Java教程中了解所有这些有效的构建基块
how to write and execute your first Java program so let's open IntelliJ IDEA here 如何编写和执行第一个Java程序，所以我们在这里打开IntelliJ IDEA
on the home screen let's create a new project alright on the left side select 在主屏幕上，让我们在左侧选择一个新项目。
java and make sure project sdk is not black so earlier we downloaded jdk or java，并确保项目sdk不是黑色的，所以我们早些时候下载了jdk或
Java development kit version 12 that is why JDK version 12 is selected here if Java开发套件版本12，这就是为什么在以下情况下选择JDK版本12的原因
you don't see that make sure to select it from this drop-down list alright now 您看不到确保立即从此下拉列表中选择它
let's click on next on this page select create project from template so we're 让我们单击此页面上的下一个从模板中选择创建项目，以便我们
gonna create a command line application which is an application that we can run 将创建一个命令行应用程序，它是我们可以运行的应用程序
from the command line it doesn't have a graphical user interface or a GUI 从命令行没有图形用户界面或GUI
now I know command line application is not as exciting as an application with a 现在我知道命令行应用程序不像具有
graphical user interface like a mobile app or a desktop app but trust me 图形用户界面（例如移动应用或桌面应用），但请相信我
building an application with a graphical user interface is very complicated so 使用图形用户界面构建应用程序非常复杂，因此
for now we're just gonna build command line applications to learn Java properly 现在我们只是要构建命令行应用程序以正确学习Java
once you learn Java properly then you can learn about building desktop or 正确学习Java之后，您就可以学习构建桌面或
mobile applications with Java all right now let's click on next on this page we 现在使用Java的移动应用程序让我们单击此页面上的下一个
have to give our project a name let's call it hello world now over here you 必须给我们的项目起个名字，现在我们在这里打个招呼吧
can see the location of this project so it's inside the idea projects folder now 可以看到该项目的位置，因此现在位于idea projects文件夹中
right below that you can see the base package which is set to comm that code 在右下方，您可以看到设置为将该代码通信的基本软件包
with Marsh on my machine and your mission is probably gonna be comm dot 在我的机器上使用Marsh，您的任务可能会成为交流点
package what is this well here we talked about classes and methods I told you 打包这是什么，我们在这里谈到了我告诉你的类和方法
that a class is a container for related methods so we use classes to organize 类是相关方法的容器，所以我们使用类来组织
our code by the same token we have a concept called package and we use a 同样，我们的代码也有一个叫做package的概念，我们使用
package to group related classes so as our applications grow we're gonna end up 打包以分组相关的类，以便随着我们的应用程序的增长，我们将最终
with hundreds or even thousands of classes so we should properly organize 有数百甚至数千个班级，所以我们应该适当地组织
this class us into packages now by convention the 现在按照惯例将我们归类为软件包
base package for a Java project is the domain name of your company in Reverse Java项目的基本软件包是您公司在Reverse中的域名
so my website is code with mass comm that is why I'm gonna set the base 所以我的网站是带有大众通信的代码，这就是为什么我要设置基础
package for this project to come that code with Marsh now it doesn't mean that 该项目的软件包现在将与Marsh一起提供该代码，这并不意味着
you should have an actual domain registered on an Internet this is just a 您应该在互联网上注册一个实际的域名，这仅仅是一个
way to create a namespace for our classes so now every class that we 为我们的类创建名称空间的方法，所以现在我们每个类
create in this project will belong to this package we're gonna talk about 在这个项目中创建将属于我们将要讨论的这个包
packages in more detail in the future so for now just type a base package for 软件包的详细信息，因此现在只需要输入基本软件包即可
your project it can become that your name or whatever it doesn't really 您的项目可能会变成您的名字或其他名称
matter all right now let's go forward alright here's our first Java project 没关系，让我们继续前进，这是我们的第一个Java项目
now this code editor might look a little bit intimidating at first but trust me 现在，这个代码编辑器乍看起来可能有点吓人，但请相信我
it's really easy and you're gonna learn about it throughout this course on the 这真的很容易，您将在本课程的整个过程中了解
left side we have the project panel where we can see all the folders and 左侧，我们有项目面板，在这里我们可以看到所有文件夹和
files in a project for example on the top we have the hello word project 项目中的文件，例如在顶部，我们有hello word项目
inside this project we have the source folder where we have the source code of 在此项目中，我们有源文件夹，在其中有以下源代码
a project now in this folder we have another folder that is calm that code 现在，在此文件夹中有一个项目，我们还有另一个可以使该代码平静的文件夹
with Marsh that is the name of our base package and in this package we have this Marsh是我们基本软件包的名称，在此软件包中，我们拥有
class main so you can see this main file opened on the right side here now look class main，所以您现在可以在右侧看到打开的这个主文件
at the name of this file its main the Java so all Java files should have the 该文件的名称主要是Java，因此所有Java文件都应具有
Java extension okay now let's collapse the project panel by clicking on this Java扩展现在可以通过单击此按钮来折叠项目面板
icon that is better so see what we have here on top of this file we have the 更好的图标，因此请查看此文件顶部的内容
package statement and this is used to specify what package this class belongs package语句，用于指定此类所属的包
to so the main class that we have here belongs to this package now this package 因此，我们在这里拥有的主类现在属于此程序包
statement is terminated by semicolon so in Java wherever we have a statement we 语句以分号终止，因此在Java中，只要有一条语句，
should terminate that statement with a semicolon this is exactly like c-sharp 应该使用分号终止该语句，这完全类似于c-sharp
or C++ now below this package statement we have our main class exactly like what 或C ++，现在在此包语句下方，我们的主类与
you saw in the previous tutorial so we have public class main with a pair of 您在上一教程中看到了，所以我们有一个公共类main和一对
curly braces inside this class we have our main method so it's a public method 此类中的花括号我们有我们的main方法，所以它是一个公共方法
which may it's accessible from other parts of this 可能可以从此其他部分访问
program it's static we haven't talked about static metals yet we'll talk about 程序是静态的我们没有谈论静态金属，但是我们将谈论
them in the future for now just remember that the main method in your program 他们将来只是记住程序中的主要方法
should always be static the return top of this method is void which means this 应始终为静态，此方法的返回顶部为空，这意味着
method is not gonna return a value and here in parentheses we have one 方法不会返回值，在括号中，我们有一个
parameter for this function we can use these parameters to pass values to our 该函数的参数，我们可以使用这些参数将值传递给我们
program again we'll look at this in the future now right after this parameters 再次编程，我们以后将在此参数之后对此进行研究
as you can see the left brace and this is where we write the code in this 如您所见，左括号是我们在此处编写代码的地方
method now by default we have this line prefix with two slashes this indicates a 方法现在默认情况下，我们具有带两个斜杠的行前缀，表示
comment we use these comments to explain our code to other people so these 评论，我们使用这些评论向其他人解释我们的代码，所以这些
comments don't get executed now let's remove this comment and write a bit of 现在不执行注释，让我们删除此注释并写一些
code to print something on the terminal so here we're gonna use the system class 代码在终端上打印一些东西，所以在这里我们将使用系统类
in Java so capital S system here in this tooltip you can see the system class is 在Java中，因此在此工具提示中的大写S系统中，您可以看到系统类是
defined in this package Java dot Lang or language also look at this icon on the 在此包中定义的Java点Lang或语言也可以在
left side this indicates a class now inside this class we have various 左侧表示当前在该类中有一个类
members we can use the dot operator to see the members defined in the system 成员，我们可以使用点运算符来查看系统中定义的成员
class now the member that we're gonna access is out look at the icon of this 现在上课，我们要访问的成员不在此图标上
member it's F which is short for field you're gonna talk about fields in the 成员F是字段的缩写，您将要讨论字段中的字段
future when we talk about classes and object-oriented programming now what is 未来，当我们谈论类和面向对象的编程时，现在是什么
interesting here is the type of this field and you can see that on the right 有趣的是此字段的类型，您可以在右侧看到
side that is print string so print string is another class that is defined 这是打印字符串的一面，所以打印字符串是另一个已定义的类
in Java so once again we use the dot operator to look at the methods or 在Java中，因此我们再次使用点运算符查看方法或
members defined in the print stream class the method we're gonna use is 打印流类中定义的成员，我们将使用的方法是
print Ln which is short for line look at the icon for this method so M indicates 打印Ln（它是行的缩写），请查看此方法的图标，因此M表示
a method now you press ENTER and IntelliJ automatically adds these 现在按ENTER的方法，IntelliJ自动添加这些
parentheses as well as a semicolon so now with the code on line six we're 括号和分号，所以现在有了第六行的代码
calling or executing the print line method earlier I told you that inside 之前我曾告诉您，调用或执行打印行方法
this parenthesis we can pass values to our methods here we want to print the 我们可以在此括号中将值传递给我们的方法，在这里我们要打印
hello word on the terminal so let's type double 在终端上打个招呼的单词，让我们键入double
quotes and inside these quotes right hello world so hello word is textual 引号和这些引号内的内容是您好世界，所以您好词是文字
data in Java whenever we deal with textual data we should always surround Java中的数据，无论何时处理文本数据，都应始终围绕
them with double quotes now we say we have his string so a string is a 他们用双引号引起来，现在我们说我们有他的字符串，所以字符串是
sequence of characters all right so we're done with our first program now to 好的字符序列，所以我们现在完成第一个程序
execute this we can click on this icon on the toolbar look at the shortcut on 执行此操作，我们可以单击工具栏上的此图标，查看
Mac it's ctrl + R I always prefer to use shortcuts because they're faster so ctrl Mac的ctrl + RI总是更喜欢使用快捷方式，因为它们更快，所以ctrl
+ R now IntelliJ is building our application and we can see the result in + R现在IntelliJ正在构建我们的应用程序，我们可以在
this little terminal window so here's our hello work message so that was our 这个小终端窗口，所以这是我们的工作信息，这就是
first Java program next I'm going to explain how Java code gets executed 第一个Java程序接下来，我将解释如何执行Java代码
under the hood hey Marsh here I just wanted to let you 在引擎盖下，嘿，沼泽，我只是想让你
know that you really don't have to memorize anything in this course I've 知道你真的不需要在这个课程中记住任何东西
put together a comprehensive cheat sheet with summary notes that you can download 整理完整的备忘单，并附上摘要说明，您可以下载
below this video so I've done my best to create the best possible Java course and 在此视频下面，所以我尽了最大努力来创建最好的Java课程，
I would really appreciate it if you support me by liking and sharing this 如果您喜欢并分享我的支持，我将不胜感激
video on the social networks that you use often also be sure to subscribe and 您经常使用的社交网络上的视频也请务必订阅并
enable the notifications so next time I upload a video you'll get notified thank 启用通知，以便下次我上传视频时，您会收到通知，谢谢
you so much and let's continue watching 你这么多，让我们继续看
all right now let's see what exactly happens under the hood the moment we run 现在，让我们看看在跑步时引擎盖下到底发生了什么
a Java program in IntelliJ there are basically two steps involved IntelliJ中的Java程序基本上涉及两个步骤
here compilation and execution in the compilation step IntelliJ uses the Java 在这里，编译和执行步骤IntelliJ使用Java
compiler to compile our code into a different format called Java bytecode 编译器将我们的代码编译成另一种称为Java字节码的格式
this Java compiler comes with the Java development kit that we downloaded at 该Java编译器随附了我们从以下位置下载的Java开发工具包：
the beginning of the course let me show you so here we can right click on this 本课程的开始让我向您展示，所以在这里我们可以右键单击
main the Java and in this context menu we have an item called open in terminal Java的主菜单，在此上下文菜单中，我们有一个名为open in terminal的项目
it's down below unfortunately it's not visible in my recording window it's 不幸的是它在下面，在我的录制窗口中看不到
called open in terminal on Mac and probably open in command prompt on 在Mac上称为在终端上打开，并可能在命令提示符下打开
Windows so let's open that we get this terminal window or command prompt on Windows，让我们打开以打开此终端窗口或命令提示符
windows here we're currently inside of this folder code with Maj that is where Windows，我们目前在此文件夹代码中，其中Maj就是
we have our main the Java file now we can invoke the Java compiler like this 我们有了主Java文件，现在我们可以像这样调用Java编译器了
Java C and pass the name or Java file as an argument so main the Java if you're Java C并将名称或Java文件作为参数传递，因此如果您是
on Mac or Linux make sure to spell this with a capital M because these operating 在Mac或Linux上，请确保使用大写字母M进行拼写，因为
systems are case-sensitive so enter now let's look at the content of this folder 系统区分大小写，所以现在输入，让我们看一下该文件夹的内容
on Mac or Linux we can type LS on windows we type dir so let's take a look 在Mac或Linux上，我们可以在Windows上键入LS，然后键入dir，让我们来看一下
in this folder now we have a new file main class this is the bytecode 在此文件夹中，我们现在有一个新的文件主类，这是字节码
representation of this Java file now let me use IntelliJ to run our Java program 现在，让我使用IntelliJ来运行此Java文件的表示形式的Java程序
this class file gets stored somewhere else let me show you so back to the 此类文件存储在其他位置，让我向您展示，回到
project panel here in our project we have this source folder where we 在我们项目的项目面板中，我们有这个源文件夹
our source code and we have this out folder where we have the result of the 我们的源代码，我们有这个out文件夹，其中有
compilation so inside this folder we have production inside this we have 编译，因此在此文件夹中，我们在其中有生产
hello world the same name as our project inside hello world we have comm which is hello world与hello world中的项目同名，我们有comm
the name of our top level package inside this package we have a sub package that 在此包内的顶级包的名称中，我们有一个子包，
is code with Marsh and here we have our main dat class file so this was the 是Marsh的代码，这里有我们的主要dat类文件，所以这是
compilation step now this Java bytecode that we have in this file is platform 现在的编译步骤此文件中包含的Java字节码是平台
independent and that means it can run on Windows Mac Linux or any operating 独立的，这意味着它可以在Windows Mac Linux或任何操作系统上运行
systems that has a Java runtime environment if you go to java.com slash 如果您转到java.com，则具有Java运行时环境的系统
download we can download java or more accurately java runtime environment for 下载我们可以下载Java或更准确地说是Java运行时环境
various operating systems this Java Runtime environment has a software Java Runtime环境中的各种操作系统都有一个软件
component called Java Virtual Machine or JVM this JVM takes our Java bytecode and 组件，称为Java虚拟机或JVM，此JVM接收我们的Java字节码，
translates it to the native code for the underlying operating system so if you're 将其转换为基础操作系统的本机代码，因此，如果您
on Windows machine this Java Virtual Machine converts or Java bytecode into 在Windows计算机上，此Java虚拟机将Java字节码转换为
the native code that windows can understand this architecture is the Windows可以理解此架构的本机代码是
reason why Java applications are portable or platform independent we can Java应用程序可移植或独立于平台的原因，我们可以
write a Java program on a Windows machine and execute it on Linux Mac or 在Windows计算机上编写Java程序，然后在Linux Mac上执行该程序，或者
any other operating systems that have a Java runtime environment 具有Java运行时环境的任何其他操作系统
c-sharp and python also have the same architecture that's why they are c-sharp和python也具有相同的架构，这就是为什么
platform independent as well now let me show you how to invoke this java virtual 现在，我也向您展示如何调用平台虚拟机
machine to run a Java program so back to this terminal window let me expand this 机器来运行Java程序，所以回到这个终端窗口让我扩展它
currently we are inside of this folder code with Marsh and in this folder we 目前，我们在Marsh的此文件夹代码中，在此文件夹中，
have this class file now let's go one level up so CD dot dot and one more time 现在有这个类文件，让我们上一层，这样CD点和另一时间
so now we are inside the source folder we can invoke Java Virtual Machine like 所以现在我们在源文件夹中，我们可以像这样调用Java虚拟机
this they type Java and then we type the full pass to our main class file what do 他们输入Java，然后输入完整的通行证到我们的主类文件
I mean by that well earlier we defined this package comm that code with Marsh 我是说，很早以前我们用Marsh定义了该程序包comm
and this class the main class is part of this package so the full path 而这个主类是这个包的一部分，所以完整的路径
this class is calm dot code with Marsh dot main make sure to use a capital M 此类是带有Marsh点主要内容的平静点代码，请确保使用大写M
here because this is case sensitive now when we press ENTER Java will look at 这里是因为这是区分大小写的，现在当我们按Enter时，Java将查看
this folder calm inside this folder it will look at this other folder code with 这个文件夹在这个文件夹内比较安静，它将与其他文件夹代码一起查看
Marsh and then it will find main that class in that folder it will load the 沼泽，然后它将在该文件夹中找到该类的主类，它将加载
bytecode and convert it to the native code for the operating system we are 字节码并将其转换为我们正在使用的操作系统的本机代码
using so take a look so it executed our program hello world beautiful let me run 使用一下，让它执行了我们的程序
a program using IntelliJ all these steps are hidden from us we don't see the 使用IntelliJ的程序，所有这些步骤对我们都是隐藏的，我们看不到
compilation or execution steps 编译或执行步骤
so you have seen Java in action now let me tell you five interesting facts about 因此，您已经了解了Java的实际应用，现在让我告诉您有关以下五个有趣的事实
Java Java was developed by James Gosling in 1995 at Sun Microsystems which was Java Java是James Gosling于1995年在Sun Microsystems开发的。
later acquired by Oracle in 2010 it was originally called oke after an oak tree 后来于2010年被甲骨文收购，原名橡树后叫oke
that stood outside Gosling's office later it was renamed to green and was 站在高斯林办公室外面，后来改名为绿色，
finally renamed to Java inspired by Java coffee that's why its logo looks like 最终受到Java咖啡的启发而重命名为Java，这就是其徽标看起来像
this we have four editions of Java for building different kinds of applications 这有四个Java版本，用于构建不同类型的应用程序
we have Java standard edition this is the core Java platform which is what 我们有Java标准版，这是Java的核心平台
we're using in this course it contains all of the libraries that every Java 我们在本课程中使用它包含每个Java的所有库
developer must learn we have Java Enterprise Edition which is 开发人员必须了解我们拥有Java Enterprise Edition
used for building very large-scale and distributed systems it's built on top of 用于构建超大型和分布式系统，它建立在
java standard edition and provides additional libraries for building fault Java标准版，并提供用于构建故障的其他库
tolerant distributed multi-tiered software we have java micro Edition 容忍的分布式多层软件，我们有Java Micro Edition
which is a subset of Java standard edition designed for mobile devices so 这是专为移动设备设计的Java标准版的子集，因此
it has libraries specific to mobile devices and finally we have Java card 它具有特定于移动设备的库，最后还有Java卡
which is used in smart cards the latest version of Java is Java standard edition 在智能卡中使用的Java的最新版本是Java标准版
12 which was released just a few months ago in March 2019 Java has close to 9 12几个月前于2019年3月发布，Java已经接近9
million developers worldwide currently about 3 billion mobile phones run Java 全球有100万名开发人员，目前大约有30亿部手机运行Java
as well as 120 million TV sets and every blu-ray player according to indeed.com 的确有1.2亿台电视机和每个蓝光播放器
the average salary of a Java developer is just over $100,000 per year in the US 在美国，Java开发人员的平均年薪刚刚超过100,000美元
so as we can see Java is everywhere which means more opportunities for you 因此我们可以看到Java无处不在，这对您意味着更多的机会
to get hired as a professional programmer now let me give you a quick 现在被聘为专业程序员
overview of how I've structured this course so you can get the most out of it 我如何组织本课程的概述，以便您可以充分利用本课程
this course is the first part of my complete four-part Java series each part 本课程是完整的Java教程（共四部分）的第一部分，每个部分
is about three to four hours long so it can easily complete it in a day or two 大约需要三到四个小时，因此可以在一两天内轻松完成
in the first part which is what you're watching you're gonna learn the 在第一部分，这是你正在看的东西，你将学习
fundamentals of programming with Java in the next section you'll learn about the 在下一部分中，您将学习Java编程的基础知识
type system in Java you will learn how to work with various types such as Java中的类型系统，您将学习如何使用各种类型的系统，例如
purrs strings boolean's and arrays by the end of this section you will build a 在本节末尾将字符串boolean和数组放倒，您将构建一个
mortgage calculator as your first Java project will be improving this 抵押计算器，这是您的第一个Java项目，它将对此进行改进
calculator bit by bit routers course next you will learn about control flow 计算器点对点的路由器课程接下来，您将了解控制流程
statements that are used to build algorithms we'll be talking about 我们将要讨论的用于构建算法的语句
various types of conditional statements and loops later in this section we'll 在本节后面的各种类型的条件语句和循环中，我们将
add data validation to our mortgage calculator to force the user to enter 将数据验证添加到我们的抵押计算器，以强制用户输入
valid values at this point you'll be able to build basic algorithms and 此时，您可以建立有效的值
that's great but being a good programmer requires 很好，但是要成为一名优秀的程序员需要
knowing how to write good code code that is clean and expressive that's what 知道如何编写干净，表达力强的优质代码
separates an outstanding programmer from an average programmer so in the 将优秀的程序员与普通的程序员分开，因此
following section we'll talk about clean coding I will show you various 下一节我们将讨论干净的编码，我将向您展示各种
techniques that professional programmers use to make their code clean and 专业程序员用来使代码干净整洁的技术
maintainable and finally in the last section you will learn how to find and 可维护的，最后在最后一部分中，您将学习如何查找和
fix errors in your java programs as well as how to package them for deployment so 修复Java程序中的错误以及如何打包它们以进行部署，以便
others can use them so the materials in the first part will give you a solid 其他人可以使用它们，因此第一部分中的材料将为您提供坚实的基础
foundation on how to start programming in Java in the second part we'll talk 在第二部分中，我们将介绍如何启动Java编程的基础
about object oriented programming which is a style of programming use in most if 关于面向对象的编程，这是大多数情况下使用的一种编程风格
not all Java applications whether you want to use Java to build web mobile or 并非所有Java应用程序，无论您是想使用Java来构建Web移动版还是
desktop applications you need to understand object-oriented programming 您需要了解面向对象编程的桌面应用程序
well because otherwise you're going to constantly hit obstacles in the third 好吧，因为否则你将在第三阶段不断遇到障碍
part we're going to talk about core Java API is or application programming 我们将要讨论的核心Java API是应用程序编程
interfaces you'll learn about many of the useful classes in the standard Java 接口，您将了解标准Java中的许多有用的类
library and finally in the last part we'll be looking at the advanced 库，最后一部分，我们将介绍高级
features in Java such as streams threads database programming and so on so I hope Java中的功能，例如流线程数据库编程等等，所以我希望
you're gonna join me on this journey and master Java the most popular programming 您将加入我的旅程，并掌握Java最受欢迎的编程
language behind millions of apps and websites 数百万个应用和网站背后的语言
[Music] [音乐]
you 你
[Music] [音乐]
intersection we're gonna look at the fundamentals of programming in Java 交集，我们将看一下Java编程的基础
you're gonna learn about variables and constants primitive and reference types 您将了解有关变量和常量的原始类型和引用类型
you're gonna learn about casting or type conversion you will learn how to work 您将学习有关转换或类型转换的知识，您将学习如何工作
with numbers strings and arrays and how to read input from the user 数字字符串和数组，以及如何从用户读取输入
once you learn all this I'm gonna give you a project you're gonna build a 一旦你学会了所有这些，我就会给你一个项目，你将建立一个
mortgage calculator on your own so make sure to pay great attention to all the 自行计算抵押贷款计算器，因此请务必特别注意
materials you're gonna learn because you're gonna use most of them in this 您将要学习的材料，因为您将在其中使用大多数材料
project are you ready now let's jump in and get started in this tutorial we're 项目已经准备好了，现在让我们开始学习本教程吧
gonna talk about variables in Java we use variables to temporarily store data 我们将讨论Java中的变量，我们使用变量来临时存储数据
in computer's memory here is an example imagine in this program you want to 在计算机的内存中，这是一个示例，您想在此程序中
store someone's age in the memory so we declare a variable like this ant age 将某人的年龄存储在内存中，以便我们声明一个像这样的蚂蚁年龄的变量
equals 30 so int or integer is the type of this variable so in this variable we 等于30，因此int或integer是此变量的类型，因此在此变量中，
can only store integers which are whole numbers like 1 2 3 4 numbers that don't 只能存储整数，例如1 2 3 4整数
have a decimal point now in Java we have several different types I'm gonna talk 现在在Java中有一个小数点，我要讲几种不同的类型
about them in the next tutorial so first we specify the type of our variable then 关于它们，在下一个教程中，所以首先我们指定变量的类型，然后
we give it a name or a label this is also called an identifier because we use 我们给它起一个名称或标签，也称为标识符，因为我们使用
it to identify our variable this equal sign is called the assignment operator 它用来标识我们的变量，这个等号称为赋值运算符
and 30 is the initial value that we are assigning to this variable so we say on 而30是我们分配给该变量的初始值，因此我们在
line 6 we're initializing this variable which means we're giving it an initial 第6行，我们正在初始化此变量，这意味着我们要给它一个初始值
value you always have to initialize our variables before reading them so with 值，您始终必须在读取变量之前初始化我们的变量，因此
this line we're storing number 30 somewhere in computer's memory and we're 在这行中，我们将30号存储在计算机内存中的某个位置，
assigning this label to that memory location now on line 7 instead of 现在在第7行将此标签分配给该内存位置，而不是
printing hello world we can print the value of the age variable take a look so 打印hello world我们可以打印age变量的值
I'm gonna run this program using ctrl + R there you go now we see 30 on the 我要使用ctrl + R运行该程序，现在您可以看到30
terminal beautiful we can also change the value of our variable so after we 美丽的终端，我们还可以更改变量的值，所以在我们完成之后
initialize it per house we can change it to 35 now when we run this program again 初始化每个房子，我们现在可以将其更改为35再次运行该程序时
we see it 35 beautiful we can also initialize multiple variables on the 我们看到它35漂亮，我们也可以在
same line but this is something that I don't recommend because it makes your 同一行，但是我不建议这样做，因为它会使您
code ugly and hard to read here is an example we can declare another variable 代码丑陋且难以阅读，这是一个示例，我们可以声明另一个变量
like temperature and set it to 20 so using a comma we can declare multiple 例如温度，并将其设置为20，因此使用逗号可以声明多个
variables on the same line now even though this is technically possible it's 现在即使在同一行上的变量，即使在技术上是可行的，
not something that I recommend so it's always better to declare one variable on 不是我推荐的东西，所以总是最好在上面声明一个变量
each line like this we can also copy the value of one variable into another here 像这样的每一行，我们也可以在此处将一个变量的值复制到另一个变量中
is an example let me delete these variables and declare a new variable 是一个例子，让我删除这些变量并声明一个新变量
called my age we set it to 30 and then we declare another variable like her age 叫我的年龄，我们将其设置为30，然后声明另一个变量，例如她的年龄
and we set it to my age so now when we print her age we're gonna see 30 take a 然后将其设置为我的年龄，现在打印她的年龄时，我们将看到30
look so on line seven recapping the value of 看起来在第七行重新获得了价值
this variable into this other variable now one thing I want you to pay 这个变量变成另一个变量现在我要你付钱
attention to here is the convention I have used for naming our variables as I 注意这里是我用来命名变量的约定
told you before this is called the camelcase notation so we should 之前告诉过你这叫做驼峰符号，所以我们应该
capitalize the first letter of every word except the first word so in this 除第一个单词外，每个单词的首字母大写
case the first word my it's all in lowercase but the second word starts 第一个单词我的全都小写，但第二个单词开始
with a capital letter so this is all about declaring and initializing 加上大写字母，这就是声明和初始化
variables in the next tutorial we're going to talk about various types in 在下一个教程中，我们将讨论各种变量
Java in this tutorial we're going to talk about various types in Java 在本教程中的Java中，我们将讨论Java中的各种类型
basically we have two categories of types we have primitive types for 基本上我们有两类类型，我们有原始类型
storing simple values and non primitive types or reference types for storing 存储简单值和非原始类型或引用类型以进行存储
complex objects so in the category of primitive types we have white which 复杂的对象，因此在基本类型的类别中，我们有白色的
takes one byte of memory and in one bite we can store values from 128 to 127 now 占用一个字节的内存，一口气我们现在可以存储128到127的值
the more bytes we have the larger numbers we can store so next we have 字节越多，我们可以存储的数字越大，因此接下来
short which takes two bytes and with this we can store values up to 32,000 短，占用两个字节，因此我们最多可以存储32,000个值
next we have integer which we have seen before integers take four bytes of 接下来我们有一个整数，在整数取四个字节之前
memory and allow us to store values up to two billion then we have long which 记忆并允许我们存储多达20亿的值，那么
takes eight bytes and with this we can store even larger numbers now all these 占用八个字节，因此我们现在可以存储更大的数字
types are for storing whole numbers that don't have a decimal point if you want 类型用于存储没有小数点的整数（如果需要）
to store a number that has a decimal point you have to use float or double 要存储具有小数点的数字，必须使用浮点或双精度
float takes four bytes double takes eight bytes so obviously we double we float需要四个字节，double需要八个字节，所以显然我们要加倍
can store larger numbers next we have char for storing a single character like 接下来可以存储更大的数字，我们有char用于存储单个字符，例如
ABC and this chart I've take two bytes so they support international letters ABC和这张图表我占用了两个字节，因此它们支持国际字母
and finally we have boolean for storing boolean values which can be true or 最后我们有布尔值来存储可以为true或
false just like yes or no in English now let's take a look at a few examples 假，就像英语中的是或否一样，现在让我们来看一些示例
earlier we use an integer for storing someone's age but as you learned 之前我们使用整数来存储某人的年龄，但是如您所知
integers take four bytes and allow us to store values up to two billion we don't 整数占用四个字节，允许我们存储不超过20亿的值
need four bytes of memory to store someone's age all we need is one byte 需要四个字节的内存来存储某人的年龄我们只需要一个字节
because with one bite we can store values up to 127 so I'm gonna change 因为一口就能存储多达127个值，所以我要改变
this to byte that is better now let's look at another example let's say we 这个字节更好，现在让我们看另一个例子，假设我们
want to store the number of times a YouTube video has been watched so we 想要存储观看YouTube视频的次数，因此我们
define an integer called views count note that I'm always using meaningful 定义一个称为views count note的整数，我一直在使用有意义的
names for my variables because these names help us understand what this code 我的变量的名称，因为这些名称有助于我们了解这段代码
does I've seen some people use variable names like V or V 1 or n nobody knows 我见过有人使用变量名，如V或V 1或n没人知道吗
what these variables do so as a best practice always use meaningful and 这些变量作为最佳做法的用途始终是有意义的，
descriptive names for your variables so views count we set this to a large 您变量的描述性名称，因此视图很重要，我们将其设置为较大
number like 1 2 3 4 5 6 7 8 9 now in Java whenever you deal with a large 每当您处理大型邮件时，Java中的数字就像1 2 3 4 5 6 7 8 9
number like this you can use an underscore to separate every three 这样的数字，您可以使用下划线将每三个分隔
digits just like how we use a comma in our documents to make our numbers more 就像我们在文档中使用逗号使数字更数字一样
readable we can use an underscore in Java so with integers we can store 可读，我们可以在Java中使用下划线，因此我们可以存储整数
values up to two billion but let's say the number of times this video has been 价值高达20亿，但我们可以说这部影片的观看次数
watched is three billion so I had a three here now we have a red on the line 看了三十亿，所以我在这里有三个
that indicates an error let me hover our mouse over it we see 表示有错误，我将鼠标悬停在上面
this tooltip integer number too large so we need to change the type of this 此工具提示的整数太大，因此我们需要更改此类型
variable to long however the error is still there what's going on here the 变长，但是错误仍然存​​在，这里发生了什么
reason we're getting this error is that by default Java sees these numbers as 我们收到此错误的原因是，默认情况下，Java将这些数字视为
integers so even though we have defined the type of this variable as long Java 整数，因此即使我们已将此变量的类型定义为long Java
compiler sees this value as an integer and he thinks this value is too large 编译器将此值视为整数，并且他认为该值太大
for an integer to solve this problem we need to add an L as a suffix or this 对于要解决此问题的整数，我们需要添加L作为后缀
number we can use an uppercase or a lowercase L but as you can see a 数字，我们可以使用大写或小写的L，但是如您所见
lowercase L kind of looks like a 1 so it's better to use a capital L so these 小写的L看起来像1，所以最好使用大写L
are examples of whole numbers now let's declare a variable for storing a number 是整数的示例，现在让我们声明一个用于存储数字的变量
with a decimal point so double price we set this to 1099 obviously the double 小数点后加倍，我们将其设置为1099，显然是
variable is too large for storing the price of a product so we can change this 变量太大，无法存储产品价格，因此我们可以更改此变量
to float that is better but you have a compilation error here take a look 浮动是更好的，但您在这里有编译错误
incompatible types required float but found double the reason we're seeing 不兼容的类型需要浮动，但发现原因是我们看到的两倍
this error is that by default Java sees these numbers with a decimal point as 此错误是默认情况下Java将这些数字视为带小数点的
double so even though we set the type of this variable to float Java sees this 因此，即使我们将此变量的类型设置为float，Java也会看到它
number as a double so just like how we added a suffix to this number to 作为一个双精度数字，就像我们向该数字添加后缀的方式一样
represent it as a long we need to add a suffix here to represent this number as 将其表示为长，我们需要在此处添加后缀以将该数字表示为
a float and that suffix is F once again we can use an upper case or lower case F 浮点数，后缀再次为F我们可以使用大写或小写F
so these are examples of numbers now let's store a character so char we call 这些是数字的例子，现在我们存储一个字符，我们称之为char
it letter and we set it to a note that we should always surround single 它的字母，我们将其设置为一个音符，我们应该始终包围单
characters with single quotes and multiple characters or strings with 带单引号的字符和带多个字符的字符串或带
double quotes okay so char represents only one 双引号好了，所以char只代表一个
character string represents a series of characters and finally let's see an 字符串代表一系列字符，最后让我们看一下
example of a boolean so we define a boolean variable called is eligible is 布尔的示例，因此我们定义一个布尔变量，名为
this person eligible for loan or not we said this to true or false these are the 这个人是否符合贷款条件，我们说的是对还是错，这些是
boolean values now note that all these words coded in orange are reserved 布尔值现在请注意，所有这些用橙色编码的单词都保留了
keywords in Java just like public static void class Java中的关键字就像public static void class
package these are all reserved keywords so we cannot use these reserved keywords 打包这些都是保留关键字，因此我们不能使用这些保留关键字
the name our variables classes and methods in the last tutorial you learned 在您上一次学习的教程中将我们的变量类和方法命名为
that we use primitive types to store simple values like numbers boolean 我们使用原始类型来存储简单值，例如数字布尔值
values or single characters in contrast use reference types to store complex 值或单个字符相反，使用引用类型存储复杂的
objects like data objects or mail messages these are complex objects now 数据对象或邮件对象之类的对象，这些现在都是复杂的对象
in Java we have eight primitive types that you have seen before all the other 在Java中，您已经在其他所有类型中看到过八种原始类型
types are reference types let me show you an example so here in this program 类型是引用类型，让我为您展示一个示例，因此在此程序中
first I'm gonna declare a primitive type let's say white age equals 30 now 首先，我要声明一个原始类型，假设现在白人年龄等于30岁
declaring and initializing a reference type is slightly different from 声明和初始化引用类型与
primitive type let me show you so let's type date now here in this 我要告诉你原始类型，所以现在在这里输入日期
tooltip box which is called intellisense we can see various classes that have 工具提示框（称为智能感知），我们可以看到具有
date in their name so IntelliJ is helping us complete our code by 以他们的名字注明日期，以便IntelliJ通过以下方式帮助我们完成代码
suggesting these class names now here we have a date class in this package Java 现在建议这些类名，我们在此包Java中有一个日期类
the util so this package contains a lot of utility classes that are useful in a util，因此此软件包包含许多实用程序类，这些类在
lot of programs you also have a date class in a different package Java SQL or 很多程序在不同的Java包中也有一个日期类
sequel which is used for programming databases so this is the benefit of 续集，用于对数据库进行编程，因此这是
packages we can have the same class but in different packages they don't 包，我们可以有相同的类，但在不同的包中，它们没有
conflict so packages create a namespace for our classes okay now in this case if 发生冲突，因此包在这种情况下现在可以为我们的类创建名称空间
we select the first date class and press enter or tab IntelliJ automatically adds 我们选择第一个日期类别，然后按Enter键或IntelliJ选项卡会自动添加
this line for us import Java that you till the date so because currently we 这行对我们来说是导入Java，直到现在为止，因为目前我们
are in this package in order to use a class from a different package we need 在此软件包中，以便使用我们需要的其他软件包中的类
to import it so here we're importing the date class in this package will talk 导入它，所以在这里我们要导入这个包中的date类
about packages in more detail in the future so back to our date variable 关于软件包的详细信息，请回到我们的date变量
let's give this variable a name collect now now we set this here we need to use 现在给这个变量一个名字，现在我们在这里设置我们需要使用
the new operator to allocate memory for this variable and this is one of the 新操作符为该变量分配内存，这是其中之一
differences between the primitive and reference types when declaring primitive 声明基元时基元和引用类型之间的差异
types we don't need to allocate memory memory is allocated and released by Java 我们不需要分配内存的类型内存是由Java分配和释放的
Runtime environment but when dealing with reference types we should always 运行时环境，但是在处理引用类型时，我们应该始终
allocate memory now we don't have to release this memory Java Runtime 现在分配内存，我们不必释放该内存Java运行时
environment will automatically take care of that so we use the new operator and 环境会自动处理该问题，因此我们使用新的运算符
then repeat the name of our class in this case date and then we add 然后在这种情况下重复我们班级的名称，然后添加
parentheses followed by a semicolon in this example this variable we have 在此示例中，我们在此变量后加上小括号和分号
defined here is an instance of the date class so this class is defined template 这里定义的是date类的实例，因此该类是定义的模板
or blueprints for creating new objects new instances as another example we can 或用于创建新对象新实例的蓝图，作为另一个示例，我们可以
have a class called human and we can have objects like John Bob Mary and so 有一个叫人类的类，我们可以有像约翰·鲍勃·玛丽这样的物体
on so an object is an instance of a class now this object or this class 因此，对象是类的实例，现在是该对象或此类
have members that we can access using the dot operator so we can type now dot 有我们可以使用点运算符访问的成员，因此我们现在可以输入点
and these are all the members defined in this class or in this object for example 这些都是此类或该对象中定义的所有成员
we have a method called get time and this returns the time component of this 我们有一个叫做get time的方法，它返回这个的时间部分
object this is another difference between primitive types and reference 对象，这是原始类型和引用之间的另一个区别
types these primitive types don't have members so if you type age dot we don't 类型这些原始类型没有成员，所以如果您键入年龄点，我们不会
see anything these items you see here are not members of age their code 看到的所有这些项目您在这里看到的都不是其代码的年龄成员
snippets which allow us to quickly generate code for example we can select 片段，使我们能够快速生成代码，例如我们可以选择
for I and this automatically generates this block of code for us we'll talk 对我来说，这会自动为我们生成此代码块
about this in the future so this age variable is a primitive type it's not an 关于这个，所以这个年龄变量是原始类型，不是
object it doesn't have any members and that's why when we use the dot operator 对象它没有任何成员，这就是为什么当我们使用点运算符时
we don't see anything here now let's delete this line and instead print the 我们现在在这里看不到任何内容，让我们删除此行，而是打印
value of this data object so once again we can type system this is a class so we 这个数据对象的值，所以我们可以再次输入system这是一个类，所以我们
can use the dot operator to access its members here we have out which is a 可以使用点运算符访问其成员，这里我们是
field and the type of this field is print string which is another class in 字段，此字段的类型是打印字符串，它是
Java so once again we can use a dot operator and call the print line Java，因此我们可以再次使用点运算符并调用打印行
function now let me show you a very cool shortcut instead of typing all this we 功能现在让我向您展示一个非常酷的快捷方式，而不是键入所有这些内容
can use one of these code snippets so we type s oh you see and press tab and this 可以使用以下代码片段之一，因此我们键入s，哦，您会看到并按tab
generates this piece of code for us all right now let's pass our data object 现在为我们生成这段代码，让我们传递数据对象
here note that I have not surrounded this variable with double quotes because 在这里请注意，我没有将此变量用双引号引起来，因为
this is a string and if you run this program we'll see now on the terminal 这是一个字符串，如果您运行此程序，我们现在将在终端上看到
there you go we don't want this you want the value of 你去那里，我们不想要这个，你想要的价值
our data object not a label so let's remove the quotes I run the program 我们的数据对象不是标签，所以我们删除运行程序的引号
again so here's the current date I'm on my machine I've learned a little bit 再次，这是我在机器上的当前日期，我已经学到了一点
about the differences between the primitive and reference types so you 关于原始类型和引用类型之间的差异，因此您
know that we use primitive types for storing simple values and reference 知道我们使用原始类型来存储简单值和引用
types for storing complex objects but there's a very important difference 存储复杂对象的类型，但有一个非常重要的区别
between these two categories of types in terms of memory management and that's 就内存管理而言，这两种类型之间是
what we're going to talk about in this tutorial 我们将在本教程中讨论的内容
so I'm going to declare a primitive variable X and set it to 1 and then I'm 所以我要声明一个原始变量X并将其设置为1，然后
going to declare another variable like Y and set it to X so in this example we 将要声明另一个变量，例如Y并将其设置为X，因此在此示例中，我们
have two different variables x and y and these two variables are at different 有两个不同的变量x和y，这两个变量位于不同的位置
memory locations so they're completely independent of each other in other words 内存位置，因此它们彼此完全独立
if I change the value of X Y is not going to get affected let me show you so 如果我更改XY的值不会受到影响，请让我告诉你
I'm gonna update X to 2 and then print Y so syu t tab y let's take a look 我要将X更新为2，然后打印Y，所以让我们来看一下
so run as you can see Y is not affected because x and y are completely 这样运行就可以看到Y不受影响，因为x和y完全相同
independent of each other however when we use a reference time this behavior is 彼此独立，但是当我们使用参考时间时，此行为是
different let's take a look so I'm gonna delete 让我们来看看，我将删除
all the code here in Java we have a point class that is defined in this Java中的所有代码，我们都有一个在此定义的点类
package Java that awt so we press ENTER and now we have this import statement on 打包awt的Java，所以我们按ENTER，现在我们有了这个import语句
the top beautiful let's declare a variable like point 1 and set it to new 最漂亮的顶部让我们声明一个像点1的变量并将其设置为new
point here we can pass the initial values for x and y so I'm gonna pass 1 这里我们可以传递x和y的初始值，所以我要传递1
and 1 so intelligent automatically adds these labels x and y now just like 和1这样的智能现在会自动添加这些标签x和y，就像
before I'm gonna declare another variable point 2 and set it to point 1 在我要声明另一个变量点2并将其设置为点1之前
and this is where things get interesting when Java Runtime environment executes 这就是执行Java Runtime环境时有趣的地方
line 8 first it's going to allocate some memory to store this point object let's 第8行，首先要分配一些内存来存储该点对象，让我们
see if the address of that memory location is 100 then it's going to 看看该内存位置的地址是否为100，那么它将
allocate a separate part of the memory and it's going to attach this label to 分配内存的单独部分，并将此标签附加到
that memory location point 1 in that memory location it's going to store the 该内存位置中的那个内存位置点1将要存储
address of our point object so this is the critical difference between 点对象的地址，所以这是
primitive and reference types when we declare a primitive variable like a byte 当我们声明一个原始变量（如字节）时，原始和引用类型
the value that we assigned to that variable will be stored in that memory 我们分配给该变量的值将存储在该内存中
location but when we use a reference type like this point class our variable 位置，但是当我们使用此类点类的引用类型时，我们的变量
is going to hold the ax of that point object in memory not the 将要在内存中保存该点对象的斧头，而不是
actual pointer object now look at line 9 here we're copying the value that we 实际的指针对象现在看第9行，这里我们复制的是
have in this variable into this other variable so that value as you learn is 将此变量包含到另一个变量中，以便您学习的价值是
not the point object is the address or the reference to the point object in 不是点对象是地址或指向点对象的引用
memory that is why we refer to these tiles as reference types because they 内存，这就是为什么我们将这些磁贴称为引用类型的原因，因为它们
don't store the actual values they store a reference to an object somewhere in 不存储实际值，它们存储对对象的引用
the memory so in this example point one and point two are referencing the exact 内存，因此在此示例中，第一点和第二点引用的是精确的
same point object in memory we only have one point object so these two variables 内存中的同一点对象我们只有一个点对象，所以这两个变量
are not independent of each other they're referencing the same object and 彼此不独立，它们引用的是同一对象，并且
that means if I update this point object through either of these variables the 这意味着如果我通过这些变量之一更新该点对象，
changes will be visible to the other variable I'm gonna show you so using the 更改将对其他要显示给您的变量可见，因此使用
first variable point one we're going to update the value of x so we use the dot 第一个变量点一个我们将更新x的值，所以我们使用点
operator and here we can see the members of this object X on Y are both fields 运算符，在这里我们可以看到此对象X的成员都是Y
which are variables that exist inside of a class so we said X just like a regular 是存在于类内部的变量，所以我们说X就像常规
variable to a different value let's say two now because point one and point two 变量设为不同的值现在我们说两个，因为第一点和第二点
are referencing the exact same object if we print point two we're going to see 如果我们打印第二点，我们将引用完全相同的对象
the change that we just made take a look so S or ut tab let's print 看一下刚才所做的更改，让我们打印S或ut标签
point to run the program there you go so the change was visible so remember 指向要在那里运行的程序，以便可以看到更改，因此请记住
this reference types are copied by the references whereas primitive types are 此引用类型由引用复制，而原始类型为
copied by their value and these values are completely independent of each other 通过它们的值复制，并且这些值彼此完全独立
in this tutorial we're gonna look at strings in Java so earlier in the course 在本教程中，我们将在本课程的前面介绍Java中的字符串
we printed the hello world message on a terminal this hello word that we have 我们在终端上打印了hello world消息
here is a string or more accurately it's a string literal 这是一个字符串或更准确地说是一个字符串文字
that means a string value now let's extract this from here and store it in a 这意味着现在要从此处提取一个字符串值，并将其存储在
string variable so cut just before this line we type string now look this string 字符串变量，因此在此行之前剪切，我们键入字符串，现在看该字符串
class is defined in Java that Lang package what is interesting is that we 在Java中定义的那个Lang包类，有趣的是我们
don't have an import statement to import this package or import this class 没有导入声明来导入此包或此类
because this package is automatically imported so we can use any classes that 因为此包是自动导入的，所以我们可以使用
are defined in this package now let's declare a variable called message and 是在此包中定义的，现在让我们声明一个名为message的变量，
because this is a reference type we should instantiate this variable using 因为这是一个引用类型，所以我们应该使用
the new operator so Neal string and here in parenthesis we type our message hello 新运算符，所以Neal字符串，然后在括号中输入消息hello
world however here we have this little warning take a look new string is 世界，但是在这里，我们有这个小警告，看看新的字符串是
redundant because in Java there is a shorter way to initialize string 冗余，因为在Java中，有一种较短的初始化字符串的方法
variables let me show you so instead of using the new operator we simply set 变量让我向您展示，因此我们不用设置新的运算符
this to our string literal now on the surface this looks like a primitive type 现在表面上看起来像原始类型的字符串文字
because we are not using the new operator but this is just a shorthand to 因为我们没有使用new运算符，但这只是
initialize a string variable strings are reference types in Java but because we 初始化字符串变量字符串是Java中的引用类型，但是因为我们
use them often there is a short way to create them 经常使用它们有一种创建它们的简短方法
so now let's pass message to the print line method and run our program you get 现在让我们将消息传递给打印行方法，然后运行我们的程序
the exact same result as before beautiful now let's look at a few 与美丽之前完全相同的结果现在让我们来看一些
interesting things that you can do with strings we can concatenate or joining a 您可以使用字符串来做的有趣的事情，我们可以串联或连接一个字符串
string with another one using the plus operator so here we can combine this 使用加号运算符与另一个字符串进行组合，因此在这里我们可以将其组合
with another string with two exclamation marks and here's the result now because 与另一个带有两个感叹号的字符串，这是现在的结果，因为
string is a class it has members that we can access using the dot operator so we 字符串是一个类，它具有可以使用点运算符访问的成员，因此我们
can type message dot and these are all the methods or functions do 可以键入消息点，这些都是方法或函数
find in the string class for example we have this method here ends with and with 例如，在字符串类中找到，我们在此方法以和结尾
this we can check to see if our string ends with a character or sequence of 我们可以检查字符串是否以字符或序列结尾
characters for example here we can pass their string to see if our message ends 例如字符，我们可以传递其字符串以查看消息是否结束
with two exclamation marks now instead of printing the message 现在带有两个感叹号，而不是打印消息
let's print this expression here so let's run the program we get true so 让我们在这里打印此表达式，让我们运行我们得到的程序，这样
this method that we have called here returns a boolean value which can be 我们在这里调用的此方法返回一个布尔值，该值可以是
true or false we also have another method starts with let's take a look now 是或否，我们还有另一种方法，让我们现在来看
in this case we get false because our message doesn't start with two 在这种情况下，我们会得到错误的信息，因为我们的信息不是以两个开头
exclamation marks another useful method is length so we can call that to get the 感叹号是另一个有用的方法，即长度，所以我们可以称其为
length of a string which is the number of characters so message dot length take 字符串的长度，即字符数，因此消息点的长度
a look so in this string we have 13 characters 在这个字符串中，我们有13个字符
and this is useful in situations where you want to check the length of the 这在需要检查长度的情况下很有用
input by the user for example you might have a sign-up form with a username 用户输入的信息，例如，您可能具有带有用户名的注册表单
field you can check the length of someone's username and give them an 字段，您可以检查某人的用户名长度，并给他们一个
error if the username is longer than let's say 20 characters pretty useful we 如果用户名的长度超过20个字符，则错误
also have another method that is index of and this returns the index of the 也有另一个方法是index，它返回的索引
first occurrence of the character or the string that we pass here for example if 例如，如果我们第一次在此处传递字符或字符串，
you pass H the index of H is 0 so let's run the program there you go we get 0 if 您传递H时，H的索引为0，所以让我们在其中运行程序，如果得到，则为0
you pass e we get 1 because the index of the first II in this message is 1 now 您通过e我们得到1，因为此消息中第一个II的索引现在为1
what if you pass a character or a string that doesn't exist in this message let's 如果您传递此消息中不存在的字符或字符串该怎么办，让我们
say Skye we get negative 1 so with this method we can check to see if a string 说斯凯，我们得到负1，所以用这种方法，我们可以检查一下是否有一个字符串
contains certain characters or words or sentences and so on another useful 包含某些字符，单词或句子，等等。
method is replace and with this we can replace one or more characters with 方法是replace，用这个我们可以替换一个或多个字符
something else for example we can replace any exclamation marks with 例如，我们可以将任何感叹号替换为
let's say a store so this replace metal has two parameters one is target the 假设有一家商店，所以这种替换金属有两个参数，一个是目标
other is replacement and here we're passing two values for these parameters 另一个是替换，这里我们为这些参数传递两个值
here's the first value here is the second value and we have separated these 这是第一个值，这里是第二个值，我们将它们分开
values using a comma now in programming terms we refer to this values as 现在在编程术语中使用逗号来表示这些值
arguments a lot of programmers don't know the difference between parameters 许多程序员都不知道参数之间的区别
and arguments parameters are the holes that we define in our methods arguments 和arguments参数是我们在方法arguments中定义的漏洞
are the actual values that we pass to these methods so in this case target and 是我们传递给这些方法的实际值，因此在这种情况下为target和
replacement or parameters but exclamation mark and asterisk are 替换或参数，但是带有感叹号和星号
arguments now let's run this program and see what happens so our explanation 参数，现在让我们运行该程序，看看会发生什么，所以我们的解释
marks are replaced with stars now what is important here is that this method 现在用星号代替了标记这里重要的是这种方法
does not modify our original string it returns a new string so if we print our 不修改我们的原始字符串，它返回一个新字符串，因此如果我们打印我们的
original string right after SRU T tab message you can see the original string SRU T选项卡消息后的原始字符串，您可以看到原始字符串
is not changed because in Java strings are immutable 不会更改，因为在Java中字符串是不可变的
we cannot mutate them we cannot change them so any methods that modify a string 我们不能使它们变异，我们不能改变它们，所以任何修改字符串的方法
will always return a new string object okay we also have another useful method 总是会返回一个新的字符串对象，好吧，我们还有另一个有用的方法
to lowercase let's take a look 小写让我们看一下
so to lowercase converts all characters to lowercase and once again you can see 因此将其转换为小写字母会将所有字符转换为小写字母，再次可以看到
that the original string is not affected because this method returns a new string 原始字符串不受影响，因为此方法返回一个新字符串
okay we also have two uppercase and another useful method is trim trim and 好的，我们还有两个大写字母，另一个有用的方法是修剪修剪和
with this we can get rid of extra white spaces that can be at the beginning or 有了这个，我们可以摆脱多余的空白，这些空白可以在开头或
the end of a string sometimes our users type unnecessary spaces in form fields 字符串的结尾，有时我们的用户在表单字段中键入不必要的空格
so using the trim method we can get rid of these white spaces let me show you so 因此，使用修剪方法，我们可以摆脱这些空白，让我向您展示
I'm gonna add a couple of spaces before and after our message now when we trim 现在，当我们修剪时，我要在消息前后添加几个空格
it these white spaces are gonna get removed take a look so here's the 这些空白将被删除看看吧，这是
original string you can see two white spaces at the beginning and here's our 原始字符串，您可以在开头看到两个空格，这是我们的
string after trimming so these are some useful methods in the string class but 修剪后的字符串，所以这些是字符串类中的一些有用方法，但是
this glass has more methods than we don't have time to cover in this lecture 这种玻璃杯比我们没有时间讲更多的方法
but as we go through the course you're gonna learn more about the string class 但是当我们完成本课程时，您将学到更多关于字符串类的知识
and other useful classes in Java third times will include special characters in Java第三次使用其他有用的类时，将在
our strings like a tab or a new line or a backslash or double quotes so in this 我们的字符串，例如制表符或换行符或反斜杠或双引号，因此在此
tutorial I'm gonna show you how to include these special characters in your 本教程将向您展示如何在您的广告中添加这些特殊字符
strings so here we have the string hello Marsh let's say we want to surround 字符串，所以这里有字符串hello Marsh，假设我们要包围
Marsh with double quotes now here's the problem if we add a double quote here 如果我们在此处添加双引号，那么带双引号的沼泽现在是问题所在
Java compiler thinks this is the termination of our string so it doesn't Java编译器认为这是我们字符串的终止，因此不会
stand what we have after that's why we have a compilation error the fixes 站在那之后我们拥有的就是这就是为什么我们有一个编译错误的修复
problem we need to prefix this double code with a backslash 问题，我们需要在此双重代码前加上反斜杠
so using this backslash we have escaped the double quote now one more time let's 因此，使用此反斜杠，我们又避开了双引号，让我们
add backslash double code here now let's run the program and see what we get so 现在在这里添加反斜杠双代码，让我们运行程序，看看会得到什么
we get hello Marsh in double quote beautiful so double quote is one of 我们用双引号打招呼你好，马什，所以双引号是其中之一
those special characters that you need to be aware of another special character 您需要知道的其他特殊字符
is backslash let's say we want to store the pass to a directory on a Windows 是反斜杠，假设我们要将通行证存储到Windows上的目录中
machine so that will look like this C Drive 机器，看起来像这个C盘
backslash windows backslash whatever now if you want to store this in a string we 如果您想将反斜杠存储在字符串中，则立即使用反斜杠Windows
need to escape each backslash let me show you so C Drive backslash now we 需要转义每个反斜杠，让我告诉你，所以C盘反斜杠现在我们
have a problem Java compiler thinks we're escaping the Java编译器认为我们在逃避
double code here so it thinks our string is not terminated with another double 此处使用双精度代码，因此它认为我们的字符串未以另一双精度字符终止
code but that's not what we want you want to add a backslash here so we need 代码，但这不是我们想要的，您想要在此处添加反斜杠，因此我们需要
to prefix our backslash with another backslash now we type windows one more 给我们的反斜杠加上另一个反斜杠的前缀，现在我们再键入一次Windows
time something let's run the program so even though we have two backslashes in 时间，让我们运行程序，即使我们有两个反斜杠
our code we actually see one backslash in a terminal window in other escape 我们的代码实际上在另一个转义符的终端窗口中看到一个反斜杠
sequences backslash N and we use that to add a new line to our strings so let's 序列反斜杠N，我们使用它在字符串中添加新行，因此
change this to backslash N and run the program to see what happens now our 将其更改为反斜杠N并运行程序以查看现在发生了什么
string is broken down onto multiple lines by the first line we have C Drive 字符串由第一行分为C行
then we have Windows so wherever we had a backslash n Java will insert a new 那么我们就拥有Windows，因此无论我们在哪里使用反斜杠n Java都会插入一个新
line we can also add a tab in our strings 行，我们还可以在字符串中添加一个制表符
so if you add backslash T here there will be a tab which means C Drive and 因此，如果您在此处添加反斜杠T，将出现一个选项卡，表示C驱动器和
windows let's take a look so C Drive here we have a tab and then windows now Windows让我们看一下，所以C驱动器在这里有一个选项卡，然后是Windows
in Java we have a few more escape sequences but quite honestly they're 在Java中，我们还有其他一些转义序列，但老实说，它们是
hardly used so remember these four escape sequences that we cover in this 很少使用，因此请记住我们在本节中介绍的这四个转义序列
tutorial 教程
in this tutorial we're going to talk about arrays in Java we use arrays to 在本教程中，我们将讨论Java中的数组，我们使用数组来
store a list of items like a list of numbers or a list of people or a list of 存储项目列表，例如数字列表或人员列表或列表
messages let me show you so here we have an integer variable you want to convert 消息让我告诉你，所以在这里我们有一个要转换的整数变量
this to an integer array so right after int we add square brackets now we have a 这是一个整数数组，所以在int之后，我们添加了方括号，现在我们有了一个
compilation error because we're storing a single number in this array so to fix 编译错误，因为我们在此数组中存储了一个数字，以便修复
this we need to remove one because arrays are reference types we need to 我们需要删除一个，因为数组是我们需要的引用类型
use the new operator here then we repeat the type one more time enter a and here 在这里使用new运算符，然后再重复一次输入a，然​​后在这里
in square brackets we specify the size or the length of this array how many 在方括号中，我们指定此数组的大小或长度
items do we want to include in this array let's say five also we should 我们要包含在该数组中的项目我们说五个也应该
change the name of this variable from number two numbers because we're dealing 从第二个数字更改此变量的名称，因为我们正在处理
with a list of items so always pay attention to the name of your variables 带有项目列表，因此请始终注意变量的名称
now you can access individual items in this array using an index so we type 现在您可以使用索引访问此数组中的单个项目，因此我们键入
numbers square brackets to reference the first element or first item we use zero 数字方括号引用第一个元素或第一个项目，我们使用零
now we can set this to a value like 1 similarly we can set the second item to 现在我们可以将其设置为类似1的值，我们可以将第二项设置为
2 now what if we use an invalid index let's say 10 this array doesn't have 10 2现在如果我们使用无效索引怎么办呢？假设10这个数组没有10
items so let's see what happens numbers of 10 we said this to 3 when we run this 项目，所以让我们看看当我们运行10时，我们说3时发生了什么
program we get an exception exceptions are Javas way to report errors so in 程序，我们得到一个异常异常是Java报告错误的方式，因此在
this case an exception was raised and our program crashed we'll talk about 这种情况下引发了异常，我们的程序崩溃了，我们将讨论
exceptions in detail later in the course so now let's remove the last line and 本课程稍后将详细讨论异常，因此现在让我们删除最后一行，
treinta sorry so sou t tab numbers let's see what we treinta对不起，所以标签编号让我们看看我们
get we get this weird string in sort of the 得到我们得到这个奇怪的字符串
items in our array here's the reason by default when we print an array Java 数组中的项目是我们打印数组Java时默认的原因
returns the string which is calculated based on the address of this object in 返回根据此对象的地址计算得出的字符串
memory so if you have another array and we print that we're gonna see something 内存，所以如果您有另一个数组并且我们打印出来，我们将看到一些东西
different because each object is gonna be in a different memory space okay now 不同，因为每个对象现在都将位于不同的存储空间中
how can we see the actual items in this array well we have a class in Java 我们如何才能很好地看到此数组中的实际项目，我们在Java中有一个类
called arrays let me show you arrays so this class is defined in Java 所谓的数组让我向您展示数组，因此此类是在Java中定义的
that util package let's press ENTER now this is important on the top beautiful 该util包让我们现在按ENTER键，这对于
so we can use the dot operator to access the members of this class here we have a 因此我们可以使用点运算符来访问此类的成员，这里我们有一个
method called two string now as you see it this method is implemented multiple 如您所见，该方法现在称为两个字符串，该方法实现了多个
times so in the first implementation this method gets a float array in the 因此，在第一个实现中，此方法会在
second implementation it takes an integer array and so on so for all 第二种实现需要一个整数数组，以此类推
primitive types as well as reference types this method is implemented 此方法实现的基本类型和引用类型
multiple times this is what we call method overloading now we can call this 多次，这就是我们所谓的方法重载，现在我们可以称之为
method and pass our integer array and this will return the string 方法并传递我们的整数数组，这将返回字符串
representation of this array so we can cut this from here and pass it to our 该数组的表示形式，因此我们可以从此处剪切它，并将其传递给我们
print method like this now let's run the program one more time and here's our 这样的打印方法现在让我们再运行一​​次程序，这是我们的
array beautiful so the first two items are initialized the others are set to 0 数组很漂亮，因此前两个项目被初始化，其他项目设置为0
by default because here we're dealing with an integer array if you had a 默认情况下，因为如果您有一个整数数组，我们在这里处理整数数组
boolean array all items why default get initialized to false if you have a 布尔数组所有项目，如果您有一个默认值，为什么将default初始化为false
string array all items get initialized to an empty string 字符串数组所有项目均初始化为空字符串
okay now this syntax for creating and initializing an array is a little bit 好吧，现在这种用于创建和初始化数组的语法有点
tedious and it's an older syntax there is a newer way to initialize an array if 繁琐且语法较旧，如果存在以下情况，则有更新的方法可以初始化数组
we know the items ahead of time like in this case so I'm going to remove these 我们在这种情况下提前知道了这些项目，所以我要删除这些项目
two lines I'm also gonna remove the new operator here we use curly braces and 两行我也要删除新的运算符，在这里我们使用花括号和
inside these braces we add all the items in this array let's say 2 3 5 1 & 4 now 在这些括号内，我们将所有元素添加到该数组中，假设现在是2 3 5 1＆4
we have 5 items so the length of this array is gonna be 5 we can read that 我们有5个项目，所以这个数组的长度将是5我们可以读到
using the lengths so if we type numbers dot look here we 使用长度，所以如果我们输入数字点，请看这里
have is filled look at the icon it's an F so this is a field which is like a 充满了，看看它的图标是F，所以这是一个像
variable in a class and the type of this field is an integer so this returns the 类中的变量，并且此字段的类型为整数，因此返回
number of items in this array let's get that and printed using our print method 该数组中的项目数让我们获取并使用我们的打印方法进行打印
like this take a look so we get five now in Java arrays have a fixed size so once 像这样看一下，所以我们现在得到了五个Java数组，它们的大小是固定的，所以一次
we create them we cannot add or remove additional items to them they have a 我们创建了它们，因此无法向其中添加或删除其他项，因为它们具有
fixed length if you want to be able to add or remove additional items from an 固定长度，如果您希望能够添加或删除其他内容
array you should use one of the collection classes that we'll talk about 数组，您应该使用我们将要讨论的集合类之一
later in the course for now all I want you to remember is that arrays have a 在本课程的稍后部分，我要记住的是数组有一个
fixed length now currently our array is not sorted these numbers are in some 固定长度，目前我们的数组未排序，这些数字在某些位置
kind of random order we can easily sort this array using the sort method of the 一种随机顺序，我们可以使用
arrays class let me show you so I'm gonna remove this line and call arrays 数组类让我告诉你，所以我要删除此行并调用数组
dot sort once again you can see this method is overloaded because it's 点排序再次，您可以看到此方法被重载，因为
implemented with different parameter types so we call this method and pass 用不同的参数类型实现，因此我们调用此方法并传递
our numbers array now when we run this program we can see 现在，当我们运行该程序时，我们的数字数组可以看到
our array is sorted beautiful 我们的数组很漂亮
so yeah I've learned that we use arrays to store a list of objects in Java we 是的，我了解到我们使用数组在Java中存储对象列表
can also create multi-dimensional arrays for example we can create a 也可以创建多维数组，例如，我们可以创建一个
two-dimensional array to store a matrix or we can create a three-dimensional 二维数组来存储矩阵，或者我们可以创建三维
array to store data for cube these are useful in scientific computations let me 数组，用于存储多维数据集的数据这些在科学计算中很有用
show you so here we have a single dimensional array to convert this to a 向您展示，所以在这里，我们有一个一维数组将其转换为
two-dimensional array we need to add another pair of square brackets now we 二维数组，我们需要添加另一对方括号
have a compilation error because we need to repeat these brackets on the other 出现编译错误，因为我们需要在另一个上重复这些括号
side so let's say we want to create a 2 by 3 matrix so 2 rows and 3 columns we 因此，假设我们要创建一个2 x 3矩阵，所以我们需要2行3列
add in other brackets here and change these lengths to 2 and 3 so now we have 在这里添加其他括号并将这些长度更改为2和3，所以现在我们有了
2 rows and 3 columns now to access individual items in this array we need 现在需要2行3列来访问此数组中的单个项目
to supply two indexes first the index of the row so we can go to the first row 提供两个索引，首先提供该行的索引，以便我们可以转到第一行
and then the first column and initialize that to 1 now let us print this so and 然后将第一列并将其初始化为1现在让我们将其打印为
so you t-tap once again we use our arrays class dot to string and pass this 所以您再次点击我们使用数组类点字符串并传递此
object take a look once again we get this weird 对象再看一次，我们变得很奇怪
string because here we're dealing with a multi-dimensional array to solve this 字符串，因为这里我们要处理多维数组来解决这个问题
problem we need to use another method in this class called deep to string use 问题，我们需要在此类中使用另一种方法，称为“深度使用字符串”
this for printing multi-dimensional arrays take a look now we have this 这用于打印多维数组，现在来看一下
matrix which has two rows and in each row we have three columns we can also 有两行的矩阵，每一行有三列，我们也可以
create a three dimensional array all we have to do is to add another pair of 创建一个三维数组，我们要做的就是添加另一对
brackets and specify the length of that dimension pretty easy now what about the 括号并指定尺寸的长度，现在非常简单
curly brace syntax let me show you so let's revert this back to a two 花括号语法让我告诉你，让我们将其恢复为两个
dimensional array we're gonna get rid of the new operator and use curly braces 维数组，我们将摆脱新的运算符，并使用花括号
now let's say in this matrix we're gonna have two rows and three columns so each 现在让我们说在这个矩阵中，我们将有两行三列
row is an array itself because it's a list of items right 行本身是数组，因为它是项目列表
so we add another array here let's say 1 2 3 then comma now we add the second row 所以我们在这里添加另一个数组，假设1 2 3然后用逗号添加第二行
which is another array in this row we're gonna have 3 numbers 4 5 & 6 这是该行的另一个数组，我们将有3个数字4 5＆6
now let's remove this line we don't need it anymore and print this array so 现在让我们删除此行，我们不再需要它并打印此数组，因此
here's the end result you have learned a lot about variables ya learned that when 这是最终结果，您已经了解了很多有关变量的知识，您了解到
declaring them we need to initialize them and we can always change their 声明它们，我们需要初始化它们，我们可以随时更改它们
value later on throughout the lifetime of our programs however there are times 在我们程序的整个生命周期中更有价值，但是有时
that we don't want the value of a variable to change for example let's 我们不想改变变量的值，例如，让我们
declare a variable called pi and set it to 3.14 now here we need to add an F to 声明一个名为pi的变量并将其设置为3.14，现在我们需要在其中添加F
represent this as a float because by default Java compiler sees this number 将其表示为浮点数，因为默认情况下Java编译器会看到此数字
as a decimal okay now you know that we use pi to calculate the area of a circle 作为小数，现在您知道我们使用pi来计算圆的面积
what if before we calculate the area of a circle I come here and type pi equals 如果在我们计算圆的面积之前我来这里并键入pi等于
1 then all our calculations are gonna get messed up we don't want this to 1然后我们所有的计算都搞砸了，我们不希望这样
happen that's when we use constants so if we type final here Java compiler will 发生这种情况的时候我们使用常量，所以如果我们在此处键入final，Java编译器将
treat this as a constant so once we initialize this we cannot change its 将此视为常量，因此一旦初始化，就无法更改
value later on you can see here we have a compilation error and it says cannot 稍后您可以看到这里的值，我们有一个编译错误，它说不能
assign a value to final variable pi so pi is a final variable or a constant now 为最终变量pi赋值，因此pi现在是最终变量或常量
by convention we use all capital letters to name constants so this should be PI 按照惯例，我们使用所有大写字母来命名常量，因此这应该是PI
beautiful now I tell you a little side story in one of my early courses that I 现在很美丽，我在我的一门早期课程中告诉您一个小故事
created years ago that was c-sharp basics for beginners there I used the 在几年前创建的那是初学者的c-sharp基础知识
same example to teach the concept of constants but I pronounce this word as P 教常量概念的同一示例，但我将此单词发音为P
instead of Pi and believe it or not to this day people make fun of me for 而不是Pi，不管你信不信，今天人们为我取笑
saying P instead of Pi but that's how we learned this back in Iran we pronounce 说P而不是Pi，但这就是我们在伊朗重新学到的方式
it as P and I think Greek people also say P but anyway I just thought to share 作为P，我认为希腊人也说P，但无论如何我只是想分享
this video to change the mood now you're done with constants next we're gonna 这个视频可以改变心情，现在您已经完成了常量操作，接下来我们要
talk about arithmetic expressions 谈论算术表达式
in this tutorial we're going to talk about arithmetic expressions in Java so 在本教程中，我们将讨论Java中的算术表达式，因此
in Java we have the same arithmetic operators that we have in math we have 在Java中，我们拥有与数学中相同的算术运算符
addition subtraction multiplication division and modulus which is the 加减乘除和模是
remainder of a division let's look at a few examples so I'm gonna declare an 除法的其余部分，让我们看一些示例，以便我声明一个
integer called result and here we can type 10 plus 3 now when we print result 整数，称为结果，在这里我们可以在打印结果时键入10加3
it's gonna be 13 pretty straightforward there you go so this is addition we also 你去那里将是13个非常简单的地方，所以这也是我们
have subtraction multiplication division is an interesting one let's take a look 进行减法乘法除法是一个有趣的过程，让我们来看看
so here the result is a whole number because in Java the division of two 所以这里的结果是一个整数，因为在Java中将两个数相除
whole numbers is a whole number if you want to get a floating-point number here 如果要在此处获取浮点数，则整数就是整数
you need to convert these numbers to a float or a double let me show you 您需要将这些数字转换为浮点数或双精度数，让我告诉您
so we prefix this number with parentheses and in parentheses we type 所以我们在这个数字前加上括号，然后在括号中输入
double now we are casting or converting this number to a double similarly we 现在，我们正在将这个数字转换或转换为类似的两倍
should do that here and now we have a compilation error because on the left 应该在这里做，现在我们有一个编译错误，因为在左边
side we declared an integer but here the result of this expression is a double 边我们声明了一个整数，但是这里的表达式的结果是一个双精度
and by the way an expression is a piece of code that produces a value so what we 顺便说一句，表达式是一段产生值的代码，所以我们
have here is an expression because it produces a value so to fix this problem 这里有一个表达式，因为它产生一个值，因此可以解决此问题
we need to change this to double now when we run this program we get this 我们需要在运行该程序时将其更改为现在的两倍
floating point number beautiful so these are the arithmetic operators and these 浮点数很漂亮，所以这些是算术运算符
numbers that we have here are called operands we also have increment and 我们在这里拥有的数字称为操作数，我们也有增量和
decrement operators let me show you so I'm gonna declare a new variable int X 减量运算符让我告诉你，所以我要声明一个新变量int X
we set it to 1 now if you want to increase the value of x by 1 we use the 我们现在将其设置为1，如果您想将x的值增加1，我们使用
increment operator now let's print this on a terminal so we get 2 there you go 增量运算符现在让我们在终端上打印它，以便我们得到2
we can apply this operator as a postfix or as a prefix and we get the same 我们可以将此运算符用作后缀或前缀，并且得到相同的结果
result take a look too however if we use this 结果也看一下，但是如果我们使用这个
on the right side of an assignment operator we get different results let me 在赋值运算符的右侧，我们得到了不同的结果
show you so I'm gonna declare another variable Y we set it to X plus plus in 告诉你，所以我要声明另一个变量Y我们将其设置为X加
this case because we have applied the increment operator as a postfix first 这种情况是因为我们首先将增量运算符用作后缀
the value of x will get copied to Y so Y would be 1 and then X will be x的值将被复制到Y，因此Y将为1，然后X将为
incremented by 1 so if you print x and y x is gonna be 2 and Y is gonna be 1 take 加1，所以如果您打印x和y，x将是2，Y将是1
a look so X is 2 and Y is 1 beautiful however if you apply this as a prefix 看起来X等于2，Y等于1，但是如果将其作为前缀使用
first X will be incremented by 1 so it will be 2 and then it will be copied to 第一个X将增加1，因此它将是2，然后将其复制到
Y so in this case both X&Y will be to take a look so we get two Y，所以在这种情况下，两个X＆Y都会一起看，所以我们得到两个
and two now what if you want to increment X by more than one let's say 现在有两个，如果您想将X增加一个以上，该怎么办
by two well there are two ways to do this let's remove Y we don't really need 通过两种方式，有两种方法可以执行此操作，让我们删除不需要的Y
it anymore we can write x equals x plus 2 so first we add 2 to X the result will 它可以写x等于x加2了，所以首先我们将2加到X上，结果将是
be three and then three will be copied into X the other way is to use the 是三个，然后三个将被复制到X中，另一种方法是使用
Augmented or compound assignment operator so we can write X plus equals 增强或复合赋值运算符，因此我们可以写X加等于
two what we have on line eight is exactly identical to what we have on 我们在第八行中拥有的与我们拥有的完全相同
line seven well as you can see it's shorter so this is a better way to write 如您所见，第七行还比较短，所以这是一种更好的书写方式
the same code now this is one of the Augmented assignment operators we have 现在是相同的代码这是我们拥有的增强赋值运算符之一
the Augmented assignment operator for other arithmetic operators so we can 其他算术运算符的增强赋值运算符，因此我们可以
type X minus equals 2 and this would reduce the value of x by 2 we also have 类型X减等于2，这会使x的值减少2，我们也有
multiply and divide so these are the Augmented or compound assignment 乘法和除法，所以这些是增强或复合分配
operators right now I've got a question for you here we have declared this 运算符现在我在这里有一个问题要问您，我们已经声明了
variable X it goes to 10 plus 3 times 2 what do you think is the result of this 变量X变为10加3乘以2您认为这是什么结果
expression the result is 16 let's run this program and find out so run there 表达式的结果是16让我们运行该程序并查找，以便在那里运行
you go we got 16 but why well this is a very basic math concept that 你走了，我们有16，但是为什么这是一个非常基本的数学概念，
unfortunately a lot of people don't know in math the multiplication and division 不幸的是，很多人在数学上不知道乘法和除法
operators have a higher priority so they get applied first in this example this 运算符具有更高的优先级，因此在本示例中，它们首先被应用
expression 3 times 2 is evaluated first the result is 6 and then 6 is added to 表达式3乘以2，首先计算结果为6，然后将6加到
10 that's why we get 16 now if you want to change the order of these operators 10就是为什么如果您想更改这些运算符的顺序，我们现在得到16
you can always use parentheses for example if you want this expression to 您可以始终使用括号，例如，如果您希望此表达式
be evaluated first we wrap it in parentheses so like this now Java 首先被评估，我们将其包装在括号中，就像现在这样
compiler will first evaluate this expression the result will be 13 and 编译器将首先对该表达式求值，结果将为13，
then 13 is multiplied by 2 so we get 26 take a look 然后13乘以2，我们得到26
there you go so be aware of the order of these operations parentheses always have 你去那里，所以要知道这些操作括号的顺序总是
the highest priority then we have multiplication and division and finally 最高优先级，那么我们就有乘法和除法，最后
we have addition and subtraction in this tutorial we're going to talk about 我们将在本教程中进行加减运算
casting and type conversion so I'm gonna declare a short variable call X and set 转换和类型转换，所以我要声明一个简短的变量调用X并设置
it to 1 and then I'm gonna declare an integer called Y and set it to X plus 2 将其设置为1，然后我将声明一个名为Y的整数并将其设置为X加2
in this example we're adding a short to an integer what do you think the result 在此示例中，我们将整数添加为short，您认为结果如何
is gonna be well let's take a look so sou t let's print Y we get 3 that is 会好起来的，让我们看一下，所以让我们打印Y，我们得到3
what you were expecting but let me explain what happens under the hood for 您在期待什么，但让我解释一下
this expression to get executed because we're dealing with two different types 之所以要执行该表达式，是因为我们要处理两种不同的类型
of values one is a short the other is an integer one of these values should be 的值之一是短整数，另一个是整数，这些值之一应该是
converted to the other type so they are equal now I got a question for you how 转换为其他类型，所以它们相等现在我有一个问题要问你
many bytes do we have in a short variable we have 2 bytes how many bytes 我们在短变量中有多少个字节，我们有2个字节，多少个字节
do we have in an integer 4 bytes so any values that we store in a short variable 我们有4个整数字节，所以我们存储在short变量中的所有值
can also be stored in an integer variable right so when this piece of 也可以存储在一个整数变量中，因此当
code is gonna get executed this is what's gonna happen first Java looks at 代码将要执行，这就是首先要看的Java
the value in this variable it's 1 right it's going to allocate another variable 该变量的值是1，它将分配另一个变量
an anonymous variable somewhere in memory we don't know where that is we 内存中某个地方的匿名变量，我们不知道那是哪里
don't know the name of that variable it doesn't have a name it's anonymous that 不知道该变量的名称，它没有一个匿名名称，
variable is gonna be an integer then Java is gonna copy the value of x into 变量是整数，然后Java将x的值复制到
that memory space and then it will add these two numbers together this is what 该内存空间，然后它将这两个数字加在一起，这就是
we call implicit casting let me type it here implicit casting that means 我们称之为隐式强制转换，让我在此处键入隐式强制转换，这意味着
automatic casting or automatic conversion we don't have to worry about 自动转换或自动转换，我们不必担心
it whenever we have a value and that value can be converted to a data type 只要有值就可以将其转换为数据类型
that is bigger casting or conversion happens implicitly or automatically so 较大的转换或转换会隐式或自动发生，因此
byte can be automatically converted to short and this can be converted to int 字节可以自动转换为short，也可以转换为int
and long okay now what about floating-point numbers 现在好了，浮点数呢
let's look at an example I'm gonna change this to a double one point one 让我们看一个示例，我将其更改为双1点
now here we have a compilation error because on the right side of the 现在在这里，我们有一个编译错误，因为在
assignment operator we have a floating-point number a double on the 赋值运算符我们有一个浮点数
left side we have an integer so we need to change this to double now when we 左侧，我们有一个整数，因此我们需要将其更改为现在的两倍
execute this code we're gonna get 3.1 let's verify this there you go now let's 执行此代码，我们将得到3.1让我们验证一下，现在开始吧
see how casting happens here in this case we're dealing with a double and an 看看在这种情况下这里是如何进行投射的
integer an integer is less precise than a double because in a double we can have 整数整数不如双精度，因为在双精度中我们可以拥有
digits after the decimal point so in this example Java is going to 小数点后的数字，因此在此示例中，Java将
automatically cast this integer to a double so that will be two point zero 自动将此整数转换为双精度数，以便成为零的两点
and then two point zero will be added to one point one okay so back to this chain 然后将两点零加到一个点就可以了，所以回到这个链
here we're gonna have float and then double so as a general rule of thumb 在这里，我们要浮起来，然后再翻一番，作为一般经验法则
implicit casting happens whenever you're not gonna lose data there is no chance 每当您不丢失数据时都将发生隐式强制转换
for data loss now what if you want Y to be an integer so in this example we 现在要解决数据丢失的问题，如果您希望Y为整数，那么在此示例中，我们
don't care about the digits after the decimal point you want to see three on 不要在意小数点后的数字
the terminal how should we do this this is where we should explicitly cast the 终端我们应该如何做到这一点，这是我们应该显式转换
result so we should cast X to an integer like this parentheses int this is 结果，因此我们应该将X强制转换为这样的整数，将其括入int这是
explicit casting we convert X to an integer so the result would be one 显式转换我们将X转换为整数，因此结果将为1
without a decimal point one will be added to 2 and Y would be three take a 如果没有小数点，则将1加到2，将Y加上3，则取一个
look there you go so this is all about implicit and explicit casting now this 看那里，所以这就是隐式和显式转换的全部
explicit casting can only happen between compatible types so all these types are 显式转换只能在兼容类型之间发生，因此所有这些类型都是
compatible because they're all numbers but we cannot cast a string to a number 兼容，因为它们都是数字，但是我们不能将字符串转换为数字
in other words if X was a string like this let's say 1 换句话说，如果X是这样的字符串，那么说1
we cannot cast eggs to an integer because they are not compatible so how 我们不能将鸡蛋转换为整数，因为它们不兼容，如何
do we do this well for all these primitive types you 我们对所有这些原始类型都做得很好吗
have learned you have wrapper classes so in Java we have a class which is a 已经了解到您有包装器类，因此在Java中，我们有一个类
reference type called integer this class is defined in Java the Lang package and 引用类型，称为整数，此类在Java Lang包中定义，并且
in this class we have a method called parse int so this method takes a string 在此类中，我们有一个称为parse int的方法，因此该方法需要一个字符串
and returns an integer so integer is the wrapper class for the int primitive type 并返回一个整数，因此integer是int基本类型的包装器类
we also have short and in this class we have parse short so it takes a string 我们也有short，在这个类中有parse，所以需要一个字符串
and returns a short similarly we have float and double and obviously the name 并返回一个类似的short，我们有float和double，显然是名字
of these metas are different so here we have parse float so back to this example 这些元数据是不同的，所以这里我们解析浮点数，回到本例
let's say we get X as a string and we want to convert it to an integer this is 假设我们以字符串形式获取X，我们希望将其转换为整数，这是
how we do it integer dot parse int we pass X here and 我们如何做整数点解析int我们在这里传递X
then add it to take a look so we get 3 you might be curious why this matters 然后添加它看看，我们得到3您可能会好奇为什么这很重要
why should we parse or convert a string to a number to add it to something else 我们为什么要解析或将字符串转换为数字以将其添加到其他内容
well pretty much in most frameworks for building user interfaces whether you're 在大多数用于构建用户界面的框架中，无论您是
building a desktop or a mobile application or web application we always 建立我们一直以来的桌面，移动应用程序或Web应用程序
receive input from the user as a string so if you have a form with a bunch of 从用户那里接收作为字符串的输入，因此，如果您的表单中有一堆字符串
text boxes or drop-down lists almost always we get values as a strings so 文本框或下拉列表中，几乎总是以字符串形式获取值，因此
that's why we need to convert these strings to their numeric representation 这就是为什么我们需要将这些字符串转换为数字表示形式的原因
ok now what if X is a floating-point number here what will happen when we try 好吧，现在如果X是一个浮点数怎么办？
to parse this as an integer let's take a look once again we get an 将此解析为整数，让我们再次看一下
exception which is how Java reports errors to our programs we're going to 异常，这就是Java向我们的程序报告错误的方式
talk about exceptions in detail in the future 将来详细讨论异常
so if the user enters one point one we cannot use this method instead we should 因此，如果用户输入一个点，我们就不能使用此方法，而应该
use float or double let's say double because that's easier double parse 使用float或double可以说double，因为这样更容易进行double分析
double so we parse this number as a double add two to it and then store the double，因此我们将这个数字解析为double，然后将其加2，然后存储
result in a double and then we will get 3.1 导致翻倍，然后我们将得到3.1
beautiful next we're gonna look at the math class for performing mathematical 美丽的下一个我们要看数学课
operations in this tutorial we're going to look at the math class for performing 在本教程中的操作中，我们将看一下用于执行的数学类
mathematical operations so in Java we have this math class that is defined in 数学运算，因此在Java中，我们具有在
Java that Lang package so it's always there we don't need to explicitly import Java那个Lang包，所以它总是存在，我们不需要显式导入
it now this class has a number of useful methods the first method I'm gonna show 现在该类有许多有用的方法，我要展示的第一个方法
you is the round method and with this we can round a floating-point number to a 您是四舍五入方法，因此我们可以将浮点数四舍五入为
whole number so as you can see this method is overloaded which means it's 整数，因此您可以看到此方法已重载，这意味着
implemented twice in the first implementation it takes a float and 在第一个实现中实现两次，它需要一个浮点数，
returns an int and a second implementation it takes a double and 返回一个int和第二个实现，它需要一个double和
returns a log so let's pass 1.1 as a float to this method and store the 返回一个日志，因此让我们将1.1作为一个float传递给此方法并存储
result in an integer like this now we print the result and we get one 结果是这样的整数现在我们打印结果，我们得到一个
beautiful another useful method is seal or sealing which returns the smallest 美丽的另一个有用的方法是密封或密封，返回最小
integer that is greater than or equal to this number so the ceiling of 1.1 is 2 大于或等于此数字的整数，因此1.1的上限为2
now here we have a compilation error because this method returns a double but 现在这里出现编译错误，因为此方法返回一个double但
we're storing the result in an integer so here we need to explicitly cast this 我们将结果存储在整数中，因此在这里我们需要显式转换
to an integer and now you can see the ceiling of this number is 2 we have 到一个整数，现在您可以看到该数字的上限是2
another useful method that is floor so the floor of a number is the largest 另一个有用的方法是下限，因此数字的下限是最大的
integer that is smaller or equal to this number in this case it's gonna be 1 小于或等于此数字的整数，在这种情况下将为1
let's take a look there you go another useful method is max which returns the 让我们看一下，您可以使用另一个有用的方法max来返回
greater of two values and once again this method is overloaded so in the 两个值中的较大者，此方法再次被重载，因此在
first implementation we get two integers we have other implementations for longs 第一个实现我们得到两个整数，我们有很长一段时间的其他实现
floats and doubles so let's pass two integers here one and two this will 浮点数和双精度数，因此让我们在此处传递两个整数，一和二将
return the greater number which is 2 there you go similar to this we have min 返回更大的数字是2，你去类似于这个我们有分钟
is pretty straightforward in the useful method is random for 非常简单，有用的方法是随机的
generating a random value between 0 and 1 once again we get a compilation error 再次生成0到1之间的随机值，我们会收到编译错误
because this method returns a double so let's change that to double now every 因为此方法返回一个double，所以我们现在将其更改为double
time we run this program we get a different number and this number is a 当我们运行该程序时，我们得到一个不同的数字，这个数字是
floating-point number between 0 to 1 now what if you want a number between 0 to 现在，如果要在0到1之间的数字，该怎么办？
let's say 100 instead of 0 to 1 well we simply multiply this by 100 take a look 假设是100而不是0到1，我们只需将其乘以100即可
so every time we run this we get a different number between 0 to 100 now if 因此，每次运行此命令时，如果
we don't want these digits after the decimal point we can either round this 我们不希望小数点后的这些数字可以四舍五入
number or cast it to an integer let me show you so we can call math that round 数字或将其转换为整数让我告诉你，以便我们可以这样算算
and pass the result of this expression so I'm gonna cut this add parenthesis to 并传递这个表达式的结果，所以我要剪掉这个加括号
call the round method and then paste that expression now let's run this code 调用round方法，然后粘贴该表达式，现在让我们运行此代码
so every time we get a double we still have the fraction here so we can change 所以每次我们加倍时，这里仍然有分数，因此我们可以更改
the type to an INT now we have a compilation error because the round 类型为INT现在我们有一个编译错误，因为
method returns a long but here we have declared an integer this is one of those 方法返回一个long值，但是在这里我们声明了一个整数，它是其中之一
cases where implicit casting cannot happen because we have a value that is 因为我们有一个值是
represented in 8 bytes of memory and you want to store that in a variable that 表示为8个字节的内存，您想将其存储在
has only 4 bytes of memory so implicit casting doesn't work but we can use 只有4个字节的内存，因此隐式强制转换不起作用，但是我们可以使用
explicit casting because we know the result of this expression is a number 显式强制转换，因为我们知道此表达式的结果是一个数字
between 0 to 100 so we can definitely store it in an integer so let's add int 在0到100之间，因此我们绝对可以将其存储为整数，因此我们将int加
here now let's run this again there you go now what if we don't use the round 现在在这里，让我们再次运行它，那里，你现在走，如果我们不使用回合
method here let's see what happens so I'm gonna remove the call to the round 方法，让我们看看会发生什么，所以我要删除对回合的调用
method and simply I apply this casting over here let's see what we get now 方法，然后简单地将这个转换应用到这里，让我们看看我们现在得到了什么
every time we run this program we get 0 do you know why because here we're 每次我们运行该程序时，我们都会得到0，您知道为什么吗，因为在这里
applying this casting to the result of this method call not this entire 将此转换应用于此方法调用的结果，而不是整个
expression as you saw earlier every time we call the random method it generates a 正如您在前面每次调用random方法时看到的那样，它会生成一个
number between 0 to one so when we cast that number to an 0到1之间的数字，所以当我们将该数字转换为
integer we'll lose the fraction we always get zero and zero times 100 is 整数，我们将失去分数，我们总是得到零，零乘以100是
zero to solve this problem we need to wrap this entire expression in 零以解决此问题，我们需要将整个表达式包装在
parenthesis so we add parentheses around this and now let me run this we get an 括号，因此我们在此周围加上括号，现在让我运行它，我们得到一个
integer between 0 and 100 quite often when we worked with numbers we need to 在处理数字时，通常需要0到100之间的整数
format them as currency values for example we might have a number like this 将它们格式化为货币值，例如，我们可能会有这样的数字
imagine this is the price of a product to display this to the user you want to 想象这是产品价格，向您想要显示给用户的价格
add a dollar sign here and separate every three digits with a comma or if 在此处添加美元符号，并用逗号分隔每三个数字，或者
you might have a number like 0.1 and we want to represent this as a percentage 您可能有一个像0.1这样的数字，我们想用一个百分比来表示
value so instead of 0.1 you want to display 10% so in this tutorial I'm 值，而不是0.1，您想显示10％，所以在本教程中
gonna show you how to format numbers in Java we have this class number format 将向您展示如何使用Java格式化数字，我们拥有此类的数字格式
that is defined in Java the text package in this package we have a lot of classes 用Java定义的文本包中的文本包我们有很多类
for handling text dates numbers and so on so let's press ENTER here it's 用于处理文本日期数字等等，所以我们在这里按ENTER
important on the top beautiful now I want to declare a variable so let's give 重要的是，现在我想声明一个变量，所以让我们给
this variable a name currency now we need to instantiate this new number 这个变量是一个名称货币，我们现在需要实例化这个新数字
format however we get a compilation error here let's take a look number 格式，但是这里出现编译错误，让我们看一下数字
format is abstract it cannot be instantiated so in Java we have this 格式是抽象的，无法实例化，因此在Java中
concept of abstract classes so some classes are abstract and they're 抽象类的概念，所以有些类是抽象的，它们是
basically like a half-baked cake we cannot use the new operator to create an 基本上就像一个半生的蛋糕，我们不能使用new运算符来创建
instance of them we're going to talk about abstract classes and why they 他们的实例，我们将讨论抽象类，以及为什么它们
exist later in the course for now remember that we cannot create an 存在于课程的后期，请记住，我们无法创建
instance of the number format class because it's abstract so there is 数字格式类的实例，因为它是抽象的，所以有
another way let me show you this class has a bunch of methods that start with 让我向您展示这种方法的另一种方式是一堆以
get so we have get currency instance and when we call this method this will 得到，所以我们得到了货币实例，当我们调用此方法时，
create an instance of the number format class and return it so instead of using 创建数字格式类的实例并返回它，而不是使用
the new operator we're gonna use this method here this is what we call a 我们将在这里使用此方法的新运算符，这就是我们所说的
factory method because it's like a factory it creates new are 工厂方法，因为就像创建新工厂一样
jects now look at the return type of this method it's a number format object 现在，请查看此方法的返回类型，它是一个数字格式的对象
okay so we call this method now we get the return value and store it in a 好吧，我们现在调用此方法，我们得到返回值并将其存储在
variable of type number format called currency okay let me say improperly zoom 类型数字格式的变量称为货币，可以，我说的是不正确的缩放
out so you can see all the code there you go so on the right side of the 出来，这样您就可以看到所有代码，在右侧
assignment operator we're calling the get currency instance method we get the 赋值运算符，我们正在调用get Currency实例方法，
result which is a number from an object and store it in this variable okay now 结果是来自对象的数字并将其存储在此变量中
I'm gonna zoom in so you can see clear a name 我要放大，以便您可以看到一个清晰的名字
alright that's better now this object has a method for formatting values once 好了，现在更好了，该对象具有一种格式化值的方法
again you can see this method is overloaded we have multiple 再次您可以看到此方法已重载，我们有多个
implementations we can give it a long or a double or whatever so I'm gonna call 我们可以给它加长或加倍，等等，我称之为
this method and pass a value like one two three four five six seven point 这种方法并传递一个像一二三四五六七点的值
eight nine one so we have three digits after decimal and a few other digits 八九一，所以我们在小数点后有三位数和其他几位数
here now this method will return a string representation of this number 现在，此方法将返回此数字的字符串表示形式
formatted as a currency so let's get that and store it in a string variable 格式化为货币，让我们获取并将其存储在字符串变量中
like result and then we're gonna print result on a terminal see what we get 像结果一样，然后我们将在终端上打印结果，看看得到了什么
so we get this dollar sign every three digits are separated using a comma and 所以我们用逗号将美元符号每三位数分开
we only have two digits after the decimal point so this class is very 我们小数点后只有两位数字，所以这个类非常
handy in formatting numbers as currencies we have another method for 在将数字格式化为货币时很方便，我们还有另一种方法
formatting a number as a percent and that is get percent instance right now 将数字格式化为百分比，然后立即获取百分比实例
it returns an instance of the number format class specialized for formatting 它返回专门用于格式化的数字格式类的实例
numbers as a percent so we need to rename this variable from currency to 数字作为百分比，因此我们需要将此变量从Currency重命名为
percent now we don't want to manually change this because there are multiple 现在我们不想手动更改此百分比，因为有多个
references to this variable this other reference was not updated so let me show 对该变量的引用，其他引用未更新，因此让我展示
you the proper way to rename objects in intellij if you right click here you can 您可以使用正确的方法在intellij中重命名对象，如果右键单击此处可以
see this refactor menu and here we have rename now look at this shortcut on the 看到这个重构菜单，这里我们已经重命名了，现在在
right side unfortunately it's not visible in my recording window but on 不幸的是，它在我的录制窗口中不可见，但在右侧
Mac is Shift + F I always use shortcuts because they're Mac是Shift + F，我一直使用快捷键，因为它们
faster so let's press shift and f6 now we get this red box and below this we 更快，所以我们先按shift和f6键，现在我们得到一个红色框，下面是红色框
can see a few suggestions for a better name we can choose one of these or pick 可以看到一些更好的名字的建议，我们可以选择其中之一或选择
our own name I'm gonna change this to percent and note that as I'm typing this 我们将使用自己的名字将其更改为百分比，并注意在输入
the other reference gets updated automatically so this is very helpful 其他参考会自动更新，因此非常有帮助
now let's press Enter okay we're done with renaming now let's 现在让我们按Enter好吧，我们已经完成了重命名。现在让我们
pass a different value here so let's say we have a number like 0.1 we want to 在这里传递一个不同的值，所以我们想有一个像0.1这样的数字
format this as a percent let's run the program there you go you get 10% 将其格式化为百分比让我们运行该程序，您得到10％
beautiful now let me show you a cool technique in this program we don't 现在很漂亮，让我向您展示此程序中的一项很酷的技术，我们不
really need this percent object because we have used it in a single place it 确实需要这个百分比对象，因为我们在一个地方使用了它
would make sense to have this as a separate variable if you have multiple 如果您有多个变量，将其作为单独的变量将是有意义的
references to it so what can we do here we can completely get rid of this object 引用它，所以我们在这里可以做些什么，我们可以完全摆脱这个对象
so let's delete this piece of code we're basically calling this method of the 因此，让我们删除这段代码，我们基本上是在调用此方法
number format class as you know this method returns a number format object so 您知道数字格式类，此方法返回一个数字格式对象，因此
right after calling this method and before the semicolon we can use the dot 在调用此方法之后，在分号之前，我们可以使用点
operator to access the methods or members of the number format object so 操作员访问数字格式对象的方法或成员，以便
here we call the format method straight away and pass our value this is what we 在这里，我们立即调用format方法并传递我们的值，这就是我们
call method chaining we're chaining multiple methods together so here's one 调用方法链接我们将多个方法链接在一起，所以这是一个
method and here's another method now this returns a string so we can store it 方法，这是另一个方法，现在它返回一个字符串，因此我们可以存储它
in this result variable let me cut this expression from here and put it over 在这个结果变量中，让我从此处剪切此表达式并将其放在
here now we have double semicolons I'm going to delete one of them 现在我们这里有双分号，我要删除其中一个
all right beautiful in this tutorial I'm gonna show you how to read input from 好的，在本教程中，我将向您展示如何从中读取输入
the user in Java we have this scanner class that is defined in Java did util 在Java用户中，我们拥有在Java did util中定义的此扫描程序类
package let's import this and create a scanner object so new scanner now here 包，让我们导入它并创建一个扫描仪对象，所以现在这里有新的扫描仪
inside this parenthesis we need to specify where we're gonna read data from 在此括号内，我们需要指定要从何处读取数据
we're gonna read it from the terminal window are we gonna read it from a file 我们将从终端窗口中读取它我们将从文件中读取它
or what to work with the terminal window we type system dot in this is one of the 或使用终端窗口的方式，我们在其中键入系统点是
fields in the system class a fill as I told you before is like a variable that 我之前告诉您的系统类中的字段就像一个变量，
we define in a class so we have system that in we also have system that out 我们在一个类中定义，所以我们拥有系统，我们也拥有系统
which we used to print something on the terminal right now let's use system that 我们以前在终端上打印一些东西，现在让我们使用
in and terminate this statement with a semicolon now this object has a bunch of 并以分号终止该语句，现在该对象有一堆
methods for reading data and all these methods start with next so we have next 读取数据的方法，所有这些方法都从next开始，所以我们接下来
byte for reading a byte we have next line for reading a line we have next 字节，用于读取一个字节，我们有下一行字节，用于读取我们的下一行
boolean for ending a boolean and so on so let's call the next byte method and 布尔值以结束布尔值，依此类推，所以我们调用下一个字节方法，
see what we get this returns a byte value so we can 看看我们得到什么，它返回一个字节值，所以我们可以
store it in a byte variable let's say someone's age and then we print it on a 将其存储在字节变量中，假设某人的年龄，然后将其打印在
terminal saying you are plus h so here we're concatenating a string with a byte 终端说你加h，所以在这里我们将一个字符串和一个字节连接起来
and in this scenario we're gonna have implicit casting or implicit type 在这种情况下，我们将具有隐式强制转换或隐式类型
conversion so Java will automatically convert this byte variable here tray 转换，因此Java会在此处托盘自动转换此字节变量
string so they can be added together okay now let's run this program and see 字符串，以便可以将它们添加在一起好吧，现在让我们运行该程序，看看
what happens so here in the terminal window we can type 20 enter and it says 这样会发生什么，所以在终端窗口中，我们可以输入20 enter，它说
your 20 beautiful but this is pretty boring let's add a label here and ask 您的20个漂亮，但这很无聊，让我们在此处添加标签并询问
the user to type something for example before reading data we're gonna call the 例如，用户在读取数据之前先键入一些内容，我们将其称为
print line method and say age : now let's run the program so we get this 打印行方法并说出年龄：现在让我们运行程序，以便我们得到
label here however whatever we type will appear on the next line this is because 标签在这里，但是我们键入的任何内容都会出现在下一行，这是因为
the print line method adds a new line after this label the Soviets issue 打印线方法在此标签后由苏联人添加了新行
recall the print method now let's run the program one more time we get this 回想一下打印方法，现在让我们再运行一​​次程序，
popup box because our program hasn't finished execution so we need to tell 弹出框，因为我们的程序尚未完成执行，所以我们需要告诉
IntelliJ that you want to stop and rerun this program all right now whatever we 您想要立即停止并重新运行此程序的IntelliJ
type appears right in front of this label beautiful ENTER we are 20 now what 类型出现在此标签的前面美丽的Enter我们现在20岁了
if we type a floating-point number like 20 point 1 we get an exception because 如果我们输入一个像20 point 1这样的浮点数，则会得到一个异常，因为
this method can only parse white values if you want to get a floating-point 如果要获取浮点，则此方法只能解析白色值
number we need to call next float or next double what if you want to read our 我们需要打电话给下一个float或下一个double如果您想阅读我们的数字
string we don't have next string we have next and next line let's look at the 我们没有下一个字符串我们有下一个和下一行我们来看一下
differences so first I'm going to call the next method here we have a 差异，所以首先我要在这里调用下一个方法
compilation issue because this method returns a string I'm gonna change this 编译问题，因为此方法返回一个字符串，我将更改此字符串
to a string let me collapse this that's better 串起来让我收起来更好
we should also rename this variable so what was the shortcut Shift + f6 now 我们还应该重命名此变量，所以快捷键Shift + f6现在是什么
let's train just a name enter beautiful and one last time we should also update 让我们训练一个名字输入漂亮的名字，最后一次我们也应该更新
the label name run the program so I'm gonna type my name here Marsh it says 标签名称运行程序，所以我要在此处键入我的名字
your Amash pretty under for one more time this time I'm gonna type my 这次，您的Amash会再下一次我要输入我的
full-name maha Madani but we don't get the last name here's the reason these 全名玛哈·马达尼（Maha Madani），但我们没有姓氏，这是这些的原因
wars that we have here these are called tokens every time we call the next 我们在这里进行的战争每次称为下一场战争时都称为令牌
method it reads one token so here we have a space we have two tokens and we 它读取一个令牌的方法，所以在这里我们有一个空间，有两个令牌，我们
need to call the next method two times to get the full name the first time we 我们第一次需要调用两次next方法来获取全名
call it it returns Marsh the second time we call it it returns Hamid ani then we 叫它返回沼泽，第二次我们叫它返回Hamid ani，然后我们
need to combine this suit together this is not ideal so that's when we use the 需要将这套西装结合在一起，这是不理想的，所以当我们使用
next line method with this method we get the in turn line that the user enters no 使用此方法的下一行方法，我们得到用户未输入的反行
matter how many spaces or tabs are there take a look so Marsh 不管有多少个空格或制表符，请看一下沼泽
comma Donny and we get the full name now what if I type a few spaces before my 逗号Donny，我们现在得到全名，如果我在我的空格之前输入几个空格
name let's see what happens so those spaces also appear here and 名称，让我们看看会发生什么，以便这些空格也出现在此处，
this looks a little bit odd this is where we use the trim method remember we 这看起来有点奇怪，这是我们使用trim方法的地方，请记住
trim we can get rid of all these white spaces before or after a string so this 修剪，我们可以去除字符串之前或之后的所有这些空白，因此
next line method returns a string that we are storing in this variable right 下一行方法返回一个我们要存储在此变量中的字符串
now just before storing the result in this variable here we can use the dot 现在在将结果存储在此变量之前，我们可以使用点
operator to access the members of this string object so we call the trim method 操作员访问此字符串对象的成员，因此我们调用trim方法
and then store the result in this variable once again we're chaining 然后将结果再次存储在此变量中
multiple methods here let's run the program so a few spaces Maharani and you 这里有多种方法，让我们运行程序，以便在Maharani和您之间留一些空格
get this beautiful output 得到这个美丽的输出
right now it's time for a project I want you to use what you have learned in a 现在该是一个项目的时候了，我希望您使用从中学到的知识
section and build a mortgage calculator like this so when you run this program 部分并构建像这样的抵押贷款计算器，以便在您运行该程序时
we get a few questions the first question is the principal or the amount 我们得到几个问题，第一个问题是本金或金额
of loan we want to get let's say $100,000 the second question is the 我们要获得的贷款金额为100,000美元，第二个问题是
annual interest rate let's say three point nine two percent and the third 年利率让我们说三点九百分之二三
question is period in years let's say you want to get a loan for thirty years 问题是几年的期限，假设您想获得三十年的贷款
so this program calculates our monthly payments and displays it as a currency 因此，此程序会计算我们的每月付款并将其显示为货币
this is a great exercise for you to practice all the materials you learn in 这是锻炼您学习所有材料的绝佳练习
this section now before you get started I want to give you a few hints here is a 现在开始本节之前，我想给您一些提示，这里是
formula for calculating the mortgage or the monthly payments I found this page 我在此页面上计算抵押或每月付款的公式
on wikiHow comm is called calculate mortgage payments so let's see how this Wiki上的comm是如何计算抵押贷款付款的，所以让我们看看
works mortgage equals P or principal or the amount we're gonna loan multiplied 工程抵押贷款等于P或本金，或者我们要贷的金额乘以
by R which is our monthly interest rate this is very important so this number 以R为单位，这是我们的每月利率，这一点非常重要，因此该数字
that we get here is our annual interest rate we need to divide this by 12 also 我们到达这里是我们的年利率，我们还需要将其除以12
take into account that this number is represented as a percent to calculate 考虑到此数字表示为要计算的百分比
the actual interest rate you need to divide this number by 100 so in this 您需要将该数字除以100的实际利率
example the interest rate is zero point zero three nine two so whatever the user 例子利率是零点零三九二二所以无论用户
enters divided by 100 and then divided by twelve to get to the monthly interest 输入除以100，然后除以12得出月利息
rate now we have this monthly interest rate we need to multiply this by this 现在我们有这个月利率我们需要乘以这个
expression here we need to add 1 to this interest rate and raise it to the power 此处的表达式需要将这个利率加1并提高到幂
of n where n is the number of payments so we need to multiply this number by 12 的n，其中n是付款次数，因此我们需要将此数字乘以12
or 12 months to calculate the number of payments now to raise this number to the 或12个月来计算现在的付款次数，以将该数目提高到
power of n you need to use the power method of the math class so this math n的幂需要使用数学类的幂方法，因此此数学
class has this power or power method that takes two arguments or two values a 类具有此power或power方法，该方法带有两个参数或两个值
and B so go ahead and spend ten to fifteen minutes on this exercise when 和B，所以继续练习十到十五分钟，
you're done come back see my solution 你做完了回来看看我的解决方案
all right let's see what I've done here and by the way don't worry if your coat 好吧，让我们看看我在这里做了什么，顺便说一句，不用担心你的外套
is different from mine we all think and coat differently so 与我不同，我们都认为和外套不同
it's perfectly fine if your code is different 如果您的代码不同，那会很好
just look at my coat see what I've done here and see if there are ways you can 看看我的外套，看看我在这里做了什么，看看是否有办法
improve your coat that's what matters okay so here in our 改善你的外套好不好，所以在这里
main method first I've declared two final variables or constants 首先我声明了两个最终变量或常量
the first one is months in year which I've set to 12 and the second one is 第一个是一年中设定为12的月份，第二个是
percent which I've set to 100 the reason I declared this constant is that I 我将其设置为100的百分比我声明此常数的原因是
didn't want to have magic numbers in this code so over here where we 不想在这段代码中有幻数，所以在这里
calculate the monthly interest we get the annual interest divided by percent 计算每月的利息，我们得到的年度利息除以百分比
and then months in here this code is very self-explanatory someone else 然后几个月的时间里，这段代码是非常不言自明的
reading this code we'll have no problem understanding what's going on here in 阅读此代码，我们将毫无疑问地了解这里发生了什么
contrast if you had a magic number here like divided by 12 and then for whatever 如果您有一个神奇的数字，例如12除以
someone else reading this code would wonder what is this for doing here what 其他人读这段代码会想知道这是做什么的
is 12 it's quite obvious to you that 12 is the number of months in a year but 是12对您来说很明显12是一年中的月份数
trust me sometimes other people cannot see this straight away so as a best 相信我，有时候其他人看不到这个，所以最好
practice avoid magic numbers in your code always use constants or final 练习避免代码中的幻数始终使用常量或最终形式
variables to describe them so let's revert this back all right so 变量来描述它们，所以让我们将其恢复原样吧
after declaring this constant I've created this scanner object here we ask 声明此常量后，我在这里创建了此扫描器对象
the first question principle and we read the answer as an 第一个问题原理，我们将答案读为
integer I thought integer is a good datatype for 我认为整数是一个很好的数据类型
storing the principle because short is not enough with short we can store a 存储原理，因为短是不够的，短我们可以存储一个
maximum of $32,000 that's not enough what if someone wants to find us 最高$ 32,000这还不够，如果有人想找到我们
$1,000,000 so int is good and it allows us to store a value up to two billion $ 1,000,000，所以int是好的，它使我们可以存储高达20亿的值
next we ask the second question annual interest rate we read this as a float 接下来，我们问第二个问题，即年利率
here I could use double but the interest rate is a small number so float is 在这里我可以使用double，但是利率是一个很小的数字，所以float是
sufficient for that we don't really need double so we get the annual interest and 足够了，我们真的不需要加倍，所以我们得到了年度利息，
then based on that we calculate the monthly interest also see how I have 然后基于此我们可以计算出每月利息
named my variables all variables have a proper meaningful names there are no 给我的变量命名所有变量都有一个适当的有意义的名称，没有
magic words here like MI as it short for monthly interest 像MI这样的魔术词，它是每月利息的缩写
or m1 or m2 do not use magic names for your variables always use meaningful and 或m1或m2不要使用魔术名称，因为您的变量始终使用有意义的
descriptive names alright next we get the period we read this as a byte 描述性名称好，接下来我们得到以字节为单位的句点
because the maximum number we want to support is 30 so one byte is sufficient 因为我们要支持的最大数目是30，所以一个字节就足够了
to store the number 30 or anything smaller now based on the number of years 根据年数存储数字30或现在更小的数
we calculate the number of payments note the camel notation here I've capitalized 我们计算付款次数请注意我在这里大写的骆驼符号
the first letter of every word except the first word so we get the Earth's and 每个单词的第一个字母（第一个单词除外），因此我们得到了地球的
x months in here again the code is very self-explanatory X个月再次在这里的代码是非常不言自明的
once we collect all this data then we calculate the mortgage so we get a 一旦我们收集了所有这些数据，我们就计算了抵押贷款，因此我们得到了
principal multiplied by this expression and then divide it by this other 主体乘以该表达式，然后除以另一个
expression let's have a quick look here now this looks a little bit complicated 表达式让我们快速浏览一下现在看起来有点复杂
so if you're not download my source code look below this video I'm attached it 因此，如果您不下载我的源代码，请看下面的视频
for you to download and finally after we calculate the mortgage we use the number 供您下载，最后在计算抵押金后，我们使用数字
format class to format this value as a currency so we get a result store it in format类，用于将该值格式化为货币，因此我们将结果存储在
this string mortgage format it and print it over here now here we can also avoid 这个字符串抵押格式化它并在这里打印出来在这里我们也可以避免
declaring this variable and simply add this expression over here but it decided 声明这个变量，然后在这里简单地添加这个表达式，但是它决定了
to do this to increase the readability of this code otherwise this line would 这样做以增加此代码的可读性，否则此行将
be so long but that's just my personal preference you don't have to follow this 这么长，但这只是我个人的喜好，您不必遵循这一点
alright so this was my implementation of the mortgage calculator 好吧，这就是我对抵押计算器的实现
however this program has a number of problems the first problem is input 但是该程序有很多问题，第一个问题是输入
validation so if we run this and enter a non numeric value like XYZ our program 验证，因此如果我们运行此程序并输入非数字值（如XYZ），则程序
crashes or as another example if we enter a negative value here our program 崩溃或作为另一个示例，如果我们在程序中输入负值
is not gonna behave properly so that's where conditional statements come to the 将无法正常运行，所以这就是条件语句
rescue in the next section we're going to talk about conditional statements you 在下一节中我们将讨论条件语句
will learn how to use these statements to validate the values entered by the 将学习如何使用这些语句来验证由
user so that brings us to the end of this section I hope you learn a lot and 用户，这使我们到本节的结尾，希望您能学到很多知识，
thank you for watching 谢谢你的收看
so in this section you'll learn all about variables and constants you learn 因此，在本节中，您将学到所有有关变量和常量的知识
about primitive and reference types you learn that primitive types store simple 关于基本类型和引用类型，您将了解基本类型存储简单
values but reference types hold references to complex objects that's why 值，但引用类型包含对复杂对象的引用，这就是为什么
we call them reference types you also learn about casting and type conversion 我们称它们为参考类型，您还将了解有关转换和类型转换的信息
you learn about two types of casting implicit and explicit you learn how to 您学习了隐式和显式两种类型的转换，您将学习如何
work with numbers strings and arrays and finally you learned how to read input 使用数字字符串和数组，最后您学会了如何读取输入
from the terminal I hope you learned a lot I've been enjoying the course so far 我希望从航站楼学到很多东西，到目前为止我一直很喜欢这个课程
in the next section we're going to look at conditional statements for 在下一节中，我们将研究条件语句
controlling the flow of our programs so I'll see you in the next section 控制我们程序的流程，以便在下一节中介绍您
[Music] [音乐]
hey Marcia I just want to make a quick 嘿，玛西娅，我只是想快点
announcement this course you've been watching is actually the beginning of my 宣布您一直在观看的这门课程实际上是我的开始
complete Java series in this course we only talk about the basics but in my 本课程中完整的Java系列，我们仅讨论基础知识，但是在我的课程中
complete series we go way beyond that so if you're serious about Java if you're 完整的系列文章，我们将超越其他内容，如果您对Java认真的话，
looking for a job as a Java developer I highly encourage you to enroll in my 寻找Java开发人员的工作我强烈建议您注册我的
complete Java series it's exactly the same structure the same quality but it 完整的Java系列，它的结构完全相同，质量相同，但是
has way more content plus you will get a certificate of completion that you can 有更多内容的途径，您还将获得结业证书，可以
add to your resume so if you're interested I put the link down below you 添加到您的简历，所以如果您有兴趣，我将链接放在您下面
can get the course with a discount and if you're not happy for any reasons you 可以打折获得该课程，如果由于任何原因您不满意，您可以
can ask for a refund within the first 30 days so I hope to see you in the course 可以在前30天内要求退款，所以希望在课程中见到您
in this section we're going to look at control flow or controlling the flow of 在本节中，我们将研究控制流程或控制流程
execution of our programs you're gonna start off by talking about comparison 执行我们的程序，您将从谈论比较开始
operators for comparing values then I'm gonna talk about logical operators like 用于比较值的运算符，然后我将讨论诸如
the logical and logical or and logical not we use these operators for 逻辑与逻辑或与逻辑不，我们将这些运算符用于
implementing real world rules and then we're gonna talk about three different 执行现实世界的规则，然后我们将讨论三种不同的
types of control flow statements we're going to talk about conditional 我们将要讨论的条件控制语句类型
statements for making decisions in our programs then we're going to talk about 在程序中做出决策的声明，然后我们将讨论
loops for executing code repeatedly and finally we're going to revisit our 循环重复执行代码，最后我们将重新讨论
mortgage calculator and add error handling to it 抵押计算器并向其中添加错误处理
so if a user enters an invalid value we keep asking them to try again all right 因此，如果用户输入的值无效，我们会继续要求他们再次尝试
now let's jump in and get started we're gonna start this section by talking 现在让我们开始吧，我们将通过谈论开始本节
about comparison operators we use these operators to compare primitive values 关于比较运算符，我们使用这些运算符来比较原始值
for example our x on y equal or not so I'm gonna declare two integers x and y 例如我们在y上的x相等或不相等，所以我要声明两个整数x和y
now let's compare this to see if they're equal or not 现在让我们比较一下，看看它们是否相等
so first I'm gonna add this print line statement now to compare these variables 所以首先我要添加此打印行语句以比较这些变量
we type x equals y so here we have two equal signs and this is the Equality 我们输入x等于y，所以这里有两个等号，这就是等式
operator don't confuse this with a single equal sign that we use for 运算符不要将此与我们用于的单个等号混淆
assignment that is the operator used over here okay so two equal signs 分配是在这里使用的运算符好，所以两个等号
repairs the Equality operator now when we run 我们运行时立即修复Equality运算符
this program we get true because these values are equal this expression that we 这个程序之所以成立，是因为这些值等于这个表达式，
have here is called a boolean expression so earlier I told you that an expression 这里有一个称为布尔表达式，所以我之前告诉过你一个表达式
is a piece of code that produces a value this piece of code produces a boolean 是一段产生值的代码，这段代码产生一个布尔值
value true or false that's why we refer to it as a boolean expression we also 值true或false这就是为什么我们将其称为布尔表达式
have the inequality operator so when we run this program we're gonna see false 有不等式运算符，所以当我们运行该程序时，我们将看到false
let's take a look we get false because these two variables are equal so the 让我们看一下我们得到的是假的，因为这两个变量相等，所以
expression X is not equal to Y returns false we also have greater than greater 表达式X不等于Y返回false我们也有大于大于
than or equal to less than and less than or equal to so if I change Y to 2 this 小于等于小于和小于等于所以如果我将Y更改为2
expression is gonna evaluate to true because X is less than or equal to Y 由于X小于或等于Y，因此表达式将为true
take a look there you go next we're going to talk about logical 看一看，下一步，我们将讨论逻辑
operators in the last tutorial you learn that a boolean expression produces a 在上一教程中，您将了解一个布尔表达式会产生一个
boolean value now there are times we want to combine multiple boolean 布尔值现在有些时候我们想合并多个布尔值
expressions let me show you so I'm going to declare an integer called temperature 表达式让我告诉你，所以我将声明一个称为温度的整数
and set it to 22 next we declare a boolean variable is warm now we want to 并将其设置为22接下来，我们声明一个布尔变量为暖，现在我们想
check to see if temperature is greater than 20 and less than 30 so we write a 检查温度是否大于20且小于30，所以我们写了一个
boolean expression like this temperature greater than 20 here we need to use the 布尔表达式，例如此温度大于20，在这里我们需要使用
end operator so these two ampersands represent the logical and operator in 最终运算符，因此这两个与号代表逻辑和
Java after this we add our second condition Java之后，我们添加第二个条件
temperature less than 30 now if both these conditions are true the result of 如果这两个条件都成立，则温度现在低于30
this boolean expression is going to be true 这个布尔表达式将是正确的
otherwise if at least one of these is false the result would be false let's 否则，如果其中至少一项为假，则结果为假。
take a look so I'm gonna print is warm on a terminal 看看，我要在终端上打印温暖
the result is true but if you change the temperature to let's say 12 the result 结果是正确的，但是如果您将温度更改为12
is gonna be false and this is how this works Java will evaluate this expression 将是错误的，这就是它的工作方式Java将评估此表达式
from left to right first it looks at the first condition the first condition is 从左到右首先查看第一个条件，第一个条件是
false because temperature is not greater than 20 so because this expression is 否，因为温度不大于20，所以因为此表达式是
false it doesn't matter what we have after the and operator Java will ignore false和and运算符Java将忽略之后我们拥有什么都没关系
the other expressions because the and operator will return true if both 其他表达式，因为如果两个操作符和操作符都将返回true
conditions are true okay now let's look at another operator that is the or 条件是正确的，现在让我们看看另一个运算符
operator so I'm going to delete all this code and start with a new example let's 运算符，所以我将删除所有这些代码，并以一个新示例开始
declare a boolean has high income we set it to true and another boolean has good 声明布尔值高的收入，我们将其设置为true，而另一个布尔值的高收入
credit we set this to true as well so let's say we're building an application 信用，我们也将其设置为true，所以说我们正在构建一个应用程序
for processing loans we want to see if a new applicant is eligible for a loan or 为了处理贷款，我们想看看新申请人是否有资格获得贷款或
not so we declare another boolean is 不是，所以我们声明另一个布尔值是
eligible and by the way look at the names I have used for these variables 合格，顺便看看我为这些变量使用的名称
they're very meaningful and descriptive so here's the rule an applicant is 它们非常有意义且具有描述性，因此这是申请人的规则
eligible if they have high income or good credit if one of these conditions 如果他们具有以下条件之一，则符合高收入或良好信用条件：
is true they are eligible so we write has high income or so these two vertical 是的，他们有资格，所以我们写的收入很高，所以这两个垂直
bars represent the or operator so if they have high income or good credit 条形代表或运算符，因此，如果他们有高收入或良好信誉
then they are eligible so with the or operator if at least one condition is 那么他们符合资格，所以如果至少一个条件是
true the result will be true in this example when Java evaluates this true在此示例中，当Java对此进行评估时，结果将为true
expression it starts from the left side this boolean variable is true so it 从左侧开始的表达式，此布尔变量为true，因此它
doesn't matter what we have after Java will not evaluate the rest of this 紧随Java之后的其他内容，我们将不会评估其余的内容
expression it simply returns true however if this variable was false Java 表达式，它只是返回true，但是如果此变量为false，则Java
would continue evaluating this expression hoping that the next boolean 会继续评估此表达式，希望下一个布尔值
value or the next boolean expression is true so this is the or operator and 值或下一个布尔表达式为true，因此这是or运算符，
finally we have the not operator that we use to reverse a value let me show you 最后，我们有一个not运算符，我们用它来反转一个值，让我告诉你
how that works so 怎么运作的
let's declare another boolean variable has criminal record we set this to false 让我们声明另一个布尔变量具有犯罪记录，我们将其设置为false
so here's the rule we want to implement in order for someone to be eligible for 所以这是我们要实施的规则，以便某人有资格
a loan they should either have high income or good credit and they should 他们应该有高收入或良好信用的贷款，他们应该
not have any criminal records so let's see how we can implement this rule we 没有任何犯罪记录，所以让我们看看如何执行此规则
have implemented the first part they should either have high income or good 已经实施了第一部分，他们应该是高收入或良好
credit now we want to make sure they don't have any criminal records so we 现在，我们要确保他们没有任何犯罪记录，所以我们
need to combine the result of this expression with this boolean value using 需要使用以下方法将此表达式的结果与此布尔值进行组合
the and operator so first we wrap this expression in parentheses then we apply and运算符，因此我们首先将此表达式用括号括起来，然后应用
the end operator and here we add has criminal record now you want to make 终端操作员，我们要在此处添加犯罪记录
sure they don't have criminal record and this is where we use the not operator so 确保他们没有犯罪记录，这就是我们使用not运算符的地方，因此
the not operator will reverse the value of this boolean variable or expression not运算符将反转此布尔变量或表达式的值
in this case this variable is set to false so when we apply the not operator 在这种情况下，此变量设置为false，因此当我们应用not运算符时
to it the result would be true so if the first condition is true and the second 对它的结果将为真，所以如果第一个条件为真，第二个条件为真
condition is true then that person is eligible for a new loan so as you can 条件为真，则该人有资格获得新贷款，因此您可以
see these logical operators are very useful in implementing real word rules 看到这些逻辑运算符在实现实词规则中非常有用
next we're going to talk about if statements in this tutorial we're going 接下来，我们将讨论本教程中的if语句
to look at if statements in Java if statements are extremely important 查看Java中的if语句if语句非常重要
because they allow us to build programs that can make decisions based on certain 因为它们使我们能够构建可以基于某些因素做出决策的程序
conditions here's an example in this file we have a bunch of conditions if 条件这是此文件中的一个示例
temperature is greater than 30 perhaps you want to display two messages to the 温度大于30，也许您想显示两条消息到
user it's a hot day drink plenty of water otherwise if it's between 20 and 用户在炎热的日子里要喝大量的水，否则，如果它介于20和
30 you want to print it's a nice day and otherwise we want to print it's a cold 30您想打印这是一个美好的一天，否则我们想打印这是一个寒冷的天气
day so let's see how we can implement these rules in a Java program back to 今天，让我们看看如何在Java程序中实现这些规则
our main file we start by declaring a variable temperature we set it to 32 now 我们的主文件首先声明一个可变温度，现在将其设置为32
we use an if statement followed by a pair of parentheses inside this 我们在其中使用if语句，后跟一对括号
parenthesis we type an expression or a boolean value so 括号中，我们键入一个表达式或布尔值，因此
let's say temp is greater than 30 now if this condition is true the statement 假设现在temp大于30如果条件成立
that we had after this if statement will be executed so let's print it's a hot 在执行if语句之后我们拥有的，所以让我们打印一下
day let's run the program we get this message because temperature is greater 一天，我们运行程序，因为温度更高，我们会收到此消息
than 30 now what if you want to print another message like drink plenty of 现在超过30，如果您想打印另一条消息，例如喝大量
water here we should add curly braces to define a code block so if this condition 在这里水，我们应该添加花括号来定义代码块，所以如果这种情况
is true all the code that we have inside of this block would be executed let's 是的，我们在此块中拥有的所有代码都将被执行，让我们
add another message here drink water okay so this was our first condition now 在这里添加另一个消息喝水好，所以这是我们现在的第一个条件
let's add a second condition so after the right brace we type LS if once again 让我们添加第二个条件，以便在右大括号之后再次键入LS
we add our parentheses and inside these parentheses we type a boolean expression 我们添加括号，并在这些括号内键入一个布尔表达式
if 10 is greater than 20 and it's less than or equal to 30 如果10大于20并且小于或等于30
you want to print a different message like beautiful day now here I haven't 您想在这里打印另一条消息，例如美丽的一天，我还没有
added the braces because we have a single statement so braces are only 添加了花括号，因为我们只有一个语句，所以花括号仅
required if we have multiple statements now this is a little bit controversial 如果我们现在有多个语句是必需的，这有点争议
some people believe we should always add braces whether we have a single 有些人认为，无论是否有一个，我们都应始终添加大括号
statement or not other people like myself believe this creates unnecessary 声明或其他不喜欢我的人相信这会造成不必要的影响
noise in the code in this tutorial I'm gonna add the braces first and then 本教程代码中的噪音我要先添加大括号，然后再添加
remove them so you can see the difference so let's add a pair of curly 删除它们，这样您就可以看到区别，让我们添加一对卷曲的
braces here and finally our last condition so if none of these two 在这里支撑，最后是我们的最后一个条件，所以如果这两个都不
conditions are true let me print a different message so here we simply type 条件是真实的，让我打印不同的消息，所以我们在这里键入
LS we don't have any more conditions so let's add a code block and print cold LS我们没有其他条件了，所以我们添加一个代码块并冷打印
day now let me define a few terms here we have an if statement and this 现在让我定义一些术语这里我们有一个if语句
statement has three clauses or three sections here's the first Clause here's 语句包含三个子句或三个部分，这是这里的第一个子句
the second Clause and here's the third Clause pay attention to how a formatted 第二条和第三条要注意格式化
this if statement so first we have the if Clause the else if 这个if语句，所以首先我们有if子句，else if
and else clauses are placed after these right braces so we have some kind of 而else子句放在这些右括号之后，因此我们有某种
hierarchy here here we have a parent followed by two children now let's get 在这里，我们有一个父级，接着是两个孩子，现在让我们开始
rid of these unnecessary braces and reformat our code to see the difference 消除这些不必要的花括号并重新格式化我们的代码以查看区别
so I'm gonna remove the braces for the else clause and also one more time here 所以我要去掉else子句的花括号，在这里再删除一次
that's better now we can simplify this boolean expression basically we don't 更好，现在我们可以简化这个布尔表达式了
need this piece of code here here's the reason if the first condition is not 需要这段代码，这是第一个条件不是的原因
true what does it mean that means the temperature is less than or equal to 30 true这意味着温度小于或等于30
so this expression here is unnecessary let's delete this and simplify our code 所以这里的表达式是不必要的，让我们删除它并简化我们的代码
that's better now look at how this code is formatted on the top we have if the 现在最好查看一下如何在顶部显示此代码的格式（如果
else if Clause is a little bit indented but the else Clause is not indented it's 否则，如果条款有点缩进，但其他条款没有缩进，则为
at the same level as the if Clause and this looks a little bit ugly the code is 与if子句处于同一级别，这看起来有点难看，代码是
not symmetrical so if you want to get rid of the curly braces a better way to 不对称的，所以如果您想摆脱花括号，是一种更好的方法
format this code is like this so instead of adding the else if or else classes 格式化此代码是这样的，所以不要添加else if or else类
after curly braces we add them on a new line now all these clauses are at the 花括号之后，我们将它们添加在新行上，现在所有这些子句都位于
same level the code is easier to read in this tutorial I'm gonna show you a very 相同级别的代码在本教程中更易于阅读，我将向您展示
cool technique for simplifying if statements so let's start by declaring a 简化if语句的很酷的技术，所以让我们首先声明一个
variable called income and set it to 120,000 now here we can use an 称为收入的变量，现在将其设置为120,000，我们可以使用
underscore in between these three digits to make our code more readable now let's 在这三个数字之间加下划线以使我们的代码更具可读性，现在让我们
say we want to declare a boolean variable called has high income if the 说我们要声明一个布尔变量，该变量具有高收入，如果
income is more than $100,000 you want to set this to true otherwise we want to 收入超过$ 100,000，您需要将此设置为true，否则我们希望
set this to false so here we can write an if statement like this if income is 将其设置为false，以便在这里我们可以编写一个if语句，如果收入为
greater than $100,000 we want to set has high income to true however we get a 大于100,000美元，我们想要设置的收入很高，但是我们得到了
compilation error here let's take a look declaration not 编译错误在这里，让我们看一下声明
allowed here so we cannot declare a variable here we 在这里允许，所以我们不能在这里声明变量
can only declare variables inside code blocks like this code block over here so 只能在此处的代码块内声明变量，因此
to declare this variable we need to add curly braces to define a new code block 要声明此变量，我们需要添加大括号以定义新的代码块
now we have a different problem this variable that we have defined is scoped 现在我们有一个不同的问题，我们定义的这个变量的作用域
to this code block so it's only available here we cannot access it 到此代码块，因此仅在此处可用，我们无法访问它
outside of this block let me show you so if we print has high income you can see 在这个区块之外让我告诉您，如果我们打印的收入高，您可以看到
we have a compilation error cannot resolve symbol has high income because 我们有一个编译错误无法解析符号具有高收入，因为
this variable is not available outside of the block in which it's declared so 该变量在声明它的块之外不可用，因此
to solve this problem we can declare this variable after setup this block 为了解决这个问题，我们可以在设置此块后声明此变量
boolean has high income and then we can simply set it to true in this block now 布尔值很高，然后我们现在可以在此块中将其设置为true
we don't need these braces anymore so let's simplify the code we add an else 我们不再需要这些花括号，因此我们简化代码，添加一个else
clause otherwise we said has high income to false let's remove this print method 子句，否则我们说虚假收入很高，让我们删除此打印方法
we don't need it anymore so this is one way to implement this scenario but this 我们不再需要它，所以这是实现此方案的一种方法，但是
code looks very amateurish a professional programmer doesn't write 代码看起来非常业余，专业的程序员不会写
code like this let's improve it step by step one way to improve this is to give 这样的代码让我们逐步改进它，一种改进方法是
this boolean variable an initial value for example we can set it to false 这个布尔变量是一个初始值，例如我们可以将其设置为false
initially and then we implement this condition so if the income is more than 最初，然后我们执行此条件，因此，如果收入大于
$100,000 then we set this variable to true with this we no longer need is else $ 100,000，然后我们将此变量设置为true，我们不再需要其他
Clause so that was one improvement but it's 条款，这是一个改进，但它是
still not ideal in situations like this we can completely get rid of this if 在这种情况下还是不理想的，如果
statement here let me show you instead of hard-coding false here we type our 此处的声明让我向您展示，而不是硬编码为false，我们在此处输入
expression income is greater than $100,000 so here we have a boolean 表达式的收入大于$ 100,000，所以这里有一个布尔值
expression if this boolean expression evaluates to true this boolean variable 如果此布尔表达式的计算结果为true，则为此布尔变量
is going to be true otherwise it's going to be false so this is the simplest the 将是正确的，否则它将是错误的，所以这是最简单的
most elegant and the most professional way to implement this scenario now one 现在最优雅，最专业的方法来实现此方案
more improvement before we finish this tutorial I 在完成本教程之前，我还有更多改进
personally prefer to wrap this expression in parentheses even though 个人更喜欢将此表达式用括号括起来，即使
technically we don't need parentheses here but these parentheses make our code 从技术上讲，我们这里不需要括号，但是这些括号构成了我们的代码
more clear more readable let me show you so I'm gonna wrap this inside these 更清晰，更易读，让我向您展示，以便将其包装在这些文件中
parentheses now it's very clear we're obviously on the right side of this 括号现在很明显，我们显然在这个右边
assignment operator we have a boolean expression in this tutorial we're gonna 赋值运算符在本教程中，我们将有一个布尔表达式
look at the ternary operator in Java so we're gonna continue with the example 看一下Java中的三元运算符，所以我们将继续该示例
from the last tutorial we had this income variable imagine this is the 在上一个教程中，我们有这个收入变量，想象这是
income or customers now depending on their income 收入或客户现在取决于他们的收入
you want to put these customers in different classes if their income is 如果他们的收入是
more than $100,000 you want to put them in the first class otherwise we want to 超过100,000美元，您希望将它们放在头等舱，否则我们想
put them in the economy class so here's one way to implement the scenario we 将他们纳入经济舱，所以这是实施方案的一种方法
declare this string variable class name note that we cannot call this class 声明此字符串变量的类名称，请注意，我们无法调用此类
because class is a reserved keyword so class name now we write our first 因为class是保留关键字，所以类名现在我们写第一个
condition if income is greater than $100,000 we said class name to first 条件，如果收入大于$ 100,000，我们说的是班级名称
otherwise we set it to economy 否则，我们将其设置为经济
now as you learn in the last tutorial this code looks very amateurish a 现在，当您在上一教程中学习时，此代码看起来非常业余
professional programmer doesn't write code like this so one way to simplify 专业的程序员不会像这样写代码，所以一种简化的方法
this is to give this variable an initial value so we assume they are in the 这是给这个变量一个初始值，所以我们假设它们在
economy class and then we check this condition if this condition is true we 经济舱，然后我们检查此条件是否为真
put them in the first class so with this we can get rid of this else clause that 将它们放在第一类中，这样我们就可以摆脱掉else子句
is better now in the last tutorial I showed you how to simplify this even 现在更好，在上一个教程中，我向您展示了如何简化该过程
further but the technique you learned there cannot be used here in other words 更进一步，但是您在这里学到的技术无法用在这里
we cannot add income greater than $100,000 here because here we have a 我们不能在这里增加超过100,000美元的收入，因为在这里我们有一个
boolean expression but on the left side we have declared a string variable so we 布尔表达式，但在左侧我们声明了一个字符串变量，因此我们
want to set this to a different string depending on the result of this 想要根据此结果将其设置为其他字符串
expression and this is where we use the ternary operator so we start with our 表达式，这是我们使用三元运算符的地方，所以我们从我们的
condition then we type a question mark if this condition is true we add this 条件，然后我们输入一个问号，如果此条件为真，则添加
value here otherwise we add the other value so this question mark and colon is 值在这里，否则我们添加另一个值，所以这个问号和冒号是
the ternary operator in Java it has three pieces first we have a condition Java中的三元运算符首先有三个部分
if this condition is true this value will be returned and assigned to our 如果此条件为真，则将返回该值并将其分配给我们
class named variable otherwise this other value will be returned now we can 类名为变量的类，否则将返回其他值
completely get rid of this if statement so put the ternary operator in your tool 完全摆脱此if语句，因此将三元运算符放入您的工具中
box it's very helpful next we're going to look at switch statements in Java in 框非常有用，接下来我们将在Java中查看switch语句
this tutorial we're going to look at switch statements in Java we use three 在本教程中，我们将介绍Java中的switch语句，我们使用三个
statements to execute different parts of code depending on the value of an 语句，用于根据代码的值执行代码的不同部分
expression kind of similar to if statements let me show you so let's say 表达式类似于if语句让我向您展示，所以说
we're gonna write a program and check the role of the current user and then 我们将编写一个程序并检查当前用户的角色，然后
we're gonna print different messages or give them different features depending 我们将打印不同的消息或为它们提供不同的功能，具体取决于
on their row so let's declare a string variable called role and here we set 在他们的行上，所以我们声明一个名为role的字符串变量，然后在此处进行设置
this to admin now to check the role of the user we can write an if statement 现在要管理员检查用户的角色，我们可以编写一个if语句
like this if role equals admin then perhaps 像这样，如果角色等于管理员，那么也许
we want to print you are an admin now you might be wondering why we have this 我们想打印您现在是管理员，您可能想知道为什么我们有这个
condition here it's obvious that this condition is always true because we have 这里的条件很明显，因为我们有
set roll to admin but this is just for demonstration in a real program we are 设置管理员身份，但这只是为了在真实程序中进行演示
not gonna hard code this admin here so we're gonna read the role of the current 不会在这里硬编码这个管理员，所以我们将阅读最新的角色
user from somewhere else we don't know what it is at the time of 来自其他地方的用户，我们不知道当时的情况
writing code okay so here we have one condition 编写代码好，所以这里有一个条件
let's write another condition else if role equals moderator perhaps we want to 让我们写另一个条件，如果角色等于主持人，也许我们想
display a different message so you are a moderator and finally if the role is 显示不同的消息，以便您是主持人，最后，如果该角色是
none of these values you want to print you are a guest so this is one way to 您不想打印所有这些值，您是访客，所以这是一种方法
implement this scenario using an if statement we can also implement this 使用if语句实现这种情况，我们也可以实现
using a switch statement and sometimes that looks a little bit cleaner let me 使用switch语句，有时看起来更干净一些
show you so we start with a switch statement then we add parentheses and 向您展示，所以我们从switch语句开始，然后添加括号和
inside this parenthesis we add our variable in this case row next we define 在此括号内，我们在此案例行中添加变量，然后定义
a block of code and in this block we add one or more case clauses so we have a 一段代码，在这一段中，我们添加一个或多个case子句，因此我们有一个
case for an admin we add a colon here now what do we want to do here if the 对于管理员而言，我们现在在此处添加冒号，如果
role is admin you want to print you are an admin so I'm gonna copy this line 角色是管理员，您要打印，您是管理员，所以我要复制此行
from here and then paste it over here now after this line we need to add a 从这里，然后将其粘贴到这里，在此行之后，我们需要添加一个
break statement to jump out of this switch block okay then we add another break语句可以跳出此开关块，然后添加另一个
case Clause so case moderator once again you got a colon and here we're gonna 案例子句，所以案例主持人再一次出现了冒号，在这里我们要
print this other message so we paste it here and then we're gonna break 打印此其他消息，以便我们将其粘贴在此处，然后我们将断开
now optionally we can have a default clause here so if none of these previous 现在可选地，我们可以在此处有一个默认子句，因此如果这些都不是以前的
cases apply the code that we write in this section will be executed so here we 案例应用我们在本节中编写的代码将被执行，因此在这里我们
want to print you are a guest now here we don't need to use a break 要打印您现在是客人，我们不需要休息
statement because we're at the end of the switch block so will automatically 声明，因为我们位于switch块的末尾，所以它将自动
jump out of this block in contrast if we didn't use this break statement here 相反，如果我们在这里不使用该break语句，则跳出该块
Java will continue executing these other lines here so if the role is admin first Java将在此处继续执行这些其他行，因此，如果该角色首先是管理员
it will execute this line and then it will jump to this case block it will 它将执行此行，然后将跳转到此case块，它将
execute this other line and then after it executes this break statement it will 执行另一行，然后在执行该break语句后将执行
jump out of this switch block okay so this is how we use a switch statement 跳出这个switch块好吧，这就是我们使用switch语句的方式
now compare this with wave statement some people prefer to use if statements 现在将此与wave语句进行比较有些人更喜欢使用if语句
others prefer to use a switch statement now one more thing before we finish this 其他人则喜欢使用switch语句，现在再完成一件事
tutorial here we're comparing the value of role with strings but we could also 在本教程中，我们将比较角色和字符串的值，但我们也可以
use integers other than the long type so if roll was a byte short or an integer 使用除long类型以外的其他整数，因此，如果roll是一个字节的short或整数
our cases would look like this case one case two and so on now here we have a 我们的案例看起来像是这个案例，一个案例是两个案例，依此类推。
compilation error because roll is a string let's change this to an integer 编译错误，因为roll是一个字符串，让我们将其更改为整数
and we can initialize this to one so as you can see with sweet statements we can 我们可以将其初始化为一个，这样您就可以通过甜美的语句看到
execute different code depending on the value of an expression 根据表达式的值执行不同的代码
all right now it's time for an exercise this exercise I'm gonna give you is a 现在是时候进行练习了，我要给你的练习是
popular interview question so I want you to write a program that behaves like 流行的面试问题，所以我希望您编写一个行为类似的程序
this here we should enter a number if this number is divisible by five we get 如果这个数字可以被五整除，我们应该在这里输入一个数字
this so if you run the program again and enter ten once again we get fit now if 因此，如果您再次运行该程序并再次输入10，我们现在可以适应
this number is divisible by 3 we get buzz if this number is divisible by both 这个数字可以被3整除如果这个数字可以被3整除，我们会得到嗡嗡声
five and three like fifteen or thirty or whatever we get fizzbuzz and if this 五个和三个，就像十五岁或三十岁，或我们得到的嗡嗡声，如果这
number is not divisible by five or three like two we get the same number printed 数字不能被五或三除尽，就像两个一样，我们得到相同的数字
on the terminal so go ahead and spend five to ten minutes on this exercise 在终端上，所以继续练习，花五到十分钟
you'll see my solution next 您接下来会看到我的解决方案
all right to read the number first we need to use the scanner object so 可以先读取号码，我们需要使用扫描仪对象
scanner we import this and instantiate it and as you know here we need to pass 扫描器，我们将其导入并实例化，如您所知，我们需要通过
system that in to read data from the terminal now we print a message so we're 系统从终端读取数据，现在我们打印一条消息，所以我们
gonna use the print method instead of print line here we add a label like 我们将使用打印方法而不是打印行，在这里我们添加一个标签，例如
number and then we call scanner the next int to read a number we store it in this 数字，然后我们将下一个int称为扫描仪，以读取一个数字，并将其存储在此数字中
variable number okay so the first part is done now we need to check to see if 可变数字好了，所以第一部分现在完成了，我们需要检查是否
this number is divisible by five or not so we can write an if statement like 这个数字可以除以五，所以我们可以写一个if语句，例如
this if number here we use the modulus operator which returns the remainder of 如果这里是数字，我们使用模运算符，该运算符将返回
a division so we divide this by five and if the remainder equals zero that means 除法，因此我们将其除以五，如果余数等于零，则意味着
this number is divisible by 5 so we print fizz now 这个数字可以被5整除，所以我们现在打印嘶嘶声
otherwise if this number is divisible by 3 we print buzz we need another 否则，如果此数字可被3整除，我们将打印嗡嗡声，我们需要另一个
condition if this number is divisible by 5 and 3 so here we use the and operator 条件，如果这个数字可以被5和3整除，那么这里我们使用and运算符
number divisible by 3 equals 0 in this case we want to print fizz buzz 被3整除的数字等于0在这种情况下，我们要打印嘶嘶声
otherwise you want to print the same number like this now this is not the 否则，您现在要打印相同的数字，这不是
right solution as I will show you in a second this program has a book but it's 正确的解决方案，我将在第二秒钟向您展示，该程序有一本书，但是
a very common solution that I see amongst my students so let's run this 我在学生中看到的非常普遍的解决方案，所以让我们运行
program and see what is wrong here all right here we enter five we get fizz 程序，看看这里出了什么问题好了，在这里，我们输入五个，我们得到了嘶嘶声
utiful what if you enter ten ten is also divisible by 5 so we get fits so far so 如果您输入十，那么十也会被五整除，所以到目前为止
good what about a number that is divisible by 3 we get buzz good what if 好的被3整除的数字会引起嗡嗡声
we enter a number that is divisible by both 5 & 3 like 15 we get physican why 我们输入一个可以被5和3整除的数字，例如15，我们得到了Physcan为什么
is that here's the reason with this implementation if we enter 15 this first 这是执行此操作的原因，如果我们首先输入15
condition will evaluate to true so we get fizz these other else clauses will 条件将评估为true，所以我们得到了其他的其他条款
be ignored and that is why this line will not be executed so in situations 被忽略，这就是为什么在某些情况下将不执行此行的原因
like this you should have the most specific conditions on the top and the 这样，您应该在顶部和
most generic ones on the bottom in this case we want to move this condition to 在这种情况下，最普通的位于底部，我们希望将此条件移至
the top so if the number is divisible by 5 & 3 we're gonna print fizzbuzz so this 如果数字被5和3整除，我们将打印fizzbuzz，因此这
is very specific otherwise if the number is only divisible by 5 we print fizz 非常具体，否则，如果数字只能被5整除，我们将打印出嘶嘶声
else if it's divisible by 3 we print buzz and finally if none of his 否则，如果被3整除，我们将打印嗡嗡声，最后，如果没有
conditions is true then we print the same number now let's run the program 条件为真，那么我们打印相同的数字，现在让我们运行程序
one more time so we enter 15 and we get fizzbuzz beautiful so here's one way to 再过一次，我们输入15，我们的Fizzbuzz变得很漂亮，所以这是一种方法
solve this problem now I've seen some people argue that we have repeated this 现在解决这个问题我已经看到有人争论说我们已经重复了这个
expression twice number is divisible by 5 we have that here on line 12 as well 表达式两次被5整除我们在第12行也有
as line 14 in programming we have this concept called dry which is short for 作为编程的第14行，我们有一个叫做dry的概念，它是
don't repeat yourself so some people argue that here we have repeated this 不要重复自己，所以有人认为在这里我们重复了这个
expression and this is not a good solution here is another way let me show 表达，这不是一个好的解决方案，这是让我展示的另一种方式
you so we're gonna get rid of this second condition here instead we're 你，所以我们要摆脱这第二种情况，相反，我们
gonna add a code block over here so if the number is divisible by 5 first we 要在此处添加代码块，所以如果数字可以被5整除
check to see if the number is also divisible by 3 if that's the case we 如果是这种情况，请检查数字是否也可被3整除
print fizzbuzz otherwise we print just fizz 打印fizzbuzz，否则我们只打印fizz
like this okay now we no longer need these two lines because we already 像这样好吧，现在我们不再需要这两行，因为我们已经
implemented this concept here so first we check to see if the number is 在这里实现了这个概念，所以我们首先检查数字是否为
divisible by five if not we check to see if it's divisible by three and otherwise 如果不能被五整除，我们检查是否可以被三整除，否则
so here is another way to solve this problem but in my opinion this approach 所以这是解决这个问题的另一种方法，但我认为这种方法
is kind of amateurish and ugly because these nested if-else statements are 有点业余和丑陋，因为这些嵌套的if-else语句是
considered a bad practice now this is not terribly bad but the more you nest 现在被认为是一种不好的做法，这还不是很糟糕，但是您嵌套的次数越多
these L statements the more confusing your code is going to be to other people 这些L语句会使您的代码对其他人更加混乱
so I personally prefer the previous solution even though we had a bit of 所以我个人更喜欢以前的解决方案，即使我们有一些
repetition in the code the more you program the more you build software the 代码中的重复次数越多，您编写的程序就越多，构建的软件就越多
more you realize that there is no way to build ideal software programming and 此外，您意识到没有办法构建理想的软件编程，并且
problem solving is all about trade-offs every solution has certain strengths and 解决问题的关键在于权衡取舍，每个解决方案都有一定的优势，
certain weaknesses this solution doesn't have any repetition or duplication in 该解决方案的某些弱点没有重复或重复
the code but it has a nested structure and these nested structures make our 代码，但它具有嵌套结构，这些嵌套结构使我们
code hard to read and understand the previous solution had a bit of 难以阅读和理解的代码以前的解决方案有一点
repetition but it had a flat structure there is no nesting here and this code 重复，但结构平坦，此处没有嵌套，此代码
is cleaner and easier to read there are times that we want to repeat one or more 更干净，更容易阅读有些时候我们想重复一个或多个
statements for example let's say we have this hello world message here let's say 举例说，我们在这里有这个世界问候。
we want to print this five times on the terminal we don't want to repeat this 我们想在终端上打印五遍，我们不想重复
code like this this looks very ugly that's where we use loops in Java we 这样的代码看起来很难看，这就是我们在Java中使用循环的地方
have a few different types of loops the first one that I'm going to talk about 有几种不同类型的循环我将要讨论的第一个循环
in this tutorial is for loops so let's see how we can use a for loop here I'm 本教程中的是for循环，所以让我们看看如何在这里使用for循环
going to delete all this code you start by typing the for keyword followed by 要删除所有这些代码，请先输入for关键字，然后再输入
parentheses and inside these parentheses we need to do three things first we need 括号，在这些括号内，我们需要先做三件事
to declare a loop or counter variable so let's declare a variable called I and 声明一个循环或计数器变量，所以我们声明一个名为I和
initialize it to 0 quit often use variable names like I J and K for loop 将其初始化为0退出通常使用变量名（例如IJ和K）进行循环
counters next we add a semicolon to terminate the first statement then we 接下来，我们添加一个分号来终止第一个语句，然后我们
write a boolean expression that determines how many times this loop is 编写一个布尔表达式，确定此循环的次数
gonna get executed so I than five as long as I is less than five 要被执行，所以我比五岁，只要我小于五岁
this loop will be executed once again we add a semicolon and finally we increment 这个循环将再次执行，我们添加一个分号，最后我们增加
I by one like this so this is the basic structure for a for loop now here we can 我一个这样，所以这是for循环的基本结构，现在我们可以
repeat one or more statements using this for loop so we can add our hello world 使用此for循环重复一个或多个语句，以便我们添加问候世界
message here like here we have a single statement so we don't need braces but if 像这里这样的消息我们只有一个语句，所以我们不需要大括号，但是如果
you have multiple statements that we want to repeat we need to define a code 您有多个要重复的语句，我们需要定义一个代码
block here now I'm gonna remove this because you don't really need them so 现在阻止在这里，我要删除它，因为您真的不需要它们，所以
let's run this code and see what we get you get hello world printed five times 让我们运行这段代码，看看能给您带来什么，您将获得五次打印的hello world
on the terminal beautiful now let me explain how this code gets executed when 现在在美丽的终端上，让我解释一下如何在以下情况下执行此代码
Java sees this for loop first it will execute this statement so here we are Java首先看到此for循环，它将执行此语句，因此在这里
initializing I to zero then Java evaluates this condition is this 将I初始化为零，然后Java评估此条件是这个
condition true obviously it is because zero is less than five so the control 条件为真，显然是因为零小于五，所以控制
moves to line seven this line gets executed now at the end of this 移至第七行，此行现在在此结尾处执行
iteration or at the end of this loop the control moves here 迭代或此循环结束时，控件移至此处
so I is incremented by one now we are at the beginning of the second iteration 所以我增加了一个现在我们在第二个迭代的开始
once again this condition is evaluated is one less than five obviously it is so 再一次，这个条件被评估为小于五分之一，显然是这样
once again the body of this loop gets executed now fast forward at the end of 再次，该循环的主体现在在末尾快进了
the fifth iteration I will become five five is not less than five so the loop 第五次迭代，我将变成五个五不小于五个，所以循环
condition will be false and control moves outside of this for loop now 条件将为false，并且控制权现在移至该for循环之外
here's one thing you need to remember if you want to execute something five times 如果要执行五次，这是您需要记住的一件事
you can initialize your loop counter or loop variable to zero and use the less 您可以将循环计数器或循环变量初始化为零，然后使用
than operator here another way is to initialize this to one and then use the 比运算符这里的另一种方法是将其初始化为一个，然后使用
less than or equal to operator here now to make this more interesting let's 现在小于或等于运算符使这个更有趣，让我们
print I over here so here we add a space and then concatenate this string with I 在这里打印我，所以在这里我们添加一个空格，然后用I连接此字符串
take a look so we get hello ward one two three four five in contrast if we 看一下，如果我们得到相反的结果，我们得到一二三四五的问候
initialize I to zero and use the less than operator 将我初始化为零并使用小于运算符
we will get hello word zero one two three four we can also print these 我们会得到你好词零一二三四我们也可以打印这些
numbers in reverse order so we initialize I to five and execute this 数字相反的顺序，所以我们将I初始化为5并执行
loop as long as I is greater than zero but here instead of incrementing I we 只要我大于零就循环，但在这里我们不递增
decrement it now we get hello world five four three two one 递减它，我们得到世界五个四三二一的问候
so this is all about for loops next we're gonna look at while loops in this 所以这就是所有关于for循环的内容，接下来我们来看一下while循环
tutorial we're gonna talk about while loops in Java while loops are very 我们将要讨论的教程Java中的while循环while循环非常
similar to for loops in terms of their functionality but they're different in 在功能上类似于for循环，但它们的区别在于
terms of syntax let me show you so we're gonna continue with the example from the 语法术语让我向您展示，所以我们将继续从
last tutorial I'm gonna rewrite this code using a while loop so first we 在上一个教程中，我将使用while循环重写此代码，因此首先我们
declare a loop variable and I initialize it to zero 声明一个循环变量，然后将其初始化为零
next we type while and here in parentheses we type our loop condition 接下来，我们输入while并在括号中输入循环条件
while I is greater than zero then we're gonna execute the code inside this block 当我大于零时，我们将在此块中执行代码
so I'm gonna copy this from here paste it in this block and finally we need to 所以我要从这里复制它，将其粘贴到此块中，最后我们需要
decrement I like this so at the end of each iteration we decrement I just like 我喜欢这样递减，所以在每次迭代结束时我们都喜欢递减
our for loops so as you can see we can achieve the same thing using a for loop 我们的for循环，如您所见，我们可以使用for循环实现相同的功能
or a while loop however the implementation using the for 或while循环，但是使用for的实现
loop is a little bit lighter and cleaner so in situations where you know ahead of 循环更轻更清洁，因此在您提前知道的情况下
time how many times you want to execute one or more statements it's better to 您想执行一次或多次语句的时间最好是
use a for loop while loops are better in situations where we don't know exactly 使用for循环，而在我们不完全了解的情况下，循环会更好
how many times you want to repeat something for example let's say we're 您想重复多少次，例如说我们
going to write a program and ask the user to continuously enter something 将要编写一个程序并要求用户连续输入内容
until they type quit the moment they type quit we're gonna terminate the 直到他们键入quit之后，我们才会终止
program in that situation we don't know how many times the user is going to 程序在这种情况下，我们不知道用户要去多少次
enter something so let's write that program using a while loop 输入一些东西，让我们使用while循环编写该程序
I'm gonna delete everything from here 我要从这里删除所有内容
all right we're gonna start with our while loop now what is our loop 好吧，我们将从while循环开始，现在是什么循环
condition here we don't have a counter variable in this example instead we want 条件，我们在此示例中没有计数器变量，而是
to check to see if the user entered quit or not so here we can declare a string 检查用户是否输入退出，所以在这里我们可以声明一个字符串
called input and initialize it to an empty string then we can write a while 调用输入并将其初始化为空字符串，然后我们可以写一会儿
loop like this while input does not equal to quit now this code is not gonna 这样的循环，而输入不等于退出，现在此代码将不再
work because input is a string which is a reference type and we cannot use 因为输入是一个字符串，这是一个引用类型，所以我们不能使用
comparison operators between reference types because these operators will 比较引用类型之间的运算符，因为这些运算符将
compare the address or a string objects not their value so if you have two 比较地址或字符串对象而不是它们的值，因此如果您有两个
strings quit and quit but stored in different memory locations to have 字符串退出并退出，但存储在不同的内存位置以具有
different addresses so we can use the inequality operator to compare their 不同的地址，因此我们可以使用不等式运算符比较它们的地址
value instead we need to use the equals method of string objects so we want to 值，我们需要使用字符串对象的equals方法，因此我们想
check to see if the input equals quit now here we need to apply the not 现在检查输入是否等于退出，这里我们需要应用not
operator so as long as the input does not equal quit we're gonna continuously 运算符，只要输入不等于退出，我们将不断
ask the user to enter something so here we can print a label like input and then 要求用户输入内容，以便在这里我们可以打印输入标签，然后
we can use a scanner object to read something from the terminal so let's 我们可以使用扫描器对象从终端读取某些内容，所以让我们
create a scanner object and instantiate it using system dot in I didn't call 创建一个扫描器对象，并使用我未调用的系统点实例化它
scanner that next this will return a string so we can store it in this input 扫描仪，接下来它将返回一个字符串，因此我们可以将其存储在此输入中
variable now with this implementation in every iteration we're going to create a 现在，在每次迭代中使用此实现变量，我们将创建一个
new scanner object so if the user enters 10 numbers we're gonna create 10 scanner 新的扫描仪对象，因此，如果用户输入10个数字，我们将创建10个扫描仪
objects in memory this is unnecessary and it's actually a bad practice because 内存中的对象，这是不必要的，实际上是一种不好的做法，因为
it's going to pollute our memory so it's better to create the scanner object 它会污染我们的内存，所以最好创建扫描器对象
outside of a while loop and then simply use it here also here we are assuming 在while循环之外，然后在这里简单地使用它，这里我们假设
that the user is typing everything in lowercase so if they type quit in 用户正在以小写形式键入所有内容，因此如果他们键入quit
uppercase or any combinations of lowercase and uppercase characters this 大写字母或小写和大写字符的任意组合
logic is not gonna work the way we want so over here right after reading 阅读后，逻辑无法按照我们想要的方式工作
something from the terminal we're gonna call the 来自终端的东西，我们将其称为
to lowercase method of string objects to convert it to lowercase now to make this 字符串对象的小写方法，现在将其转换为小写
program more interesting let's echo back whatever the user enters so we simply 程序更有趣，让我们回显用户输入的内容，因此我们只需
print that on the terminal now let's run this and see what happens 现在在终端上打印它，让我们运行它，看看会发生什么
so I'm going to enter if your numbers like 1 2 & 3 whatever we type gets a 所以我要输入您输入的数字1 2＆3是否等于
code back but the moment we type quit our program terminates so while loops 返回代码，但是在我们键入quit的那一刻我们的程序终止，所以while循环
are useful in situations where we don't know ahead of time how many times we 在我们不知道多少次的情况下很有用
want to repeat something in Java we have another type of loop called a do-while 要重复Java中的某些内容，我们有另一种称为do-while的循环
loop it's very similar to a while loop but it gets executed at least once let 循环与while循环非常相似，但至少让一次执行
me show you what I mean so I'm gonna rewrite the same code using a do-while 我告诉你我的意思，所以我要使用do-while重写相同的代码
loop we start with a do keyword then we create a code block at the end of this 循环，我们从do关键字开始，然后在此结尾处创建一个代码块
code block we type while followed by our loop condition so not input dot equals 我们在循环条件之后输入的代码块，因此输入点不等于
quit and then we terminate this using a semicolon now inside the body of this 退出，然后我们现在在它的主体内部使用分号终止它
loop we'll simply copy all these lines we have here 循环，我们将简单地复制所有这些行
now compared these two types of loops with while loops we check the condition 现在将这两种类型的循环与while循环进行比较，我们检查条件
first so if the condition is false the first time this loop will never get 首先，如果条件为假，则第一次循环将永远不会得到
executed in contrast with do-while loops we check the condition last so do-while 与do-while循环执行相反，我们最后检查条件，所以do-while
loops always get executed at least once even if the condition is false that is 循环总是至少执行一次，即使条件为假，即
the only difference the reality most of the time we use while loops do-while 大部分时间我们使用while循环do-while时唯一的现实差异
loops are rarely used but there are certain cases for that so just be aware 循环很少使用，但在某些情况下，请注意
of them but most of the time prefer to use wire loops 其中的大多数时间更喜欢使用线环
we're gonna continue with the example from the last tutorial this program we 我们将继续该程序的上一个教程中的示例
have written has a tiny problem let me show you so I'm gonna run this enter a 写的有一个小问题，让我告诉你，所以我要运行这个输入
couple numbers these numbers get echo back beautiful 几个数字这些数字得到回声美丽
if we type quit the program terminates but the word quit also gets echoed back 如果我们键入quit，程序会终止，但是quit这个词也会回显
this is a bit weird so let's look at a couple ideas for solving this problem 这有点怪异，所以让我们看一些解决这个问题的想法
back to our code one way to solve this problem is to check the input before 回到我们的代码中，解决此问题的一种方法是先检查输入
printing it so here we can type an if statement if the input does not equal 打印它，因此如果输入不相等，我们可以在此处键入if语句
quit then we're print it so not equal input dot equals quit if this condition 退出，然后我们将其打印出来，因此在这种情况下，不等于输入点等于退出
is true then we're going to print the input let's take a look so one to quit 是真的，那么我们将打印输入，让我们看一下以便退出
beautiful we solved the problem there is another way to solve this problem as 美丽，我们解决了这个问题，还有另一种解决这个问题的方法，
well let me show you we can reverse this condition so if the user types quit you 好吧，让我向您展示，我们可以扭转这种情况，因此，如果用户类型退出了您
can immediately jump out of the loop using the break statement so I'm gonna 可以立即使用break语句跳出循环，所以我要
remove the nut operator if the user types quit we're gonna break out of the 如果用户类型退出，请删除螺母运算符，我们将打破
loop otherwise we're gonna continue execution and print this input on a 循环，否则我们将继续执行并将此输入打印在
terminal so when Java sees the break statement it will ignore everything else 终端，因此当Java看到break语句时，它将忽略其他所有内容
after and it will terminate the loop let's run the program 之后，它将终止循环，让我们运行程序
once again we enter a couple numbers followed by quit beautiful so this is 再次输入几个数字，然后退出美丽，所以这是
the break statement we also have the continuous statement that moves control break语句我们还有连续的语句来控制
to the beginning of a loop let me show you so let's imagine if the user types 到循环开始，让我向您展示，让我们想象一下，如果用户输入
pass we don't want to echo that but also we don't want to terminate the loop you 通过我们不想回声，但我们也不想终止循环
want to ask the user to try one more time so after we read the input we can 想让用户再试一次，所以在阅读输入内容之后，我们可以
check to see if input equals pass this is where we use the continuous statement 检查输入是否等于传递，这是我们使用连续语句的地方
when Java sees this it would move control to the beginning of the loop so 当Java看到这一点时，它将把控制移到循环的开始，因此
all these other statements are gonna get ignored and what the user types is not 所有其他这些语句将被忽略，而用户键入的不是
gonna get printed on the terminal let's run the program and see this in action 要在终端上打印，让我们运行程序并查看实际运行情况
so we type 1 2 pass it doesn't get echoed back one more time and finally 所以我们输入1 2 pass不会再回响一次，最后
quit so to recap the break statement terminates a loop and the continue 退出以便重述break语句终止循环并继续
statement moves control to the beginning of a loop now one last thing before 语句现在将控制权移至循环的开始
finish this tutorial in this implementation we don't really need this 在此实现中完成本教程，我们真的不需要这个
loop condition because the moment user types quit this break statement is gonna 循环条件，因为在用户类型退出时，此break语句将
kick in and terminate the loop so we can simplify this code by using a true as 插入并终止循环，因此我们可以通过使用true作为来简化此代码
our loop condition so this is always true and this loop is gonna get executed 我们的循环条件，因此这始终是正确的，并且此循环将被执行
forever until the user types quit this is a very common technique that you see 直到用户类型退出，这永远是一种非常常见的技术
amongst professional programmers just remember if you're using this technique 在专业程序员中，只要记住您是否正在使用此技术
make sure to have a break statement otherwise you will end up with an 确保有一个break语句，否则您将得到一个
infinite loop that executes forever it never terminates and that can be very 永远执行的无限循环永远不会终止，这可能非常
dangerous in terms of memory consumption so if you're using while true make sure 就内存消耗而言，这很危险，因此，如果您在使用true的同时确保
you have a break statement in your loop 您的循环中有一个break语句
the last type of loop we want to look at is the for each loop in Java we use for 我们要查看的最后一种循环类型是用于Java的每个循环
each loops to iterate over arrays or collections let me show you so I'm going 每个循环都遍历数组或集合，让我告诉你，所以我要
to start by declaring a string array called fruit and we initialize this with 首先声明一个名为fruit的字符串数组，然后使用
three items let's say Apple mango and orange now let's say we want to iterate 三个项目，例如苹果芒果和橙子，现在我们要迭代
over this array and print each item on a terminal we can use any of the loops you 在这个数组上并在终端上打印每个项目，我们可以使用任何循环
learn about earlier like a for loop or a while loop but we can also use the for 可以像for循环或while循环那样早点学习，但是我们也可以使用for
each loop which is a bit easier let me show you first I'm going to use the for 每个循环都比较容易，让我先向您展示，我将使用for
loop to iterate over this array so we type for here we declare our loop 循环遍历此数组，因此我们在此处输入声明循环
variable or loop counter into I we set it to zero as long as I is less than 变量或循环计数器置为I，只要我小于
fruits that length we're going to increment I by one after each iteration 结果长度，我们将在每次迭代后将I加1
and here we simply print fruits of I let's run the program and see what we 在这里，我们简单地打印出我的成果，让我们运行程序，看看我们
get so we get each item on a new line beautiful now there is another way to 得到，所以我们将每条商品换成新的漂亮行，现在还有另一种方法
write the same code using the for each loop here we type for in parentheses 使用for每个循环编写相同的代码，我们在括号中键入
with declare and loop variable but the type of this variable should be based on 与声明和循环变量，但此变量的类型应基于
the type of items in our array so here we have a string array and that means 数组中项目的类型，所以这里有一个字符串数组，这意味着
every item in this array is a string so here we should declare a string variable 该数组中的每个项目都是一个字符串，因此在这里我们应该声明一个字符串变量
we call it fruit here we type a colon and then the name of our array fruits 我们在这里称它为fruit，我们输入一个冒号，然后是数组fruits的名称
now in each iteration fruit will hold the value of one item in this array so 现在在每次迭代中，水果将在此数组中保留一项的值，因此
here we don't have to declare a numeric counter we don't have to write a boolean 在这里，我们不必声明数字计数器，而不必编写布尔值
expression like this we don't have to increment our counter it's much easier 这样的表达方式，我们不必增加计数器，就容易多了
to iterate over an array now if we print fruit we get the exact same result as 现在遍历数组如果我们打印水果，我们得到的结果与
before take a look so the first three items are from our 先来看一下，所以前三项来自我们
for loop and here's the result our for each loop for循环，这是每个循环的结果
so this is the for each loop however this for each loop has a couple of 所以这是每个循环的，但是每个循环都有两个
limitations one limitation is that it's always forward only so we cannot iterate 局限性局限性之一是它总是向前转发，因此我们不能迭代
over this array from the end to the beginning in contrast we can easily do 相比之下，从头到尾在这个数组上我们可以轻松地做到
this with a for loop so here we can initialize I to fruits that length then 这个带有for循环，所以在这里我们可以将I初始化为该长度的结果
we change this operator to greater than and replace this value with zero so as 我们将此运算符更改为大于并将该值替换为零，以便
long as I is greater than zero we're going to decrement I the second 只要我大于零，我们就将第二个递减
limitation of the for each loop is that here we don't have access to the index 每个循环的局限性在于，这里我们无法访问索引
of each item all we have is this loop variable which holds the value of each 我们拥有的每一项都是这个循环变量，它保存了每一项的值
item in this array in contrast in our for loops we can access both the index 与此数组相反，在我们的for循环中，我们可以访问两个索引
and the actual item so I represents the index of each item and fruits of I 和实际项目，因此我代表每个项目的索引和I的成果
returns the item at the given index so if you need the index then you'll have 返回给定索引处的项目，因此，如果您需要索引，则将有
to use the for loop otherwise it's much easier to use the for each loop 使用for循环，否则使用for循环要容易得多
all right now let's get back to our mortgage calculator and implement some 现在，让我们回到抵押贷款计算器并实施一些
basic error handling so here I've changed this question by 基本错误处理，因此在此我通过以下方式更改了此问题
adding this label that identifies the range of values we can enter so the 添加此标签以标识我们可以输入的值的范围，以便
minimum amount of loan we can get is $1,000 and maximum is 1 million dollars 我们可以获得的最低贷款额为1,000美元，最高为100万美元
so if I enter 1 here I get this message enter a number between one thousand and 因此，如果我在此处输入1，则会收到此消息，请输入一个介于1000和
one million and now we are asked this question one more time if I keep 一百万，如果我们继续，现在又被问到这个问题
entering invalid values I get asked the same question now let's enter a valid 输入无效值现在我被问到相同的问题，让我们输入一个有效值
value like 1 million dollars next we'll have to enter the annual interest rate 值一百万美元，接下来我们必须输入年利率
now here we need to enter a value that is greater than 0 and less than or equal 现在在这里我们需要输入一个大于0且小于或等于的值
to 30 so if I enter 0 we get this message enter a value greater than 0 and 到30，所以如果我输入0，我们会收到此消息，输入一个大于0的值，然后
less than or equal to 30 once again we're asked this question one 小于或等于30再次被问到这个问题
more time so let's enter a valid value like 3.9 to here we need to enter a 更多的时间，所以让我们输入一个有效值，例如3.9到这里，我们需要输入一个
value between 1 and 30 so if you enter 0 we get an error message and we're asked 值介于1到30之间，因此，如果您输入0，则会收到错误消息，并要求我们
the same question so let's enter 30 and finally we get the result here's our 相同的问题，所以让我们输入30，最后我们得到的结果是我们的
mortgage or monthly payments so go ahead and spend five to ten minutes on 抵押或每月还款，请花五到十分钟
extending this mortgage calculator by adding error-handling to it you'll see 通过添加错误处理来扩展此抵押计算器，您将看到
my solution X 我的解决方案X
all right let me show you how I'm gonna solve this problem step by step so for 好吧，让我告诉你我将如何逐步解决这个问题，因此
each question we want to validate the value that the user enters if the value 我们想要验证用户输入的每个问题的值（如果该值
is invalid you want to keep asking the same question so this is where we can 是无效的，您想继续问同样的问题，所以我们可以在这里
use an infinite loop let me show you so here's our first question principal I'm 使用无限循环让我告诉你，所以这是我的第一个问题委托人
gonna wrap these two lines inside an infinite loop while true 要将这两行包裹在一个无限循环中，而true
so we're gonna keep asking the same question until the user enters a valid 因此我们将继续询问相同的问题，直到用户输入有效的
value so here after we read the principle we can write an if statement 值，因此在阅读原理之后，我们可以编写一条if语句
like this if principle is greater than or equal to 1000 and it is less than or 如果原理大于或等于1000并且小于或等于
equal to 1 million and here we can use an underscore to separate these digits 等于一百万，在这里我们可以使用下划线将这些数字分开
to make our code more readable so if the user enters a valid value then we can 使我们的代码更具可读性，因此，如果用户输入有效值，则我们可以
break out of this infinite loop otherwise we're gonna print an error 打破这个无限循环，否则我们将打印一个错误
message so enter a value between 1001 million like this ok now if you look on 消息，因此请输入1001百万之间的值
the right side here you can see this red bar this indicates an error and here in 在右侧，您可以看到这个红色的条形，表示有错误，在这里
this preview window you can see exactly where we have an error it's down below 在此预览窗口中，您可以看到下面的确切错误信息
on line 30 where we calculate the mortgage so if you click on this red bar 在第30行，我们在其中计算抵押贷款，因此，如果您单击此红色栏
we jump over here principal is highlighted in red so here we have a 我们跳过这里，校长以红色突出显示，所以这里有一个
compilation error cannot resolve symbol principle here is the reason because 编译错误无法解决符号原理，这是因为
we've wrapped these few lines inside this while loop and earlier I told you 我们已经在while循环中包裹了这几行，之前我告诉过你
that whenever you declare a variable that variable scope to the Block in 每当您声明一个变量时，该变量作用于Block中
which it's defined so this is where we have declared the principal variable and 它是定义的，所以我们在这里声明了主体变量，
it's scoped to this block it's not available outside of this block that's 它的作用域是此块，在该块之外不可用
why we get this compilation error so to solve this problem we need to declare 为什么我们得到这个编译错误，所以要解决这个问题，我们需要声明
this outside of this while loop we can do it right here after radicular our 在while循环之外，我们可以在使我们发根
constants so let's say int principle and we can initialize it to 0 now we remove 常量，所以我们说int原理，现在我们可以将其初始化为0
the declaration from here and the error is gone now we need to repeat the same 从这里开始声明，错误消失了，我们需要重复相同的操作
pattern with other questions so real quick here's our second question where 与其他问题的模式如此快速，这是我们的第二个问题，
we read the annual interest once again we add an infinite loop 我们再次读取年度利息，然后添加无限循环
now the moment we read the annual interests invalidate the data so if 现在我们读取年度利息的那一刻使数据无效，所以如果
annual interest is greater than or equal to let's say one and it is less than or 年利息大于或等于一个，而小于或等于
equal to 30 then we're gonna break out of this infinite loop 等于30，那么我们将摆脱无限循环
now here we should also calculate the monthly interest so the proper way to do 现在在这里，我们还应该计算每月的利息，这样正确的方法
this is like this if the user enters a valid value we add a code block here 就像这样，如果用户输入有效值，我们在此处添加代码块
first we calculate the monthly interest and then break out of the loop otherwise 首先我们计算每月利息，然后跳出循环
we print an error message enter a value between 1 and 30 okay now if you look to 我们会输出一条错误消息，输入1到30之间的值。
the right side again we have two compilation errors monthly interest is 右边又是两个编译错误，每月利息是
not resolved because we have declared it inside of this block so let's move the 尚未解决，因为我们已在此代码块中声明了它，所以让我们移动
declaration to the top here we remove the float keyword and declare monthly 在此处进行顶部声明，我们删除float关键字，然后每月进行声明
interest over here that's better and finally for the last 对这里的兴趣更好，最后是最后
question one more time we're to wrap it in this infinite loop 再问一次我们要把它包装在这个无限循环中
this is where we read the number of years and right after this line we need 这是我们读取年数的地方，在此行之后，我们需要
to do our data validation so if yours is greater than or equal to one and it's 进行我们的数据验证，因此如果您的数据大于或等于1并且
less than or equal to 30 here we add a code block this is where we calculate 小于或等于30，这里我们添加一个代码块，这是我们计算的地方
the number of payments and then we break actually I forgot to type an S here 付款数量，然后我们实际上中断了，我忘了在这里键入S
otherwise if the user enters an invalid value will simply print an error message 否则，如果用户输入无效值，将仅显示错误消息
enter a value between 1 and 30 now here once again we have a compilation error 现在输入1到30之间的值，这里再次出现编译错误
because number of payments cannot be resolved so we remove the declaration 由于付款数量无法解决，因此我们删除了声明
from here and we'll be to the top right here number of payments so this is how 从这里开始，我们将到达这里的右上角付款次数，所以这就是
we add data validation to this program the problem is that this code the code 我们向该程序添加数据验证，问题是该代码
inside the main method is now getting a little bit too long and this hurt the 里面的主要方法现在变得有点太长，这伤害了
maintainability of our program someone else reading this code they have to look 我们程序的可维护性，其他人必须阅读此代码，
at all these statements to figure out what's going on this is where we need to 在所有这些陈述中找出正在发生的事情，这是我们需要
break this code down into smaller easier to read and easier to understand chunks 将此代码分解为更小的可读性和更容易理解的块
and that's what I'm gonna show you next 这就是我接下来要告诉你的
so in this section you'll learn how to control the flow of execution of your 因此，在本节中，您将学习如何控制执行流程
programs we started off by talking about the comparison operators for comparing 我们从谈论比较运算符开始的程序开始
primitive values then we talked about the logical operators like and or and 原始值，然后我们讨论了逻辑运算符，例如和或和
not I showed you how we can use these operators for implementing real word 我没有告诉你如何使用这些运算符实现实词
rules and then we talked about three types of control flow statements you 规则，然后我们讨论了三种类型的控制流语句
learn about conditional statements like if and switch for making decisions in 了解条件语句，例如if并切换为
our programs then you learn about loops for executing code repeatedly we looked 我们的程序，然后您了解了重复执行代码的循环
at four types of loops for loops while loops do-while loops and for each loops 在四种类型的循环中，while循环，dos-while循环和每个循环
and finally we looked at the break and continue statements for breaking or 最后，我们查看了break并继续执行break或
jumping to the beginning of a loop I hope you learned a lot and been enjoying 跳到循环的开始，希望您学到很多东西并一直在享受
the course so far [Music] 到目前为止的课程
as Martin Fowler said any fool can write code that a computer can understand good 正如马丁·福勒（Martin Fowler）所说，任何傻瓜都可以编写计算机可以理解的代码
programmers write code that humans can understand I can't agree more if you 程序员编写的代码人类可以理解，如果您
have seen any of my courses you probably know that I've put a lot of emphasis on 看过我的任何课程，您可能都知道，我非常重视
writing clean code so I have dedicated this entire section on clean coding 编写干净的代码，所以我将整个部分专门用于干净的代码
we're going to continue extending our mortgage calculator and add new features 我们将继续扩展抵押贷款计算器并添加新功能
to it along the way you will see our code starts to get messy and hard to 沿途您会看到我们的代码开始变得混乱且难以理解
maintain so I will show you a few techniques for changing the structure of 保持，所以我将向您展示一些更改结构的技术
the code and make it clean and beautiful are you ready let's jump in and get 代码，使代码整洁漂亮准备好了，让我们进入并获取
started 开始了
hey guys maj here i want to congratulate you on your determination for learning I 嘿，在这里，我要祝贺您学习的决心，我
would really appreciate it if you support me by liking and sharing this 如果您喜欢并分享我的支持，我将不胜感激
video also subscribe to my channel and enable notifications so next time I 视频还订阅了我的频道并启用了通知，所以下次我
upload a video you get notified now if you want to learn more I would encourage 如果您想了解更多信息，请上传您现在得到通知的视频，我鼓励
you to enroll in my ultimate Java series as I told you earlier this YouTube 您可以参加我之前在YouTube上告诉您的终极Java系列
tutorial is the first two hours of this series if you're serious about learning 如果您认真学习，本教程是本系列的前两个小时
Java and want to become a professional job of developer I highly encourage you Java，并希望成为开发人员的专业工作，我强烈鼓励您
to enroll in this series in case you're interested I put the link down below in 如果您有兴趣，请参加本系列文章，我在下面将链接放在
the description box thank you on have a fantastic day 描述框感谢您度过了愉快的一天
[Music] [音乐]