Java Tutorial for Beginners

Java Tutorial for Beginners


Java Tutorial for Beginners

Java tutorial for beginners - Learn Java, the language behind millions of apps and websites.
🔥 Want to master Java? Get my complete Java mastery bundle: http://bit.ly/2tKoy8C
👍 Subscribe for more Java tutorials like this: https://goo.gl/6PYaGF

📕 Get my FREE #Java cheat sheet: http://bit.ly/2JNlQnw

⭐️ Want to learn more from me? Check out these links:

Courses: https://codewithmosh.com
Twitter: https://twitter.com/moshhamedani
Facebook: https://www.facebook.com/programmingw
Blog: http://programmingwithmosh.com

TABLE OF CONTENTS

0:00:00 Introduction
0:01:46 Installing Java
0:03:59 Anatomy of a Java Program
0:08:41 Your First Java Program
0:15:59 Cheat Sheet
0:16:29 How Java Code Gets Executed
0:22:54 Course Structure
0:25:22 Types
0:25:57 Variables
0:29:07 Primitive Types
0:34:27 Reference Types
0:39:15 Primitive Types vs Reference Types
0:43:39 Strings
0:50:42 Escape Sequences
0:53:22 Arrays
0:58:47 Multi-Dimensional Arrays
1:01:23 Constants
1:03:15 Arithmetic Expressions
1:07:18 Order of Operations
1:08:40 Casting
1:15:08 The Math Class
1:19:50 Formatting Numbers
1:25:40 Reading Input
1:30:45 Project: Mortgage Calculator
1:32:55 Solution: Mortgage Calculator
1:37:14 Types Summary
1:38:43 Control Flow
1:39:30 Comparison Operators
1:41:16 Logical Operators
1:45:52 If Statements
1:50:18 Simplifying If Statements
1:53:47 The Ternary Operator
1:56:16 Switch Statements
2:00:07 Exercise: FizzBuzz
2:06:05 For Loops
2:09:53 While Loops
2:14:19 Do…While Loops
2:15:36 Break and Continue
2:18:52 For-Each Loop
2:21:59 Project: Mortgage Calculator
2:23:27 Solution: Mortgage Calculator
2:28:28 Control Flow Summary
2:29:25 Clean Coding

#Programming


Content

0.03 -> Hi! My name is Mosh and I'm gonna be your instructor in this Java course. In this
3.99 -> course, you're gonna learn everything you need to get started programming in Java.
7.41 -> We'll start off by installing all the necessary tools to build Java
11.07 -> applications then you're gonna learn about the basics of Java you'll learn
14.7 -> how Java code gets executed you'll learn how to build simple algorithms and
19.41 -> throughout this course I'm gonna share with you lots of tips and shortcuts from
23.1 -> my years of experience I'll teach you how to write good code like a
26.64 -> professional developer so we'll end up watching this course you will have a
30.39 -> solid foundation in Java and be ready to learn about advanced Java features I've
35.25 -> designed this course for anyone who wants to learn Java if you're a beginner
38.46 -> don't worry I'll make Java super simple and hold your hands through this entire
42.3 -> course you're not too old or too young you'll write your first Java program in
46.26 -> minutes my name is mosh I'm a software engineer
48.719 -> with two decades of experience and I've taught over 3 million people how to code
53.37 -> and how to become professional software engineers I have a coding school at code
58.109 -> with mass comm where you can find plenty of courses that help you take her coding
62.43 -> skills to the next level I hope you'll stick around and learn this beautiful
65.82 -> and powerful programming language and now award from this video sponsor as
70.14 -> someone who runs an online business I cannot stress enough the importance of
74.31 -> staying safe online which is why I was so excited when dashlane reached out to
78.21 -> me if you don't know - Lane is the password manager and VPN recommended by
83.189 -> Apple and Google and it's a fantastic safeguard for keeping your information
87.27 -> secure it's completely free to use for your first device so head over to - Ling
91.59 -> comm / marché - give it a go if you want to upgrade to the premium to get VPN or
96.21 -> dark web monitoring you can use the promo code mosh to get 10% off sign up
101.28 -> for - 9 today and keep yourself safe online now back to the course
108.75 -> in this java tutorial we're going to download and install the necessary tools
113.89 -> to build java applications so open up your browser and search for jdk download
121.14 -> jdk is short for Java development kit and it's basically a software
125.86 -> development environment for building Java applications it has a compiler it
130.51 -> has a bunch of code that we can reuse it has a Java Runtime environment at a
135.01 -> bunch of other stuff so over here you can see this page on Oracle com Java se
141.52 -> which is short for Java standard edition click on this now over here click on
147.64 -> this icon now on this page we can see Java development kit for various
153.31 -> platforms like Linux Mac OS and Windows here I'm on a Mac so I'm gonna download
158.47 -> this dmg file over here now before we do this first we need to accept the license
164.44 -> agreement all right now let's download the dmg let me open this we're gonna say
169.87 -> this package let's double click this and here we see this installation wizard
174.55 -> it's super easy just click continue and install you have to enter your
179.62 -> computer's password and then alright done beautiful so we can move this to
187.36 -> trash now the next piece of software we need is a code editor there are so many
191.92 -> cool editors for building Java applications the popular ones are
195.31 -> NetBeans Eclipse and IntelliJ in this Java course I'm gonna use IntelliJ but
200.98 -> if you have a favorite editor feel free to use that to take this course that's
204.43 -> perfectly fine so let's search for IntelliJ download all right you can see
212.47 -> download IntelliJ IDEA click on this link over here download the community
218.02 -> edition which is absolutely free and it's more than enough for this course so
222.22 -> download all right now let's drag and drop this onto the Applications folder
229.05 -> beautiful alright we've installed all the necessary tools to build Java
233.35 -> applications so next we're gonna look at the anatomy of a Java program
242.2 -> in this java tutorial we're gonna look at the anatomy of java programs the
247.55 -> smallest building block in java programs are functions
250.73 -> if function is a block of code that performs a task as a metaphor think of
255.38 -> the buttons on the remote control of your TV each button performs a task
259.45 -> functions in programming languages are exactly the same for example we can have
263.48 -> a function for sending emails to people we can have a function for converting
267.98 -> someone's weight in pounds to kilograms we can have a function for validating
271.97 -> users input and so on now let's see how we can code a function in Java we start
277.25 -> by specifying the return type of that function some functions return a value
281.12 -> like a number at day time and so on other functions don't return anything so
285.92 -> the return type of this functions is void void is a reserved keyword in Java
290.09 -> and that's why I've coded that in blue here now after the return type we have
295.1 -> the name of our function so here we should give our function a proper
298.82 -> descriptive name like send email this name clearly identifies the purpose of
304.82 -> this function okay now after the name we have a pair of parentheses and inside
310.31 -> these parentheses we add the parameters for this function we use these
314.3 -> parameters to pass values to our function for example our send email
318.77 -> function should have parameters like who is the receiver what is the subject of
322.46 -> this email what is the content of this email and so on now in this tutorial
326.15 -> we're not gonna worry about parameters we'll look at them in the future
329.18 -> now after the parentheses we had a pair of curly braces and inside these braces
334.7 -> we write the actual Java code now one thing I want you to pay attention to
339.169 -> here is that in Java we put the left brace on the same line where we define
344.33 -> our function in other programming languages like C sharp it's more
348.74 -> conventional to put the left brace on a new line but we don't do that in Java so
353.33 -> we put the left brace on the same line where we define our function now every
358.61 -> Java program should have at least one function and that function is called
362.33 -> main so main is the entry point to our programs whenever we execute a Java
368.03 -> program the main function gets called and the code inside this
372.06 -> function gets executed okay now these functions don't exist on their own they
376.68 -> should always belong to a class so a class is a container for one or more
382.26 -> related functions basically we use these classes to organize our code just like
386.639 -> how products are organized in a supermarket in a supermarket we have
390.54 -> various sections like vegetables fruits cleaning products and so on each section
395.669 -> contains related products by the same token a class in java contains related
401.49 -> functions now every Java program should have at least one class that contains
406.53 -> the main function can you guess the name of that class its main so this is how we
412.11 -> define a class in Java we start with a class keyword then we give our class a
417.06 -> proper descriptive name and then we add a pair of curly braces now the functions
422.49 -> that we define in between these curly braces belong to this class and more
427.29 -> accurately we refer to them as methods so a method is a function that is part
432.24 -> of a class in some programming languages like Python we can have a function that
436.89 -> exists outside of a class so we call it a function but when a function belongs
441.66 -> to a class we refer to it as a method of that class okay now in Java all these
447.63 -> classes and methods should have an access modifier an access modifier is a
453.06 -> special keyword that determines if other classes and methods in this program can
458.19 -> access these classes and methods we have various access modifiers like public
463.229 -> private and so on now most of the time we use the public access modifier so we
468.36 -> put that in front of our class and Method declarations so this is the basic
473.49 -> structure of a Java program at a minimum we have a main class and inside this
478.86 -> main class we have the main method now you might be curious why we have a
483 -> capital m in the name of this class because in Java we use different
487.139 -> conventions for naming our classes and our methods to name our classes we use
491.76 -> the Pascal naming convention and that basically means the first letter of
496.32 -> every word should be uppercase in contrast to name our methods we use the
501.15 -> camel naming convention and that means the first letter of every word should be
505.44 -> operate case except the first wart so that is
508.7 -> why we have a capital m in the name of this class alright now that you
512.6 -> understand the anatomy of a Java program let's create a new Java project and see
517.43 -> all these building blocks in action in this Java tutorial you're gonna learn
526.67 -> how to write and execute your first Java program so let's open IntelliJ IDEA here
533.3 -> on the home screen let's create a new project alright on the left side select
538.88 -> java and make sure project sdk is not black so earlier we downloaded jdk or
544.58 -> Java development kit version 12 that is why JDK version 12 is selected here if
550.7 -> you don't see that make sure to select it from this drop-down list alright now
554.84 -> let's click on next on this page select create project from template so we're
560.51 -> gonna create a command line application which is an application that we can run
564.11 -> from the command line it doesn't have a graphical user interface or a GUI
568.82 -> now I know command line application is not as exciting as an application with a
573.14 -> graphical user interface like a mobile app or a desktop app but trust me
577.7 -> building an application with a graphical user interface is very complicated so
581.48 -> for now we're just gonna build command line applications to learn Java properly
585.8 -> once you learn Java properly then you can learn about building desktop or
589.85 -> mobile applications with Java all right now let's click on next on this page we
595.85 -> have to give our project a name let's call it hello world now over here you
601.1 -> can see the location of this project so it's inside the idea projects folder now
606.56 -> right below that you can see the base package which is set to comm that code
610.94 -> with Marsh on my machine and your mission is probably gonna be comm dot
615.11 -> package what is this well here we talked about classes and methods I told you
620.63 -> that a class is a container for related methods so we use classes to organize
625.31 -> our code by the same token we have a concept called package and we use a
630.17 -> package to group related classes so as our applications grow we're gonna end up
634.91 -> with hundreds or even thousands of classes so we should properly organize
638.81 -> this class us into packages now by convention the
642.35 -> base package for a Java project is the domain name of your company in Reverse
646.31 -> so my website is code with mass comm that is why I'm gonna set the base
651.02 -> package for this project to come that code with Marsh now it doesn't mean that
655.7 -> you should have an actual domain registered on an Internet this is just a
659.18 -> way to create a namespace for our classes so now every class that we
663.29 -> create in this project will belong to this package we're gonna talk about
667.25 -> packages in more detail in the future so for now just type a base package for
672.02 -> your project it can become that your name or whatever it doesn't really
675.56 -> matter all right now let's go forward alright here's our first Java project
682.04 -> now this code editor might look a little bit intimidating at first but trust me
686.21 -> it's really easy and you're gonna learn about it throughout this course on the
689.57 -> left side we have the project panel where we can see all the folders and
693.38 -> files in a project for example on the top we have the hello word project
699.34 -> inside this project we have the source folder where we have the source code of
704.03 -> a project now in this folder we have another folder that is calm that code
709.46 -> with Marsh that is the name of our base package and in this package we have this
714.26 -> class main so you can see this main file opened on the right side here now look
720.32 -> at the name of this file its main the Java so all Java files should have the
725.03 -> Java extension okay now let's collapse the project panel by clicking on this
730.88 -> icon that is better so see what we have here on top of this file we have the
736.43 -> package statement and this is used to specify what package this class belongs
741.89 -> to so the main class that we have here belongs to this package now this package
747.89 -> statement is terminated by semicolon so in Java wherever we have a statement we
752.93 -> should terminate that statement with a semicolon this is exactly like c-sharp
756.83 -> or C++ now below this package statement we have our main class exactly like what
762.71 -> you saw in the previous tutorial so we have public class main with a pair of
767.84 -> curly braces inside this class we have our main method so it's a public method
772.94 -> which may it's accessible from other parts of this
775.58 -> program it's static we haven't talked about static metals yet we'll talk about
780.17 -> them in the future for now just remember that the main method in your program
783.769 -> should always be static the return top of this method is void which means this
788.36 -> method is not gonna return a value and here in parentheses we have one
792.44 -> parameter for this function we can use these parameters to pass values to our
796.73 -> program again we'll look at this in the future now right after this parameters
800.899 -> as you can see the left brace and this is where we write the code in this
805.79 -> method now by default we have this line prefix with two slashes this indicates a
811.61 -> comment we use these comments to explain our code to other people so these
816.019 -> comments don't get executed now let's remove this comment and write a bit of
820.76 -> code to print something on the terminal so here we're gonna use the system class
825.709 -> in Java so capital S system here in this tooltip you can see the system class is
831.89 -> defined in this package Java dot Lang or language also look at this icon on the
838.13 -> left side this indicates a class now inside this class we have various
842.959 -> members we can use the dot operator to see the members defined in the system
848.06 -> class now the member that we're gonna access is out look at the icon of this
853.52 -> member it's F which is short for field you're gonna talk about fields in the
857.75 -> future when we talk about classes and object-oriented programming now what is
862.13 -> interesting here is the type of this field and you can see that on the right
865.19 -> side that is print string so print string is another class that is defined
870.5 -> in Java so once again we use the dot operator to look at the methods or
875.63 -> members defined in the print stream class the method we're gonna use is
880.49 -> print Ln which is short for line look at the icon for this method so M indicates
885.8 -> a method now you press ENTER and IntelliJ automatically adds these
892.43 -> parentheses as well as a semicolon so now with the code on line six we're
897.32 -> calling or executing the print line method earlier I told you that inside
901.91 -> this parenthesis we can pass values to our methods here we want to print the
906.35 -> hello word on the terminal so let's type double
909.709 -> quotes and inside these quotes right hello world so hello word is textual
915.709 -> data in Java whenever we deal with textual data we should always surround
920.389 -> them with double quotes now we say we have his string so a string is a
925.279 -> sequence of characters all right so we're done with our first program now to
930.23 -> execute this we can click on this icon on the toolbar look at the shortcut on
935.12 -> Mac it's ctrl + R I always prefer to use shortcuts because they're faster so ctrl
940.639 -> + R now IntelliJ is building our application and we can see the result in
946.97 -> this little terminal window so here's our hello work message so that was our
952.459 -> first Java program next I'm going to explain how Java code gets executed
956.72 -> under the hood hey Marsh here I just wanted to let you
961.76 -> know that you really don't have to memorize anything in this course I've
964.79 -> put together a comprehensive cheat sheet with summary notes that you can download
968.269 -> below this video so I've done my best to create the best possible Java course and
973.67 -> I would really appreciate it if you support me by liking and sharing this
977.54 -> video on the social networks that you use often also be sure to subscribe and
981.709 -> enable the notifications so next time I upload a video you'll get notified thank
986.06 -> you so much and let's continue watching
989.92 -> all right now let's see what exactly happens under the hood the moment we run
994.76 -> a Java program in IntelliJ there are basically two steps involved
998.66 -> here compilation and execution in the compilation step IntelliJ uses the Java
1004.54 -> compiler to compile our code into a different format called Java bytecode
1009.1 -> this Java compiler comes with the Java development kit that we downloaded at
1014.14 -> the beginning of the course let me show you so here we can right click on this
1018.07 -> main the Java and in this context menu we have an item called open in terminal
1024.309 -> it's down below unfortunately it's not visible in my recording window it's
1028.54 -> called open in terminal on Mac and probably open in command prompt on
1032.77 -> Windows so let's open that we get this terminal window or command prompt on
1038.77 -> windows here we're currently inside of this folder code with Maj that is where
1043.689 -> we have our main the Java file now we can invoke the Java compiler like this
1048.61 -> Java C and pass the name or Java file as an argument so main the Java if you're
1055.51 -> on Mac or Linux make sure to spell this with a capital M because these operating
1059.47 -> systems are case-sensitive so enter now let's look at the content of this folder
1065.29 -> on Mac or Linux we can type LS on windows we type dir so let's take a look
1072.4 -> in this folder now we have a new file main class this is the bytecode
1077.25 -> representation of this Java file now let me use IntelliJ to run our Java program
1082.75 -> this class file gets stored somewhere else let me show you so back to the
1087.7 -> project panel here in our project we have this source folder where we
1092.89 -> our source code and we have this out folder where we have the result of the
1097.39 -> compilation so inside this folder we have production inside this we have
1102.43 -> hello world the same name as our project inside hello world we have comm which is
1107.08 -> the name of our top level package inside this package we have a sub package that
1112.36 -> is code with Marsh and here we have our main dat class file so this was the
1117.28 -> compilation step now this Java bytecode that we have in this file is platform
1122.13 -> independent and that means it can run on Windows Mac Linux or any operating
1127.63 -> systems that has a Java runtime environment if you go to java.com slash
1133.48 -> download we can download java or more accurately java runtime environment for
1138.67 -> various operating systems this Java Runtime environment has a software
1142.96 -> component called Java Virtual Machine or JVM this JVM takes our Java bytecode and
1149.79 -> translates it to the native code for the underlying operating system so if you're
1155.02 -> on Windows machine this Java Virtual Machine converts or Java bytecode into
1159.85 -> the native code that windows can understand this architecture is the
1163.63 -> reason why Java applications are portable or platform independent we can
1168.67 -> write a Java program on a Windows machine and execute it on Linux Mac or
1173.26 -> any other operating systems that have a Java runtime environment
1176.74 -> c-sharp and python also have the same architecture that's why they are
1180.91 -> platform independent as well now let me show you how to invoke this java virtual
1185.74 -> machine to run a Java program so back to this terminal window let me expand this
1191.88 -> currently we are inside of this folder code with Marsh and in this folder we
1196.12 -> have this class file now let's go one level up so CD dot dot and one more time
1203.73 -> so now we are inside the source folder we can invoke Java Virtual Machine like
1209.08 -> this they type Java and then we type the full pass to our main class file what do
1214.72 -> I mean by that well earlier we defined this package comm that code with Marsh
1220.24 -> and this class the main class is part of this package so the full path
1226.39 -> this class is calm dot code with Marsh dot main make sure to use a capital M
1234.82 -> here because this is case sensitive now when we press ENTER Java will look at
1240.19 -> this folder calm inside this folder it will look at this other folder code with
1244.42 -> Marsh and then it will find main that class in that folder it will load the
1249.28 -> bytecode and convert it to the native code for the operating system we are
1253.21 -> using so take a look so it executed our program hello world beautiful let me run
1259.27 -> a program using IntelliJ all these steps are hidden from us we don't see the
1263.44 -> compilation or execution steps
1268.16 -> so you have seen Java in action now let me tell you five interesting facts about
1273.33 -> Java Java was developed by James Gosling in 1995 at Sun Microsystems which was
1280.08 -> later acquired by Oracle in 2010 it was originally called oke after an oak tree
1285.57 -> that stood outside Gosling's office later it was renamed to green and was
1289.92 -> finally renamed to Java inspired by Java coffee that's why its logo looks like
1294.9 -> this we have four editions of Java for building different kinds of applications
1299.55 -> we have Java standard edition this is the core Java platform which is what
1304.02 -> we're using in this course it contains all of the libraries that every Java
1308.01 -> developer must learn we have Java Enterprise Edition which is
1311.91 -> used for building very large-scale and distributed systems it's built on top of
1316.98 -> java standard edition and provides additional libraries for building fault
1321.99 -> tolerant distributed multi-tiered software we have java micro Edition
1327.03 -> which is a subset of Java standard edition designed for mobile devices so
1332.04 -> it has libraries specific to mobile devices and finally we have Java card
1336.6 -> which is used in smart cards the latest version of Java is Java standard edition
1342.48 -> 12 which was released just a few months ago in March 2019 Java has close to 9
1348.18 -> million developers worldwide currently about 3 billion mobile phones run Java
1352.89 -> as well as 120 million TV sets and every blu-ray player according to indeed.com
1358.95 -> the average salary of a Java developer is just over $100,000 per year in the US
1365.28 -> so as we can see Java is everywhere which means more opportunities for you
1369.42 -> to get hired as a professional programmer now let me give you a quick
1376.11 -> overview of how I've structured this course so you can get the most out of it
1380.04 -> this course is the first part of my complete four-part Java series each part
1385.35 -> is about three to four hours long so it can easily complete it in a day or two
1389.34 -> in the first part which is what you're watching you're gonna learn the
1392.94 -> fundamentals of programming with Java in the next section you'll learn about the
1397.05 -> type system in Java you will learn how to work with various types such as
1401.24 -> purrs strings boolean's and arrays by the end of this section you will build a
1405.83 -> mortgage calculator as your first Java project will be improving this
1410.48 -> calculator bit by bit routers course next you will learn about control flow
1415.13 -> statements that are used to build algorithms we'll be talking about
1418.88 -> various types of conditional statements and loops later in this section we'll
1423.29 -> add data validation to our mortgage calculator to force the user to enter
1427.7 -> valid values at this point you'll be able to build basic algorithms and
1432.38 -> that's great but being a good programmer requires
1434.9 -> knowing how to write good code code that is clean and expressive that's what
1440.03 -> separates an outstanding programmer from an average programmer so in the
1443.96 -> following section we'll talk about clean coding I will show you various
1447.35 -> techniques that professional programmers use to make their code clean and
1451.25 -> maintainable and finally in the last section you will learn how to find and
1455.72 -> fix errors in your java programs as well as how to package them for deployment so
1460.76 -> others can use them so the materials in the first part will give you a solid
1465.11 -> foundation on how to start programming in Java in the second part we'll talk
1470.33 -> about object oriented programming which is a style of programming use in most if
1474.86 -> not all Java applications whether you want to use Java to build web mobile or
1479.93 -> desktop applications you need to understand object-oriented programming
1483.5 -> well because otherwise you're going to constantly hit obstacles in the third
1487.79 -> part we're going to talk about core Java API is or application programming
1492.14 -> interfaces you'll learn about many of the useful classes in the standard Java
1496.67 -> library and finally in the last part we'll be looking at the advanced
1500.45 -> features in Java such as streams threads database programming and so on so I hope
1506.33 -> you're gonna join me on this journey and master Java the most popular programming
1510.8 -> language behind millions of apps and websites
1515.73 -> you
1524.29 -> intersection we're gonna look at the fundamentals of programming in Java
1528.17 -> you're gonna learn about variables and constants primitive and reference types
1532.76 -> you're gonna learn about casting or type conversion you will learn how to work
1537.11 -> with numbers strings and arrays and how to read input from the user
1541.31 -> once you learn all this I'm gonna give you a project you're gonna build a
1544.73 -> mortgage calculator on your own so make sure to pay great attention to all the
1548.75 -> materials you're gonna learn because you're gonna use most of them in this
1551.66 -> project are you ready now let's jump in and get started in this tutorial we're
1562.13 -> gonna talk about variables in Java we use variables to temporarily store data
1566.84 -> in computer's memory here is an example imagine in this program you want to
1571.28 -> store someone's age in the memory so we declare a variable like this ant age
1576.85 -> equals 30 so int or integer is the type of this variable so in this variable we
1583.58 -> can only store integers which are whole numbers like 1 2 3 4 numbers that don't
1588.59 -> have a decimal point now in Java we have several different types I'm gonna talk
1592.52 -> about them in the next tutorial so first we specify the type of our variable then
1597.44 -> we give it a name or a label this is also called an identifier because we use
1602.18 -> it to identify our variable this equal sign is called the assignment operator
1606.98 -> and 30 is the initial value that we are assigning to this variable so we say on
1612.59 -> line 6 we're initializing this variable which means we're giving it an initial
1617.18 -> value you always have to initialize our variables before reading them so with
1622.34 -> this line we're storing number 30 somewhere in computer's memory and we're
1626.33 -> assigning this label to that memory location now on line 7 instead of
1630.92 -> printing hello world we can print the value of the age variable take a look so
1636.65 -> I'm gonna run this program using ctrl + R there you go now we see 30 on the
1643.55 -> terminal beautiful we can also change the value of our variable so after we
1648.77 -> initialize it per house we can change it to 35 now when we run this program again
1655.74 -> we see it 35 beautiful we can also initialize multiple variables on the
1661.38 -> same line but this is something that I don't recommend because it makes your
1664.95 -> code ugly and hard to read here is an example we can declare another variable
1669.53 -> like temperature and set it to 20 so using a comma we can declare multiple
1675.81 -> variables on the same line now even though this is technically possible it's
1679.95 -> not something that I recommend so it's always better to declare one variable on
1685.47 -> each line like this we can also copy the value of one variable into another here
1690.99 -> is an example let me delete these variables and declare a new variable
1695.07 -> called my age we set it to 30 and then we declare another variable like her age
1701.1 -> and we set it to my age so now when we print her age we're gonna see 30 take a
1709.62 -> look so on line seven recapping the value of
1714.97 -> this variable into this other variable now one thing I want you to pay
1718.78 -> attention to here is the convention I have used for naming our variables as I
1723.55 -> told you before this is called the camelcase notation so we should
1727.51 -> capitalize the first letter of every word except the first word so in this
1732.67 -> case the first word my it's all in lowercase but the second word starts
1737.35 -> with a capital letter so this is all about declaring and initializing
1740.92 -> variables in the next tutorial we're going to talk about various types in
1744.82 -> Java in this tutorial we're going to talk about various types in Java
1754.62 -> basically we have two categories of types we have primitive types for
1759.07 -> storing simple values and non primitive types or reference types for storing
1763.81 -> complex objects so in the category of primitive types we have white which
1768.46 -> takes one byte of memory and in one bite we can store values from 128 to 127 now
1776.26 -> the more bytes we have the larger numbers we can store so next we have
1780.19 -> short which takes two bytes and with this we can store values up to 32,000
1785.08 -> next we have integer which we have seen before integers take four bytes of
1789.28 -> memory and allow us to store values up to two billion then we have long which
1794.29 -> takes eight bytes and with this we can store even larger numbers now all these
1798.67 -> types are for storing whole numbers that don't have a decimal point if you want
1803.29 -> to store a number that has a decimal point you have to use float or double
1807 -> float takes four bytes double takes eight bytes so obviously we double we
1811.27 -> can store larger numbers next we have char for storing a single character like
1816.04 -> ABC and this chart I've take two bytes so they support international letters
1821.53 -> and finally we have boolean for storing boolean values which can be true or
1825.88 -> false just like yes or no in English now let's take a look at a few examples
1830.53 -> earlier we use an integer for storing someone's age but as you learned
1835.66 -> integers take four bytes and allow us to store values up to two billion we don't
1840.67 -> need four bytes of memory to store someone's age all we need is one byte
1844.87 -> because with one bite we can store values up to 127 so I'm gonna change
1850.12 -> this to byte that is better now let's look at another example let's say we
1855.13 -> want to store the number of times a YouTube video has been watched so we
1859.15 -> define an integer called views count note that I'm always using meaningful
1864.07 -> names for my variables because these names help us understand what this code
1867.94 -> does I've seen some people use variable names like V or V 1 or n nobody knows
1874 -> what these variables do so as a best practice always use meaningful and
1878.23 -> descriptive names for your variables so views count we set this to a large
1883.33 -> number like 1 2 3 4 5 6 7 8 9 now in Java whenever you deal with a large
1890.08 -> number like this you can use an underscore to separate every three
1894.789 -> digits just like how we use a comma in our documents to make our numbers more
1900.1 -> readable we can use an underscore in Java so with integers we can store
1906.549 -> values up to two billion but let's say the number of times this video has been
1910.6 -> watched is three billion so I had a three here now we have a red on the line
1915.58 -> that indicates an error let me hover our mouse over it we see
1919.149 -> this tooltip integer number too large so we need to change the type of this
1923.98 -> variable to long however the error is still there what's going on here the
1929.74 -> reason we're getting this error is that by default Java sees these numbers as
1933.159 -> integers so even though we have defined the type of this variable as long Java
1937.779 -> compiler sees this value as an integer and he thinks this value is too large
1942.22 -> for an integer to solve this problem we need to add an L as a suffix or this
1947.23 -> number we can use an uppercase or a lowercase L but as you can see a
1951.85 -> lowercase L kind of looks like a 1 so it's better to use a capital L so these
1957.76 -> are examples of whole numbers now let's declare a variable for storing a number
1961.72 -> with a decimal point so double price we set this to 1099 obviously the double
1969.01 -> variable is too large for storing the price of a product so we can change this
1973 -> to float that is better but you have a compilation error here take a look
1978.47 -> incompatible types required float but found double the reason we're seeing
1984.419 -> this error is that by default Java sees these numbers with a decimal point as
1988.409 -> double so even though we set the type of this variable to float Java sees this
1993.69 -> number as a double so just like how we added a suffix to this number to
1999.299 -> represent it as a long we need to add a suffix here to represent this number as
2003.74 -> a float and that suffix is F once again we can use an upper case or lower case F
2010.059 -> so these are examples of numbers now let's store a character so char we call
2016.759 -> it letter and we set it to a note that we should always surround single
2022.19 -> characters with single quotes and multiple characters or strings with
2026.87 -> double quotes okay so char represents only one
2030.44 -> character string represents a series of characters and finally let's see an
2035.299 -> example of a boolean so we define a boolean variable called is eligible is
2041.24 -> this person eligible for loan or not we said this to true or false these are the
2048.53 -> boolean values now note that all these words coded in orange are reserved
2053.359 -> keywords in Java just like public static void class
2058.01 -> package these are all reserved keywords so we cannot use these reserved keywords
2062.54 -> the name our variables classes and methods in the last tutorial you learned
2072.649 -> that we use primitive types to store simple values like numbers boolean
2077.149 -> values or single characters in contrast use reference types to store complex
2082.97 -> objects like data objects or mail messages these are complex objects now
2088.849 -> in Java we have eight primitive types that you have seen before all the other
2092.81 -> types are reference types let me show you an example so here in this program
2098.329 -> first I'm gonna declare a primitive type let's say white age equals 30 now
2105.56 -> declaring and initializing a reference type is slightly different from
2109.73 -> primitive type let me show you so let's type date now here in this
2114.73 -> tooltip box which is called intellisense we can see various classes that have
2119.29 -> date in their name so IntelliJ is helping us complete our code by
2123.64 -> suggesting these class names now here we have a date class in this package Java
2128.8 -> the util so this package contains a lot of utility classes that are useful in a
2133.75 -> lot of programs you also have a date class in a different package Java SQL or
2139.3 -> sequel which is used for programming databases so this is the benefit of
2143.71 -> packages we can have the same class but in different packages they don't
2147.94 -> conflict so packages create a namespace for our classes okay now in this case if
2154 -> we select the first date class and press enter or tab IntelliJ automatically adds
2160.54 -> this line for us import Java that you till the date so because currently we
2167.17 -> are in this package in order to use a class from a different package we need
2172.09 -> to import it so here we're importing the date class in this package will talk
2176.95 -> about packages in more detail in the future so back to our date variable
2181.35 -> let's give this variable a name collect now now we set this here we need to use
2187.84 -> the new operator to allocate memory for this variable and this is one of the
2192.22 -> differences between the primitive and reference types when declaring primitive
2196.09 -> types we don't need to allocate memory memory is allocated and released by Java
2200.44 -> Runtime environment but when dealing with reference types we should always
2204.34 -> allocate memory now we don't have to release this memory Java Runtime
2208.06 -> environment will automatically take care of that so we use the new operator and
2212.59 -> then repeat the name of our class in this case date and then we add
2216.7 -> parentheses followed by a semicolon in this example this variable we have
2222.43 -> defined here is an instance of the date class so this class is defined template
2227.859 -> or blueprints for creating new objects new instances as another example we can
2233.68 -> have a class called human and we can have objects like John Bob Mary and so
2238.81 -> on so an object is an instance of a class now this object or this class
2244.76 -> have members that we can access using the dot operator so we can type now dot
2250.07 -> and these are all the members defined in this class or in this object for example
2254.87 -> we have a method called get time and this returns the time component of this
2259.76 -> object this is another difference between primitive types and reference
2263.3 -> types these primitive types don't have members so if you type age dot we don't
2268.88 -> see anything these items you see here are not members of age their code
2272.93 -> snippets which allow us to quickly generate code for example we can select
2276.95 -> for I and this automatically generates this block of code for us we'll talk
2281.21 -> about this in the future so this age variable is a primitive type it's not an
2286.49 -> object it doesn't have any members and that's why when we use the dot operator
2290.99 -> we don't see anything here now let's delete this line and instead print the
2295.28 -> value of this data object so once again we can type system this is a class so we
2301.19 -> can use the dot operator to access its members here we have out which is a
2305.03 -> field and the type of this field is print string which is another class in
2309.71 -> Java so once again we can use a dot operator and call the print line
2314.57 -> function now let me show you a very cool shortcut instead of typing all this we
2319.22 -> can use one of these code snippets so we type s oh you see and press tab and this
2325.85 -> generates this piece of code for us all right now let's pass our data object
2329.75 -> here note that I have not surrounded this variable with double quotes because
2334.55 -> this is a string and if you run this program we'll see now on the terminal
2338.75 -> there you go we don't want this you want the value of
2342.17 -> our data object not a label so let's remove the quotes I run the program
2349.1 -> again so here's the current date I'm on my machine I've learned a little bit
2359.99 -> about the differences between the primitive and reference types so you
2363.74 -> know that we use primitive types for storing simple values and reference
2367.88 -> types for storing complex objects but there's a very important difference
2371.63 -> between these two categories of types in terms of memory management and that's
2376.55 -> what we're going to talk about in this tutorial
2378.5 -> so I'm going to declare a primitive variable X and set it to 1 and then I'm
2384.08 -> going to declare another variable like Y and set it to X so in this example we
2390.29 -> have two different variables x and y and these two variables are at different
2394.49 -> memory locations so they're completely independent of each other in other words
2399.89 -> if I change the value of X Y is not going to get affected let me show you so
2405.23 -> I'm gonna update X to 2 and then print Y so syu t tab y let's take a look
2413.48 -> so run as you can see Y is not affected because x and y are completely
2419.63 -> independent of each other however when we use a reference time this behavior is
2424.849 -> different let's take a look so I'm gonna delete
2427.67 -> all the code here in Java we have a point class that is defined in this
2434.99 -> package Java that awt so we press ENTER and now we have this import statement on
2442.28 -> the top beautiful let's declare a variable like point 1 and set it to new
2447.44 -> point here we can pass the initial values for x and y so I'm gonna pass 1
2454.49 -> and 1 so intelligent automatically adds these labels x and y now just like
2462.23 -> before I'm gonna declare another variable point 2 and set it to point 1
2467.98 -> and this is where things get interesting when Java Runtime environment executes
2472.88 -> line 8 first it's going to allocate some memory to store this point object let's
2479.119 -> see if the address of that memory location is 100 then it's going to
2483.29 -> allocate a separate part of the memory and it's going to attach this label to
2488.24 -> that memory location point 1 in that memory location it's going to store the
2493.4 -> address of our point object so this is the critical difference between
2498.08 -> primitive and reference types when we declare a primitive variable like a byte
2502.339 -> the value that we assigned to that variable will be stored in that memory
2506.03 -> location but when we use a reference type like this point class our variable
2510.74 -> is going to hold the ax of that point object in memory not the
2514.7 -> actual pointer object now look at line 9 here we're copying the value that we
2520.07 -> have in this variable into this other variable so that value as you learn is
2524.66 -> not the point object is the address or the reference to the point object in
2529.52 -> memory that is why we refer to these tiles as reference types because they
2533.96 -> don't store the actual values they store a reference to an object somewhere in
2538.64 -> the memory so in this example point one and point two are referencing the exact
2544.91 -> same point object in memory we only have one point object so these two variables
2550.49 -> are not independent of each other they're referencing the same object and
2554.63 -> that means if I update this point object through either of these variables the
2558.89 -> changes will be visible to the other variable I'm gonna show you so using the
2563.96 -> first variable point one we're going to update the value of x so we use the dot
2569.36 -> operator and here we can see the members of this object X on Y are both fields
2574.97 -> which are variables that exist inside of a class so we said X just like a regular
2580.16 -> variable to a different value let's say two now because point one and point two
2586.07 -> are referencing the exact same object if we print point two we're going to see
2591.14 -> the change that we just made take a look so S or ut tab let's print
2597.95 -> point to run the program there you go so the change was visible so remember
2607.67 -> this reference types are copied by the references whereas primitive types are
2612.68 -> copied by their value and these values are completely independent of each other
2622.06 -> in this tutorial we're gonna look at strings in Java so earlier in the course
2627.2 -> we printed the hello world message on a terminal this hello word that we have
2631.01 -> here is a string or more accurately it's a string literal
2634.97 -> that means a string value now let's extract this from here and store it in a
2639.38 -> string variable so cut just before this line we type string now look this string
2647.3 -> class is defined in Java that Lang package what is interesting is that we
2652.58 -> don't have an import statement to import this package or import this class
2656.45 -> because this package is automatically imported so we can use any classes that
2660.77 -> are defined in this package now let's declare a variable called message and
2667.42 -> because this is a reference type we should instantiate this variable using
2671.9 -> the new operator so Neal string and here in parenthesis we type our message hello
2679.22 -> world however here we have this little warning take a look new string is
2685.1 -> redundant because in Java there is a shorter way to initialize string
2689.78 -> variables let me show you so instead of using the new operator we simply set
2694.16 -> this to our string literal now on the surface this looks like a primitive type
2700.58 -> because we are not using the new operator but this is just a shorthand to
2704.87 -> initialize a string variable strings are reference types in Java but because we
2709.85 -> use them often there is a short way to create them
2712.43 -> so now let's pass message to the print line method and run our program you get
2719.75 -> the exact same result as before beautiful now let's look at a few
2723.62 -> interesting things that you can do with strings we can concatenate or joining a
2727.94 -> string with another one using the plus operator so here we can combine this
2732.77 -> with another string with two exclamation marks and here's the result now because
2742.52 -> string is a class it has members that we can access using the dot operator so we
2747.35 -> can type message dot and these are all the methods or functions do
2751.88 -> find in the string class for example we have this method here ends with and with
2757.16 -> this we can check to see if our string ends with a character or sequence of
2761.24 -> characters for example here we can pass their string to see if our message ends
2765.95 -> with two exclamation marks now instead of printing the message
2770.329 -> let's print this expression here so let's run the program we get true so
2779.839 -> this method that we have called here returns a boolean value which can be
2783.98 -> true or false we also have another method starts with let's take a look now
2790.089 -> in this case we get false because our message doesn't start with two
2794.39 -> exclamation marks another useful method is length so we can call that to get the
2800.839 -> length of a string which is the number of characters so message dot length take
2806.599 -> a look so in this string we have 13 characters
2811.22 -> and this is useful in situations where you want to check the length of the
2815 -> input by the user for example you might have a sign-up form with a username
2818.75 -> field you can check the length of someone's username and give them an
2822.5 -> error if the username is longer than let's say 20 characters pretty useful we
2827.96 -> also have another method that is index of and this returns the index of the
2834.619 -> first occurrence of the character or the string that we pass here for example if
2839.299 -> you pass H the index of H is 0 so let's run the program there you go we get 0 if
2847.25 -> you pass e we get 1 because the index of the first II in this message is 1 now
2855.289 -> what if you pass a character or a string that doesn't exist in this message let's
2859.49 -> say Skye we get negative 1 so with this method we can check to see if a string
2867.829 -> contains certain characters or words or sentences and so on another useful
2873.14 -> method is replace and with this we can replace one or more characters with
2878.72 -> something else for example we can replace any exclamation marks with
2885.5 -> let's say a store so this replace metal has two parameters one is target the
2892.05 -> other is replacement and here we're passing two values for these parameters
2896.73 -> here's the first value here is the second value and we have separated these
2900.569 -> values using a comma now in programming terms we refer to this values as
2906.05 -> arguments a lot of programmers don't know the difference between parameters
2910.23 -> and arguments parameters are the holes that we define in our methods arguments
2915.54 -> are the actual values that we pass to these methods so in this case target and
2920.67 -> replacement or parameters but exclamation mark and asterisk are
2925.079 -> arguments now let's run this program and see what happens so our explanation
2933.45 -> marks are replaced with stars now what is important here is that this method
2938.28 -> does not modify our original string it returns a new string so if we print our
2945.75 -> original string right after SRU T tab message you can see the original string
2955.23 -> is not changed because in Java strings are immutable
2959.13 -> we cannot mutate them we cannot change them so any methods that modify a string
2963.839 -> will always return a new string object okay we also have another useful method
2970.41 -> to lowercase let's take a look
2975.48 -> so to lowercase converts all characters to lowercase and once again you can see
2981.03 -> that the original string is not affected because this method returns a new string
2986.22 -> okay we also have two uppercase and another useful method is trim trim and
2994.46 -> with this we can get rid of extra white spaces that can be at the beginning or
2998.94 -> the end of a string sometimes our users type unnecessary spaces in form fields
3003.71 -> so using the trim method we can get rid of these white spaces let me show you so
3008.3 -> I'm gonna add a couple of spaces before and after our message now when we trim
3015.5 -> it these white spaces are gonna get removed take a look so here's the
3021.859 -> original string you can see two white spaces at the beginning and here's our
3026.48 -> string after trimming so these are some useful methods in the string class but
3031.64 -> this glass has more methods than we don't have time to cover in this lecture
3035.39 -> but as we go through the course you're gonna learn more about the string class
3038.45 -> and other useful classes in Java third times will include special characters in
3049.1 -> our strings like a tab or a new line or a backslash or double quotes so in this
3054.5 -> tutorial I'm gonna show you how to include these special characters in your
3057.71 -> strings so here we have the string hello Marsh let's say we want to surround
3061.94 -> Marsh with double quotes now here's the problem if we add a double quote here
3067.16 -> Java compiler thinks this is the termination of our string so it doesn't
3071.84 -> stand what we have after that's why we have a compilation error the fixes
3075.8 -> problem we need to prefix this double code with a backslash
3080 -> so using this backslash we have escaped the double quote now one more time let's
3086.359 -> add backslash double code here now let's run the program and see what we get so
3092.57 -> we get hello Marsh in double quote beautiful so double quote is one of
3097.67 -> those special characters that you need to be aware of another special character
3101.21 -> is backslash let's say we want to store the pass to a directory on a Windows
3105.56 -> machine so that will look like this C Drive
3109.19 -> backslash windows backslash whatever now if you want to store this in a string we
3115.76 -> need to escape each backslash let me show you so C Drive backslash now we
3122.75 -> have a problem Java compiler thinks we're escaping the
3125.9 -> double code here so it thinks our string is not terminated with another double
3130.22 -> code but that's not what we want you want to add a backslash here so we need
3134.839 -> to prefix our backslash with another backslash now we type windows one more
3140.96 -> time something let's run the program so even though we have two backslashes in
3148.73 -> our code we actually see one backslash in a terminal window in other escape
3153.92 -> sequences backslash N and we use that to add a new line to our strings so let's
3159.68 -> change this to backslash N and run the program to see what happens now our
3165.41 -> string is broken down onto multiple lines by the first line we have C Drive
3170.03 -> then we have Windows so wherever we had a backslash n Java will insert a new
3175.49 -> line we can also add a tab in our strings
3178.28 -> so if you add backslash T here there will be a tab which means C Drive and
3183.71 -> windows let's take a look so C Drive here we have a tab and then windows now
3191.21 -> in Java we have a few more escape sequences but quite honestly they're
3194.78 -> hardly used so remember these four escape sequences that we cover in this
3199.43 -> tutorial
3205.14 -> in this tutorial we're going to talk about arrays in Java we use arrays to
3209.799 -> store a list of items like a list of numbers or a list of people or a list of
3214.66 -> messages let me show you so here we have an integer variable you want to convert
3219.13 -> this to an integer array so right after int we add square brackets now we have a
3226.029 -> compilation error because we're storing a single number in this array so to fix
3230.95 -> this we need to remove one because arrays are reference types we need to
3235.539 -> use the new operator here then we repeat the type one more time enter a and here
3241.119 -> in square brackets we specify the size or the length of this array how many
3245.619 -> items do we want to include in this array let's say five also we should
3249.97 -> change the name of this variable from number two numbers because we're dealing
3254.26 -> with a list of items so always pay attention to the name of your variables
3258.539 -> now you can access individual items in this array using an index so we type
3264.069 -> numbers square brackets to reference the first element or first item we use zero
3270.309 -> now we can set this to a value like 1 similarly we can set the second item to
3277.809 -> 2 now what if we use an invalid index let's say 10 this array doesn't have 10
3283.119 -> items so let's see what happens numbers of 10 we said this to 3 when we run this
3290.26 -> program we get an exception exceptions are Javas way to report errors so in
3297.339 -> this case an exception was raised and our program crashed we'll talk about
3301.27 -> exceptions in detail later in the course so now let's remove the last line and
3306.779 -> treinta sorry so sou t tab numbers let's see what we
3312.4 -> get we get this weird string in sort of the
3316.73 -> items in our array here's the reason by default when we print an array Java
3321.56 -> returns the string which is calculated based on the address of this object in
3325.97 -> memory so if you have another array and we print that we're gonna see something
3329.87 -> different because each object is gonna be in a different memory space okay now
3334.94 -> how can we see the actual items in this array well we have a class in Java
3339.62 -> called arrays let me show you arrays so this class is defined in Java
3344.66 -> that util package let's press ENTER now this is important on the top beautiful
3350.78 -> so we can use the dot operator to access the members of this class here we have a
3355.94 -> method called two string now as you see it this method is implemented multiple
3361.43 -> times so in the first implementation this method gets a float array in the
3366.65 -> second implementation it takes an integer array and so on so for all
3370.52 -> primitive types as well as reference types this method is implemented
3373.85 -> multiple times this is what we call method overloading now we can call this
3379.49 -> method and pass our integer array and this will return the string
3386.15 -> representation of this array so we can cut this from here and pass it to our
3390.71 -> print method like this now let's run the program one more time and here's our
3397.43 -> array beautiful so the first two items are initialized the others are set to 0
3401.96 -> by default because here we're dealing with an integer array if you had a
3405.53 -> boolean array all items why default get initialized to false if you have a
3410.27 -> string array all items get initialized to an empty string
3413.78 -> okay now this syntax for creating and initializing an array is a little bit
3420.29 -> tedious and it's an older syntax there is a newer way to initialize an array if
3425.24 -> we know the items ahead of time like in this case so I'm going to remove these
3429.41 -> two lines I'm also gonna remove the new operator here we use curly braces and
3435.76 -> inside these braces we add all the items in this array let's say 2 3 5 1 & 4 now
3442.79 -> we have 5 items so the length of this array is gonna be 5 we can read that
3447.5 -> using the lengths so if we type numbers dot look here we
3451.67 -> have is filled look at the icon it's an F so this is a field which is like a
3456.049 -> variable in a class and the type of this field is an integer so this returns the
3461.089 -> number of items in this array let's get that and printed using our print method
3467.229 -> like this take a look so we get five now in Java arrays have a fixed size so once
3476.93 -> we create them we cannot add or remove additional items to them they have a
3480.799 -> fixed length if you want to be able to add or remove additional items from an
3484.849 -> array you should use one of the collection classes that we'll talk about
3487.849 -> later in the course for now all I want you to remember is that arrays have a
3492.349 -> fixed length now currently our array is not sorted these numbers are in some
3497.359 -> kind of random order we can easily sort this array using the sort method of the
3502.13 -> arrays class let me show you so I'm gonna remove this line and call arrays
3507.799 -> dot sort once again you can see this method is overloaded because it's
3513.319 -> implemented with different parameter types so we call this method and pass
3517.999 -> our numbers array now when we run this program we can see
3523.47 -> our array is sorted beautiful
3530.71 -> so yeah I've learned that we use arrays to store a list of objects in Java we
3535.73 -> can also create multi-dimensional arrays for example we can create a
3539.69 -> two-dimensional array to store a matrix or we can create a three-dimensional
3544.099 -> array to store data for cube these are useful in scientific computations let me
3549.559 -> show you so here we have a single dimensional array to convert this to a
3553.52 -> two-dimensional array we need to add another pair of square brackets now we
3558.68 -> have a compilation error because we need to repeat these brackets on the other
3561.95 -> side so let's say we want to create a 2 by 3 matrix so 2 rows and 3 columns we
3569.18 -> add in other brackets here and change these lengths to 2 and 3 so now we have
3575.569 -> 2 rows and 3 columns now to access individual items in this array we need
3581 -> to supply two indexes first the index of the row so we can go to the first row
3586.369 -> and then the first column and initialize that to 1 now let us print this so and
3593.779 -> so you t-tap once again we use our arrays class dot to string and pass this
3600.92 -> object take a look once again we get this weird
3605.869 -> string because here we're dealing with a multi-dimensional array to solve this
3609.92 -> problem we need to use another method in this class called deep to string use
3616.67 -> this for printing multi-dimensional arrays take a look now we have this
3622.069 -> matrix which has two rows and in each row we have three columns we can also
3628.49 -> create a three dimensional array all we have to do is to add another pair of
3633.02 -> brackets and specify the length of that dimension pretty easy now what about the
3639.38 -> curly brace syntax let me show you so let's revert this back to a two
3644.869 -> dimensional array we're gonna get rid of the new operator and use curly braces
3650.26 -> now let's say in this matrix we're gonna have two rows and three columns so each
3655.67 -> row is an array itself because it's a list of items right
3659.119 -> so we add another array here let's say 1 2 3 then comma now we add the second row
3667.579 -> which is another array in this row we're gonna have 3 numbers 4 5 & 6
3673.64 -> now let's remove this line we don't need it anymore and print this array so
3680.15 -> here's the end result you have learned a lot about variables ya learned that when
3690.259 -> declaring them we need to initialize them and we can always change their
3693.529 -> value later on throughout the lifetime of our programs however there are times
3698.239 -> that we don't want the value of a variable to change for example let's
3701.749 -> declare a variable called pi and set it to 3.14 now here we need to add an F to
3709.039 -> represent this as a float because by default Java compiler sees this number
3713.509 -> as a decimal okay now you know that we use pi to calculate the area of a circle
3718.839 -> what if before we calculate the area of a circle I come here and type pi equals
3724.13 -> 1 then all our calculations are gonna get messed up we don't want this to
3728.15 -> happen that's when we use constants so if we type final here Java compiler will
3735.14 -> treat this as a constant so once we initialize this we cannot change its
3739.969 -> value later on you can see here we have a compilation error and it says cannot
3745.069 -> assign a value to final variable pi so pi is a final variable or a constant now
3751.819 -> by convention we use all capital letters to name constants so this should be PI
3758.2 -> beautiful now I tell you a little side story in one of my early courses that I
3762.769 -> created years ago that was c-sharp basics for beginners there I used the
3767.269 -> same example to teach the concept of constants but I pronounce this word as P
3771.619 -> instead of Pi and believe it or not to this day people make fun of me for
3776.18 -> saying P instead of Pi but that's how we learned this back in Iran we pronounce
3782.239 -> it as P and I think Greek people also say P but anyway I just thought to share
3786.589 -> this video to change the mood now you're done with constants next we're gonna
3791.15 -> talk about arithmetic expressions
3798.829 -> in this tutorial we're going to talk about arithmetic expressions in Java so
3803.67 -> in Java we have the same arithmetic operators that we have in math we have
3807.479 -> addition subtraction multiplication division and modulus which is the
3813.359 -> remainder of a division let's look at a few examples so I'm gonna declare an
3817.799 -> integer called result and here we can type 10 plus 3 now when we print result
3824.43 -> it's gonna be 13 pretty straightforward there you go so this is addition we also
3829.589 -> have subtraction multiplication division is an interesting one let's take a look
3836.94 -> so here the result is a whole number because in Java the division of two
3841.44 -> whole numbers is a whole number if you want to get a floating-point number here
3845.34 -> you need to convert these numbers to a float or a double let me show you
3850.05 -> so we prefix this number with parentheses and in parentheses we type
3856.23 -> double now we are casting or converting this number to a double similarly we
3862.83 -> should do that here and now we have a compilation error because on the left
3867.57 -> side we declared an integer but here the result of this expression is a double
3873.18 -> and by the way an expression is a piece of code that produces a value so what we
3878.07 -> have here is an expression because it produces a value so to fix this problem
3882.78 -> we need to change this to double now when we run this program we get this
3887.13 -> floating point number beautiful so these are the arithmetic operators and these
3892.56 -> numbers that we have here are called operands we also have increment and
3896.79 -> decrement operators let me show you so I'm gonna declare a new variable int X
3903 -> we set it to 1 now if you want to increase the value of x by 1 we use the
3907.71 -> increment operator now let's print this on a terminal so we get 2 there you go
3914.52 -> we can apply this operator as a postfix or as a prefix and we get the same
3920.67 -> result take a look too however if we use this
3924.66 -> on the right side of an assignment operator we get different results let me
3928.77 -> show you so I'm gonna declare another variable Y we set it to X plus plus in
3935.1 -> this case because we have applied the increment operator as a postfix first
3939.93 -> the value of x will get copied to Y so Y would be 1 and then X will be
3945.57 -> incremented by 1 so if you print x and y x is gonna be 2 and Y is gonna be 1 take
3954.21 -> a look so X is 2 and Y is 1 beautiful however if you apply this as a prefix
3963.08 -> first X will be incremented by 1 so it will be 2 and then it will be copied to
3968.94 -> Y so in this case both X&Y will be to take a look so we get two
3976.05 -> and two now what if you want to increment X by more than one let's say
3981.24 -> by two well there are two ways to do this let's remove Y we don't really need
3986.07 -> it anymore we can write x equals x plus 2 so first we add 2 to X the result will
3994.47 -> be three and then three will be copied into X the other way is to use the
3998.79 -> Augmented or compound assignment operator so we can write X plus equals
4005.87 -> two what we have on line eight is exactly identical to what we have on
4010.19 -> line seven well as you can see it's shorter so this is a better way to write
4014.6 -> the same code now this is one of the Augmented assignment operators we have
4019.43 -> the Augmented assignment operator for other arithmetic operators so we can
4024.08 -> type X minus equals 2 and this would reduce the value of x by 2 we also have
4029.72 -> multiply and divide so these are the Augmented or compound assignment
4035.99 -> operators right now I've got a question for you here we have declared this
4045.47 -> variable X it goes to 10 plus 3 times 2 what do you think is the result of this
4050.84 -> expression the result is 16 let's run this program and find out so run there
4057.92 -> you go we got 16 but why well this is a very basic math concept that
4062.87 -> unfortunately a lot of people don't know in math the multiplication and division
4068.18 -> operators have a higher priority so they get applied first in this example this
4073.94 -> expression 3 times 2 is evaluated first the result is 6 and then 6 is added to
4080.18 -> 10 that's why we get 16 now if you want to change the order of these operators
4084.56 -> you can always use parentheses for example if you want this expression to
4088.73 -> be evaluated first we wrap it in parentheses so like this now Java
4095.81 -> compiler will first evaluate this expression the result will be 13 and
4099.83 -> then 13 is multiplied by 2 so we get 26 take a look
4104.949 -> there you go so be aware of the order of these operations parentheses always have
4111.289 -> the highest priority then we have multiplication and division and finally
4116.149 -> we have addition and subtraction in this tutorial we're going to talk about
4125.959 -> casting and type conversion so I'm gonna declare a short variable call X and set
4132.559 -> it to 1 and then I'm gonna declare an integer called Y and set it to X plus 2
4138.559 -> in this example we're adding a short to an integer what do you think the result
4144.289 -> is gonna be well let's take a look so sou t let's print Y we get 3 that is
4152.63 -> what you were expecting but let me explain what happens under the hood for
4157.069 -> this expression to get executed because we're dealing with two different types
4161.179 -> of values one is a short the other is an integer one of these values should be
4165.829 -> converted to the other type so they are equal now I got a question for you how
4169.849 -> many bytes do we have in a short variable we have 2 bytes how many bytes
4174.169 -> do we have in an integer 4 bytes so any values that we store in a short variable
4179.149 -> can also be stored in an integer variable right so when this piece of
4184.009 -> code is gonna get executed this is what's gonna happen first Java looks at
4188.479 -> the value in this variable it's 1 right it's going to allocate another variable
4194.57 -> an anonymous variable somewhere in memory we don't know where that is we
4198.289 -> don't know the name of that variable it doesn't have a name it's anonymous that
4202.309 -> variable is gonna be an integer then Java is gonna copy the value of x into
4207.229 -> that memory space and then it will add these two numbers together this is what
4211.369 -> we call implicit casting let me type it here implicit casting that means
4217.669 -> automatic casting or automatic conversion we don't have to worry about
4221.149 -> it whenever we have a value and that value can be converted to a data type
4224.989 -> that is bigger casting or conversion happens implicitly or automatically so
4230.929 -> byte can be automatically converted to short and this can be converted to int
4235.399 -> and long okay now what about floating-point numbers
4239.73 -> let's look at an example I'm gonna change this to a double one point one
4245.25 -> now here we have a compilation error because on the right side of the
4248.64 -> assignment operator we have a floating-point number a double on the
4252.78 -> left side we have an integer so we need to change this to double now when we
4257.73 -> execute this code we're gonna get 3.1 let's verify this there you go now let's
4264.57 -> see how casting happens here in this case we're dealing with a double and an
4270.66 -> integer an integer is less precise than a double because in a double we can have
4276.75 -> digits after the decimal point so in this example Java is going to
4282.09 -> automatically cast this integer to a double so that will be two point zero
4287.43 -> and then two point zero will be added to one point one okay so back to this chain
4293.61 -> here we're gonna have float and then double so as a general rule of thumb
4300.06 -> implicit casting happens whenever you're not gonna lose data there is no chance
4305.19 -> for data loss now what if you want Y to be an integer so in this example we
4311.22 -> don't care about the digits after the decimal point you want to see three on
4314.85 -> the terminal how should we do this this is where we should explicitly cast the
4319.56 -> result so we should cast X to an integer like this parentheses int this is
4327.18 -> explicit casting we convert X to an integer so the result would be one
4331.89 -> without a decimal point one will be added to 2 and Y would be three take a
4337.23 -> look there you go so this is all about implicit and explicit casting now this
4345.18 -> explicit casting can only happen between compatible types so all these types are
4350.28 -> compatible because they're all numbers but we cannot cast a string to a number
4354.44 -> in other words if X was a string like this let's say 1
4363.09 -> we cannot cast eggs to an integer because they are not compatible so how
4367.74 -> do we do this well for all these primitive types you
4370.8 -> have learned you have wrapper classes so in Java we have a class which is a
4375.63 -> reference type called integer this class is defined in Java the Lang package and
4382.34 -> in this class we have a method called parse int so this method takes a string
4388.23 -> and returns an integer so integer is the wrapper class for the int primitive type
4394.65 -> we also have short and in this class we have parse short so it takes a string
4400.89 -> and returns a short similarly we have float and double and obviously the name
4407.19 -> of these metas are different so here we have parse float so back to this example
4413.96 -> let's say we get X as a string and we want to convert it to an integer this is
4418.74 -> how we do it integer dot parse int we pass X here and
4424.08 -> then add it to take a look so we get 3 you might be curious why this matters
4431.46 -> why should we parse or convert a string to a number to add it to something else
4435.66 -> well pretty much in most frameworks for building user interfaces whether you're
4440.82 -> building a desktop or a mobile application or web application we always
4444.93 -> receive input from the user as a string so if you have a form with a bunch of
4449.04 -> text boxes or drop-down lists almost always we get values as a strings so
4454.77 -> that's why we need to convert these strings to their numeric representation
4458.85 -> ok now what if X is a floating-point number here what will happen when we try
4465.93 -> to parse this as an integer let's take a look once again we get an
4472.2 -> exception which is how Java reports errors to our programs we're going to
4477.3 -> talk about exceptions in detail in the future
4479.52 -> so if the user enters one point one we cannot use this method instead we should
4485.52 -> use float or double let's say double because that's easier double parse
4490.82 -> double so we parse this number as a double add two to it and then store the
4496.56 -> result in a double and then we will get 3.1
4500.88 -> beautiful next we're gonna look at the math class for performing mathematical
4504.84 -> operations in this tutorial we're going to look at the math class for performing
4514.739 -> mathematical operations so in Java we have this math class that is defined in
4519.42 -> Java that Lang package so it's always there we don't need to explicitly import
4523.739 -> it now this class has a number of useful methods the first method I'm gonna show
4527.79 -> you is the round method and with this we can round a floating-point number to a
4532.38 -> whole number so as you can see this method is overloaded which means it's
4536.25 -> implemented twice in the first implementation it takes a float and
4539.85 -> returns an int and a second implementation it takes a double and
4544.17 -> returns a log so let's pass 1.1 as a float to this method and store the
4551.19 -> result in an integer like this now we print the result and we get one
4560.07 -> beautiful another useful method is seal or sealing which returns the smallest
4566.61 -> integer that is greater than or equal to this number so the ceiling of 1.1 is 2
4572.61 -> now here we have a compilation error because this method returns a double but
4577.62 -> we're storing the result in an integer so here we need to explicitly cast this
4582.84 -> to an integer and now you can see the ceiling of this number is 2 we have
4588.84 -> another useful method that is floor so the floor of a number is the largest
4594.48 -> integer that is smaller or equal to this number in this case it's gonna be 1
4598.95 -> let's take a look there you go another useful method is max which returns the
4606.93 -> greater of two values and once again this method is overloaded so in the
4611.22 -> first implementation we get two integers we have other implementations for longs
4615.09 -> floats and doubles so let's pass two integers here one and two this will
4621.81 -> return the greater number which is 2 there you go similar to this we have min
4628.53 -> is pretty straightforward in the useful method is random for
4633.46 -> generating a random value between 0 and 1 once again we get a compilation error
4638.38 -> because this method returns a double so let's change that to double now every
4644.53 -> time we run this program we get a different number and this number is a
4649.87 -> floating-point number between 0 to 1 now what if you want a number between 0 to
4654.67 -> let's say 100 instead of 0 to 1 well we simply multiply this by 100 take a look
4662.7 -> so every time we run this we get a different number between 0 to 100 now if
4668.62 -> we don't want these digits after the decimal point we can either round this
4672.22 -> number or cast it to an integer let me show you so we can call math that round
4678.34 -> and pass the result of this expression so I'm gonna cut this add parenthesis to
4684.43 -> call the round method and then paste that expression now let's run this code
4690.27 -> so every time we get a double we still have the fraction here so we can change
4697.45 -> the type to an INT now we have a compilation error because the round
4701.8 -> method returns a long but here we have declared an integer this is one of those
4705.82 -> cases where implicit casting cannot happen because we have a value that is
4710.5 -> represented in 8 bytes of memory and you want to store that in a variable that
4714.34 -> has only 4 bytes of memory so implicit casting doesn't work but we can use
4719.05 -> explicit casting because we know the result of this expression is a number
4723.1 -> between 0 to 100 so we can definitely store it in an integer so let's add int
4728.26 -> here now let's run this again there you go now what if we don't use the round
4736.39 -> method here let's see what happens so I'm gonna remove the call to the round
4741.22 -> method and simply I apply this casting over here let's see what we get now
4748.33 -> every time we run this program we get 0 do you know why because here we're
4752.68 -> applying this casting to the result of this method call not this entire
4758.14 -> expression as you saw earlier every time we call the random method it generates a
4763.39 -> number between 0 to one so when we cast that number to an
4767.679 -> integer we'll lose the fraction we always get zero and zero times 100 is
4772.78 -> zero to solve this problem we need to wrap this entire expression in
4777.52 -> parenthesis so we add parentheses around this and now let me run this we get an
4785.469 -> integer between 0 and 100 quite often when we worked with numbers we need to
4796.42 -> format them as currency values for example we might have a number like this
4799.9 -> imagine this is the price of a product to display this to the user you want to
4804.219 -> add a dollar sign here and separate every three digits with a comma or if
4809.77 -> you might have a number like 0.1 and we want to represent this as a percentage
4814.27 -> value so instead of 0.1 you want to display 10% so in this tutorial I'm
4819.73 -> gonna show you how to format numbers in Java we have this class number format
4825.25 -> that is defined in Java the text package in this package we have a lot of classes
4830.14 -> for handling text dates numbers and so on so let's press ENTER here it's
4835.51 -> important on the top beautiful now I want to declare a variable so let's give
4839.949 -> this variable a name currency now we need to instantiate this new number
4845.46 -> format however we get a compilation error here let's take a look number
4850.75 -> format is abstract it cannot be instantiated so in Java we have this
4855.25 -> concept of abstract classes so some classes are abstract and they're
4859.239 -> basically like a half-baked cake we cannot use the new operator to create an
4863.11 -> instance of them we're going to talk about abstract classes and why they
4866.59 -> exist later in the course for now remember that we cannot create an
4869.949 -> instance of the number format class because it's abstract so there is
4874.9 -> another way let me show you this class has a bunch of methods that start with
4880.39 -> get so we have get currency instance and when we call this method this will
4885.61 -> create an instance of the number format class and return it so instead of using
4890.38 -> the new operator we're gonna use this method here this is what we call a
4894.37 -> factory method because it's like a factory it creates new are
4898.27 -> jects now look at the return type of this method it's a number format object
4902.44 -> okay so we call this method now we get the return value and store it in a
4908.35 -> variable of type number format called currency okay let me say improperly zoom
4915.43 -> out so you can see all the code there you go so on the right side of the
4919.24 -> assignment operator we're calling the get currency instance method we get the
4923.47 -> result which is a number from an object and store it in this variable okay now
4928.81 -> I'm gonna zoom in so you can see clear a name
4932.76 -> alright that's better now this object has a method for formatting values once
4941.05 -> again you can see this method is overloaded we have multiple
4943.93 -> implementations we can give it a long or a double or whatever so I'm gonna call
4948.73 -> this method and pass a value like one two three four five six seven point
4954.4 -> eight nine one so we have three digits after decimal and a few other digits
4959.89 -> here now this method will return a string representation of this number
4963.96 -> formatted as a currency so let's get that and store it in a string variable
4969.61 -> like result and then we're gonna print result on a terminal see what we get
4978.24 -> so we get this dollar sign every three digits are separated using a comma and
4983.74 -> we only have two digits after the decimal point so this class is very
4988.42 -> handy in formatting numbers as currencies we have another method for
4992.29 -> formatting a number as a percent and that is get percent instance right now
4999.55 -> it returns an instance of the number format class specialized for formatting
5004.53 -> numbers as a percent so we need to rename this variable from currency to
5008.67 -> percent now we don't want to manually change this because there are multiple
5011.82 -> references to this variable this other reference was not updated so let me show
5016.08 -> you the proper way to rename objects in intellij if you right click here you can
5021.06 -> see this refactor menu and here we have rename now look at this shortcut on the
5026.13 -> right side unfortunately it's not visible in my recording window but on
5029.79 -> Mac is Shift + F I always use shortcuts because they're
5034.14 -> faster so let's press shift and f6 now we get this red box and below this we
5041.22 -> can see a few suggestions for a better name we can choose one of these or pick
5045.66 -> our own name I'm gonna change this to percent and note that as I'm typing this
5052.02 -> the other reference gets updated automatically so this is very helpful
5055.38 -> now let's press Enter okay we're done with renaming now let's
5061.02 -> pass a different value here so let's say we have a number like 0.1 we want to
5067.35 -> format this as a percent let's run the program there you go you get 10%
5072.21 -> beautiful now let me show you a cool technique in this program we don't
5077.28 -> really need this percent object because we have used it in a single place it
5081.78 -> would make sense to have this as a separate variable if you have multiple
5085.02 -> references to it so what can we do here we can completely get rid of this object
5089.37 -> so let's delete this piece of code we're basically calling this method of the
5094.47 -> number format class as you know this method returns a number format object so
5099.84 -> right after calling this method and before the semicolon we can use the dot
5103.86 -> operator to access the methods or members of the number format object so
5108.9 -> here we call the format method straight away and pass our value this is what we
5114.24 -> call method chaining we're chaining multiple methods together so here's one
5118.8 -> method and here's another method now this returns a string so we can store it
5124.17 -> in this result variable let me cut this expression from here and put it over
5129.99 -> here now we have double semicolons I'm going to delete one of them
5135.72 -> all right beautiful in this tutorial I'm gonna show you how to read input from
5146.22 -> the user in Java we have this scanner class that is defined in Java did util
5151.53 -> package let's import this and create a scanner object so new scanner now here
5159.57 -> inside this parenthesis we need to specify where we're gonna read data from
5163.14 -> we're gonna read it from the terminal window are we gonna read it from a file
5166.65 -> or what to work with the terminal window we type system dot in this is one of the
5172.89 -> fields in the system class a fill as I told you before is like a variable that
5177.39 -> we define in a class so we have system that in we also have system that out
5182.19 -> which we used to print something on the terminal right now let's use system that
5187.47 -> in and terminate this statement with a semicolon now this object has a bunch of
5192.84 -> methods for reading data and all these methods start with next so we have next
5198.45 -> byte for reading a byte we have next line for reading a line we have next
5203.22 -> boolean for ending a boolean and so on so let's call the next byte method and
5207.57 -> see what we get this returns a byte value so we can
5211.05 -> store it in a byte variable let's say someone's age and then we print it on a
5217.05 -> terminal saying you are plus h so here we're concatenating a string with a byte
5225.42 -> and in this scenario we're gonna have implicit casting or implicit type
5229.59 -> conversion so Java will automatically convert this byte variable here tray
5234.87 -> string so they can be added together okay now let's run this program and see
5239.55 -> what happens so here in the terminal window we can type 20 enter and it says
5245.58 -> your 20 beautiful but this is pretty boring let's add a label here and ask
5249.63 -> the user to type something for example before reading data we're gonna call the
5255.39 -> print line method and say age : now let's run the program so we get this
5261.63 -> label here however whatever we type will appear on the next line this is because
5267.36 -> the print line method adds a new line after this label the Soviets issue
5272.869 -> recall the print method now let's run the program one more time we get this
5278.519 -> popup box because our program hasn't finished execution so we need to tell
5282.479 -> IntelliJ that you want to stop and rerun this program all right now whatever we
5289.469 -> type appears right in front of this label beautiful ENTER we are 20 now what
5295.829 -> if we type a floating-point number like 20 point 1 we get an exception because
5302.579 -> this method can only parse white values if you want to get a floating-point
5307.229 -> number we need to call next float or next double what if you want to read our
5313.619 -> string we don't have next string we have next and next line let's look at the
5319.53 -> differences so first I'm going to call the next method here we have a
5323.039 -> compilation issue because this method returns a string I'm gonna change this
5327.359 -> to a string let me collapse this that's better
5330.629 -> we should also rename this variable so what was the shortcut Shift + f6 now
5336.659 -> let's train just a name enter beautiful and one last time we should also update
5342.899 -> the label name run the program so I'm gonna type my name here Marsh it says
5349.619 -> your Amash pretty under for one more time this time I'm gonna type my
5353.309 -> full-name maha Madani but we don't get the last name here's the reason these
5359.55 -> wars that we have here these are called tokens every time we call the next
5364.289 -> method it reads one token so here we have a space we have two tokens and we
5369.389 -> need to call the next method two times to get the full name the first time we
5373.499 -> call it it returns Marsh the second time we call it it returns Hamid ani then we
5377.55 -> need to combine this suit together this is not ideal so that's when we use the
5381.989 -> next line method with this method we get the in turn line that the user enters no
5386.609 -> matter how many spaces or tabs are there take a look so Marsh
5392.399 -> comma Donny and we get the full name now what if I type a few spaces before my
5399.149 -> name let's see what happens so those spaces also appear here and
5404.09 -> this looks a little bit odd this is where we use the trim method remember we
5408.2 -> trim we can get rid of all these white spaces before or after a string so this
5414.59 -> next line method returns a string that we are storing in this variable right
5418.34 -> now just before storing the result in this variable here we can use the dot
5423.38 -> operator to access the members of this string object so we call the trim method
5429.17 -> and then store the result in this variable once again we're chaining
5433.82 -> multiple methods here let's run the program so a few spaces Maharani and you
5442.19 -> get this beautiful output
5448.76 -> right now it's time for a project I want you to use what you have learned in a
5452.91 -> section and build a mortgage calculator like this so when you run this program
5457.469 -> we get a few questions the first question is the principal or the amount
5462.03 -> of loan we want to get let's say $100,000 the second question is the
5467.61 -> annual interest rate let's say three point nine two percent and the third
5473.52 -> question is period in years let's say you want to get a loan for thirty years
5477.45 -> so this program calculates our monthly payments and displays it as a currency
5482.46 -> this is a great exercise for you to practice all the materials you learn in
5486.51 -> this section now before you get started I want to give you a few hints here is a
5490.77 -> formula for calculating the mortgage or the monthly payments I found this page
5495.36 -> on wikiHow comm is called calculate mortgage payments so let's see how this
5500.43 -> works mortgage equals P or principal or the amount we're gonna loan multiplied
5505.86 -> by R which is our monthly interest rate this is very important so this number
5511.35 -> that we get here is our annual interest rate we need to divide this by 12 also
5516.36 -> take into account that this number is represented as a percent to calculate
5520.41 -> the actual interest rate you need to divide this number by 100 so in this
5524.88 -> example the interest rate is zero point zero three nine two so whatever the user
5530.7 -> enters divided by 100 and then divided by twelve to get to the monthly interest
5535.38 -> rate now we have this monthly interest rate we need to multiply this by this
5539.85 -> expression here we need to add 1 to this interest rate and raise it to the power
5544.89 -> of n where n is the number of payments so we need to multiply this number by 12
5551.04 -> or 12 months to calculate the number of payments now to raise this number to the
5556.35 -> power of n you need to use the power method of the math class so this math
5561.69 -> class has this power or power method that takes two arguments or two values a
5566.28 -> and B so go ahead and spend ten to fifteen minutes on this exercise when
5571.53 -> you're done come back see my solution
5578.95 -> all right let's see what I've done here and by the way don't worry if your coat
5582.44 -> is different from mine we all think and coat differently so
5585.68 -> it's perfectly fine if your code is different
5587.54 -> just look at my coat see what I've done here and see if there are ways you can
5590.84 -> improve your coat that's what matters okay so here in our
5595.43 -> main method first I've declared two final variables or constants
5599.719 -> the first one is months in year which I've set to 12 and the second one is
5603.86 -> percent which I've set to 100 the reason I declared this constant is that I
5608.03 -> didn't want to have magic numbers in this code so over here where we
5612.95 -> calculate the monthly interest we get the annual interest divided by percent
5617.54 -> and then months in here this code is very self-explanatory someone else
5622.34 -> reading this code we'll have no problem understanding what's going on here in
5625.58 -> contrast if you had a magic number here like divided by 12 and then for whatever
5630.91 -> someone else reading this code would wonder what is this for doing here what
5634.7 -> is 12 it's quite obvious to you that 12 is the number of months in a year but
5638.93 -> trust me sometimes other people cannot see this straight away so as a best
5643.31 -> practice avoid magic numbers in your code always use constants or final
5648.71 -> variables to describe them so let's revert this back all right so
5654.86 -> after declaring this constant I've created this scanner object here we ask
5659.27 -> the first question principle and we read the answer as an
5663.5 -> integer I thought integer is a good datatype for
5665.87 -> storing the principle because short is not enough with short we can store a
5670.85 -> maximum of $32,000 that's not enough what if someone wants to find us
5676.04 -> $1,000,000 so int is good and it allows us to store a value up to two billion
5680.739 -> next we ask the second question annual interest rate we read this as a float
5685.969 -> here I could use double but the interest rate is a small number so float is
5690.41 -> sufficient for that we don't really need double so we get the annual interest and
5694.37 -> then based on that we calculate the monthly interest also see how I have
5699.44 -> named my variables all variables have a proper meaningful names there are no
5704.33 -> magic words here like MI as it short for monthly interest
5708.35 -> or m1 or m2 do not use magic names for your variables always use meaningful and
5714.35 -> descriptive names alright next we get the period we read this as a byte
5721.46 -> because the maximum number we want to support is 30 so one byte is sufficient
5725.57 -> to store the number 30 or anything smaller now based on the number of years
5729.89 -> we calculate the number of payments note the camel notation here I've capitalized
5734.96 -> the first letter of every word except the first word so we get the Earth's and
5739.73 -> x months in here again the code is very self-explanatory
5744.31 -> once we collect all this data then we calculate the mortgage so we get a
5749.69 -> principal multiplied by this expression and then divide it by this other
5754.79 -> expression let's have a quick look here now this looks a little bit complicated
5758.57 -> so if you're not download my source code look below this video I'm attached it
5762.62 -> for you to download and finally after we calculate the mortgage we use the number
5767.54 -> format class to format this value as a currency so we get a result store it in
5774.14 -> this string mortgage format it and print it over here now here we can also avoid
5779.54 -> declaring this variable and simply add this expression over here but it decided
5784.67 -> to do this to increase the readability of this code otherwise this line would
5788.48 -> be so long but that's just my personal preference you don't have to follow this
5792.1 -> alright so this was my implementation of the mortgage calculator
5795.79 -> however this program has a number of problems the first problem is input
5801.08 -> validation so if we run this and enter a non numeric value like XYZ our program
5809.96 -> crashes or as another example if we enter a negative value here our program
5815.15 -> is not gonna behave properly so that's where conditional statements come to the
5818.93 -> rescue in the next section we're going to talk about conditional statements you
5822.62 -> will learn how to use these statements to validate the values entered by the
5826.46 -> user so that brings us to the end of this section I hope you learn a lot and
5830.6 -> thank you for watching
5837.09 -> so in this section you'll learn all about variables and constants you learn
5841.21 -> about primitive and reference types you learn that primitive types store simple
5844.989 -> values but reference types hold references to complex objects that's why
5849.159 -> we call them reference types you also learn about casting and type conversion
5853.54 -> you learn about two types of casting implicit and explicit you learn how to
5857.71 -> work with numbers strings and arrays and finally you learned how to read input
5862.329 -> from the terminal I hope you learned a lot I've been enjoying the course so far
5865.96 -> in the next section we're going to look at conditional statements for
5868.84 -> controlling the flow of our programs so I'll see you in the next section
5881.51 -> hey Marcia I just want to make a quick announcement this course you've been
5885.56 -> watching is actually the beginning of my complete Java series in this course we
5890.45 -> only talk about the basics but in my complete series we go way beyond that so
5895.07 -> if you're serious about Java if you're looking for a job as a Java developer I
5898.73 -> highly encourage you to enroll in my complete Java series it's exactly the
5902.75 -> same structure the same quality but it has way more content plus you will get a
5908.09 -> certificate of completion that you can add to your resume so if you're
5911.72 -> interested I put the link down below you can get the course with a discount and
5914.96 -> if you're not happy for any reasons you can ask for a refund within the first 30
5919.58 -> days so I hope to see you in the course in this section we're going to look at
5925.04 -> control flow or controlling the flow of execution of our programs you're gonna
5930.2 -> start off by talking about comparison operators for comparing values then I'm
5934.46 -> gonna talk about logical operators like the logical and logical or and logical
5939.53 -> not we use these operators for implementing real world rules and then
5944.239 -> we're gonna talk about three different types of control flow statements we're
5947.93 -> going to talk about conditional statements for making decisions in our
5951.5 -> programs then we're going to talk about loops for executing code repeatedly and
5956.03 -> finally we're going to revisit our mortgage calculator and add error
5960.08 -> handling to it so if a user enters an invalid value we
5964.07 -> keep asking them to try again all right now let's jump in and get started we're
5974.93 -> gonna start this section by talking about comparison operators we use these
5978.83 -> operators to compare primitive values for example our x on y equal or not so
5985.1 -> I'm gonna declare two integers x and y now let's compare this to see if they're
5992.36 -> equal or not so first I'm gonna add this print line
5996.17 -> statement now to compare these variables we type x equals y so here we have two
6002.77 -> equal signs and this is the Equality operator don't confuse this with a
6006.76 -> single equal sign that we use for assignment that is the operator used
6011.02 -> over here okay so two equal signs repairs
6014.98 -> the Equality operator now when we run this program we get true because these
6020.62 -> values are equal this expression that we have here is called a boolean expression
6025.65 -> so earlier I told you that an expression is a piece of code that produces a value
6030.67 -> this piece of code produces a boolean value true or false that's why we refer
6035.23 -> to it as a boolean expression we also have the inequality operator so when we
6041.17 -> run this program we're gonna see false let's take a look we get false because
6046.12 -> these two variables are equal so the expression X is not equal to Y returns
6052.48 -> false we also have greater than greater than or equal to less than and less than
6060.28 -> or equal to so if I change Y to 2 this expression is gonna evaluate to true
6065.53 -> because X is less than or equal to Y take a look there you go
6071.32 -> next we're going to talk about logical operators in the last tutorial you learn
6081.61 -> that a boolean expression produces a boolean value now there are times we
6085.93 -> want to combine multiple boolean expressions let me show you so I'm going
6089.98 -> to declare an integer called temperature and set it to 22 next we declare a
6096.46 -> boolean variable is warm now we want to check to see if temperature is greater
6101.71 -> than 20 and less than 30 so we write a boolean expression like this temperature
6107.41 -> greater than 20 here we need to use the end operator so these two ampersands
6113.32 -> represent the logical and operator in Java
6116.35 -> after this we add our second condition temperature less than 30 now if both
6123.19 -> these conditions are true the result of this boolean expression is going to be
6126.88 -> true otherwise if at least one of these is
6129.31 -> false the result would be false let's take a look so I'm gonna print is warm
6134.44 -> on a terminal
6138.19 -> the result is true but if you change the temperature to let's say 12 the result
6145.96 -> is gonna be false and this is how this works Java will evaluate this expression
6150.4 -> from left to right first it looks at the first condition the first condition is
6154.96 -> false because temperature is not greater than 20 so because this expression is
6159.52 -> false it doesn't matter what we have after the and operator Java will ignore
6165.31 -> the other expressions because the and operator will return true if both
6169 -> conditions are true okay now let's look at another operator that is the or
6174.01 -> operator so I'm going to delete all this code and start with a new example let's
6179.35 -> declare a boolean has high income we set it to true and another boolean has good
6187.57 -> credit we set this to true as well so let's say we're building an application
6192.219 -> for processing loans we want to see if a new applicant is eligible for a loan or
6196.96 -> not so we declare another boolean is
6201.09 -> eligible and by the way look at the names I have used for these variables
6205.57 -> they're very meaningful and descriptive so here's the rule an applicant is
6210.94 -> eligible if they have high income or good credit if one of these conditions
6215.59 -> is true they are eligible so we write has high income or so these two vertical
6223.54 -> bars represent the or operator so if they have high income or good credit
6229.679 -> then they are eligible so with the or operator if at least one condition is
6235.51 -> true the result will be true in this example when Java evaluates this
6240.31 -> expression it starts from the left side this boolean variable is true so it
6245.11 -> doesn't matter what we have after Java will not evaluate the rest of this
6249.07 -> expression it simply returns true however if this variable was false Java
6255.67 -> would continue evaluating this expression hoping that the next boolean
6259.6 -> value or the next boolean expression is true so this is the or operator and
6264.1 -> finally we have the not operator that we use to reverse a value let me show you
6268.929 -> how that works so
6271.13 -> let's declare another boolean variable has criminal record we set this to false
6277.57 -> so here's the rule we want to implement in order for someone to be eligible for
6282.199 -> a loan they should either have high income or good credit and they should
6287.12 -> not have any criminal records so let's see how we can implement this rule we
6292.639 -> have implemented the first part they should either have high income or good
6296.3 -> credit now we want to make sure they don't have any criminal records so we
6301.37 -> need to combine the result of this expression with this boolean value using
6305.6 -> the and operator so first we wrap this expression in parentheses then we apply
6311.929 -> the end operator and here we add has criminal record now you want to make
6317.75 -> sure they don't have criminal record and this is where we use the not operator so
6322.639 -> the not operator will reverse the value of this boolean variable or expression
6327.139 -> in this case this variable is set to false so when we apply the not operator
6332.33 -> to it the result would be true so if the first condition is true and the second
6337.85 -> condition is true then that person is eligible for a new loan so as you can
6343.31 -> see these logical operators are very useful in implementing real word rules
6347.71 -> next we're going to talk about if statements in this tutorial we're going
6357.59 -> to look at if statements in Java if statements are extremely important
6361.82 -> because they allow us to build programs that can make decisions based on certain
6365.929 -> conditions here's an example in this file we have a bunch of conditions if
6371.36 -> temperature is greater than 30 perhaps you want to display two messages to the
6375.56 -> user it's a hot day drink plenty of water otherwise if it's between 20 and
6381.08 -> 30 you want to print it's a nice day and otherwise we want to print it's a cold
6386.12 -> day so let's see how we can implement these rules in a Java program back to
6391.58 -> our main file we start by declaring a variable temperature we set it to 32 now
6398.78 -> we use an if statement followed by a pair of parentheses inside this
6402.77 -> parenthesis we type an expression or a boolean value so
6406.94 -> let's say temp is greater than 30 now if this condition is true the statement
6413.69 -> that we had after this if statement will be executed so let's print it's a hot
6420.25 -> day let's run the program we get this message because temperature is greater
6426.889 -> than 30 now what if you want to print another message like drink plenty of
6431.239 -> water here we should add curly braces to define a code block so if this condition
6437.21 -> is true all the code that we have inside of this block would be executed let's
6442.369 -> add another message here drink water okay so this was our first condition now
6449.449 -> let's add a second condition so after the right brace we type LS if once again
6455.719 -> we add our parentheses and inside these parentheses we type a boolean expression
6459.849 -> if 10 is greater than 20 and it's less than or equal to 30
6467.329 -> you want to print a different message like beautiful day now here I haven't
6474.5 -> added the braces because we have a single statement so braces are only
6478.489 -> required if we have multiple statements now this is a little bit controversial
6483.02 -> some people believe we should always add braces whether we have a single
6486.38 -> statement or not other people like myself believe this creates unnecessary
6490.429 -> noise in the code in this tutorial I'm gonna add the braces first and then
6494.36 -> remove them so you can see the difference so let's add a pair of curly
6499.07 -> braces here and finally our last condition so if none of these two
6503.3 -> conditions are true let me print a different message so here we simply type
6507.71 -> LS we don't have any more conditions so let's add a code block and print cold
6515.63 -> day now let me define a few terms here we have an if statement and this
6521.84 -> statement has three clauses or three sections here's the first Clause here's
6527.809 -> the second Clause and here's the third Clause pay attention to how a formatted
6533.179 -> this if statement so first we have the if Clause the else if
6538.23 -> and else clauses are placed after these right braces so we have some kind of
6543.6 -> hierarchy here here we have a parent followed by two children now let's get
6548.88 -> rid of these unnecessary braces and reformat our code to see the difference
6552.75 -> so I'm gonna remove the braces for the else clause and also one more time here
6559.61 -> that's better now we can simplify this boolean expression basically we don't
6565.32 -> need this piece of code here here's the reason if the first condition is not
6570.63 -> true what does it mean that means the temperature is less than or equal to 30
6574.89 -> so this expression here is unnecessary let's delete this and simplify our code
6580.26 -> that's better now look at how this code is formatted on the top we have if the
6585.84 -> else if Clause is a little bit indented but the else Clause is not indented it's
6591.03 -> at the same level as the if Clause and this looks a little bit ugly the code is
6595.68 -> not symmetrical so if you want to get rid of the curly braces a better way to
6600.57 -> format this code is like this so instead of adding the else if or else classes
6607.44 -> after curly braces we add them on a new line now all these clauses are at the
6613.32 -> same level the code is easier to read in this tutorial I'm gonna show you a very
6623.7 -> cool technique for simplifying if statements so let's start by declaring a
6628.68 -> variable called income and set it to 120,000 now here we can use an
6635.79 -> underscore in between these three digits to make our code more readable now let's
6640.86 -> say we want to declare a boolean variable called has high income if the
6646.35 -> income is more than $100,000 you want to set this to true otherwise we want to
6650.82 -> set this to false so here we can write an if statement like this if income is
6657.48 -> greater than $100,000 we want to set has high income to true however we get a
6667.56 -> compilation error here let's take a look declaration not
6671.13 -> allowed here so we cannot declare a variable here we
6674.489 -> can only declare variables inside code blocks like this code block over here so
6680.4 -> to declare this variable we need to add curly braces to define a new code block
6685.559 -> now we have a different problem this variable that we have defined is scoped
6690.329 -> to this code block so it's only available here we cannot access it
6694.769 -> outside of this block let me show you so if we print has high income you can see
6702.959 -> we have a compilation error cannot resolve symbol has high income because
6707.519 -> this variable is not available outside of the block in which it's declared so
6712.169 -> to solve this problem we can declare this variable after setup this block
6716.749 -> boolean has high income and then we can simply set it to true in this block now
6724.409 -> we don't need these braces anymore so let's simplify the code we add an else
6729.57 -> clause otherwise we said has high income to false let's remove this print method
6735.749 -> we don't need it anymore so this is one way to implement this scenario but this
6740.55 -> code looks very amateurish a professional programmer doesn't write
6744.179 -> code like this let's improve it step by step one way to improve this is to give
6749.729 -> this boolean variable an initial value for example we can set it to false
6754.739 -> initially and then we implement this condition so if the income is more than
6760.07 -> $100,000 then we set this variable to true with this we no longer need is else
6766.289 -> Clause so that was one improvement but it's
6769.86 -> still not ideal in situations like this we can completely get rid of this if
6774.689 -> statement here let me show you instead of hard-coding false here we type our
6781.139 -> expression income is greater than $100,000 so here we have a boolean
6787.469 -> expression if this boolean expression evaluates to true this boolean variable
6792.989 -> is going to be true otherwise it's going to be false so this is the simplest the
6797.429 -> most elegant and the most professional way to implement this scenario now one
6802.679 -> more improvement before we finish this tutorial I
6805.62 -> personally prefer to wrap this expression in parentheses even though
6809.04 -> technically we don't need parentheses here but these parentheses make our code
6813.03 -> more clear more readable let me show you so I'm gonna wrap this inside these
6818.16 -> parentheses now it's very clear we're obviously on the right side of this
6822.18 -> assignment operator we have a boolean expression in this tutorial we're gonna
6832.17 -> look at the ternary operator in Java so we're gonna continue with the example
6836.4 -> from the last tutorial we had this income variable imagine this is the
6840.51 -> income or customers now depending on their income
6843.78 -> you want to put these customers in different classes if their income is
6847.14 -> more than $100,000 you want to put them in the first class otherwise we want to
6851.55 -> put them in the economy class so here's one way to implement the scenario we
6856.77 -> declare this string variable class name note that we cannot call this class
6862.35 -> because class is a reserved keyword so class name now we write our first
6867.78 -> condition if income is greater than $100,000 we said class name to first
6875.84 -> otherwise we set it to economy
6881.729 -> now as you learn in the last tutorial this code looks very amateurish a
6885.84 -> professional programmer doesn't write code like this so one way to simplify
6889.619 -> this is to give this variable an initial value so we assume they are in the
6895.499 -> economy class and then we check this condition if this condition is true we
6899.67 -> put them in the first class so with this we can get rid of this else clause that
6904.679 -> is better now in the last tutorial I showed you how to simplify this even
6908.909 -> further but the technique you learned there cannot be used here in other words
6913.53 -> we cannot add income greater than $100,000 here because here we have a
6921.179 -> boolean expression but on the left side we have declared a string variable so we
6926.19 -> want to set this to a different string depending on the result of this
6929.88 -> expression and this is where we use the ternary operator so we start with our
6935.159 -> condition then we type a question mark if this condition is true we add this
6941.579 -> value here otherwise we add the other value so this question mark and colon is
6948.69 -> the ternary operator in Java it has three pieces first we have a condition
6954.059 -> if this condition is true this value will be returned and assigned to our
6958.679 -> class named variable otherwise this other value will be returned now we can
6964.229 -> completely get rid of this if statement so put the ternary operator in your tool
6970.019 -> box it's very helpful next we're going to look at switch statements in Java in
6979.8 -> this tutorial we're going to look at switch statements in Java we use three
6984.09 -> statements to execute different parts of code depending on the value of an
6987.929 -> expression kind of similar to if statements let me show you so let's say
6992.099 -> we're gonna write a program and check the role of the current user and then
6995.579 -> we're gonna print different messages or give them different features depending
6998.789 -> on their row so let's declare a string variable called role and here we set
7005.209 -> this to admin now to check the role of the user we can write an if statement
7009.889 -> like this if role equals admin then perhaps
7015.5 -> we want to print you are an admin now you might be wondering why we have this
7020.69 -> condition here it's obvious that this condition is always true because we have
7024.32 -> set roll to admin but this is just for demonstration in a real program we are
7029.06 -> not gonna hard code this admin here so we're gonna read the role of the current
7033.53 -> user from somewhere else we don't know what it is at the time of
7036.86 -> writing code okay so here we have one condition
7040.22 -> let's write another condition else if role equals moderator perhaps we want to
7047.21 -> display a different message so you are a moderator and finally if the role is
7054.47 -> none of these values you want to print you are a guest so this is one way to
7060.32 -> implement this scenario using an if statement we can also implement this
7064.64 -> using a switch statement and sometimes that looks a little bit cleaner let me
7068.9 -> show you so we start with a switch statement then we add parentheses and
7075.11 -> inside this parenthesis we add our variable in this case row next we define
7080.09 -> a block of code and in this block we add one or more case clauses so we have a
7086.57 -> case for an admin we add a colon here now what do we want to do here if the
7091.61 -> role is admin you want to print you are an admin so I'm gonna copy this line
7097.07 -> from here and then paste it over here now after this line we need to add a
7102.14 -> break statement to jump out of this switch block okay then we add another
7107.87 -> case Clause so case moderator once again you got a colon and here we're gonna
7114.41 -> print this other message so we paste it here and then we're gonna break
7120.7 -> now optionally we can have a default clause here so if none of these previous
7125.41 -> cases apply the code that we write in this section will be executed so here we
7129.58 -> want to print you are a guest now here we don't need to use a break
7134.59 -> statement because we're at the end of the switch block so will automatically
7138.7 -> jump out of this block in contrast if we didn't use this break statement here
7143.86 -> Java will continue executing these other lines here so if the role is admin first
7148.45 -> it will execute this line and then it will jump to this case block it will
7152.44 -> execute this other line and then after it executes this break statement it will
7156.52 -> jump out of this switch block okay so this is how we use a switch statement
7161.94 -> now compare this with wave statement some people prefer to use if statements
7168.19 -> others prefer to use a switch statement now one more thing before we finish this
7172.69 -> tutorial here we're comparing the value of role with strings but we could also
7177.19 -> use integers other than the long type so if roll was a byte short or an integer
7182.8 -> our cases would look like this case one case two and so on now here we have a
7190.27 -> compilation error because roll is a string let's change this to an integer
7195.13 -> and we can initialize this to one so as you can see with sweet statements we can
7201.46 -> execute different code depending on the value of an expression
7210.25 -> all right now it's time for an exercise this exercise I'm gonna give you is a
7214.49 -> popular interview question so I want you to write a program that behaves like
7218.9 -> this here we should enter a number if this number is divisible by five we get
7225.08 -> this so if you run the program again and enter ten once again we get fit now if
7232.37 -> this number is divisible by 3 we get buzz if this number is divisible by both
7237.95 -> five and three like fifteen or thirty or whatever we get fizzbuzz and if this
7245.27 -> number is not divisible by five or three like two we get the same number printed
7251.81 -> on the terminal so go ahead and spend five to ten minutes on this exercise
7255.74 -> you'll see my solution next
7261.19 -> all right to read the number first we need to use the scanner object so
7265.61 -> scanner we import this and instantiate it and as you know here we need to pass
7272.24 -> system that in to read data from the terminal now we print a message so we're
7279.74 -> gonna use the print method instead of print line here we add a label like
7283.67 -> number and then we call scanner the next int to read a number we store it in this
7292.19 -> variable number okay so the first part is done now we need to check to see if
7297.2 -> this number is divisible by five or not so we can write an if statement like
7301.64 -> this if number here we use the modulus operator which returns the remainder of
7307.31 -> a division so we divide this by five and if the remainder equals zero that means
7313.31 -> this number is divisible by 5 so we print fizz now
7321.849 -> otherwise if this number is divisible by 3 we print buzz we need another
7330.409 -> condition if this number is divisible by 5 and 3 so here we use the and operator
7338.199 -> number divisible by 3 equals 0 in this case we want to print fizz buzz
7345.099 -> otherwise you want to print the same number like this now this is not the
7350.989 -> right solution as I will show you in a second this program has a book but it's
7355.13 -> a very common solution that I see amongst my students so let's run this
7358.789 -> program and see what is wrong here all right here we enter five we get fizz
7365.82 -> utiful what if you enter ten ten is also divisible by 5 so we get fits so far so
7372.9 -> good what about a number that is divisible by 3 we get buzz good what if
7380.88 -> we enter a number that is divisible by both 5 & 3 like 15 we get physican why
7387.869 -> is that here's the reason with this implementation if we enter 15 this first
7393.57 -> condition will evaluate to true so we get fizz these other else clauses will
7399.27 -> be ignored and that is why this line will not be executed so in situations
7405.03 -> like this you should have the most specific conditions on the top and the
7410.07 -> most generic ones on the bottom in this case we want to move this condition to
7415.38 -> the top so if the number is divisible by 5 & 3 we're gonna print fizzbuzz so this
7424.619 -> is very specific otherwise if the number is only divisible by 5 we print fizz
7430.4 -> else if it's divisible by 3 we print buzz and finally if none of his
7436.5 -> conditions is true then we print the same number now let's run the program
7441.21 -> one more time so we enter 15 and we get fizzbuzz beautiful so here's one way to
7447.119 -> solve this problem now I've seen some people argue that we have repeated this
7452.07 -> expression twice number is divisible by 5 we have that here on line 12 as well
7458.07 -> as line 14 in programming we have this concept called dry which is short for
7462.96 -> don't repeat yourself so some people argue that here we have repeated this
7467.82 -> expression and this is not a good solution here is another way let me show
7471.84 -> you so we're gonna get rid of this second condition here instead we're
7477.36 -> gonna add a code block over here so if the number is divisible by 5 first we
7483.449 -> check to see if the number is also divisible by 3 if that's the case we
7488.699 -> print fizzbuzz otherwise we print just fizz
7494.589 -> like this okay now we no longer need these two lines because we already
7499.179 -> implemented this concept here so first we check to see if the number is
7503.679 -> divisible by five if not we check to see if it's divisible by three and otherwise
7508.839 -> so here is another way to solve this problem but in my opinion this approach
7512.769 -> is kind of amateurish and ugly because these nested if-else statements are
7517.689 -> considered a bad practice now this is not terribly bad but the more you nest
7522.669 -> these L statements the more confusing your code is going to be to other people
7527.05 -> so I personally prefer the previous solution even though we had a bit of
7530.619 -> repetition in the code the more you program the more you build software the
7534.489 -> more you realize that there is no way to build ideal software programming and
7539.05 -> problem solving is all about trade-offs every solution has certain strengths and
7543.579 -> certain weaknesses this solution doesn't have any repetition or duplication in
7547.959 -> the code but it has a nested structure and these nested structures make our
7552.309 -> code hard to read and understand the previous solution had a bit of
7556.51 -> repetition but it had a flat structure there is no nesting here and this code
7561.099 -> is cleaner and easier to read there are times that we want to repeat one or more
7570.999 -> statements for example let's say we have this hello world message here let's say
7576.789 -> we want to print this five times on the terminal we don't want to repeat this
7580.359 -> code like this this looks very ugly that's where we use loops in Java we
7585.699 -> have a few different types of loops the first one that I'm going to talk about
7588.76 -> in this tutorial is for loops so let's see how we can use a for loop here I'm
7593.469 -> going to delete all this code you start by typing the for keyword followed by
7598.659 -> parentheses and inside these parentheses we need to do three things first we need
7603.879 -> to declare a loop or counter variable so let's declare a variable called I and
7609.3 -> initialize it to 0 quit often use variable names like I J and K for loop
7615.399 -> counters next we add a semicolon to terminate the first statement then we
7620.919 -> write a boolean expression that determines how many times this loop is
7625.239 -> gonna get executed so I than five as long as I is less than five
7631.239 -> this loop will be executed once again we add a semicolon and finally we increment
7637.39 -> I by one like this so this is the basic structure for a for loop now here we can
7644.35 -> repeat one or more statements using this for loop so we can add our hello world
7649.38 -> message here like here we have a single statement so we don't need braces but if
7654.67 -> you have multiple statements that we want to repeat we need to define a code
7659.05 -> block here now I'm gonna remove this because you don't really need them so
7664.63 -> let's run this code and see what we get you get hello world printed five times
7669.82 -> on the terminal beautiful now let me explain how this code gets executed when
7674.62 -> Java sees this for loop first it will execute this statement so here we are
7679.9 -> initializing I to zero then Java evaluates this condition is this
7685.239 -> condition true obviously it is because zero is less than five so the control
7689.68 -> moves to line seven this line gets executed now at the end of this
7694.54 -> iteration or at the end of this loop the control moves here
7698.35 -> so I is incremented by one now we are at the beginning of the second iteration
7703.719 -> once again this condition is evaluated is one less than five obviously it is so
7709.09 -> once again the body of this loop gets executed now fast forward at the end of
7715.51 -> the fifth iteration I will become five five is not less than five so the loop
7721 -> condition will be false and control moves outside of this for loop now
7726.13 -> here's one thing you need to remember if you want to execute something five times
7729.13 -> you can initialize your loop counter or loop variable to zero and use the less
7734.41 -> than operator here another way is to initialize this to one and then use the
7739.6 -> less than or equal to operator here now to make this more interesting let's
7744.58 -> print I over here so here we add a space and then concatenate this string with I
7750.51 -> take a look so we get hello ward one two three four five in contrast if we
7757.33 -> initialize I to zero and use the less than operator
7762.659 -> we will get hello word zero one two three four we can also print these
7767.65 -> numbers in reverse order so we initialize I to five and execute this
7773.71 -> loop as long as I is greater than zero but here instead of incrementing I we
7780.55 -> decrement it now we get hello world five four three two one
7786.4 -> so this is all about for loops next we're gonna look at while loops in this
7797.29 -> tutorial we're gonna talk about while loops in Java while loops are very
7801.25 -> similar to for loops in terms of their functionality but they're different in
7804.849 -> terms of syntax let me show you so we're gonna continue with the example from the
7808.599 -> last tutorial I'm gonna rewrite this code using a while loop so first we
7813.849 -> declare a loop variable and I initialize it to zero
7818.199 -> next we type while and here in parentheses we type our loop condition
7823.679 -> while I is greater than zero then we're gonna execute the code inside this block
7830.11 -> so I'm gonna copy this from here paste it in this block and finally we need to
7835.9 -> decrement I like this so at the end of each iteration we decrement I just like
7843.219 -> our for loops so as you can see we can achieve the same thing using a for loop
7847.69 -> or a while loop however the implementation using the for
7851.77 -> loop is a little bit lighter and cleaner so in situations where you know ahead of
7857.17 -> time how many times you want to execute one or more statements it's better to
7862.119 -> use a for loop while loops are better in situations where we don't know exactly
7866.11 -> how many times you want to repeat something for example let's say we're
7870.19 -> going to write a program and ask the user to continuously enter something
7874.119 -> until they type quit the moment they type quit we're gonna terminate the
7878.079 -> program in that situation we don't know how many times the user is going to
7881.889 -> enter something so let's write that program using a while loop
7885.76 -> I'm gonna delete everything from here
7889.66 -> all right we're gonna start with our while loop now what is our loop
7893.41 -> condition here we don't have a counter variable in this example instead we want
7897.85 -> to check to see if the user entered quit or not so here we can declare a string
7903.69 -> called input and initialize it to an empty string then we can write a while
7909.46 -> loop like this while input does not equal to quit now this code is not gonna
7916.24 -> work because input is a string which is a reference type and we cannot use
7920.95 -> comparison operators between reference types because these operators will
7924.97 -> compare the address or a string objects not their value so if you have two
7929.35 -> strings quit and quit but stored in different memory locations to have
7933.46 -> different addresses so we can use the inequality operator to compare their
7938.02 -> value instead we need to use the equals method of string objects so we want to
7944.71 -> check to see if the input equals quit now here we need to apply the not
7949.63 -> operator so as long as the input does not equal quit we're gonna continuously
7954.85 -> ask the user to enter something so here we can print a label like input and then
7962.47 -> we can use a scanner object to read something from the terminal so let's
7966.91 -> create a scanner object and instantiate it using system dot in I didn't call
7974.8 -> scanner that next this will return a string so we can store it in this input
7980.14 -> variable now with this implementation in every iteration we're going to create a
7985.6 -> new scanner object so if the user enters 10 numbers we're gonna create 10 scanner
7990.94 -> objects in memory this is unnecessary and it's actually a bad practice because
7995.53 -> it's going to pollute our memory so it's better to create the scanner object
7999.43 -> outside of a while loop and then simply use it here also here we are assuming
8006.39 -> that the user is typing everything in lowercase so if they type quit in
8010.26 -> uppercase or any combinations of lowercase and uppercase characters this
8014.61 -> logic is not gonna work the way we want so over here right after reading
8020.85 -> something from the terminal we're gonna call the
8023.53 -> to lowercase method of string objects to convert it to lowercase now to make this
8028.989 -> program more interesting let's echo back whatever the user enters so we simply
8034.27 -> print that on the terminal now let's run this and see what happens
8040.67 -> so I'm going to enter if your numbers like 1 2 & 3 whatever we type gets a
8046.28 -> code back but the moment we type quit our program terminates so while loops
8052.61 -> are useful in situations where we don't know ahead of time how many times we
8056.39 -> want to repeat something in Java we have another type of loop called a do-while
8066.44 -> loop it's very similar to a while loop but it gets executed at least once let
8071.39 -> me show you what I mean so I'm gonna rewrite the same code using a do-while
8074.72 -> loop we start with a do keyword then we create a code block at the end of this
8081.35 -> code block we type while followed by our loop condition so not input dot equals
8089.57 -> quit and then we terminate this using a semicolon now inside the body of this
8095.27 -> loop we'll simply copy all these lines we have here
8101.679 -> now compared these two types of loops with while loops we check the condition
8106.82 -> first so if the condition is false the first time this loop will never get
8111.499 -> executed in contrast with do-while loops we check the condition last so do-while
8117.92 -> loops always get executed at least once even if the condition is false that is
8123.229 -> the only difference the reality most of the time we use while loops do-while
8127.639 -> loops are rarely used but there are certain cases for that so just be aware
8131.3 -> of them but most of the time prefer to use wire loops
8139.69 -> we're gonna continue with the example from the last tutorial this program we
8144.26 -> have written has a tiny problem let me show you so I'm gonna run this enter a
8150.56 -> couple numbers these numbers get echo back beautiful
8153.8 -> if we type quit the program terminates but the word quit also gets echoed back
8159.59 -> this is a bit weird so let's look at a couple ideas for solving this problem
8163.75 -> back to our code one way to solve this problem is to check the input before
8169 -> printing it so here we can type an if statement if the input does not equal
8175.28 -> quit then we're print it so not equal input dot equals quit if this condition
8184.01 -> is true then we're going to print the input let's take a look so one to quit
8191.65 -> beautiful we solved the problem there is another way to solve this problem as
8195.889 -> well let me show you we can reverse this condition so if the user types quit you
8201.11 -> can immediately jump out of the loop using the break statement so I'm gonna
8205.28 -> remove the nut operator if the user types quit we're gonna break out of the
8211.16 -> loop otherwise we're gonna continue execution and print this input on a
8215.99 -> terminal so when Java sees the break statement it will ignore everything else
8220.25 -> after and it will terminate the loop let's run the program
8225 -> once again we enter a couple numbers followed by quit beautiful so this is
8230.58 -> the break statement we also have the continuous statement that moves control
8234.81 -> to the beginning of a loop let me show you so let's imagine if the user types
8239.52 -> pass we don't want to echo that but also we don't want to terminate the loop you
8243.96 -> want to ask the user to try one more time so after we read the input we can
8248.881 -> check to see if input equals pass this is where we use the continuous statement
8255.27 -> when Java sees this it would move control to the beginning of the loop so
8260.551 -> all these other statements are gonna get ignored and what the user types is not
8264.66 -> gonna get printed on the terminal let's run the program and see this in action
8268.61 -> so we type 1 2 pass it doesn't get echoed back one more time and finally
8275.76 -> quit so to recap the break statement terminates a loop and the continue
8282.15 -> statement moves control to the beginning of a loop now one last thing before
8286.08 -> finish this tutorial in this implementation we don't really need this
8290.131 -> loop condition because the moment user types quit this break statement is gonna
8295.2 -> kick in and terminate the loop so we can simplify this code by using a true as
8301.08 -> our loop condition so this is always true and this loop is gonna get executed
8306.21 -> forever until the user types quit this is a very common technique that you see
8311.31 -> amongst professional programmers just remember if you're using this technique
8315.54 -> make sure to have a break statement otherwise you will end up with an
8319.26 -> infinite loop that executes forever it never terminates and that can be very
8323.79 -> dangerous in terms of memory consumption so if you're using while true make sure
8328.171 -> you have a break statement in your loop
8335.211 -> the last type of loop we want to look at is the for each loop in Java we use for
8340.501 -> each loops to iterate over arrays or collections let me show you so I'm going
8345.001 -> to start by declaring a string array called fruit and we initialize this with
8350.73 -> three items let's say Apple mango and orange now let's say we want to iterate
8359.251 -> over this array and print each item on a terminal we can use any of the loops you
8363.541 -> learn about earlier like a for loop or a while loop but we can also use the for
8368.161 -> each loop which is a bit easier let me show you first I'm going to use the for
8372.121 -> loop to iterate over this array so we type for here we declare our loop
8377.131 -> variable or loop counter into I we set it to zero as long as I is less than
8382.74 -> fruits that length we're going to increment I by one after each iteration
8387.9 -> and here we simply print fruits of I let's run the program and see what we
8394.86 -> get so we get each item on a new line beautiful now there is another way to
8400.681 -> write the same code using the for each loop here we type for in parentheses
8406.44 -> with declare and loop variable but the type of this variable should be based on
8410.82 -> the type of items in our array so here we have a string array and that means
8415.381 -> every item in this array is a string so here we should declare a string variable
8420.951 -> we call it fruit here we type a colon and then the name of our array fruits
8427.61 -> now in each iteration fruit will hold the value of one item in this array so
8434.371 -> here we don't have to declare a numeric counter we don't have to write a boolean
8438.69 -> expression like this we don't have to increment our counter it's much easier
8443.041 -> to iterate over an array now if we print fruit we get the exact same result as
8450.121 -> before take a look so the first three items are from our
8454.411 -> for loop and here's the result our for each loop
8458.57 -> so this is the for each loop however this for each loop has a couple of
8462.17 -> limitations one limitation is that it's always forward only so we cannot iterate
8467.15 -> over this array from the end to the beginning in contrast we can easily do
8471.83 -> this with a for loop so here we can initialize I to fruits that length then
8477.74 -> we change this operator to greater than and replace this value with zero so as
8483.35 -> long as I is greater than zero we're going to decrement I the second
8487.73 -> limitation of the for each loop is that here we don't have access to the index
8491.75 -> of each item all we have is this loop variable which holds the value of each
8497.21 -> item in this array in contrast in our for loops we can access both the index
8503.3 -> and the actual item so I represents the index of each item and fruits of I
8508.43 -> returns the item at the given index so if you need the index then you'll have
8513.53 -> to use the for loop otherwise it's much easier to use the for each loop
8522.46 -> all right now let's get back to our mortgage calculator and implement some
8526.851 -> basic error handling so here I've changed this question by
8530.69 -> adding this label that identifies the range of values we can enter so the
8534.801 -> minimum amount of loan we can get is $1,000 and maximum is 1 million dollars
8539.54 -> so if I enter 1 here I get this message enter a number between one thousand and
8545.721 -> one million and now we are asked this question one more time if I keep
8550.16 -> entering invalid values I get asked the same question now let's enter a valid
8555.801 -> value like 1 million dollars next we'll have to enter the annual interest rate
8561.95 -> now here we need to enter a value that is greater than 0 and less than or equal
8566.48 -> to 30 so if I enter 0 we get this message enter a value greater than 0 and
8571.971 -> less than or equal to 30 once again we're asked this question one
8576.891 -> more time so let's enter a valid value like 3.9 to here we need to enter a
8583.341 -> value between 1 and 30 so if you enter 0 we get an error message and we're asked
8589.101 -> the same question so let's enter 30 and finally we get the result here's our
8594.2 -> mortgage or monthly payments so go ahead and spend five to ten minutes on
8599.421 -> extending this mortgage calculator by adding error-handling to it you'll see
8603.891 -> my solution X
8610.31 -> all right let me show you how I'm gonna solve this problem step by step so for
8615.239 -> each question we want to validate the value that the user enters if the value
8619.56 -> is invalid you want to keep asking the same question so this is where we can
8623.46 -> use an infinite loop let me show you so here's our first question principal I'm
8628.979 -> gonna wrap these two lines inside an infinite loop while true
8635.65 -> so we're gonna keep asking the same question until the user enters a valid
8640.33 -> value so here after we read the principle we can write an if statement
8644.95 -> like this if principle is greater than or equal to 1000 and it is less than or
8652.42 -> equal to 1 million and here we can use an underscore to separate these digits
8658.15 -> to make our code more readable so if the user enters a valid value then we can
8662.561 -> break out of this infinite loop otherwise we're gonna print an error
8666.88 -> message so enter a value between 1001 million like this ok now if you look on
8678.01 -> the right side here you can see this red bar this indicates an error and here in
8683.08 -> this preview window you can see exactly where we have an error it's down below
8687.19 -> on line 30 where we calculate the mortgage so if you click on this red bar
8692.521 -> we jump over here principal is highlighted in red so here we have a
8697.66 -> compilation error cannot resolve symbol principle here is the reason because
8702.16 -> we've wrapped these few lines inside this while loop and earlier I told you
8707.41 -> that whenever you declare a variable that variable scope to the Block in
8711.61 -> which it's defined so this is where we have declared the principal variable and
8715.84 -> it's scoped to this block it's not available outside of this block that's
8720.97 -> why we get this compilation error so to solve this problem we need to declare
8724.96 -> this outside of this while loop we can do it right here after radicular our
8730.24 -> constants so let's say int principle and we can initialize it to 0 now we remove
8736.57 -> the declaration from here and the error is gone now we need to repeat the same
8742.271 -> pattern with other questions so real quick here's our second question where
8747.521 -> we read the annual interest once again we add an infinite loop
8755.45 -> now the moment we read the annual interests invalidate the data so if
8760.4 -> annual interest is greater than or equal to let's say one and it is less than or
8766.189 -> equal to 30 then we're gonna break out of this infinite loop
8771.32 -> now here we should also calculate the monthly interest so the proper way to do
8776.6 -> this is like this if the user enters a valid value we add a code block here
8782.33 -> first we calculate the monthly interest and then break out of the loop otherwise
8789.46 -> we print an error message enter a value between 1 and 30 okay now if you look to
8800.36 -> the right side again we have two compilation errors monthly interest is
8804.53 -> not resolved because we have declared it inside of this block so let's move the
8810.92 -> declaration to the top here we remove the float keyword and declare monthly
8817.67 -> interest over here that's better and finally for the last
8822.87 -> question one more time we're to wrap it in this infinite loop
8831.91 -> this is where we read the number of years and right after this line we need
8836.84 -> to do our data validation so if yours is greater than or equal to one and it's
8843.14 -> less than or equal to 30 here we add a code block this is where we calculate
8849.83 -> the number of payments and then we break actually I forgot to type an S here
8856.271 -> otherwise if the user enters an invalid value will simply print an error message
8860.65 -> enter a value between 1 and 30 now here once again we have a compilation error
8867.35 -> because number of payments cannot be resolved so we remove the declaration
8872.39 -> from here and we'll be to the top right here number of payments so this is how
8881.15 -> we add data validation to this program the problem is that this code the code
8885.74 -> inside the main method is now getting a little bit too long and this hurt the
8890.78 -> maintainability of our program someone else reading this code they have to look
8894.771 -> at all these statements to figure out what's going on this is where we need to
8898.73 -> break this code down into smaller easier to read and easier to understand chunks
8903.65 -> and that's what I'm gonna show you next
8908.561 -> so in this section you'll learn how to control the flow of execution of your
8912.68 -> programs we started off by talking about the comparison operators for comparing
8917.48 -> primitive values then we talked about the logical operators like and or and
8922.67 -> not I showed you how we can use these operators for implementing real word
8927.38 -> rules and then we talked about three types of control flow statements you
8932.061 -> learn about conditional statements like if and switch for making decisions in
8936.98 -> our programs then you learn about loops for executing code repeatedly we looked
8942.05 -> at four types of loops for loops while loops do-while loops and for each loops
8947.21 -> and finally we looked at the break and continue statements for breaking or
8951.86 -> jumping to the beginning of a loop I hope you learned a lot and been enjoying
8955.88 -> the course so far
8966.739 -> as Martin Fowler said any fool can write code that a computer can understand good
8972.3 -> programmers write code that humans can understand I can't agree more if you
8977.01 -> have seen any of my courses you probably know that I've put a lot of emphasis on
8980.729 -> writing clean code so I have dedicated this entire section on clean coding
8985.05 -> we're going to continue extending our mortgage calculator and add new features
8989.069 -> to it along the way you will see our code starts to get messy and hard to
8993.18 -> maintain so I will show you a few techniques for changing the structure of
8997.02 -> the code and make it clean and beautiful are you ready let's jump in and get
9001.85 -> started
9004.09 -> hey guys maj here i want to congratulate you on your determination for learning I
9009.14 -> would really appreciate it if you support me by liking and sharing this
9012.71 -> video also subscribe to my channel and enable notifications so next time I
9017.21 -> upload a video you get notified now if you want to learn more I would encourage
9020.779 -> you to enroll in my ultimate Java series as I told you earlier this YouTube
9025.489 -> tutorial is the first two hours of this series if you're serious about learning
9029.119 -> Java and want to become a professional job of developer I highly encourage you
9033.319 -> to enroll in this series in case you're interested I put the link down below in
9037.159 -> the description box thank you on have a fantastic day

Source: https://www.youtube.com/watch?v=eIrMbAQSU34