The Complete Android Developer Course: Beginner To Advanced!

⭐️ Course Contents ⭐️
⌨️ (0:00) Course Introduction
⌨️ (3:12) Setup The Environment
⌨️ (18:52) Create Your First Application (Part 1)
⌨️ (43:53) Create Your First Application (Part 2)
⌨️ (59:20) Challenge
⌨️ (1:07:19) Variables and Arithmetic Operators
⌨️ (1:31:12) Relational and Logical Operators) Conditional Statements
⌨️ (1:47:43) Loops
⌨️ (2:04:13) Challenge (Create a Game)
⌨️ (2:16:00) Simple Arrays
⌨️ (2:27:51) Object Oriented Programming (Part 1)
⌨️ (2:49:30) Object Oriented Programming (Part 2)
⌨️ (3:06:02) Object Oriented Programming (Part 3)
⌨️ (3:19:41) OOP Challenge
⌨️ (3:43:56) Collections in Java
⌨️ (4:06:14) Static Keyword – Inner Classes
⌨️ (4:17:04) Interfaces and Abstract Classes
⌨️ (4:35:26) Concurrency – Exceptions
⌨️ (4:46:05) Singleton Pattern
⌨️ (4:56:00) Challenge (Part 1)
⌨️ (5:18:16) Challenge (Part 2)
⌨️ (5:39:40) User Interface Basics (Part 1)
⌨️ (6:04:21) User Interface Basics (Part 2)
⌨️ (6:33:09) User Interface Basics (Part 3)
⌨️ (6:58:39) Layouts
⌨️ (7:26:53) Images
⌨️ (7:37:32) ListView and Spinner
⌨️ (8:00:57) Different XML Files (Part 1)
⌨️ (8:29:41) Different XML Files (Part 2)
⌨️ (8:41:09) Material Design
⌨️ (8:59:58) Snackbar and CardView
⌨️ (9:16:51) RecyclerView (Part 1)
⌨️ (9:39:02) RecyclerView (Part 2)
⌨️ (10:03:50) RecyclerView (Part 3)
⌨️ (10:22:26) Fonts
⌨️ (10:38:35) Challenge (Part 1)
⌨️ (10:57:16) Challenge (Part 2)
⌨️ (10:13:18) Create App’s First Page – Intents
Hello, everyone, and welcome to the complete Android app development masterclass. My name is Mason. I’m a professional Android app developer. And I’m going to be your instructor for this
course. Before starting the course, let’s talk about it for a few minutes. This course is about 15 hours. And by watching it, you’re going to learn a lot about Android app development.
But it is impossible to cover everything in just 15 hours. For that, I recorded an extended
version of this course, which is about 60 hours. And in that you’re going to learn everything that you need for a successful career in Android app development. You can find the extended
course by clicking on the link down below this video. Let’s see what we are going to cover in this video. In this 15 hours, you will start from the very beginning. That means
that even if you don’t have any prior experience in programming, you’re going to do just fine.
We will cover everything in the course in the first section of the course he will set up the environment that you need for Android app development. That includes installing
Java Development Kit, Android Studio software development kit for Android app development,
and emulator for testing your applications. Besides setting up the environment. In the first section, you will create a small application to get familiar with Android app development,
and each piece in an Android application. In the second section of this course, you will learn Java language Java is the most popular language for making Android applications and
in this section, we will take a deep look at that you will learn about different variables and operators, loops and conditional statements, different collections in Java concurrency
and trading, handling exceptions Singleton pattern and a lot more. You will also learn
about object oriented programming in Java. That includes things like classes and interfaces,
inner classes and abstract classes, inheritance and polymorphism. And a lot more, we will
end this section with a challenge. So to make sure that you have learned everything we have talked about. In the next section, you will learn about user interfaces, you will learn
about how to design modern layouts for your applications. In this section, we will talk about things like buttons and text views, different layouts, and working with images and
fonts showing a list of items with ListView and recycler view. We will talk about snack
bars and card views, and adding animations to your applications. And last but not least,
we will talk about material design in Android. Once again, we will end this section with a challenge so to make sure that you have learned everything. After learning Java and
designing user interfaces, you have the ability to create a lot of applications. In the final
section of this course, we will create one real-world application from scratch. We will combine all of our knowledge about Android app development and we will create this application
together in this section beside practicing everything we have learned so far, you will learn about a lot more topics. For instance, you will learn about persisting your data
with shared preferences. And also you will learn how to show different websites in your
application. And of course a lot more. You can also include this application in your portfolio if you want. We are going to set up the environment that we need in order to
create Android applications. We are going to install a few things. First of all, we are going to install Java Development Kit or as some might call it JDK. After that, we are
going to install Android Studio. And after everything, we are going to create a virtual device in order to test our Android applications. As you can see, I have already downloaded
JDK and Android Studio. Let’s also see where we can get them from. For JDK, you can simply
search for Java JDK, and a link from the Oracle website should pop up. The first link from
the Oracle web page is probably our needed link. As you can see, we are going to download and install JDK version eight, it’s going to be enough for our purposes in this course,
down here. First of all, we need to agree to the license agreement. And after that, depending on your operating system, you can download one of these versions. I’m on Windows,
so I’m going to download and install this windows x 64 version. If you’re not sure about
this x86 or x 64, you can right-click on my computer on your desktop in properties.
Here you can see the architecture of your CPU minus 64-bit for you it might be x86.
Depending on that select one of these two files, just click on this download and the
download should be started. Sometimes like here, it might ask you to log into your Oracle
account. Let’s do that creating an Oracle account is free and you won’t be charged.
Okay, let’s select Sign in. And as you can see the download has begun. I’m going to select
the canceling here because I have downloaded the JDK previously. Let’s select that. It’s
going to be a really simple installation process. Let’s select Next.
Once again next. Once again, let’s click on Next. And as you can see, we have successfully
installed JDK. Let’s click on this plus, if you want to make sure that you have installed
Java JDK successfully, you can always go to your command prompt by going to start menu
and typing cmd. In here, you can type Java dash version. As you can see, we are seeing
some information about the version of Java installed on our system. As long as it’s 1.8.
and higher, we are good to go. Let’s close our command prompt. Next, we are going to
download Android Studio. For that we can go to developer.android.com. Let’s see developer.android.com.
During the course, we will come back to this website a lot because this is the official
website for Android developers. Okay, for downloading Android Studio, just go to this Andrew the studio tab. And from here, you can download one of the versions. If you don’t
see your version of operating system, you can click on this download options. But in here, I’m just going to click on the download, let’s say download Android Studio, we need
to agree to the terms and conditions. And as you can see, the download has started. Once again, I’m going to select to cancel in here because I have already downloaded
Android Studio. After the download is finished, just click on that. Once again, this is going
to be a simple installation process. Let’s select Next in here, make sure to check this Android virtual device. Let’s select Next in here you need to select a path to install
Android Studio. Notice that installing Android Studio and all of its SDKs. And also emulators
sometimes might take something like 20 or even 40 gigabytes. So make sure to have at
least 20 gigabytes on the drive that you are going to install your Android Studio. After
that. Let’s select Next, and install. After the installation is finished, just click on
this next. And let’s start our Android Studio. Don’t worry about these three projects that
I have in here. Basically, these are the recent projects that I was working on. I have Android
Studio installed on my system previously, and I uninstalled it for this video. But for
you this list would be empty. Also along the way of installing Android Studio you might
see some windows is about selecting a theme or other preferences. I didn’t see them because
I have installed Android Studio previously on my system. Those are not important depending
on your preferences, select one just if you see something like installing SDK on check
that because we are going to install everything here together. In order to install SDK, you
can come to this configuring here. And you can select this SDK Manager SDK stands for
software development kit. And for developing Android applications, you need to install
at least one SDK. Once again, as you can see, I have installed two SDKs in here and return
an Android nine API’s both for you this list should be empty and none of these should be
checked. First of all, let’s check this show package details in order to see the package details for every SDK. By the time I’m recording this video, the latest version of Android
is Android 10 or aka level 29. To be precise, I suggest that you always install the latest
version. But sometimes there might be some preview versions in here, I don’t suggest
installing those because those can be buggy. And those are not yet released on a stable
channel. So make sure to install the latest stable version. If you want, you can install
the complete SDK. But I don’t suggest that because these can be memory and hard drive
consuming. And these can occupy a lot of the storage on your device. But the necessary
things in here is first of all Android SDK versions, whatever the version of your API
is. So the first one, also the sources for that version of SDK. The second option, also
I suggest that depending on the architecture of your system, select one of these two version
of atom mine was 64 architecture. So I’m going to select this one. If you are going to work
with different Google API’s. For example, if you are going to work with Google map or
YouTube API’s, check one of these, for example, this Google API’s for atom 64 and also check
one of these two Google API versions. This is going to install at least one SDK on my
system, and return or API level 29. But if you want, you can select and install multiple
SDK versions. Basically, this SDK versions and API’s means that in what version of Android
Are you going to run and test your applications later on. We will see that when we are going
to run our application On a virtual device, you’re going to need at least one SDK, installing
multiple SDKs can be memory consuming and also harddrive consuming. But if you have
the resources, I suggest that you at least install the latest version after that, install
this API level 29. Because there were a lot of changes specifically in the data storage
structure also installed API level 28, down in here installed API level 26. And after
that, level, 21, and also 19. Because in this version of SDKs, there were a lot of changes.
And I’m just saying this because of my experience in Android app development. But if you don’t
have the proper resources, selecting one of these is going to be just fine. After selecting
what you need in here, just click on this apply, make sure to have a stable internet connection because it’s going to download some stuff. Let’s click on this apply. In
here you can see a dialog that will show you some information about the stuff that are
going to be downloaded and also the size of the download file. Next, select OK in here, and our download should be started. I’m going to skip the video in here until the download
is finished. After the downloading and installing all of the components that you have selected.
Before it’s finished, just click on this finish. And let’s see what else do we have in here. Okay, we have successfully installed at least one SDK, let’s switch to this SDK tools. And
let’s see what we need from here. First of all, once again, check this show package details,
install one of these Android SDK build tools, I suggest that installed the latest version.
As you can see, mine is installed in here 29.0 point two at the time is the latest version.
Also down in here, you need to install Android emulator mine is not updated. So I’m going
to update that. The next two things that are mandatory are these SDK platform tools and
SDK tools, make sure to select those two. Also down in here we have this documentation
for Android SDK. This option is not necessary, but it’s going to help us a lot during our
journey in Android app development. Later on, we will see that these documentations will help us a lot when we want to debug something or when we faced some errors. So I strongly
suggest to install these documentations as well. The other thing that is not necessary,
but I suggest to install is this hexam installer, it is going to be an accelerator for the speed
of your emulator. The native emulator in Android Studio can be various low, especially if you
have a low memory Ram. For that reason, I suggest you to install this hex installer.
But there is one thing that I need to mention in here. I’ve seen a lot of students having problem with this hexam installer, I’ve seen them that they cannot install these hacks
them successfully. This hex m cannot be installed on all of the CPUs. And depending on the structure
of your CPU, this XM might not be installed. But there are some CPUs, especially Intel
CPUs that they have a special option in the devices bias is specifically for this emulator
accelerator. If that’s not enabled, you may want to enable that in the BIOS settings as
well. And also if for any reason you couldn’t install this handsome installer, this emulator
accelerator. And also for that matter, the Android emulator itself, there is no need to worry there is another way as well. Besides the native Android emulator, you can install
external emulators as well for example, the one that I know that is working very well
is called genymotion. In here, I’m not going to install that but I will put a link to download
genymotion in the description of this video. And also besides all of these, you can always
install and test your applications on the real device that you have. Later on in the
course we will see that how we can install and test our applications on our mobile phones.
Okay, after selecting everything from here, just click on Apply once again. Once again we are seeing the download the information like Select OK and that the download begin.
Once again. I’m going to skip the video in here until the download is finished. Once again When the download and installing is finished, just click on this finish. And we
have successfully installed SDK platforms and SDK tools just before I close this window,
let’s switch to this appearance in here. From here you can change all of the preferences
that you want. For example, you can change the theme Dracula is the dark theme. Also
we have this high contrast theme. The intelligent theme is the light theme. I’m going to stay with that because it’s good for recording purposes. If you want you can use custom fonts
for the fonts of your Windows is from here. You can also change the size of your font. I’m not going to do that. Also then in here, you can change The font size. Also for the
presentation mode. If you want to change the size of your codes, you can search for fonts
in here, I believe it’s in this editor tab in here in this font, you can see that mine
is 18, you can increase or decrease that if you want. Let’s delete this one. And also,
let’s minimize this editor. I have another suggestion for you in here as well, in this appearance inside the system settings inside the updates, make sure that your updates are
being checked from a stable channel. We have other channels like developer channels and
betha channels as well make sure that you have the stable channel. Others can be buggy.
Okay, that’s it for installing SDK, we will be coming back to this setting menu later
on. Let’s select OK. OK, now it’s time to install at least one emulator. For that we
can once again select this configure this time, we can click on this AV manager AV D
stands for Android virtual device. As you can see, once again, I have two devices installed
in here pixel two and pixel three, this list should be empty for you. But because I had
Android Studio installed on my system, I can see them let’s select this create visual device
in order to create a new virtual device. First of all, you can create your own and customize
the hardware by selecting this new hardware profile. From here you can name your device,
you can select that if it’s a phone or tablet, you can set the screen size and resolution.
Also then in here you can specify some ram to it and all sorts of sensors and hardwares.
I’m not going to create my customized device. Instead, I’m going to install one of the predefined
devices. As you can see, I have pixel two and pixel three in here. Let’s select another
device from here. Also from this left panel, you can see all sorts of devices like TV viewers
and tablets. Let’s select one device from here. I don’t even know all of these devices.
Let’s just select this pixel Excel, select Next. Okay, from here, you need to specify
the Android version that is going to be installed on this device. I’m going to install Android
q or API level 29 on this device, so we need to download that first, let’s select Download
once again, I think it’s going to be a large five. So I’m going to skip the downloading here until the download is finished. Yes, it’s 1.1 gigabytes. Okay, the download and
installing is finished just click on Finish. Now we can select the downloaded API, sometimes
you might not see the version of API that you’re looking for. For that, first of all, you can always refer to this list. After that, you can check the second tab and also the
third tab. But we are going to install Android q on our device. Let’s select Next in here
we can customize our device. For example, we can change the name of our device, we can also change the default orientation. For example, the landscape and portrait I’m going to save
its portrait. In this Show advanced settings, we can do some more customization, for example,
you can change the camera, the network. Also down in here, you can change the size of RAM,
and SD card and all sorts of customization. I’m not going to change any of these. I’m
just going to click on this finish. And as simple as that we have installed a new emulator
on our device. Okay, let’s close this window. Okay, I think it’s a good point. To finish off this video. In this video, we have installed JDK eight we have installed Android Studio.
After that we have installed SDK and we have done some customization on the appearance.
And after everything we have installed a new virtual device.
We are going to create our first application we can do so by opening Android Studio and
selecting this start a new Android Studio project. First of all, in this window, we can see some templates. These templates are just some small applications that have some
codes written to some level. For example, in this basic activity, you can see that we
have a small button and also a menu above in here. We can see other templates in here,
for example, a template for Google Maps, we have a template for navigation drawer. Don’t
worry about all of these because we will learn how to create all of these elements ourselves
later on. Even we select this ad no activity template. But the Android team are nice guys,
and they have provided us these templates. In a lot of these templates. You can see the
word activity Don’t worry about this activity yet. If you remember from the content videos, I said that we are going to have an entire session which we will talk about activities
and fragments. But for now you can think of activities as different parts of your app.
application. For example, if your application has different pages and activity is one of those pages, this definition is not accurate, it might not even be correct. But for now
we can think of an activity as a page of our application. In our first application, I’m
going to save it this empty activity, which has no element, it’s just going to create some files for us. But if you want to save it, at no activity, you can do so as well.
Once again, later on, we will see that how we can create all of these templates ourselves. Okay, let’s select empty activity. And let’s hit next. In here, we need to provide some
information about our application. For example, the name of our application, I’m going to
change the name of my application to let’s say, Hello world. As it’s a cost among developers
to name their first project, I level just for no reason. The second part, you will define
the package name for your application. This package name should be unique to every application
that is being released into the world. later on. When you want to publish your application to play store, your application will be identified with this package name. The convention in
here is to provide the name of your website in backwards. For example, My website is called
maker.org. So my package name is or dot maker dot the name of my project. Don’t worry if
you don’t have any website, if you don’t, you can use something like conduct example. Let’s say comm dot example dot the name of my project. But since I have a website, I’m
going to use that in here, I’m going to say port that. Once again, this package name should
be unique to every application. After that, you need to define the path that you want
to save your project. If you want, you can change this path by selecting this folder
in here and select the path but in here, I’m not going to change that. After that you can
select the language that you’re going to work with. Android applications can be written
with Java language, and also kotlin language. In this course, we are going to stay with
Java. But if you want, I am currently recording a new course about developing Android applications
with kotlin. Feel free to check that if you want, but let’s select Java in here. After
that, you need to define a minimum API level. This minimum API level in here means that
how many older Android devices can install your application. For example, if you click
on this drop down in here, you can see the list of different API levels. Right now, the
minimum API level that I can select these 14, but I can go off to API level 29, or Android
10. To be precise, there is a balance in here. If you select your minimum API level very
high, you can work with a lot of newer functionalities in Android, but fewer devices will be able
to install your Android applications. For example, if I select API level 29, which is
the latest version, only less than 1% of the devices can install my application. If I select
something like 19, we can see that 95% of devices by this time can install my application,
you as the developer should decide this balance for this course, probably, we are going to
stay with API level 19 for most of our projects, because I think it’s a good balance, and 95%
is a good number. Leave this check empty for now. Also, using Android x artifacts is mandatory
since few months ago, we will talk about it later on in the course Let’s hit finish. And
let’s create our first project. If it’s the first time that you are creating your Android
applications, this process can be very slow because Android This video is going to download
some stuff, for example, the gradle file from internet so for that reason, this process
can be slow if it’s the first time that you are creating your application. The first thing
in here that is noticeable is that Android Studio have created two files for us this
activity main dot XML file, and this main activity dot java file. These have been created
because we have selected that empty activity template when we have created our project.
Inside each of these files. There are some codes. Once again, we can create all of these codes if we don’t select that empty activity. But if you select that, you can have some
codes written to some level. The other thing in here is this view pane down in here. This
build painting here will show the progress of building your Android applications whenever
you want to run your Android application, whether that is in the debug mode, which we
will be working in our entire course. Whether it’s in the production level, this building
here will show the progress of building your Android applications. I’m going to minimize it for now. And let’s talk about all the other stuff that we are seeing in here. First of
all, let’s talk about this project thing here, I can minimize and maximize that. Also, talking
here, we have multiple options. For example, we have this project option. This project
option view exactly shows the structure of files on your system. For example, you can
maximize every folder. If you want to have access to this main activity dot java file,
you can go to App folder inside the sources. Inside the main folder inside the Java folder,
you can see that we have a folder in here, which contains the main activity java file.
This is the exact hierarchy of files on our system. If you want to make sure of that,
you can right click on the name of your project and select Show in Explorer. This option in
here, it will open the Explorer on your Windows, you can see that we have a Hello World project,
don’t worry about the others inside this project. As you can see, in the hierarchy, we have
this app folder. Inside this app folder, we have a source folder, main folder, Java folder,
org, may code HelloWorld. And this is the Java file that we are seeing here. So this
project view will show the exact hierarchy of different files on your system. There is
another view in here and that’s called Android view, we are going to stay with Android view
for the most of this course, because it’s much more easy for eyes to follow. As you can see, we have this app folder. And inside that we have this Java folder, we have this
package name. And inside that we have this main activity dot java file, it’s much more
easy for eyes to follow different files. Also, besides this app folder, we have these Gradle
scripts down in here as well. Before I talk about these Gradle scripts, I need to talk
about the structure of every Android application. So in most Android applications, there are
at least two different kinds of files, the layout files, which will define the looks
of your application. The layout files in Android are called XML, you can see this activity
main dot XML file. If you click on that, you can see the layout of your application. Right
now it has only one simple text. The other part of every Android application is the Java
file. If you are developing in kotlin language, there are kotlin files. The Java or kotlin
file in most cases will define the logic of your application. For example, what happens
if you click on a button, or what to do with the user’s input, all of those will be handled
in the Java or kotlin files. Beside the layout files and Java files. In every Android applications.
There might be some other files, for example, you may have some images, and also some audio
files as well. Those will be inside this Resources folder, for example, inside the meat map folders.
Right now we have an icon for our application, which is this simple Android icon. Either
images or other files can be put inside this Resources folder as well. Basically, this
Resources folder is for the static variables or static phones on your application. So these
are the three main components of every Android application. We have layouts, we have the
Java file, and also we have the static files. Beside these we have this manifest file inside
the manifest folder. This manifest file Vila is specify some general properties for your
application. For example, you can see the icon of your application in here also, you can see the name of your application, which is HelloWorld, in this case, and some other
properties or features about your application. They will talk about this manifest file later
on in the course. But for now, let’s close it. We have all of these different files in every Android application. But for example, when you are going to download some application
from the Play Store, you will see that we have only one file, one apk file. That’s the
final application that will be released, how are we going to generate that apk file? Well,
in that case, this Gradle tooling here is going to be useful. gradle is a build tool
that will combine all of these files and other files, and we will create an APK file. No
matter if you want to release your application or if you want to test your application on
an emulator, you are going to need Gradle for that reason we are going to work extensively
with The other use of Gradle is that if you want to use other people’s code in your project
as well, for example, if you want to use YouTube player in your project, you will add its dependency
inside this built up gradle file down in here, just by writing one line of code Gradle will
download the code for YouTube video player, as simple as that Gradle will add the YouTube
player code to your project. And after that, you can use it. So Gradle is a build tool
that will handle the combination of every file, and also other codes and we will generate
an APK file, we will talk about different Gradle scripts later on in the course. Okay,
let’s close all of these extra files. Okay, this was the project when we can minimize
or maximize this pain from here. Let’s minimize that. And let’s talk about other pains available
in Android Studio. Then in here, you have some useful pains as well, for example this
to do is very helpful when you are creating or when you’re developing Android applications
to those are some works that you don’t want to do at the time, but you don’t want to forget
those works. For example, inside this main activity, I can add a to do as simple as that
I can say to do, let’s say complete this. It’s just for the developers to remind themselves or other colleagues to do something
later on. As you can see this to do has been added inside this to do pain as well. If you
want, you can check that it says that it’s inside the main activity. Later on, we will
talk about how we can define to dues and also other kinds of comments. So this is one of
the other pains. The other one is this terminal dining here. This terminal is like the command
prompt in Windows or let’s say terminal in Linux or Mac. And it’s very helpful if you’re
working with something like ADB or Android debug bridge. Later on, we will see the use
of ADB in our projects. Also, it’s very useful for working with SQL lite databases, we have
worked extensively with this terminal in the database session. After that we have this new tool here we have seen it when we have created our project, you can see the progress
of every application when it’s going to be built. In a lot of cases, there might be some
problem when we build our project. And from here, you can exactly locate the problem.
After that we have this logcat in here, it’s going to be very helpful when we are going to debug our Android applications. In fact, we are going to work a lot with this look
at Okay, let’s minimize all of these for now. Above. In here we have some menus, we have
File, Edit view and all of these, we will work with most of them, I believe in the course
but it’s impossible to work with all of them. Also, there is one thing that I need to say
in here. When you create your first application, you may get some warnings, for example, a
rendering warning or some other kind of warning, if you get those probably there isn’t something
wrong with your application, you just need to rebuild your project. For that you can come to this build option in here and make your project once again, in case if you get
some errors at the first time that you create your application. Sometimes newer versions
of Android Studio can be buggy. And creating your project once again simply can solve your
problem. Also, there is another option from here from five, you can close your project
and reopen it that can be useful or in some cases, you may want to invalidate your cache
and restart Android Studio. So if you get those warnings or errors, we can try these
three different ways in order to overcome the problem. There is one very useful tool
that has been added to Android Studio recently in the past few months. And that’s called
Android profiler. Let’s add that to this pane. down in here as well. We can add that by going
to this view to Windows and profiler, it’s going to be helpful for monitoring the resources
that your application is using, for example, the amount of RAM the CPU that your application
is using, or even the amount of network that your application is using. If you’re on our
application, we can track the live amount of different resources that our application
is going to use. Let’s run our application and see for ourselves. You can run your application
by clicking on this green triangle from here, but before that, make sure to select the current
app that you’re working. And also beside that make sure to select the device. In the previous
video we have created a virtual device and we can see the list of different virtual devices
available. I’m going to run my application on these So through API level 29, for no reason,
let’s just run. If we click on this building here, you can see the build progress, I said
that even if you are going to debug your application, or even if you are going to run your application
at the production process, you can check the progress of build as well. As you can see,
this emulator has been opened. And we will see our application in a few minutes. Also,
from down in here, you can see the current status of running your application. Right
now it has created or built my application successfully, and it’s waiting for the device
to turn on. Also, if you check this logcat in here, you can see that there are a lot
of things going on in here. We will be using this logcat extensively later on in the course,
for different debugging purposes. Let’s minimize that. Also, if you open this profiler, you
will see the live tracks of different resources that your application is using. Let’s open
our emulator. Right now we have this simple application in which has a predefined text
called hello world. And if you want, you can check the amount of CPU or other kinds of
resources that your application is using, it’s going to be helpful when you’re not sure how much resources your application
is using. Let’s minimize that for now. If you want, you can close your application with
this red button here, you can stop it. And also if you want, you can turn off your device.
By long pressing this turn off button, you can say power off and your device will be
shut down. As you can see, we are getting some warnings in here right now, these are not important. But if you want to check them, you can click on this event log. And everything
that happens since we click on this green triangle, we will be logged in here. Okay,
let’s minimize that. And let’s switch back to our activity main dot XML file. And we
have a lot of options in here as well. Right now you can see two different views of your
application. This white one is called the design view. And this one in here is the blueprint
view. There are some slight differences between these two, for example, if you have some invisible
item inside your activity that might not be visible inside this design view. But you can
keep track of that inside the blueprint view. For now, I’m going to disable this blueprint
view because we are not going to work with that just for now. In order to disable that,
you can click on this stack icon. And you can see just design, we can also minimize
and maximize that by pressing this plus button. At the left panel in here, you can drag different
elements different user interface or UI elements. For example, if you want to add a button to
your application, you can simply drag it like this. And this button will be added to your
application. As you can see right now because this element is clicked, you can see different
attributes. At the right panel, we have a lot of attributes for every user interface
element. The most important one is this ID above in here. This ID is the unique ID of
your element in which we will use it in order to have access to this button, for example,
from inside of Java file. Later on, we will talk about that if we click on our text view,
you can see that we have different attributes as well. These are just a few attributes.
If you want to check the list of all of the available attributes, you can click on this all attributes. And you can see that there are a lot of them. We are going to get familiar
with different UI elements in the UI session we have that I think in two sessions from
now, after the Java session we will be talking about user interface in a specific details.
Also, when you are inside your XML file, you have two views design view and text view design
view is this view that we are working right now. And if we switch back to text view, you
can see the exact value of every UI element in XML code. xml is a markup language. If
you are a web developer, you know the concept of a markup language. HTML is also a markup
language. markup languages are just for defining some elements that user is going to see, for
example, a button or text view, you can see that there are a lot of attributes in here but that the don’t know anything about them yet. We will be talking about them later on
in the force. So there are two ways of defining UI elements. One is that You drag simple items,
for example, a button to your design view, or from the text view, you can type it, you
can see that we have a button in here, we can select the whole button. And we can simply
press Delete, and that button will be deleted, you can see that we no longer can see that. Also, if you don’t have this preview enabled, you can enable it from here, let’s just press
Ctrl Z in order to get back our button. And let’s switch back to our design view. I’m
going to define an ID for this Hello text because later on inside my java file, I’m
going to identify that and I’m going to work with that. So by clicking on HelloWorld, you
can see the list of attributes. At the top, we have this ID, we can set an ID for our
TextView. Also, if you don’t know the difference between a text view, edit text, and all of
these don’t be very TextView is just a fancy way of saying a simple text, you can define
the ID whatever you want. For example, in here, I’m going to say txt message. But notice
that there are some conventions in here, you can’t use a spaces when you are going to define
an ID also beside that you can’t use some weird characters like $1 sign or a hashtag
sign. The other convention in here is that you type the first letter in lower cases.
For example, this team here is learn cases. And also after the first word, you type the
first letter of the second word. in capital letters. For example, we have this m here,
there are some just basic conventions among programmers by pressing Enter, we now have
an ID for this hello world. If we switch back to our TextView from down in here, we can
see that for this TextView, an attribute has been added the ID attributes. So no matter
what you do inside your TextView or design view, they are basically the same. But as
you can see, right now we have some warning for this button. The warning says that this
button is not constrained to anywhere. That’s because we are using this constraint layout
for this whole XML file. Don’t worry about this constraint layout yet we have other layouts
like relative layout and linear layouts as well. But for now, just know that when you
are using constraint layout, which is the default case, when you are creating an Android application, you need to concentrate or chain your different elements to software. For example,
if I don’t constrain this button to anywhere, it’s going to be floating at the wrong time.
Because of the different screen sizes, we cannot know the exact position or exact place
of this button. And right now if we run our application, probably This button will be
moved to the place zero and zero, which is exactly this corner in our layout. In order
to overcome this warning, we can simply add some constraints by clicking and dragging.
As simple as that I’m constraining this button to the both edges of my screen. Also for the
top and the bottom, I can constraint that as well. For the top, I’m going to constrain
that to this TextView. For the button, I’m going to constrain that to the bottom of my
screen. As you can see whenever I add a constraint, and attributes will be added to this TextView
in here as well. Now we don’t have that red one. Also, it’s worth mentioning that by default,
when we have created this button, when we have dragged that into our design view, an
ID has been added to it as well. The idea of this button is just button, we can change
that if you want. For example, I can say btn let’s say hello, or whatever you want. But
in an XML file, the ID should be a specific to every element.
Right now this button doesn’t do anything. In this video, we are going to make our application a little bit more interactive. For example, we are going to change the text of this TextView
by clicking on this button. For that we need to define this TextView in our Java file so
that we could have access to its attributes. But before I go further to my java file, I’m
going to switch to full screen so that you won’t be distracted with all of my icons down in here. If you go to this view option in here, down here at the bottom, you can see
that we have few options. I’m going to go to full screen. For the rest of the course i’m going to stay with full screen. Okay, let’s switch to our main activity dot java
file. As I said in the previous video, you can see that we have some code written in here. And that’s because when we created our project we have selected the empty activity
project for that now we have an empty activity which has an activity main dot XML file and
also main activity dot java file. In future we will see that we can create All of these
folds if we select no activity at the time of creating our project, but in here we have
these codes returned to this level. Also, we have this plus sign in here beside this
import. If you click on that, you can see that we have imported some packages to this
class. If you want, you can always minimize and maximize that input. And for that matter,
you can minimize and maximize all of your methods. For now, I’m going to leave these imports like this so that we can see what we are importing into our Java file. Don’t
be worried about all of the new keywords that you’re seeing in here, for example, this probably class extends protected void. We will talk about all of them later on in the course.
But for now, just know that this onCreate method is going to be the start point of our
application, it means that when we run our application, the code inside these two curly braces is going to be the code that will run first, once again, don’t worry about the term
method that we used. We will talk about that later on in the course. So inside this onCreate method, two things are happening, we have this super.on create, we are not going to
talk about that right now. And after that we are setting some content views by setting content views we are accessing to the activity main file, you can see that we have this activity
main in here. Basically it means that this Java class is somehow related to this activity
main layout file. We will talk in more depth about this later on in the course. But for now just know that up until this point in our code, we have said that this java file
is related to this layout file. After that, we can write the quotes that we need. For example, in here, I’m going to access to that TextView that we have created in the previous
video for that I can say text view. As you can see, when I type something intelligent,
the idea behind the Android Studio is suggesting some options. There are a lot of options in
here, but the one that I need is going to come from Android dot widget package. So in
Java, we have classes equivalent to the UI elements or user interface elements in our
layout file. If we are going to define a button we have that class in java file as well. In
here we are going to use text view. So we can import that in our project as well. As
you can see, a new line of code has been added to our imports. This line of code is importing
the TextView from the widget package into our Java file. Next, I need to name my TextView
The name is optional, and you can name it whatever you want. For example, in here, I’m going to say txt Hello, this is just the basic convention you can name your class, whatever
you want, but we will talk about conventions later on in the course, after naming your TextView you need to locate the TextView from inside the layout file. And the way to do
that is like this, you can say is equal to there is a helpful method in every activity
that is useful for finding different views by their IDs. And that method is named find
view by ID. Once again, this method is useful to find different views different user interface
elements from your layout files by date ID inside the parentheses of this method, we
need to pass the ID of our TextView from inside our layout file. And the way to do that is
like this, we can type r r is a special class in Java, which will give us the access to
all of our resources, our static files in our project. Once again, it stands for resources,
let’s select that, you can say r.id dot after that, we need to add the ID of our tapes we
have which was txt message, I believe this ID in here should be the exact ID that you
have put inside your layout file for this TextView you can see that the ID is txt message.
And we are using that to have access to our text view. So this way by using this fine
view by ID method, and after that passing the ID in this shape. Now we have access to
our text view. But you can see that there is one more error in here. And that’s because
in Java, every time you write a sentence, you need to finish your sentences with a semicolon.
That’s just syntactical and it really doesn’t matter some languages does not require the
semicolon, but in Java, you need to put a semicolon at the end of every sentence. Okay, now up to this point, we have our TextView in our Java file. Now we can use it for example,
if we want to change the text of our TextView we can say something like this, we can type the name of our TextView which is txt Hello. After that we can say dot Don’t worry about
this dot operator. We will talk about it in the next section in the Java section. But for now just know that when you are typing.on an object, you have access to all of its methods
and attributes. For example in here I can see set text once again you can see that we
have multiple options. Let’s import the first one inside the parentheses of this method. You need to pass a text. And whenever you’re going to pass a text statically, whenever
you’re going to hard code the text, you need to pass it inside double quotations. Let’s
pass a double quotation inside this double quotation, I’m going to say hello, this way.
Now we have changed the text of our TextView. Let’s run the application and see if it’s
going to work. Remember that inside our activity main layout file, the text is HelloWorld.
Inside our Java file, we are changing it to Hello, let’s run the application. As you can
see, in here, the text of our TextView has been changed to Hello. Still, our application
is not interactive, we want to change the text when we click on this button, not when we run the application. For that we need to define an event listener to be precise, an
onClick listener for this button so that when we click on this button to take off our TextView
changes, there are multiple ways of defining an onclicklistener. For our button, the simplest
one is to do that inside your main activity layout file. If you come down in here inside
your button element inside the anchor brackets of your button, if you type on click, you
can see that we have an option in here. Let’s pass a name in here, for example, I’m going to say on btn clique, this name in here is going to be the name of a method in our Java
file. As you can see, right now we are getting a red warning, it says that there is no such method in your Java file. Let’s copy the name from here. And let’s go to our main activity
and create that method. So in order to create a method, we need to do that outside of the
scope of this onCreate method. And inside the curly braces of this class and talking
about this, we need to define our method inside this class. And the way to do that is like this, we can say public void after that the name of our methods, which is on btn. Click
a pair of parentheses, because it’s going to be the onclicklistener. For our button, we need to accept a viewing here, don’t worry about this viewing here, we will talk about
all of these in Java session. Let’s name this view. After that, we need a pair of curly
braces for our method. And in here, we need to put the code that we need to execute when
the user clicks on our button. If you switch back to your activity main dot XML file. Now
you can see that the red error has been disappeared. Okay, let’s go to main activity dot java file,
I’m going to change the text of our TextView to Hello when the user clicks on our button.
So for that method, I’m going to move these two line of code to inside this method, you
can probably guess the behavior of our application right now. Let’s try it once again and see how is it going to work. You can see that the text is still says hello world. If we
click on our button, the text will change to Hello, this is okay. But this is not entirely
what we want. It’s just saying hello right now we want to say hello to the exact user.
For example, we are going to get the name of our user. And by clicking a button we are
going to say for example Hello may sound Hello Tom and something like that we are going to get the user’s input, the way to get the user’s input is by using some element in our UI called
edit text. Let’s switch to design view. You can add an edit text into your project by
going to this text option in here, you can see that we have multiple options. The first
one is for text view, we have seen the text view it’s this hello world text. After that
we have other options, plaintext, password, email, and all of these, these are all helpful
for when you want to get different kinds of users inputs. But for our purpose in here,
we are going to use plain text. Plain Text is just a simple text that we are going to get from the user. Let’s drag that into our view. If you take a look at the attributes
in here, you can see that it’s ID is edit text. Let’s quickly change that. Let’s say EDT. txt name. Because we are inside the constraint layout, we also need to constraint this edit
text as well. But before that, I’m going to change the top constraint of this button,
I am going to change its constraint from the bottom of this TextView to the bottom of these
editors. For that I am going to direct the top constraint of this button to the bottom
of this edit text. Now let’s add some other constraints for this edit text as well. For example, for the top, I’m going to constrain it to the bottom of my text view. And also
for the edges. I’m going to go to the edges of the screen. Right now it’s too close to
our text view. If you want we can add some margins. For example in here, I’m going to
say 70. That seems to be better. The other attribute that every edit text has is this
text in here. I’m talking about this attribute in here. You can see that right now it says name if you want you can change that but in here, I’m going to completely delete that,
instead, I’m going to define some hints. You can see this hint attribute in here. The difference
between hint and text is that when you click on your edit text, the hint will be deleted.
It’s just a hint for the user to know that what he or she is going to enter inside this
edit text. For example, in here, I’m going to say me, you can see that it’s somehow grayed out. Okay, now we have an edit text inside our layout file inside our Java file, we need
to have access to this edit text so that we could get the user’s input once again, the
start point of our activity. And our application is this onCreate method. So we can do that
inside this onCreate method, we can have access to our edit text like we did for this TextView.
We can say Edit Text. Once again, you can see that we have an equivalent class for this
edit text as well. Also, if you take a look at in here, you can see that this line of code has been added to our class as well. Let’s say Edit Text, edit GTX. The name, like
before, we can say is equal to find view by ID. And once again, we need to pass the ID
of our edit text which I can say r.id dot EDTA. txt name. To this point, we have defined
our edit text. Now, we need to get the text of our edit text for that we can say Edit
Text name, dot get text, you can see that we have this option. But this is not enough, you also need
to do another level of conversion. And we do that by saying.to string. Don’t worry about
this two string method yet, we will talk about that later on in the course. It’s just for converting whatever the text is inside our edit text to a text that we can use in our
Java file. Don’t worry about that. We will talk about it later on. But there is a problem with our code in here, we are going to get the user’s name and we are going to say hello
to the user. When we click on the button. For that we need to have access to this edit text inside our on button click method. But if you try to do that, for example, if you
say Edit Text name, you can see that everything is in red, it means that you don’t have access
to that edit text inside this method. And that’s because something called a scope in
Java. The scope of this edit text is this onCreate method within the curly braces of
this onCreate method, we can have access to these elements from inside are on button click
method. In order to solve that problem, we need to move these two line of code to inside our on button click method. Let’s quickly do that. Inside our on button click method,
let’s paste them. But right now we are not doing anything with the users input we are
getting the user’s input, but you’re not doing anything with that, we need to pass this line of code to the set text method. Let’s quickly delete this line of code. And inside the parentheses
of the set text method. I’m going to pass that I’m going to say Edit Text name, dot
get text.to string. This way, we are setting the text of our TextView to whatever the text
is inside our edit text. Let’s quickly run the application and see if it’s going to work. This is how our application and this edit text looks right now. If we click on that,
you can see that we can type something for example, let’s type my name. And if we click
on our button, the text of our text view changes to Mesa, this is not entirely what we wanted,
we want to say hello to myself. In order to do that we need to make some changes to our
code. Also notice that I didn’t put any double quotation in here. And that’s because we are
passing this text dynamically to our set text method. If we wanted to pass our text statically
we need to put that inside double quotation but in here we don’t need to double quotation if we want to say hello to the user, we can say something like this, we can say double
quotation, let’s add a plus in here. And inside these level quotations, let’s say hello. Also
let’s add a space so that we could see everything better. This in here is called concatenation
in programming, adding the two texts together, let’s run the application once again. And
let’s type something in here. Let’s say Mesa by pressing button. This time, you can see
that the text has been changed to allow Mesa our application is now much more interactive.
Okay, this is our challenge, we are going to create a registration form like this, we
are going to get the user’s first name, last name and email. And by pressing this register
button, we are going to show them inside these text views. Let me quickly show what I’m talking
about. So if we have a first name, last name, and also an email. If we click on this register
button, we are going to show them inside these text views to the user. It’s just a simple application so that we can practice everything that we have learned so far. Okay, I want
you to pause the video in here and create the simple application After you have created it, come back to the video and we are going to create this exact application together.
I hope you solve the challenge but if not, that’s totally okay, we are going to create that in here together. Also notice that whenever you run Android Studio, you can see a list
of recent projects in the left panel in here. Okay, let’s move on. And let’s create our project we can say start a new Android Studio project. Once again, we are going to select
this empty activity. Let’s name our project in here. I’m going to name it the first challenge
or Let’s name it registration for you need to specify a package name, mine is fine. Change
the Save location if you want. And also I’m going to stay with API level 19. Okay, let’s create our project. Let’s start by creating our layout file. I’m going to close these
jellified for now. And let’s also minimize this project name. First of all, let’s delete this HelloWorld text, you can click on it and by pressing Delete, you can delete that
in our registration form, we needed to edit texts, let’s add them quickly. In text, we
need to add three plain texts in here. Let’s quickly add them, we will constrain them later
on. Don’t worry about that right now through edit texts. And also we need a button. Let’s
add that as well. We also needed three text views. For the Edit texts.
Let’s also change the IDs. For the first one, I’m going to say Edit Text first name. Let’s
also delete this text and also add something. That’s the first thing. Let’s do the same
thing for the other two edit texts. Sometimes when you’re defining a hint, or maybe a text
attribute for your elements, you can see some annoying dialogue like this in here, you can avoid that by pressing the Escape key on your keyboard. Okay for the button. First of all,
let’s change its ID to let’s say btn register. You can name it whatever you want. Let’s also
delete this text. And let’s say register. Also, you can see that we have this onClick
attribute in here, we can define an onClick attribute for our button from here or like
we did in the previous video in our TextView. I’m going to define that right now in here,
let’s say on Register btn. Click later on, we will create this method for the text views.
First of all, let’s change their IDs. For example, for the first one, let’s say txt
first name. Also, let’s change the text to Firstly, similarly for the other two. Okay,
and let’s greatly constraint all of these elements, we can click on them. For the first
one, I’m going to constraint it to the top edge of my screen. And also both sides of
my screen. Let’s also add a margin top of maybe 100. That seems to be better. For the
next one, I’m going to constraint it to the both edges of the first name edit text, and
also to the bottom of my first name edit text, add a margin top of 24. I’m going to do the
same thing for this email. For the button, I’m going to do the same thing, but I will
probably change the top constraint. Also, if you did a mistake, when you’re constraining
your elements, you can click on the constraint by pressing Delete key you can delete the
constraint, let’s change the top constraint to maybe something like 90 db. For the first
name, I’m going to constraint it to the both edges of my button. Sometimes it may not work
just try once again. And also the top constraint to the bottom of my button. For the top constraint
once again, let’s say 90, I think 90 is fine. Let’s delete its top constraint because right
now we are not seeing our last name TextView. And let’s add it once again. For the last
name, I’m going to do the same thing. Let’s change the top constraint margin to let’s
say 24. Same thing for the email. Okay, this is going to be the look of our application.
And now that we have defined our layout file, let’s switch to our Java file. And to find the behavior of our application, I’m going to close this activity main dot XML just before
everything, let’s just copy this onClick attribute so that we don’t make any mistake when we
define this method in our Java file. In our projects being inside Java folder inside our
package folder inside our main activity dot java file, I need to create my onClick method.
Once again, I can say public void the name of my method, which I’ve copied from my layout
file, inside the parentheses, I need to say view with capital V after that view with the
Lord v. And now I have created my own button click listener. Once again, don’t be worried
about the syntax of this method. We will talk about it later on when we have talked about Java in here. First of all I’m going to define my text views. If you remember we can do something
like this. We can say TextView for the first one. Let’s say txt first name is Call to find
view by ID r.id dot txt. Firstly, I’m going to do the same thing for email. And lastly,
after you have defined your text views, it’s time to define your edit texts. You can do
that like this, we can say Edit Text. For the first one, let’s name it edit text first name is equal to find view by ID r.id dot edit text first name. Once again, I’m going
to do the same thing for edit text, email and also edit text last name. Okay, now we
have access to all of the UI elements that we wanted. Now let’s change the text of our
text views. For example, for the txt first name, I can say something like this, let’s say txt first name. That’s a text. We have seen this in the previous video. Nothing new
in here. Let’s say first name, plus the text of our edit text. Firstly, we can say Edit
Text first name, dot get text.to string. As simple as that. Now, we are setting the text
of our text view to the text of our edit text. Let’s do the same thing for last name and
email. Edit Text last name, that’s a text. Let’s say last name, first of all, was edit
text last name dot get text.to string, edit text email. That’s a text. Sorry, I have put
the wrong element in here. This should be an txt. Last name, not edit text last name.
Same thing for the email, txt email. That’s it text. Let’s say email plus edit text email dot get text.to string. That’s
all we need from our application. Let’s just run it. Okay, it seems like we have done a
good job with the layout of our application. I think it’s just better to decrease the margin top of this register button and also these first name edit text so that we could see
all of them better. Let’s also test its behavior for the first name, let’s say Emma. For the
last name, let’s say Watson for the email, Mr. [email protected] Let’s click on this
register button. And as you can see, we are seeing the text that we wanted. It seems like
our application is working perfectly. In this section of the course we are going to talk
about Java for writing Java applications, we are going to need an ID ID stands for integrated
development environment. Android Studio has been built upon an ID called IntelliJ. Until
few months ago, you could have right and drawn Java applications on Android Studio bought
I think it was around July 2019, that you can no longer do that. And that was because
of a new release in Gradle. I have recorded a video on that exact problem at the time. And it’s on my YouTube channel. If you want to take a look at that just search for makeup
on YouTube and you will probably find a video let me quickly show that this is the video that I’m talking about. Andrew the sudo does not support plain Java code anymore What to
do now. And you can also see my channel name, so make sure to check that if you need. But
basically what I was talking about in that video was that instead of Android Studio, you need to use another ID. The most famous IDs that are available for writing Java applications
are Eclipse and IntelliJ. In this course, we are going to use intelligent because it’s
a lot like Android Studio. And also it’s free. In that video I have talked about how to install
intelligent Id make sure to check that if you need but it’s a lot like installing Android
Studio beside intelligent, you’re going to need JDK Java Development Kit, which we have
already installed on our system, you can get intelligent ID from JetBrains website, let’s
say intelligent ID. The first link from the JetBrains website probably is our needed link.
Just one thing that was mentioning here is that there are two versions, the community version and also an ultimate version, the community version is free. And also it’s enough
for our needs in this course. So make sure to download that and after you have downloaded it and also installed it come back to this video to write Java applications. I have intelligent
installed on my system and I’m going to open it right now you can create a new project
in here by saying create new project. As you can see, the process is a lot like Android
Studio, I have the ultimate version of intelligence ID and for that I can see all of these options.
These might not be available for you if you’re using community version, but no matter what version you’re using, you should see this Java option in here select that in here you
need to specify the path to your JDK. If you have installed JDK with me at the beginning
of this course you should be good in here you can just select the version of your JDK
my version in here is 13. But we are not going to use All of the features of Java 13. Because
Oracle does not support this version of Java for long term, we are going to stay with all of the features of Java eight. Okay, After you have selected the Java and your JDK, let’s
click next, we are not going to create our project from template, so make sure to uncheck
this option. After that, click Next. In here, we need to name our project and also specify
a location for the name of this project. I’m going to say hello world. Also, if you don’t
see these settings in here, you have this more Settings option down in here, make sure
that your module name is exactly your project name. Also feel free to change the project
location if you need, I’m not going to change that in here. Let’s just click Finish. And let’s create our project. As you can see, intelligent ID is a lot like Android Studio.
And the reason for that once again is that Android Studio has been built upon this ID.
Let’s just close this tip dialog. And let’s switch to full screen mode. And let’s go to full screen mode. Once again, in IntelliJ ID, you can see this project pane you can
minimize and maximize that you can also move through the folders right now because we didn’t
use any template our folders are empty, we can create our project like this by clicking
on the source folder by right clicking on that selecting new package, you can specify
a package for our application. In this case, I’m going to say for the maker
dot let’s say hello world. And inside this package, we can create our Java classes Don’t
worry about the term class that are used. We are going to talk about that in object oriented session. But for now just know that we are going to need at least one Java class
in order to run our Java applications. By right clicking on our package and selecting
new Java class we can create our class Let’s name this class. Hello. As you can see, a
new Java class has been created in our package. And also we have few lines of code. Let’s minimize our project pane. Let’s focus on this Hello dot java class. Basically what
we have in here is the package that we are currently in which is orb dot maker dot HelloWorld.
And also we have the declaration of a class once again, we are going to talk about classes later on in future videos. In order to have a start point for our application, we are
going to need at least one method and that method is called the main method. In order to create that method we can type p SVM. By pressing tab or enter, we can have that method.
As you can see, this green triangle has been added in here it means that now we can run our application from this point, we will talk about the meaning of all of these new keywords
later on in the course. But for now, just know that in order to run your Java applications,
you need at least one Java class in which has one method called main this main method
is like the onCreate method of our activities. Now we can write our codes inside this main
method. For example, in order to print something to the console, we can say something like this we can say s o UT or south and by pressing tab or enter you can see this line of code
System dot out dot print line. Or alternatively, you can type this manually. Instead of using
the shortcut, you can say system, notice that the s should be in other cases, make sure
to use the upper case for us. Let’s say system dot out. Basically this out means output in
here it means that we are going to add 14 something in this case we are going to print something later on we will see that we have an in as well in which we will get the user’s
input. Let’s say System dot out dot print line, you can see that we have print and print
line. For now we are going to use the print one we will talk about the difference later on. In order to print something to the console, you need to pass it inside the double quotation,
you can say something like this. For example, let’s say hello world. Now if you have the
simplest Java application that can be written, let’s quickly run our application. In order
to run our Java application for the first time, we need to right click inside the curly
braces of our main method and select Run hello.me. Let’s do that. As you can see, we are printing
hello world into our console. Sometimes if it’s the first time that you’re running Java applications, you may get some errors in here. In the most cases those error would be gone
by rebuilding your project, you can come to this built up above in here and rebuild your
project. After the rebuild is finished, you can run your application once again by right clicking and selecting run. Hello, that’s me. In most cases, the error should be gone.
But if you had any other problem, please ask them in the q&a section of the course also
notice that for the rest of this section, we are not going to have any user interface it is possible to have a graphical user interface GUI for your Java applications but because
this is an Android course and later We’ve talked about user interface in Android applications,
we are not going to use any user interface for our Java applications, we are just going to understand the language. Okay, now let’s minimize our round pain from here. And let’s
talk about the difference between print and print line. The difference between print and print line is that when you are using print line, after printing your text, the cursor
will move to the next line. That is not the case with print. In order to see the difference,
let’s say System dot out dot print. Once again, let’s pass our hello world text. And let’s
run our application to see the difference. Also, if it’s the first time that you’re going to run your application, you can right click inside the main method and click on this run
Hello dot main. Or if it’s not the first time, you can click on this green triangle at the
left or the green triangle. On the top of your screen, you can also see the shortcut for Windows is shift plus fn. If you’re using Mac or Linux, we can hover over this green
triangle and see the shortcut. Let’s run the application and see the difference between print and print line, you can see that the text has been printed, but the cursor never
moved to the next line. If we switch these two lines of code, we can see the difference. But
let’s switch now. Now print line is the first line and print is the second line. Let’s run
the application once again. This time, you can see that the two text has been printed in two separate lines. Okay, now that we know the difference between print and print line,
let’s talk about variables in Java. But before I do that, I’m going to comment these two lines of code, you can comment them by pressing Ctrl plus slash when you comment some code
in Java, it means that you are telling the compiler to ignore these two line of code. The use of comment is for developers to remind themselves or other colleagues of something
important. For example, in here I can say slash slash, let’s say do something, this
line of code will be completely ignored. The other kind of comments in here is like this, we can say slash slash to do after that we can say do something. If we use this kind
of comment with to do at the beginning of your comment, you can see that first of all the color will be changed to blue. And also if you click on the to do tab in here, you
can see that you have one to do in your file. It’s very helpful when you’re working with multiple programmers. By clicking on this to do everyone can see the list of different
tools. There are more to comments, we will talk about them later on in the course. But for now just know that when you’re commenting something that line will be completely ignored.
Okay, now let’s talk about variables. Variables are useful for when you want to store simple
data in your job application. For example, if you want to store a number or a text variables
are useful for when you want to store some small data in your Java application. For example,
if you want to store a number or a text, if you want to store a number, you will do that like this. You can say int int stands for integer basically inside an integer, you can
store whole numbers for example, negative and positive numbers plus zero. And the way
to define them is like this. First of all, you type int after that, you need to name your variable. For example, in here, I can say number this name in here is optional.
And you can use whatever you want. After that you can assign a value to your variable. For
example, I can say five in here, this is the data type of my variable, this number is the
name of my variable. And this five is the value of my variable beside positive numbers,
you can also have negative numbers. After you have stored your variables, you can do
some operation on them, for example, you can print them and the way to do that is like this, you can say System dot out dot print line. And after that you can pass your number
or whatever is the name of your variable. Notice that in here I haven’t used double quotation. Basically, whenever you are passing some text explicitly, you need to pass the
devil quotation but whenever you’re passing your variable, you shouldn’t pass any double
quotation, let’s run the application and see if we can see our number. You can see that the value of our number is negative five in this case, but inside integers, you cannot
have decimal values. For example, in here, I can say 5.3 previoulsy, other kinds of variables
in which you can use in order to store decimal numbers, but more on that in a few minutes.
The other kind of variable that you can use in order to store whole numbers is long. Let’s
see that as well. We can see a long, you can name it my long is equal to once again, let’s
say five. Let’s also print that, let’s say System dot out dot print line and let’s pass
our log. If we run the application, you can see five down in here. The difference between
long and integer is that you can store larger numbers inside a lung. And also a long variable
will store more spaces in your device’s memory ram to be exact inside an integer you can’t
store numbers as large as the number of two to the power of 31. But inside a long, you
can store numbers as long as the number of two to the power of 63. That’s the difference
between long and integer. But what if you want to store decimal numbers floating point
numbers for that you have other kinds of variables, the first one is a double, let’s see that
we can say double, let’s name it my double. Let’s assign it the value of 4.5. Like before
we can print it, let’s pass our data. If we run the application, we should see the number of 4.5 printed, you
can see that down in here. So double is one kind of variable in which you can use in order to store floating point numbers. The other option is to use float. Let’s see that as
well. We can say float, let’s name it my float. Once again, the names are optional, you can
name your variables, whatever you want, we can say 4.5 in here once again. But right now you can see that we are getting a read error in here. And that’s because floating
point numbers by default are double in Java, it means that if you want to define a float
number, you need to cast your number to float, the way to cast your number is like this.
After the equal, we can open and close a parentheses and inside the parentheses you can say float
by casting, you will explicitly tell the ID that this is going to be a float number and
not a double number. Okay, let’s quickly print this as well. Let’s pass our float. And let’s
run the application, you can see that once again 4.5 has been printed. Once again, the difference between float and double is that inside a float, you can store smaller numbers.
And also the other difference is in the space that each one of these are going to occupy
in the device’s memory. Basically double view occupying more space, we won’t be using floats
that much in this course, I just wanted to show you that float does exist in Java. Okay,
now we have seen numbers. But what if we want to store different characters in Java, for
that we have a special kind of variable which is char, for example, we can see char, my
char is equal to, let’s say M. Notice that when I’ve assigned this character to my char
variable, I have used this single quotation that wasn’t the case when we have worked with different numbers. When you’re assigning a character, you need to use the single quotation
like before we can print our character. Let’s pass our character. And let’s run the application.
You can see em printed in here. When you’re using char variable, you can’t have multiple
characters. For example, in here, I can’t have ma you can see that we are getting an error. In order to store multiple characters, we have another kind of variable in which
we will talk about that in a minute. But before I do that, I’m going to show you just one more thing. You can also store something like this in your character, you can say backslash
u 00. A, this is a special Unicode value for registered symbol. Let’s run the application
and see what would be the result, you can see that the Registered Trademark has been printed into our console. When you’re using a char variable. You can also assign Unicode
values as well. You can search for different Unicode values. For example, if we want to show the copyright symbol, we can say something like this, we can say backslash use 00 a nine.
If you’ve run the application, we should see the copyright symbol Feel free to search for other Unicode values on the internet. Okay, now let’s talk about the string. If we want
to restore multiple characters, we can use a string, we can say a string with capital S. After that, like before, we can name our string, let’s say my string or Let’s name
it name is equal to Mesa meaning like before, if we want, we can print our string. Let’s
fascinate and let’s run the application, we can see that Mesa has been printed into our
console. But you may have noticed that there is a difference in here, the color of this string is not blue, like the other kinds of variables. That’s because of a very important
difference between a string and other kinds of variables. The other kind of variables that we have talked about so far are primitive data types. But a string is a class in Java,
do we have talked about classes and objects in Java, but for now, just know that when
you are using a class, you can have some operations on that. For example, if you type name, you
can say dot and you can see a list of different available methods. We will talk about few
of them later on in the course. But you can see that there are a lot of them. So the difference in here is that the string is a class, but others are primitive data types. There is
one more kind of variables that I’m going to talk about in here, and that’s called Boolean. Let’s see that as well. We can say Boolean, my Boolean, let’s say is equal to true when
you’re using a Boolean, that Boolean can have only true or false values. These are the only
two values that a Boolean can have. Once again, like before, we can print our Boolean. Let’s
pass my Boolean in here. And let’s run our application, you can see that true has been printed. As I said, Our Boolean can have false values as well, let’s run the application.
False has been printed Boolean is very helpful when you are working with conditional statements.
We will talk about conditional statements in the next video. Okay, this was our talk about different variables in Java, there are a few more of these in which you can check
out the Oracle documentation, but probably you’re not going to need the others. These
are all that we are going to use in this course. Okay, now that we have talked about variables, let’s quickly talk about arithmetic operators as well. But before that, I’m going to comment
all of these lines of code, because these can be distracting. The reason that I’m commenting
these lines of code and not deleting them is that I’m going to upload the source code so that if you need, you can check the source code. Okay, let’s move on down in here, let’s
quickly define two new integers, I can say int a is equal to six. After that, let’s say
int b is equal to let’s say two. You can use operators in Java, in order to perform some
kind of operation on your variable. We have different kinds of operators. The first one
that we’re going to talk about in here is called arithmetic operators. Let’s quickly see them. For example, here I can say, answer is equal to a plus b. This plus is the first
arithmetic operator that we are going to talk about thinking, this is going to add the value of B to A,
let’s quickly print our answer. And let’s run our application, you can see that the
answer is eight, it has added B to A. The other kind of arithmetic operator that I’m
going to talk about in here is minus which you can guess its value, it’s going to deduct
B from a, the answer in here should be four, we can see that four has been printed. The
other one is the star, which is used for multiplying the two values together in here, the answer
should be six times two, which is going to be 12. Let’s run the application. And you
can see 12 in here, the other one is slash, which is used for division, we are going to
divide six by two, let’s run the application, we can see that the answer is three. The thing
that you need to be extra careful with this a slash operator in here is that the second number V in this case should never be zero. Let’s quickly change the value of b to zero.
And let’s see what will be the result. Let’s run the application, you can see that we are getting a read error in here we are getting an arithmetic exception. We will talk about
exceptions later on in the course. But for now just know that whenever an exception occurs,
our application crashes. Certainly we don’t want that behavior in our application. So be careful about the second number. Or alternatively, you need to catch the exception that you think
that might occur. We will talk about exceptions later on in the course we have one more kind
of arithmetic operator in Java, and that’s called remainder. Let’s see that as well.
It’s the person so the remainder operator is going to give us the remainder of a divided
by b. In this case, if we divide six by two, the answer is three. And its remainder is
zero. So right now if I run the application, I should see zero. You can see that zero has
been printed down in here. But if I change the value of a to, let’s say five. Now the
remainder of five divided by two is one. Let’s see that if we can see one. Yes, it has been
printed from time to time this remainder operator can be useful as well. There is one more thing
that I need to talk about the slash operator the division operator right now we can expect
that if we divide five by two, we should see 2.5. Let’s run the application and see if
we can see that we can see that we are getting two as the answer. And that’s because we have
saved the answer inside an integer. If you remember at the beginning of this video, I said that integers can only contain whole numbers. If you want to have floating point
numbers, you can use something like double. But this in here is not going to work as well,
because the answer itself is an integer. If you hover over the highlight, you can see
that a divided by b is an integer. If you want to cast your number to a double after
the equal sign, you can open and close parentheses and inside the parentheses you can say Java,
this way you have cast an integer to a double. Now if we run our application, you can see
that we are seeing 2.5 so if you want to see the exact value of division use doubles. Let’s
just talk a bit more about the plus operator. After that finish off this video. So right
now if I use plus in here, you can guess the answer, five plus two is seven. Let’s run
the application. We can see seven down in here, we can also use this plus with different
texts. For example, if I have to text in here, let’s say string first name is equal to, let’s
say, Emma. Let’s also define another text. Let’s say last name is equal to Watson, we
can use the plus operator to add these two text together. For example, let’s say a string.
Full Name is the call to first name, plus our last name. If we print our full name,
you can guess what would be the result. In here we can see Mr. Watson. Let’s also add
a spacing here in order to see everything better. Mr. Watson has been printed. This
in here is called concatenating in Java, adding two text together. So you can see that this
plus operator have two usages, it can be used to concatenate two text together or whenever
we use it with different numbers, it will act as the addition operator. We are going
to talk about more operators in Java. Specifically, we will talk about relational and logical
operators. Before doing that, let’s quickly create our project by selecting create new project by selecting Java and clicking on next. And next once again, we can name our
project. Let’s name this project operators. Let’s create our project. And inside the source
folder, let’s create our package. Let’s say new package. Let’s name it orc. That’s my
code dot operators. Inside our package, let’s create our main class. Let’s name this class
main. Inside our main class, we are going to need our main method. Let’s type PS VM.
By pressing tab, we have our main method. Before I talk about relational and logical
operators. Let’s quickly talk a bit more about arithmetic operators. If you remember from the previous video, when we have to find a new integer, for example, a and we assign
a value to that we could have say something like this, we could have said in answer is equal to a plus two. Or instead of defining a new integer, we can say a is equal to a
plus two, it means that increase the value of i by two, let’s print a and see what would
be its result. Let’s run our application because it’s the first time I’m going to say run main
dot Hello, you can see that the value of a is seven, we can simplify this line of code,
we can say something like this, I’m going to comment that, and in here, I can say a
plus equal to let’s also add some spaces, these two lines of code are exactly the same,
you can use plus equal in order to simplify the line six, let’s run our application. Once
again, you can see that the result is the same. Also, if we want to increase a by one,
for example, if you want to say a is equal to a plus one, we can do something like this,
we can say a plus equal one. Or alternatively, we can say a plus plus, that is possible as
well, this a plus plus is going to increase the value of A by one. If we run our application,
we should see six as the value of a you can see six down in here. Alternatively, we can
use minus as well, we can say a minus minus it will deduct one from the value of a, if
you’re on our application, we can see four in the console. Similar to that we can say
a minus equal to let’s say two. For example. In this case, if we run our application, we
should see three as the value of a like minus equal You can also have multiplied equal and
also divided by equal it means that divide a by two and store the value or a store the
answer inside a this sentence in here is the simplified version of this line of code that
I’m going to write we can say a is equal to A divided by two, both of these lines are
the same. If you want to make sure of that you can delete this line and you can run your application. five divided by two is 2.5. Because we are saving that inside an integer, the
answer should be two. So we should see two as the value of our a and you can see two
down in here. Before talking about logical and relational operator. I just wanted to talk about these simplifications. Okay, now let’s talk about relational and to be specific
comparison operators in Java comparison operators are useful for when you want to compare two
things. For example, two numbers. Before that, let’s quickly define two numbers. I’m going to say int a is equal to five After that, let’s say int b is equal to, let’s say three.
After that we can say Boolean answer is equal to the first comparison operator that I’m
going to talk about in here is greater than operator. And the way to define that is like this, we can say, a greater than B, I have saved the result of this comparison inside
a Boolean, this left anchor bracket in here means greater than operator, it means that
if a is greater than B, assigned the value of true to this answer, right now, if we print
our answer we should see through, let’s quickly do that. Let’s see answer. And let’s run our
application, you can see that answer is true, because A is five and five is greater than
three, the other kind of comparison operator is less than, and that’s this right angle
bracket, right now, a is not less than b, so the answer should be false. If we run our
application, we should see false in the console. And we can see that the other kind of comparison
operator that I’m going to talk about in here is useful for when you want to compare the equality between two integers. And that operator is these two equal signs, right now, a is
not equal to b. So if we run our application, we should see falls into our console. Let’s
run our application. And here we can see false. Remember that when we are using two equal
signs, it means that we are checking for the equality of two number. But when we are using a single equal sign like in here and above, in here, we are assigning a value to our variable.
Let’s change the value of b to five. To see that if this is going to be true, let’s run the application. Now you can see that the answer is true. We have other kinds
of comparison operators as well. One of them is greater than equal. When we use this left
anchor bracket. And also this equal sign, it means that check if a is greater than B,
or if a is equal to b, right now, a is equal to b. So this answer should be true. If we
run our application we should see through, you can see that down here we see true. Also,
if A is five, and B would be something like three, if you run the application, we should
see through once again, we can see through. But if we change the value of a from five to something like two, because none of these condition is true, right now, we should see
false, let’s run our application. And we can see false, the other comparison operator is
less than equal. And we use that like this, we can use this right angle bracket and equal
sign this in here means that check if a is less than b, or if a is equal to b, right
now, a is less than b. So the answer should be true if we run our application we should see through. But if we change the value of a to something like five, we should see false.
And we can see false. Beside these, we also have another kind of comparison operator.
And that’s this explanation Mark plus the equal sign this in here means that check if
A is not equal to b, this exclamation mark in here as a negative meaning to our equal
operator right now, a is not equal to b. So the answer should be true. If we run our application
we should see through and through has been presented. Okay, these were the comparison
operators that I was going to talk about, feel free to practice them because we are going to use them a lot in our course. Next I’m going to talk about logical operators
in Java. But before that, let’s quickly comment these two line of code down in here, let’s define a new Boolean, let’s say Boolean answer is equal to let me write the syntax and I
will talk about it, we can say a is equal to five, or let’s say B is equal to something
like two. This is our first logical operator this in here means or you can add these two
pipelines by using shift plus backslash backslash is somewhere above the enter in English keyboard.
So in here, this OR operator means that if a is equal to five, or if b is equal to two,
assign through to this answer, if none of this condition is true, so assigned false
to this answer, right now, a is equal to five at least one of our conditions are true. So
the answer should be true. So if we print our answer, we should see through, let’s run
the application. And you can see through down in here, if we change the value of a for example
to six, because none of these conditions are true, we should see false. Let’s run the application.
And then here we can see false. The other kind of logical operator that I’m going to talk about is to add science. Let’s quickly see that as well. This in here is an operator
it means that assign through to answer if both of these conditions are true, if A is
equal to five and also if B is equal to two if one of these conditions is not true, so
assign false the answer right now, none of these conditions is true. So we should see false as our answer. Let’s run the application. And as you can guess, we see false. If we
change the value of a to five, and run our application, once again, we should see false,
because the second condition is not true. But if we change the value of v2, and run
our application, this time we should see through and we can see through them here. And these were the two kinds of logical operators that I was going to talk about or and and now that
we have talked about operators that we wanted, let’s talk about conditional statements, I’m
going to comment these two lines of code. The first kind of conditional statements that I’m going to talk about is called if statements, we can type something like that we can say
if a pair of parentheses inside the parentheses we need to provide our condition. For example,
we can say if a is greater than three, after the parentheses we need a pair of curly braces
inside the curly braces, we can put our code for example, let’s say a is greater than three.
So in here, we are saying that if our condition is true, execute the code inside the curly
braces. If it’s not true, jump to after the curly braces. For example, in here, let’s
say continue. Right now a is five, and it’s greater than three. So we should see this
text printed, let’s run our application, you can see that a is greater than three. But
if we change the value of a to something like two and run our application, you can see that we are no longer seeing this text in if statements
we can also have multiple conditions. For example, in here, I can say else, I can provide
another pair of curly braces inside these curly braces, I can say something like this, I can say a is less than three. Right now a is two, a is less than two. So we should
see only this text printed, let’s run our application, you can see that a is less than
three. As for the condition, you can provide your condition like this directly inside your
if statement parentheses or you can define your Boolean before your if statement. For
example, in here, if I say Boolean answer is equal to a greater than three. Now I can
pass my answer. inside these parentheses, I can say if answer is equal to true, let’s
minimize this ramping for now. This is going to work the same path. If you hover over the highlighting here, the intelligent ID is warning me about something the first one is that answer
equals equals to true can be simplified to answer it means that I can delete this part,
this is going to work exactly the same. If we run our application, we should see the same result a is less than three. Let’s run our application. And you can see a is less
than three. If we want to negate this answer, we can add an exclamation mark before our
variable. For example, in here we can say if answer is not equal to true, if we run
our application, we should see the first line, we can see that a is greater than three. This
is not true because A is true. But in here we have changed the meaning of this brilliant,
we have added a negative meaning. Let’s delete this for now. And let’s run our application once again. Besides else, you can also have an else if as well. Let’s briefly comment
this if statement. And let’s write another one. down in here I’m going to see if inside
this if statement. Let’s check that if a is positive or negative, I can say if a is greater
than zero, inside the curly braces, let’s print as positive. In here instead of else,
I can use something like this, I can say else if now we need to put another condition in
here we can say if a is less than zero. Once again, we need a pair of curly braces. Inside
the curly braces, we can say a is negative. You can have as many elsif cases as you want.
For example, I can add another one in here. But of course in here, I don’t think this
if statement would mean anything. But you can have as many as you want. But in every
if statement you can have at most one else case, let’s delete all of these. For example,
I can have only one l space this else is going to be the default case. In this case if we
use this else, it means that a is not greater than zero. And also it’s not less than zero.
So it is zero. In here we can safely say a is zero. This is going to be the default case
for our if statement right now A is true. So we should see a is positive. Let’s run
our application. And as you can see a is positive. Let’s change a to minus two. If we run our
application, you can guess it, we should see a is negative. And we can see that down in here. If we change the value of oil to zero, we should see a zero. And as you can see,
we can see that down in here. So if statements are one kind of conditional statements, we
have another one as well, let’s comment all of these, the other one is called switch statements.
Let’s see that as well, we can say switch inside the parentheses of the switch statement,
we need to provide the variable that we want to switch on. For example, I can pass a in
here, after the parentheses, I can provide a pair of curly braces inside the curly braces,
I can define different cases. For example, I can say in case a is equal to one after
that we need a column. And in case a is one, we can print something for example, we can
see a is one. Also, it’s very important to provide a break after every case, let’s do
that. I will talk about this break in a minute. But for now, just know that after every case,
you are going to need a break, we can define as many case as we want. For example, in here,
I can say in case a is to screen something, let’s say a is two. After that we need a break,
we can define another case. For example, for three, let’s say a is three. In every switch
statement, we are going to need a default case as well. And we can pass that like this
we can say default. And we can print something like a is not one two, or three. In the default
case, we need the break as well. Let’s quickly add that, right now the value of a is zero.
Let’s change that to two. And you can guess it we should see a is two, let’s run the application.
And you can see a is two, if we change the value of a two once again zero and run our
application. You can see a is not one, two or three. Let’s also talk about this break
in here. But before that, let’s quickly change the value of eight to one. This break in here
will cause our switch statement to break. For example, if we don’t have this break in
here at the first days, which is a is equal to one. And if we run our application,
you can see that even though a is one we can see both sentences, we can see a is one plus
a is two in here. Because we don’t have any break, we didn’t break out of the switch statement.
And we have jumped to the second statement. So don’t forget to have a break after each
case in your switch statement. We are going to talk about loops in Java. But before that,
let me just say that I have created my project, I have added this package inside my source
folder. Inside this package we have only one main class in which has this one main method,
I hope that you can come to this point by yourself. If not, please watch the previous
videos on how to create a project and create your main class and your main method. In programming
loops are useful for when you want to do a repetitive job. For example, if you want to
print something 10 times like this in here. If you want to print this Hello 10 times you
can just copy and paste it 10 times what in general, it’s a bad idea that you copy and
paste yourself whenever in programming you’re copying and pasting know that you’re doing something wrong. Instead of copy and pasting we are going to use loops in this video. The
first kind of loops that I’m going to talk about are for loops. Let me write it and we
will talk about its syntax. This is the general syntax of a for loop first of all we write
for. After that we have a pair of parentheses inside the parentheses we have three statements
in the first statement we are defining a new integer called oil you can name your integer
whatever you want, and also we are initializing our AI to have a value of zero. The second
statement is the condition of our for loop we are going to continue this loop until this
condition is not met. And the third statement is just a statement for changing the value
of i every time that we look PVC inaction what all of these mean after the parentheses
we have a pair of curly braces in which we can contain our code inside them. For example,
if we want to print something like hello here, we can include that inside this pair of curly
braces. So what this for loop is going to do is that first of all we are defining a new integer called AI and we are assigning the value of zero to that integer. After that
we are checking our condition we are checking that if i is less than 10 in the first time
that we are going to look i is Hello So this condition is met. If this condition is true,
we are going to execute the code inside the curly braces. In this case we are going to print hello. After printing Hello, we are going to execute the third statement. In this
case, we are going to increase the value of i by one. For the next time that we are going
to loop first of all we are going to check our condition we are checking that if I is
still less than 10 or not. For the second time I is one, I still is less than 10. So,
we are going to execute our code. This circling here is going to continue and continue until
our condition is false. When it’s false, we are going to break out of the photo. So in
this case, we are going to print hello 10 times, let’s run our application. And let’s see if we can see Hello 10 times, you can see that we have printed Hello 10 times it
seems to be perfect. Instead of 10. You can put any number that you want in here, for
example, you can say five, if you execute your program, we will see that Hello would
be printed five times, instead of this five in here, you can pass a variable for example,
if you had a variable before this for loop, if we had int a is equal to five in here,
later on, we can pass a instead of five to this photo. For example in here instead of
five, we can say if you run the application, we should see the same result. Inside our
for loop we can also have access to the eye itself. For example, instead of printing Hello,
we can print by, let’s quickly paint that as well. And let’s run the application, you
can see that we are printing the numbers zero to four. Okay for loop is one kind of loops
in Java. The other point is my loop. Let’s briefly see that as well. Once again, I’m
going to write the syntax of while loop and we will talk about it after that.
This is the general syntax of a wild loop. First of all, we type void. After that we have a pair of parentheses inside the parentheses we have our condition, in this case, a less
than 10. After that we have a pair of curly braces and inside the curly braces we have
our code. In this case we are going to print hello, notice that we are not changing the
value of a so this condition is always true. And if I run my application right now, we
are going to stuck in an infinite loop. Let me quickly run the application and see what
an infinite loop looks like. But before that, let me comment this for loop, you can see
that no matter what happens, we are inside an infinite loop we are printing Hello indefinitely.
In some cases, this might be our desired behavior. But in most cases, we probably do not want
that we can exit our application by pressing on this red button, we can also check our
exit code which is negative one in a normal application, we would get zero. Okay, but
how can we exit a wild? There are multiple ways for that the simplest way is to change
the value of a inside the void loop. For example, after printing Hello, I can say a plus plus,
in order to increase the value of a so that it won’t be the same every time that we are
going to loop through this while loop. So for example, for the first time a is five,
so our condition is true. So we are going to execute these two line of code, we are going to loop through the cycle until a is 10. when that’s the case, this condition is
no longer met. And we are going to break out of the Void loop. Let’s run the application
one more time. This time you can see that we have only printed Hello five time. And
also this exit code is zero which means that we have exited our application successfully
without any error. The other way that break out of avoid loop is to add a break. Let me
quickly add that this time after the first loop we are going to break out of the while
loop it means that we are going to print hello only once. Let’s run the application. And
in here you can see that we have printed Hello only once inside while loops we have another
keyword called continue. Let’s quickly see that as well. I’m going to delete this break
and before this print statement, I’m going to put an if statement. Let’s say if A is
equal to eight. Then let’s continue. This continuing here means that go to the next
record of your while loop go to the next round of looping. It means that no matter what is
the rest of the code inside the viable break out of the while loop and go to the next record,
so for example in this case, if A is five, six or seven we are going to print hello.
But if a is eight you’re not going to print hello. After that we are going to print hello
in case if a is nice. If you don’t want to stuck in an infinite loop, you also need to
change the value of a software inside the while loop. For example before this if statement,
I can say a plus was right now if I run my application, I should say hello only four
times, because if a is eight, we are going to continue the loop, it means that we are
going to jump to the next record and we are not going to print hello, let’s run the application
and see if we can see Hello four times. And as you can see, we are seeing Hello four times.
So continue is a useful keyword for when you want to jump to the next record. Okay, now
that we have talked about why loop let’s also talk about another kind of loops in Java,
the value of A in here is five. If we change our condition to let’s say a less than five,
we know that this condition is no longer true. So we are not going to go inside the violin,
it means that we are not going to run the code inside the void loop. Let’s run the application,
we shouldn’t see any Hello printed into our console. And as you can see, we can’t see any Hello. But in some cases, you may want to go inside the while loop at least once
no matter what the condition is. For those situations, you can use do while loops. Let
me comment this while loop and let’s create a divide loop. It’s a lot like via loops,
you need to type do inside the curly braces, you need to put your court for example, we
can say System dot out dot print line. And you can print hello, after the curly brace,
we need to say why. And inside the parentheses you need to specify your condition for example,
we need to say a less than five, after the parenthesis you need a semicolon, of course,
in this case, because A is equal to five, this condition here is not true. But even
with that condition, we should see this Hello at least once because we are using two wild
loops. Let’s run the application. And let’s see if we can see Hello. As you can see, we
can see Hello only once. So do while loops are useful for when you want to execute your
code, at least one no matter what the condition is. We also have another kind of loops in
Java called for each loops. But I’m not going to talk about that in here. Because I think
that if we know about different arrays and collections, and in regard to that if we know
about object oriented programming, we will understand it much better. So I’m going to
talk about for each loops after we have learned about all those stuff. Okay, I think that’s
enough for loops. Now we are going to have a quick challenge. But before we have our
challenge, we need to know about two more things, we need to know how to get the user’s input. And also we need to know how to generate a random number. Later on, we will see that
how these two are going to be useful. In order to get the users input, you can use something
called a scanner. The syntax in here might not be familiar to you because we haven’t
talked about object oriented programming. But we are going to need that in our challenge.
Because we want to have an interactive challenge. If you don’t understand the syntax, just copy and paste the code that I’m writing in here. In order to define a scanner, you can type
something like this, you can see the scanner with capital S, notice that it’s coming from Java dot util package. After that, you need to name your scanner, which I’m going to say
a scanner, after that you can say is equal to new scanner. Once again, with capital S
inside the parentheses you need to say system.in we have seen system dot out which was useful
for outputting something system that is useful for when you want to get the user’s input.
Now you have a scanner, you can get a number from user like this. You can say int answer
is equal to a scanner, dot next int this one down here by saying a scanner dot next int
our scanner is going to wait for the user’s input until the user enter some number. We
will see that in action in a minute. But before that, let’s quickly output something. For
example, let’s say answer was plus the value of answer. Also, let’s type something before
our scanner. Let’s say please enter a number. Let’s run our application. And let’s see what
these few lines of code are going to do. You can see that we are saying please enter a
number text. And in here console is waiting for the user to enter a number. For example,
I can say five by pressing Enter. You can see that answer was five. And we have exited
our application successfully. So by using a scanner, you can get the user’s input. Besides
integers beside number, you can also get a text from user if you want. Let’s quickly
see that as well. First of all, let’s go Something, let’s say enter your name. After that, I can
say something like this, I can say the string name is equal to scanner dot next, you can see that this next method is going to return a
string. Don’t worry about all of these new stuff, method, scanner classes, object oriented
programming. We will talk about all of these later on. But for now, because we are going to need the user’s input, we are going to figure out a way to receive that. So by using
scanner dot, Next, you’re going to get the user’s name. And after that, let’s create
something. Let’s say hello, close the name of the user. Let’s run our application once
again. In here, first of all, we need to enter a number. Let’s add four. After that the scanner
is waiting for my name, let’s say Mesa. And we can say hello Mesa message. So this way
we can get the user’s input. Let’s also see how we can generate a random number. I’m going
to comment these lines of code for now, in order to generate a random number, you can say something like this, you can say random with capital R, let’s name it random, is equal
to new random. And we don’t need to provide anything inside the parentheses. After that,
in order to generate a random number, we can say something like int number is equal to
random dot next int, this method in here is going to generate a random number. let’s print
that number. That’s the number plus the number. Let’s run our application. Once again, you
can see that we have this random number. It’s also useful to specify a domain for your random
number. Right now, our random number is this weird negative number, it might not be useful,
it’s good to define a domain. In order to define that domain, you can pass a number to this next int method. For example, if I put 20 in here, and if I run my application,
once again, the number should be somewhere between zero and 19. If you want your random
number to be somewhere between one to 20, you can simply add one in here, we can say
plus, but let’s run the application. Once again, we can see mine this time. This numbering
here is completely random. It means that every time that we were on our application, we should see a different number, you can see that we are generating different numbers. Okay, now
that we know how to get a random number and also how to get the user’s input, it’s time
for our challenge. Here is our challenge we are going to create again, the concept behind
this game is to generate a random number and ask the user to guess that number. continue
asking the user until you receive the correct number. Also, to make everything more fun.
After five times of guessing gronke show a game over message to the user. Here is how our game is going to work. First of all, we are going to show a welcome message. After
that we are going to ask for the user’s name. And we are going to say hello to the user.
After that, we are going to ask for the user’s permission to start the game. If you received
a positive answer, we are going to generate a random number and we are going to ask for
the users guests. If the guest is correct, we are going to show a congratulation message
and quit the game. If the guest is wrong, we are going to ask again until we receive
the correct number. Also, as a hint to the user beside the first time, every time that
you’re asking for a number tell the user to guess higher or lower. For example, if the
random number is 10. If the user has guessed seven for the first time, tell him to guess
higher if the user failed after five times, show a game over message and quit the game.
This challenging here is the combination of everything that we have learned so far. So make sure to practice before watching the next video, it’s going to help you a lot.
Okay, go solve the challenge. And after that, come back to the next video so that you see
my solution for this challenge. See you in the next video. I hope you solve the challenge
but if not, that’s totally okay. After all, it’s your first encounter with Java. And it’s
the first application that you’re writing. In this video, we are going to solve the challenge together. Let’s start by creating a new project. Like before, I’m going to select Java next.
Once again next, let’s name this project gets me going. Let’s create our project. And inside
our source folder, let’s create our package and Java file. Let’s name this package or
that may code that gives me a new Java class. Let’s name it main. Inside our main class, let’s type
p SVM and now we have our main method. Okay, first of all, we are going to show some welcome
message to the user. So let’s say System dot out dot print line, let’s say welcome to Wonderland.
After that, we are going to ask for the user’s name. So let’s say Please enter your name,
or marry have your name. After that, we need to receive the user’s name, in which we need
a scanner for that. Let’s create our scanner. Let’s say scanner is equal to new scanner
system.me. After that, let’s say string name is the call to the scanner dot next, and let’s
say hello to the user. Let’s say hello, plus the name. After that, let’s ask for the user’s
permission to start the game. Let’s say Shall we start? in here? I’m going to give the user
two option. And here are my two options. Let’s say one, yes. And the other, let’s say no.
But in order to format my text a little bit better, I’m going to add a special syntax
in here in which you haven’t seen so far. By adding backslash T. I’m adding a tab before
this. Yes. So this will be formatted with a tab. Let’s add one in here as well. Nothing
is special Just for formatting my text a little bit better. After that, I’m going to save
the user’s answer. Let’s say eight, begin answer is equal to the scanner dot next int.
And after that, I’m going to create a while loop. Let’s say vine begin answer is not equal
to one, we are going to continue asking our question. So let’s copy these three line of
code and paste them inside our while loop. After that inside the void loop, we also need
to store the new users answer for that we can say begin answer is equal to the scanner
dot next int. This way, if you are not going to break out of this while loop until the
users answer is valid, when it’s fun, we have the permission to start the game. After the
void loop, it’s time to generate a random number, let’s say random, random is equal
to a new random. After death, let’s say int x is equal to random dot next int for the
domain, I’m going to say 20. Because I want my number to be from one to 20. So I need
to add a plus one in here. After that, it’s time to ask the user to guess a number. So
let’s say please get a number. And in here, we need to save the user’s answer.
Let’s say int, user input is equal to a scanner dot next int. In here, I’m going to define
three new variables and their usage as will be clear in a minute. Let’s quickly define
them. First of all, I’m going to save the times that user has tried the game. So let’s
say eight times tried is equal to zero initially, after that, let’s define a Boolean, let’s
say Boolean. As well, initially, it’s false. And after that, a Boolean indicating if we
should finish the game. So let’s say should finish. Once again, it’s going to be false.
Initially, After defining these three new variables, I’m going to create another vial of let’s say, why should finish is not equal to two.
Then, inside the while loop, first of all, I’m going to increase the times that user has right.
So let’s say times three plus plus, after that, I’m going to create an if statement.
Let’s say if times three is less than five, then we have the permission to continue. If
it’s more than five, in the else case, we need to break out of this while loop. For
that, I’m just going to say short finish is equal to true. This is going to be the last
line of our while loop. So if we look once again, because this condition is no longer
met, we are going to break out of the while loop. But in case the time stride is less than five, we are going to check that if the users input is equal to the randomly generated
number for that I can say if user input is equal to x, then we need to change the value
of husband to true because user has won the game. And also we need to break out of this
while loop let’s say short finish is equal to true. Let’s add an else if case to this
if statement. Let’s say else. if let’s say if users input is greater than r x, which
is our randomly generated number, we are going to tell the user to guess lower for that we
can say, guessed lower. And of course, we need to wait for the new users inputs for
that we can say user input is equal to a scanner dot next int. We also need another case in
here in case the number is less than x. If that’s the case, we are going to tell the
user to guess higher. And also if you’re going to wait for the new user input, let’s say
user input is equal to scanner dot next int. We are done with this void loop, we will review
what we are doing in here. But after the void loop, we need another if statement, we can
say if the user has won the game. If that’s true, then we are going to show a message,
let’s say congratulation. And also let’s say to the user, in which try he or she has won
the game. Let’s say you have guessed in your plus times tried plus guess. But in the else
case, first level, we are going to show a game over message. Let’s say game over. And
after that, we are going to say what was the number, let’s say the number was plus x. This is our entire application. Let’s review what we are
doing in here. First of all, we are showing a welcome message to the user. After that
we are asking for the user’s name, we are creating a scanner and after that we are waiting
for the user to enter his or her name. After we received the name, we are waiting for the
user’s permission to start the game, we are saving the user’s input in here, inside the
while loop we are making sure that we have the permission. If we don’t have the permission,
we are going to ask again and again until we have the permission. After that, we made
sure that we have the permission, we are creating a random number, the random number is between
one to 20. And after that, we are asking the user to guess a number. We are saving the
user’s input in here, we have defined three new variables in here times through it has
one and short finish. Next, we are creating this viral loop. It starts from here and ends
in here. The condition of this viral loop is vile, we shouldn’t finish the game continue
the loop. First thing inside this while loop we are increasing the time stride. So if it’s
the first time because the time stride is initially zero, we are changing that to Vaughn.
After that we are checking that if times three is less than five, we are comparing the user’s
input with the randomly generated number. If that’s equal to a random number, we are
changing the value of this husband to true and also we need to break out of this loop.
So we are changing the value of this short finish to true. But in case the user input is greater than our random number, we are telling the user to guess lower and also we
are waiting for the user’s input. But in case the user’s input is less than our random number,
we are telling the user to guess higher once again, we are waiting for the users input
in case the time strike is greater than five, we are just waiting out of the loop. If that’s
the case, the husband is still false. So we are checking that in here. If has one is equal
to true it means that user has won the game. So we are showing this congratulation message.
Also we are showing that English try the user has guessed the number correctly. But in case
if the user didn’t want the game, we are showing this game over message and also we are showing
the random number. Let’s run the game and see if everything is working fine. We are
seeing the welcome message. After that it’s asking for our name. Let’s put Mesa we are
seeing Hello Mesa after that we are seeing the message for our permission, shall we start
or it was better to say Shall we begin? Let’s say to know once again, we are seeing the
message. This is our first wide look. Also this formatting here is because of that backslash
t that I have used when I have created this text I’m talking about this one, this backslash
t in here. Also beside one and two. If you enter any other number, we should see this
message once again. So let’s say three for example. You can see that we are seeing the message. Okay, let’s say Yes to continue. The console is waiting for us to get a number.
Let’s say 10 it says gets lower, let’s say five. It seems like I have won the game in
my second Troy. Okay, let’s run the game once again. May I have your name? Let’s say Mason.
Yes. Let’s begin. That’s guess another number. It says that gets lower, I’m going to guess
higher in order to debug all the features of the game, let’s say 1112 1315. You can
see that after five Troy, I can see the gameover message. And I can see that the number was
nine. Okay, it seems like our application is working perfect. Just before I finish off
this video, let’s change this message in here too. Shall we begin? I think that’s better.
Okay, this was our challenge. I hope you solved it if you did do celebrate, but if not, that’s
okay. Later on in the course, we are going to practice all of these many more times so
that we make sure that we have learned everything that we see in the course. Okay, I think that’s a good point to stop the video in here. Just before I do that, I’m going to say that in
the next video, we are going to talk about arrays in Java. See you in the next video. In this video, we are going to talk about arrays in Java.
Up until now if we wanted to store some simple data, for example, a number or maybe a text,
we could have used variables. For example, we could have said int a is equal to five.
But what if we want to store a list of different variables for example, a list of names or
a list of students. for storing a list of variables, we have multiple options in Java.
The first one that we are going to talk about in this video is called array, let’s see how we can have an array of for example strings. For that we can say for string. After that,
we need a pair of square brackets. After that, we need the name of our array. For example,
I can say students, next I can say new string once again with a pair of square brackets.
This new keyboard in here is simple English, it means that we are defining a new array
of strings inside this square bracket, we can define the size of our array. For example,
if we want to serve five different student names, I can say five in here, as simple as that now we have an array of size five. After defining your array, you can put some values
into it. For example, I can say the students with an index of let’s say, Vaughn is equal
to let’s put my name in here It may sound this way, we are assigning this value to the
element in which has the index of one in our array, there is a very important point in
here and that indexes in Java starts from zero. So in here, when I said the students
with an index of one, it means that we are referring to the second element in our array.
If you want to assign some value to your first element in your array, you can say students with an index of zero. Let’s see that as well, let’s say is equal to, for example, so we
can keep doing this for all of our elements inside our array. Besides assigning values
to your array elements, you can also get the value of those elements as well. For example,
in here, we can say print the value of a student with an index of two. You can
guess it in this case, it should be Tom. Let’s run the application and see if we can see Tom. As you can see, Tom has been printed. This is one way of assigning different values
to your array elements. The other way is like this, you can assign different values at the
time of creating your area. For example, in here I can see a string array, let’s name
it employees is equal to instead of saying new string array, I can pass a pair of curly
brace. And inside that curly brace, I can pass my values. For example, I can say Mesa,
or every other value that I want. So these are the two ways of assigning values to your
array elements, like a string array, you can also have other kinds of arrays as well. For
example, if I want to put some numbers inside an array, I can say int with a pair of square brackets, let’s say numbers is equal to, let’s say a pair of curly braces. And inside those
curly braces, I can pass my values, let’s say one, two, through six. When you’re working
with different arrays, for loops can be very helpful to access all of the elements inside
your array. For example, if I want to print all of the values inside this numbers array, I can say something like this, I can create a folder. Let’s say for int i is equal to
zero, i less than in this case, the size of my numbers array is six. So let’s say six.
After that, let’s say i plus plus. Inside the for loop, I can say print the value of
numbers with an index of I. This way, I am going to print all of the numbers inside my
numbers array before running the application. I’m going to comment this line of code so that we Have a clean console. As you can see numbers from one to six have been printed
into our console. So for loops are very helpful when you’re working with different kinds of
arrays. In here, we have hard coded these six. But what if we don’t know the size of
our array? For example, if we get some values from our database or from our server in that
case, it might be possible that we don’t know the size of our array. How can we use for loops with arrays, if you don’t know the size of our array for that, we can use a helpful
property of different areas. For example, in this case, I can say numbers, dot length.
This way, we don’t need to be worried about the size of our array, we can just use it like this. Let’s run the application once again. And as before, we have printed numbers
from one to six. Later on, we will see that beside primitive data types and strings, we
can also have different kinds of arrays for different objects, for example, for different classes in Java, but more on that later on. So using arrays is one way of having a list
of different items. But there are some limitations to arrays. For example, the size of our array
after the definition is immutable. Let me show you what I’m talking about. In here. When we define our students array, we have set the size to be five. But what if we want
to change the size of our array, for example, what if we want to define a sixth element?
For example, if you say the students with an index of five, let’s say Brian, if you
hover over these five, you can see that array index is out of bounds, it means that these
elements in here cannot fit in our array. For example, if we try to reach to that element,
you should see a problem, let’s say print the students with an index of five. Before
that, let’s comment this line of code. Let’s run our application. As you can see, we are
getting an exception in here. Once again, when we get exceptions, it means that our application crashes. And the exception type in here is array index out of bounds exception,
which means that the sixth element does not exist in our array. So arrays are immutable
after you define them, you cannot change their size. This is one of the limitations of simple
arrays in Java, there are others as well. For example, you cannot do that much with your arrays. later on. When we use other kinds of collections in Java, we will see that we
have all kinds of operations on our array elements. But I’m not going to talk about
other kinds of collections in here because I think that it’s better to talk about them after we know about object oriented programming. Later on in the course, we will talk about
other kinds of collections like array lists and maps. Just before I finish off this video,
I’m going to write a simple application in which will help us to practice what we have learned about arrays. Before that, let me comment all of these lines of codes. And let’s
start creating our application down in here. In this application, I’m going to simulate
the contacts application on the phone, I’m going to show a list of different contacts
names on a phone for to the user. And after that by typing the contacts name, we are going
to give the user that contacts number. In order to simulate that first of all, we are going to need a string array, let’s say a string array names is equal to let’s pass
our names directly. Let’s say Mesa. After that, we need a list of different numbers
for that I’m going to create that integer array. Let’s see numbers is equal to and let’s
initialize our array. After that, I’m going to show the name of all of the available contacts
on the phone. So let’s create a for loop. For int i is equal to zero, i less than names
that length, and i plus plus. let’s print the names inside our names array. Let’s say
print names with an index of I. After the for loop, I’m going to ask the user to enter
a name. So let’s say print. Let’s say please enter a name. After that we need our scanner.
So let’s create that is equal to new scanner system that mean after that, let’s get the
contacts name. Let’s say the string name is equal to a scanner dot next. Then I’m going to create a for loop and inside that loop
I’m going to find the index of the contact. So let’s say for int i is equal to zero. i
less than names, dot length, i plus plus. Let’s write an if statement in here. Let’s
say if name is equal to names with an index of joy. Then inside this if statement we are
going to print the contacts number for that I can see print numbers with an index of pi.
But right now there is a problem with my code, I’m not sure that if it’s going to work fine or not, and that’s because of these two equal signs in here. Let’s run the application and
see if it’s going to work. Let’s enter our name in here. Let’s say Sarah. And as you
can see, nothing has been printed. Let’s talk about these two equal signs in here. If you
hover over the warning in here, it says that the string values are compared using two equal signs not equal with a pair of parentheses. The problem in here is that when you’re using
two equal signs, you are checking for the equality of two strings by a reference, I’m
talking about the reference inside the devices memory Ram. Whenever you are storing a variable,
that variable allocates some space inside the device’s memory RAM, and that allocated
a space has some address. And that allocated the space has some reference in which we are
comparing those two references in here together. So even if the value of two strings in here
can be the same, but the reference might not be for that reason, it’s not a good way to
compare two strings by using these two equal signs. Instead of using these two equal signs,
there is a better option for comparing two strings, and that’s the equals method. Let’s see that as well. In here, I can say name, dot equals, and inside the parentheses, I
can pass the other string. For example, I can say names with an index of I. If you remember
from the previous videos, I said that the strings are not primitive data type, and they are objects in Java. And because they are objects, we have some properties on our strings.
For example, this equals method. Once again, we are going to talk about methods later on
in the course when we talk about object oriented programming. But for now, just know that there is a method called equals in our string class, in which we can use to check the equality
of two strings by the values. So now, if you’re on our application, we should get Sarah’s
number. Let’s run the application. In here, once again, let’s see Sarah. And as you can
see, we are getting the number. Let’s also check that if you’re getting the correct number, Sarah is the second contact in our array. And the second element in our numbers array
is this number in which we have printed into our console, it seems like our application is working fine. Okay, I think that’s enough talking about arrays in Java. In the next
video. Finally, we are going to start talking about object oriented programming. That’s a very important topic, and it’s going to help you a lot to understand Java, so make
sure to Don’t miss the next two or three videos. See you in the next video.
As I said at the end of the previous video, in this video, finally we are going to start talking about object oriented programming. Up until this point with the help of variables,
we could have defined simple data types. For example, if we wanted to have numbers, we
could have say int number is equal to five, four. Similarly, we could have defined characters,
texts, and all sorts of variables that we have talked about in previous videos. But what if we want to have a more complex data? For example, how can we define a car in Java
or a phone or every other object that you can think of? Well, that is possible with
the concept of object oriented programming. In object oriented programming, you define
your own objects, the core component of object oriented programming our classes. Right now
you can see that we are inside the class, this class is named main. But if we want, we can create our own class as well. Let me quickly delete this line of code. And let’s
create a new class. For example, inside my source folder, inside my package on the package,
I can right click and by selecting new Java class, I can create my class. In here I need
to name my class. For example, I can say phone, notice that there is a convention, you need
to use upper cases for the first letter of the name of your class. Also, you cannot have
spaces or some strange characters as the name of your classes. So in here, I’m just going
to simply name my class form. Notice that there are other options in here as well for example interface and you know, later on we will talk about them but for now, let’s just
create a class the one that has the CI con. By pressing Enter, we can create our class.
This is how a class looks like it lives inside the package right now its package is altered.
maker.org is section one. Also if you take a look at the left pane here you can see that
a new class has been added into our package. Every class has the keyword class in its declaration.
Also every class has a name and besides This too has an access modifier. We will talk about
access modifiers in few minutes. But for now let’s continue on. Every class has a pair of curly braces in which we can put our code inside them. A class is going to represent
an object. In this case, we are going to represent a phone. If you think of different objects, objects can have different properties. For example, in the case of a phone, a phone can
have some amount of memory RAM, it can have a screen size. Beside that it has a name,
and all sorts of other properties that you can think of. When you define a class, you
can have properties for your classes as well. For example, in this case, if we want to give some properties to our phone class, we can say something like this, we can say the string
name. Now we define a property called name for our phone. Notice that I’m not initializing
the property of my class in here, I’m not giving a name to my phone. And that’s because when we declare a class, we can reuse that class, we can create multiple instance of
that class with different properties. For example, if we define a new iPhone, the name
would be iPhone, if we define another phone, the name of that phone might be different.
So for that reason, I’m not initializing the properties of my class, let’s add a few more properties to our phone object, let’s say in screen size. Beside that, let’s say in
memory Ram. After that, let’s say int camera. So now that we have created this simple class,
we can instantiate it, we know for sure that the starting point of our application is this
main method inside our main class. So if I need to instantiate my class, I need to do
it in here. And here is how you can create an instance of your class you type the name
of your class, you can see that when I typed the name of my class, I’m getting some suggestions
from IntelliJ. Sometimes you may get multiple options. And that’s because different classes
with different names can live in different packages. For that reason, always take a look
at the package that your object lives in. In this case, I am going to just import this class. After that you need to name your instance for example, I can say iPhone, next I can
say is equal to new phone. This way with the help of this new keyword, we can create a
new instance of our object. Now that we have created an instance of our class, we can have
access to the properties of our objects our class, for example, if you want to define
some property for our class, we can say something like this, we can say iPhone dot, let’s say
name is equal to let’s name it iPhone 11. With the help of this dot operator, you can have access to all of the properties or to
be precise all of the fields of your class. Similar to assigning a value to your classes,
properties or fields, you can also get those properties or fields. And here is how you can get them. For example, if you want to print the name of our iPhone, we can say something
like this, we can say print, iPhone dot name. If you run our application, we should see
iPhone 11 printed into our console. And here is iPhone 11. Similar to that you can have
access to all of the fields of your class. For example, I can say iPhone, let’s say memory
RAM is equal to eight. Beside properties, your classes can also have some behaviors.
For example, in the case of a phone, you can play some music with your phone, or you can call somebody. If you want to define some behavior for your class, you can use methods,
let’s see them as built. Let’s switch back to our phone class. Here is how you can define a method I’m going to write the syntax and after that we will talk about it. First of
all, our method has a name in this case its name is play music. After that a method can
have an access modifier like we did for the declaration of our class, we will talk about this public in a minute. After that every method can have some inputs. For example,
in this case, we are getting a string called trackman. Later on, when we use this method,
we need to pass some input to this method. If you don’t want to have any inputs for your
method, you can just pass a pair of empty parentheses. But if you want, you can have one or more inputs. If you want to have multiple inputs for your method, you can separate those
inputs with a comma. For example in here I can say is drink album name. But I’m not going
to do that in here. But you can have as many inputs as you want. beside a name, an input
and also an access modifier. Every method can return something in this case, it’s void,
but if you want you can change it. For example, we can say a string as the return type of
your method. You can also have integers or other kinds of primitive data types. Even
you can return another class with your method. But more on that later on. If you don’t want
to return any From your method, you can use the keyword void. Like we did before, we will
talk more about the return type of every method later on. But for now, we are not going to
return anything from this method. So we are going to pass void inside the curly braces of my method, I can put my code for example, in this case, I’m going to play some music,
or at least I’m going to print something. In this case, I’m just going to say, playing
plus the track me. Now that I have created this method, I can use it in my main class where I have instantiated
my object. For example, in here I can see iPhone dot play music. Once again, you can
see that by typing dots, I can have access to my methods as well. Right now, I’m getting
error error in here. And that’s because when I have created my method, I have set an input
for it. So when I’m going to use my method, I need to pass a tracking. For example, let’s
say our beings are bearing the name of a track. Let’s run the application and see what would
happen, you can see that we are playing some music. So by defining methods, you can have
behaviors for your classes, there are a few reasons for using methods. The first one is
that you can reuse your method. For example, in here, after this line of code, I can say
iPhone dot play music, and I can pass a different track name, let’s pass lamenting case this
time. If we run our application, you can see that our methods are being executed one at
a time. So the first benefit of using methods is that you can reuse them. The next one is
for organizing our code. For example, imagine that you want to have some sort of calculation,
you can pass all the codes for that calculation into a method. And by calling that method,
you can pass all of those calculations to a method and later on, you can just call that
method in order to do the calculation, it’s going to help us a lot for organizing our
code. Also, by using methods, you can encapsulate things we will talk about encapsulation in
later videos. But in here inside your class, you can define your methods as private, and
that method will be accessible only from your class and not from outside of your class. If you go back to main class, you can see that we are getting red warning. If you hover
over there, it says that this method has private access. So by using private methods, you can
encapsulate different behaviors for your own class. Okay, now let’s talk about this access
modifier. You can have access modifiers, for your methods for your classes. And also for
the fields of your classes, I can make this name private, if I want as well. Basically,
we have three kind of access modifiers, we have private we have public. And also we have
protected if you don’t use anything as the access modifier. It’s equal to when you use
public. So there is no difference between public string name or just a string name.
When you use public as the access modifier. For example, if I use public in here, it means
that this field is going to be accessible from other classes as well. But if I use private
in here, let’s say private, if I go back to my main class, you can see that we are getting
a red warning, it means that we can no longer use this property. So by setting the access
modifier of our field or method, that field or method is only accessible from inside our
class, beside public and private we also have projects that we’ve been talking about protected
later on in the course both private and public are the most access modifiers that we are
going to use in the course for the access modifier of your class. You can also have private admin group inside there. But usually you wouldn’t do that right now you can see
that we are getting a red warning in here. And the warning says that modifier private not allowed in here. Do you use private mostly when you use inner classes we will talk about
inner classes later on in future videos. But basically, they are some nested classes inside
other classes. When you define them as private classes, you can have access to those nested
classes from the parent class. We will talk about that later on when we talk about inner
classes. For now let’s just change this one to public. There are a few reasons for using private as the access modifier of your fields or methods. The first one is that you may
want to limit the access of your fields or methods to the class itself and not other
classes. For example, imagine that you are writing some code that you are going to publish for other developers. In that case, you may want to protect your classes against changes.
For that reason, you may want to change the access modifiers of your fields and also methods to private but now that we have defined the access modifier of this field, as Private,
how can we have access to this name from outside of this class, there is a way to that, let’s
quickly talk about it, we can create public methods. For example, down in here, I can
say public as the return type of this method, I’m going to say void. For the name of this
method, I’m going to say set name. As you can see, when I use the keyword set, I am
getting some suggestions from IntelliJ. Basically, there is a convention among programmers to
set the name of the setter methods like this, an intelligent is smart enough to know the
convention. But of course, you can name them whatever you want. In this case, I’m going to follow the convention. So I’m going to say certainly, inside the parentheses as the
input of this method, I’m going to say, the string, let’s say name. Inside this method, I’m going to change the value of this name
to whatever the name that I’m going to receive via this method. For that I can say this dot
name is equal to name, the keyword, this is referring to the current object that we are
in. For example, right now we are inside our phone object, our phone class. So by using
this dot name, we are accessing the property of this class called name. The second naming
here is the input of our method. So we are setting the value of this name to whatever
we receive, we are the input of this method. Now because the access modifier of this method
is public, we can use it inside other classes. For example, from inside the main class, if
we want to set the name of our iPhone, we can say something like this before that, let’s delete this line of code. We can say iPhone dot, let’s say set name. Inside the parentheses
of this method, I need to pass the name of my phone, let’s say iPhone 11. So this way,
I don’t have access to the field directly. But by using the method, I can change the
value of that field. But what if we want to get the value of this field? Well, we can
write another method. And here is how we can create that method. We can say public as the
return type of this method, I’m not going to say void because I’m going to return this
name. So I’m going to say a string. Once again, there is a convention in here, we can say
get name, we don’t need anything as the input. Inside this method, we are going to return
something. And here is how you can return something from a method you type return. After
that, whatever you are going to return. In this case, I am going to return this name. So I’m going to say this dot name. Once again, this refers to the current object, this dot
names means the name inside our phone object. Now that we have created this method, inside
our main class, instead of saying iPhone dot name, I can say something like this, I can
say, iPhone dot get name. If we run our application, once again, we should see the name of our
form. Let’s run the application. You can see that iPhone 11 has been printed. Beside creating
these kinds of methods ourselves, we have another option. If we come down in here, we
can use the help of our ID, we can right click in here, we can go to this generate method.
Similarly, we can press ALT plus insert, you can see that in here we have a list of options. We have getter, we have surfer, and also we have getter and setter, if we want to create
getters and setters, we can select that. After that we can create the field that we want
to create getters and setters for for example, if we select a camera, and press OK, you can
see that Id has created some codes for us, you can see that these methods are almost
identical to the methods that we created above in here. Because creating getters and setter
methods are very usual, Id can help us with the creating of them. But why are we using
getters and setters? Well, there are a few reasons for that. The first reason is encapsulation.
Let’s quickly talk about that a bit. For example, if you don’t want to give others the permission
to change the value of our fields, we can delete the setters. In that case, we only
give them the permission to get the value of objects but not set the value of objects
or vice versa, we can create setter methods for our class. In that case, we can set the
value of our properties from other classes, but we cannot get the value of this properties.
The first reason is encapsulation. The other reason is that sometimes in your setter and
getter methods, you may want to do some other operations as well. For example, if you’re
setting a name for your phone, you may want to validate that name before setting it. In
that case, you can have some validation before assigning the value to your property. Okay,
that’s enough talking about getters and setters. That’s greatly changed the access modifiers of have these two private, and let’s create getters and setters for the ones that we didn’t
create any. down in here after everything, I’m going to press ALT plus insert, let’s
say getters and setters. And let’s select both of them, you can see that IntelliJ has created these codes for us. But because we have changed the access modifier of this memory
ram to private, now we have an error inside our main class in here, instead of saying
iPhone dot memory is equal to eight, I can say something like this, I can say iPhone dot cert memory. And I can pass eight. What we did in here is one way of instantiating
objects, we have defined our object. After that we have set different values for our
object, there is another way of instantiating your objects, let’s quickly see that as well. So if I switch back to my phone class, above the play music method, I can create something
called constructor. Let’s use the help of ID for creating that constructor. Once again,
I’m going to go to generate menu. The first item in here is constructor in here, I’m going
to select all of the fields. And I’m going to press OK. And as you can see, this code has been generated for us this is called a constructor in object oriented programming.
Our constructor needs to have public as its access modifier, it cannot be private. After
that the return type of this constructor is a phone the exact item that we are in our
constructor does not have any name. And as the input of this constructor we are receiving
all of the fields of our class we are receiving name a screen size, memory and camera inside
our constructor we are assigning different values that we have get for your the input
to the fields of our class. Now that we have created a constructor if we switch back to our main class, we can see that we are getting a red warning in here. The warning says that
expected four arguments both found zero, it means that now that we have created the constructor,
we need to pass the values directly to the parentheses of this new form. And here is
how you can pass them. The first one was the name of our phone, let’s say iPhone 11. I don’t remember the order of things. But the next one was a screen
size, let’s say five, I believe the next one was memory RAM, let’s say eight, and the other
one was camera, let’s say eight as well. Now that we have instantiated our object like
this, we don’t need to directly set fields of our class, we can safely delete this also,
let’s delete this as well. In this line, we are printing the name of our iPhone. Let’s
run the application and see if we can print the name correctly. As you can see, iPhone 11 has been printed. So when you are using constructors, you need to pass the values
of different properties at the time of instantiating your objects your class can have multiple
constructors as well. For example, if I come down in here, and once again, right click
and go to the Generate menu. If I select konstruktor, this time, I can select different items. For
example, in this case, I’m going to select only name and memory. Now if I press OK, you
can see that we have another constructor which will only receive name and memory run. But
if you hover over this phone, you can see that this constructor is never used. If we switch back to our main class, we can create another phone and we can use the second constructor.
Let’s quickly do that. I’m going to do that after these two line of code. Let’s say phone.
And this is the beauty of object oriented programming. When you create a class, you
can reuse that class as many times as you want. For example, in here, I’m going to define an entirely new font. Let’s name this one pizza, let’s say is equal to new phone. This
time I’m going to use the second constructor for the name I’m going to save pixel three, and four the memory, I’m going to say 16 you can see that the ID is happy with this way
of defining a new phone. Now we have two different instances of our phone class having two different
kinds of constructors for a class is called polymorphism in Java polymorphism has other
types as well. We will talk about it later on. Okay, I think that’s a good point to stop the video in here. In the next video we are going to continue our talk about object oriented
programming. There are many more things left that we need to talk about. See you in the next video. In the previous video, we have talked briefly about classes what those are,
how can we create them and also what are the most usages of a class in this video we are
going to talk about some important concepts regarding to object oriented programming.
The first of those concepts is inheritance. I’m going to talk about inheritance with an
example because I think that would make everything easier to understand First of all, I’m going
to create a class called doc. For this doc class, I’m going to define some properties.
For example, I’m going to define a name, let’s say private string name. After that, I’m going
to define a color, let’s say private string color. Let’s also define the number of legs,
let’s say private int, legs. And also I’m going to define a Boolean indicating that
if a dog has a tail or not, so let’s say private Boolean, let’s name it as tail. Let’s also
create a constructor for our class by pressing ALT plus insert, you can see the dialog in
here. Let’s select all of the fields. Let’s also create some getters and setters for our
class as well, once again, by pressing ALT plus insert. By selecting this getter and
setter, we can create getters and setters. Let’s also define a method for our class as
well. I’m going to say public void, let’s name this method eat, I’m going to indicate
the eating behavior of a dog. As the input of this method, I’m going to receive a food.
So let’s say let’s drink food. And inside this method, I’m just going to print something,
let’s say eating class our food. Okay, nothing special is in this class. First of all, we
have created these four fields. After that we have created the constructor, we have created
our getters and setters. And we have created this eat method, or we have talked about why
I’m doing all of this, let’s also create another class called bird. Let’s say bird. For this
bird class, let’s also have a name. Also, let’s have a color. Let’s define two more
fields, one for the number of legs, private int legs, and also another one indicating
that if our bird has a tail or not, so let’s say private Boolean,
let’s say has, for this bird class, let’s also create a constructor, and some getters
and setters. I’m going to create my eath method for this bird class as well, let’s say public
void, eat. Let’s get the name of our food. And inside this method, let’s create something. Let’s
say eating that food. You probably can see the pattern in here, there are a lot of similarities
between our bird and dog class. Both of those classes have four fields, they have a constructor,
they have all of these getters and setters. And both of them have this eat method. In programming, this is not efficient, you basically are copying and pasting yourself. Instead
of defining our bird and dot class like this, we are going to use another concept in object
oriented programming called inheritance. For that, first of all, I’m going to create an animal class. And after that, I will extend the animal class to the spirit and dog. Let’s
see how we can do that inside my package, I’m going to create my animal class, let’s
say animal. First of all, let’s add some fields, I’m going to use the four previous fields.
Let’s also copy them from here. And after that, let’s create a constructor and getters
and setters and our eat method. Don’t worry, we will reduce the amount of copying and pasting
in a minute, let’s say public, void eat string food as the input. And let’s print the name
of our foot. Now that we have created this animal class, creating bird and dot class
is going to be much simpler. For that I’m going to delete all of the quotes inside this
bird class. Let’s delete all of them. This time, instead of defining all of the fields
and constructors and methods, I’m going to say extends animal. This way we are saying
that our bird is an animal, we are extending the animal class and later on if we want,
we can customize our birth class as well. But right now you can see that we are getting a red warning. And the warning in here it says that there is no default constructor
available in this package. Basically, it says that you need the constructor, let’s press ALT plus insert in here. And let’s select constructor. And as you can see, the ID has
generated this constructor you can also see that even though we don’t have any fields in our birth class, we are receiving some arguments in the constructor. But this constructor
has rather a strange syntax. We haven’t seen this super keyword so far. Basically, this
super keyword means that do whatever you’re doing with these parameters in the parent class. In this case, our parent class is animal. So this super is passing all of these arguments
to the constructor of our animal class. It means that It setting all of the values to
the fields of our animal. Now that I have created my bird class like this, if I want
to instantiate it, for example, inside the main class, I can do as before, let’s say bird. Let’s name it Phoenix, let’s say is equal to new bird. And as you can see, the
constructor is waiting for our arguments. First of all, we need a name. That’s past
Nina, we need the color, let’s say golden. After that, we need the number of legs, which
I’m going to pass to, and a Boolean indicating if our bird has a tail or not, that’s passed
through. Once again, remember that in our bird class, we didn’t have any extra methods.
But in my main class, if I want, I can use some of the getter methods of our parent class,
in this case, animal. For example, if I want to get the name of my bird, I can say something like this, I can say Phoenix dot get name, you can see that we have all of the getter
methods. Let’s run the application. And let’s see if we can print the name of our birth,
you can see that the name has been printed successfully. Beside getter and setter methods, you can also have access to all of the other methods that we have created. For example,
if you remember inside the animal class, we have created this each method, let’s see if
we can use it in our bird instance. For example, in here, if I say Pheonix, that it, I don’t know what Phoenix is, its birth, let’s just pass meat. And let’s
run our application. And let’s see if we can successfully execute the eat method. And as you can see in here, Nina is eating meat. Now that you have created your birth class,
you can also change the dog class as well. For example, in here, I’m going to delete all of these codes once again. Instead, I’m going to extend the animal plus, once again,
let’s say extends animal, you can see the red warning. And that’s because we need the
constructor, you can see that the amount of similar codes that you were copying and pasting is a lot less right now, the bird and dog class are basically the same. But we know
for sure that a dog and a bird are not the same, they may have other fields. And also
other behaviors as well. If you want, you can customize your child classes as well.
For example, inside my birth class, I want to add another field, I’m going to say private,
let’s say ain’t veins, we know that dogs do not have wings. In here. I’m going to delete
this constructor and I’m going to create it once again, because I’m going to use the help of my ID, let’s press ALT plus insert. In here, I’m going to select Winx. And this time,
as you can see, the constructor has been changed a little bit, the super statement is as the
same it means that do whatever you’re doing with all of these four fields. But because
inside our parent class inside the animal class, we do not have a wings field, these
wings hasn’t been passed to this super statement. So we are setting the value of wings directly
inside the constructor of this bird class. This way we can customize our child classes.
Right now the duck does not have any wings field, but our bird does what’s right now
if I switch back to my main class, I should get a warning. And the warning in here says that you need to pass a wink that’s also fascinating here as well. For example, I’m going to say
to we can also create getter and setter methods for these wings as well. For example inside
our bird class down in here, let’s press up plus insert and add select getter and setters.
Now that we have created the getters and setters, let’s see if we can print the number of wings.
For example, down in here, I’m going to say Phoenix dot get wings. Let’s run the application.
And you can see that two has been printed as the number of means beside getter and setter
methods. You can also have other methods inside your child class as well. For example, inside the spirit class, if I want, I can define a fly method as well. Let’s quickly see that
let’s say public void fly. I’m not going to receive anything as the input let’s just print something. Let’s say this
dot get name. Also if you remember from the previous video, I said that this keyword refers
to the current object that you are in. In this case inside the bird class, we don’t have any getname method. But because we are extending the animal class, we can use the
start getting, let’s say is flying. So besides using fields, you can also customize your
child classes with different methods. Let’s see if we can call this method from inside
our main class, let’s say Phoenix dot fly. As you can see, Nina is flying. Also beside
the finding new methods inside your child class. You can also change the behavior of
methods of your parent class. For example, if you were Remember, inside the animal class, we had this eat method, I’m talking about this one, which inside this method we are
saying eating food inside our bird class. If we want, we can change that. For example
right in here, I’m going to press Ctrl plus Oh, you can see that we are seeing a list
of different methods, I’m going to override this eat method. So by overriding, you’re
going to say that I’m going to change the behavior of this eat method from what is happening
inside the parent class. Once again, we are seeing this super keyword, it means that do whatever you’re doing with this parameter inside the parent class. If you want, you
can delete this super statement completely, but let it be for now we will delete that later on. After this super keyword. I’m going to print another statement in here. Let’s
just say eating finished. So this way, we have changed the eat method
inside our child class from what it was inside the animal class. Right now inside our main
class, we have executed this line of code for Unix that eat let’s comment the lines after that, so that we can see what’s going to be the behavior of this eat method. Let’s
run the application, you can see that we are saying eating meats. And after that eating finished, if you want to completely change the behavior of your method inside the child
class, you can simply delete this super that eat method. For example, in here, I’m going
to say chewing food instead of eating fruit. So in both child and parent class, we have
this eighth method, but their behavior is different. Let’s run the application and see if we can see the difference, we can see that we are chewing meat. So what we did so far
is called inheritance in object oriented programming. We have inherited the birth class from the
animal class. You can also continue this inheritance multiple times. For example, if you want,
you can inherit the dog object. Let’s quickly create another class in here. For example,
let’s say Shephard. Now in here, I can say extends the dog class, you can see that once
again, we are getting this red warning. Let’s create our constructor in order to overcome
that problem, we can see that even though inside the doc field, we do not have any field.
But inside our Shephard class in the constructor, we need all of those arguments, because the
parent of this Shephard class, which is dog is the child of another class, which is animal.
So you can do this inheritance as many times as you want, you can also create another class
and extend the shefford class. But I don’t think that mean anything in here, I just wanted
to say that you have that option. Okay, I think that’s enough talking about inheritance.
The other concept that I’m going to talk about is called polymorphism. We have talked a bit
about polymorphism in the previous video, but let’s talk more about it in here. So in
previous video, I said that one kind of polymorphism is that for a class to have multiple constructors,
for example, inside our animal class, if we had another constructor in here, which will
accept different number of fields. For example, the first three now that we have two constructor,
this is one kind of polymorphism. Now we can create this animal class in two different
ways. Basically, polymorphism means having multiple forms. But there are two more kinds
of polymorphism. But before we talk about that, let’s delete this constructor, I’m not
sure that if you’re going to need that the other kind of polymorphism that we have used
so far is about this eat method. So inside this animal class, we had this eight method,
but also we had it inside the child class, which is birth. But these two eight methods have different forms, it means that we are doing different things inside each one of
these methods. This is another kind of polymorphism two methods inside a parent and child class
that are doing the same job differently. We also have another kind of polymorphism. And
that’s with the methods themselves. We don’t have this following method inside our animal
class. But in here, we can also have another method called fly as well, let’s quickly see
how we can create another method called flight. I’m going to say public void fly. But this
time inside the parentheses, I’m going to receive some arguments for example, I’m going
to receive a speed as you can see, the compiler is happy with this way of defining two different
methods with the same name. Now, if I switch back to my main class, you can see that we
are not getting an error but before that, I think I should uncomment this line of code. Even though we are not getting error warning this in here it means that we are using the
first flow method Let me close all of the unnecessary classes. Inside the bird class
we are using the first method, but if we want to use the second method, we just need to pass some arguments in here. For example, let’s pass 100 we can have methods with the
same name when we have different number of arguments, or alternatively, when we have
different kinds of arguments, for example, in this case, we are passing an integer. But
if we had another method, for example, in which we pass a string, that method can have
the fly name as well. Let’s see that briefly, let’s say public void fly. And inside the
parentheses, I don’t know, let’s pass a string. Let’s just say name, you can see that the
compiler is happy with this way of defining a method with the same name. So besides the
difference in the number of parameters, the kind of parameter is also important. This
in here is also called polymorphism. using methods with the same name in different ways,
okay, let’s delete these two, I don’t think we are going to need them. And I think it’s a good point to stop the video in here. In the next video, first thing we are going to
start talking about composition in object oriented programming. See you in the next video. In the previous video, we have talked about inheritance and polymorphism. In this
video, we are going to talk about composition in object oriented programming. In previous
videos, I said that classes are useful for when you want to define your customized data
type. For example, imagine that you are going to create a car in that car you have multiple
systems, for example, you have a system for a stereotype, your car also may have some
engine, it can also have a fuel system. If you want to simulate a car. In programming,
you can create different classes, for example, for engine for a stereo system, and also for
field system. And after that inside your car class, you can compose all of those classes
to have a car, let’s see how we can use composition in Java. In my package, I’m going to create
a class called engine. For this engine, I’m going to have two fields. First of all, I’m
going to define a model. So let’s say private string model. And after that, let’s have an
integer, private int. I’m going to name this integer, rpm, rpm stands for round per minute,
it’s just a property of different engines. I’m going to keep it really simple. So I think these two would be enough. After that, let’s quickly create our constructor and getters
and setters. Let’s create a car class. And let’s see how we can use this engine inside
that, let’s say new Java class. Let’s name it car. For this car class, I’m going to define
four fields. First of all, let’s pass a name, private string name. After that, let’s have an integer for the number of doors. After
that, let’s define the color of our car private string color. And after that, I’m going to
include or compose an engine in here. For that I can simply say private engine with
capital E. And in here, you can see that in my package, I have an engine class, I can
import that into this class. Let’s name it engine. For the sake of simplicity, I have
composed only one class inside this class. But if you want, you can create another class
for a stereo system and your system and everything else that you want. But I think you will get
my point. If I use only one class after death like other classes, I can create my constructor,
you can see that this engine has been passed to our constructor the same way that we have
passed other data types like integers and strings, we can also have getters and setters
the same way that we had for previous classes. Now that we have created this car, we can
instantiate it. For example, inside our main class. For example, if I want to have a car
object in here, I can say car, let’s name it Mercedes, is equal to new car. As you can
see, the constructor in here requires four fields and name doors color, and also an engine
for the name. I’m going to pass Mercedes AMG for the number of doors that’s passed to for
the color, let’s say silver. What What should we pass as the engine here? Well, we can pass
our engine in two ways. First of all, we can create our engine before this court class.
For example, in here I can say engine, let’s name it engine is equal to new engine. And
I can pass a model and RPM for this engine to instantiated for example, I’m going to
say three note for the RPM, let’s say 8000. After creating our engine objects, we can
pass it to the constructor of our car for example, in here I can say engine and like
that we can create our car but the other way is to pass your engine directly to the constructor
of your car. For example, I can see a new engine in here. And I can’t instantiate my
engine objects directly inside the constructor. Let’s say Renault Let’s fast 1000. Now that
we have passed it directly, we don’t need the first line. Of course, we know that Mercedes, we don’t use Renault engine, but I think it doesn’t matter in here. Now that we have instantiated
our car objects, we can have access to all of the fields like we did before. For example,
if you want to print the name of our car, we can say something like this, we can say Mercedes dot name, or that gets named to riversides. But what if we want to get the model of the
engine of this car? Well, for that, we can say something like this, let’s print engine
model. Plus, we can say Mercedes dot get engine, this method in here. After that, we can once again say dot get model. So this
get engine method will return an engine object in which we can perform another dot operator
on that, let’s run the application and see what would be the result. First of all, we can see the name of our car, and after that, you can see the engine model, which is renamed.
Also instead of using the methods of your engine, you can get the engine object directly.
For example, in here before the printer statement, I’m going to say engine, let’s name it engine
is equal to let’s say Mercedes dot get engine. You can see the return type in here. But whenever
you are not sure about the return type, you can press down the control key. And by hover
over in your method, you can see the return type, you can see the declaration of your method which says public engine get engine, it means that this method is going to return
an engine object. Now that you have your instance of engine, you can perform all kinds of operations
on that, instead of pressing down the control key and hover over your method, there is another
option to get the return type and for that matter the declaration of your method. And
here is how you can do that, you can click on the name of your method, and you can press down the control key. This way, you can see some information about the declaration of
your method. These kinds of documentation are especially useful for when you are using
a third party library or some built in methods. For example, if we click on this print line
method and press Ctrl Q, you can see that the documentation is giving us much more information.
For example, in this case, it says that this method is printing a string and then terminate
the line, you can see some information about the method itself. Also, if you want, you
can have this kind of information on the methods that you yourself create. For example, on
this get engine method. If we switch to our car class, we can create some sort of documentation.
Let’s find that method. And here is how you can do that you need to add a comment in here. In previous videos, we have seen how to create a new comment by using two slashes we could
have create our comment. But these kinds of comments are not useful in here. Instead,
we can use a single slash. And after that, we can have to start by pressing and enter.
You can see that we have these sorts of comments as well. These kinds of comments are useful
for when you have multiple lines. And in here you can have some sort of information in which
later on will be shown when you press Ctrl q on the name of your method. For example,
in here, let’s say returns the engine of our car. Now that we have provided this information
in here, if we switch back to our main class on the declaration of our method on the main,
if we press Ctrl plus Q, you can see that in here we can see that information. Sometimes
these kinds of commenting can be useful as well. But there are more to these kinds of comments. For example, we can provide some links, but more on that later on in future
videos. So this way of using different classes inside other classes is called composition
in object oriented programming. Once again, if you have created some other classes, for example, a stereo system class and also a fuel system class, you could have add them
one by one in here as well. But for the sake of simplicity, I’ve just included this engine, this way we can compose different objects inside one class. Okay, I think that’s enough
talking about composition. Let’s talk about a keyword in Java called nom. Before that,
let’s close this car class. And let’s comment all of these lines of codes. There are some
times that you don’t want to instantiate your objects immediately. For example, down in
here I can say car Mercedes is equal to I don’t want to instantiate my car object in
here like we did before. Instead, I want to post one that too few lines after this for
that you can pass null values for your objects. Let’s see now in here as well. This null keyword
in Java means nothing. It means that this car Mercedes is nothing when the value of
an object is now You cannot perform any kind of operation on that. For example, in here,
I cannot say Mercedes dot get name, for example, we are not getting a compile time error, it
means that our application is going to be created. But if you take a look at the highlighting
here, it says that this method is going to produce no pointer exception. If you remember
from previous videos, I said that whenever nullpointerexception happens, your application
is going to crash. So the error in here is going to be a runtime error and not a compile
time error. Let’s run our application. And let’s see what would happen. You can see that in here we are getting a red warning in our console says Java dot Lang dot nullpointerexception.
In general, we should always avoid exceptions in our application. Later on, we will talk
about how can we do that, but for now, let’s just assume that we know whenever an object
is now we cannot perform any operation on that, for example, we cannot use the methods
inside that object. So whenever you’re not sure about null values, for example, whenever
you are retrieving some data from a web server, or from your local database, first of all,
you need to check that if your object is null or not. And here is how you can do that. For
example, you can create an if statement before this Mercedes dot get name, I can say if let’s
say Mercedes, is not equal to Now, I’m going to put an if statement in here in case our object is not
now we are going to call this method but in the else case, I’m going to print something.
Let’s say the car was now let’s run the application once again, this time, you can see that we
are not seeing the exception, which is good. But right now, we are not doing anything helpful.
With our value being now we are just printing something, sometimes this is the desired behavior.
But sometimes you may want to check that what was the cause of your objects to be no, so
not means nothing in Java? Okay, let’s comment all of these. And let’s talk about another
keyword in Java called final. So up to this point when we want to instantiate our object
or variable, because I’ve said something like this, because I said int a is equal to five.
For example, later on, we could have changed this a for example, because I have said, A
is equal to five plus five for a plus fun. This way, we could have changed the value
of our variable. But there are some times that you need to be sure that the value of
your variable wouldn’t change. In those cases, you can use the final keyword. And here is
how you can do that before the type of your variable you can say final final int a is
equal to five. Now if I try to change the value of my variable, for example, like before,
if I say a is equal to a plus one, you can see that we are getting a warning in here
the warning says that cannot assign a value to a final variable. So whenever you are declaring
your variables and classes as final, you cannot change the value of that variable or class.
Let’s also check the case when we define our classes as final, I’m going to comment these
two line of code. And then in here, I’m going to say final, let’s say engine, let’s name
it engine is equal to new engine. Let’s pass a model and rpm. Now if I try to change the
value of this engine, for example, if I say engine is equal to new engine, with other
parameters, for example, let’s change the RPM, let’s say 7000. Once again, you can see
the error, the error says that cannot assign a value to final variable. But there is a
point in here. When you declare your objects as final, you cannot change the whole instance
of that object. But you can change the properties of your object. For example, in here, if you
remember, inside our engine class, we had two setter methods which with the help of them, we could have changed the value of two properties. For example, if we wanted to change
the RPM, because I said something like engine dot set RPM to let’s say 10,000. You can see
that even though we declared our engine as final, we can change its properties, we just
cannot change the whole instance we can change its properties using Final keywords can be
useful from time to time. Okay, I think that’s enough for this video. In the next video, we are going to have a quick challenge to make sure that we have understand the concepts
of object oriented programming. See you in the next video. This is our challenge. Imagine
that you are a doctor and you want to check on your patient. In this application. First
of all, you’re showing the name and age of the user to the doctor. And after that you
can see a list of different organs in which A doctor can select the first organ, his left
eye. Let’s select that. In here, you can see that we are seeing some details about the
organ, for example, the name, the medical condition, and also the color of the oil.
After that, we gave the doctor the option to close the oil. And also if the eye is closed,
we are going to open that let’s select one in here to close the eyes, you can see that left eye closed. Once again, we are seeing the least of our organs. Let’s select two
in this case. This time, if I don’t want to close or open the eye, I can put any number
beside one for example, if I put two in here, you can see that once again, we are seeing
the list of different organs. Let’s select three to see the heart. Once again, you can
see that we are seeing some details about the heart. After that we have the ability to change the heart rate. In a normal doctor patient situation, this wouldn’t be the available
option. But in here, we just want to demonstrate some behavior for our heart organ. So if we
select Vani, here, the scanner is asking for our new heart rate, which I can put 75. And
you can see that heart rate change to 75. Let’s select a stomach. Once again, you can see the details of the stomach, we have one
behavior in here, which is digest. If we select that, we can see that digesting has begun.
Once again, this is a hypothetical situation. And in a normal situation, you cannot order
the patient to digest the food. Let’s also select five in here to see the scheme. This
time for the skin, you can see that we are not seeing any behavior instead, you’re just seeing some details. After that, once again, we have our list, in which if we put six or
for that matter any other number, we will close the application. Let’s put six in here.
And as you can see application has been exited successfully. by solving this challenge, I
want you to practice the object oriented programming concepts that we have talked about in the
past three videos. So make sure to use them. Okay, pause the video in here and go solve
the challenge. After that, come back to the video. And let’s see my solution for the challenge as well. Here is my solution for the challenge. First of all, I am going to create a package
inside my source folder. Let’s name it or dot maker dot o p challenge. Inside this package,
I am going to create my organ class. Inside this organ class, I’m going to have two fields,
let’s say private string, name and also private string medical condition. I’m going to keep
it simple. So I think these two would be fine. After that, let’s create the constructor.
And after that, let’s create all of the getters and setters. I’m not sure that we are going to use all of these getters and setters. later on. If we didn’t use them, we just simply
delete them. Beside these, I’m going to have another method inside this class. Named get
details. Let’s see that as well. Let’s say public void, get details. And let’s print
some details about this Oregon. First of all, let’s say name. Plus, this stuff gets named.
After that, let’s print a medical condition of this organ as well. medical condition plus
these that get medical condition. That’s all we need inside this organ class. Let’s quickly
create our organs one by one. First of all, I’m going to create an organ called I. Let’s
say I inside this iclass I’m going to have a string called color. And I after that let’s
have a Boolean indicating if the eye is open or not. So let’s say private Boolean is opened.
After that, it’s time to extend the Oregon class, let’s say extends Oregon. In here we
are using inheritance. Now it’s the time to create our constructor. Let’s select both
of these two fields. And after that, let’s create all of the getters and setters. Once
again, I’m not sure that we are going to use all of these getters and setters. later on. If we didn’t use them, we just simply delete them. Okay, let’s override the get details
method. I can do that by pressing Ctrl plus or by selecting get details method. Now we
can change Use this method. For example, in this case, let’s also print the color of the
eye. Let’s say color. Plus this dot get color. Inside this iclass, I’m going to have two
other methods as well, let’s see them, let’s say public void, open. Inside this open method,
I’m going to change the value of this Boolean to true. So let’s say this dot set opened.
And let’s fast through. After that, let’s bring something indicating the AI has been
opened. Let’s say this that gets name, you can see that we don’t have any getname method.
Inside this class, we are using the getname from the parent class, which is Oregon, let’s say plus opened. Let’s have another method called close. I’m going to save public void,
close. Once again, first of all, I’m going to change the value of the Boolean to false.
Let’s say this dot set opened, and let’s pass false. After that, let’s print something.
Let’s say this dot get name, plus close. That’s all we need inside this iclass. Let’s quickly create another class
for the heart. Let’s say heart. For this heart class, I’m going to add another field called
rate, let’s say private int rate. It’s time to extend the Oregon class extends Oregon.
And after that, we need to create our constructor. After that, we need to create our getters
and setters. I’m pressing ALT plus insert to get that dialog. In case that’s confusing
for you. After that, it’s time to override the get details method by pressing Ctrl plus
Oh, and by selecting get details method, we have that in here, I’m just going to print
the heart rate, let’s say heart rate. Plus this dot get heart rate, or get rate. That’s
all we need to do inside this heart class. Let’s quickly create a stomach class. Let’s
say stomache. Inside this Islamic class, I’m going to have a Boolean that’s a private Boolean,
these empty indicating if the Islamic is empty or not. After that, it’s time to extend the
Oregon class extends Oregon. And after that, we need to create our constructor. Let’s create
our getters and setters. And after that, let’s override the get details method. Inside this
get details method. First of all, I’m going to check that if the stomach is empty or not. So let’s say if this.is empty, I’m going to use the method in here. If the stomach is
empty, I’m going to print something I’m going to say need to be fat. But in the else case,
let’s just print the stomach is full. Let’s also create another method for this class
called digest. Let’s say public void digest. Let’s just print something let’s say digesting
begin. Okay, we are done with our stomach class. Let’s quickly create the skin class
as well. For this Scan Class, I’m going to define two more fields. First of all, I’m
going to define a string for the color of the skin. After that, I’m going to define
an integer for the softness of their skin, let’s say private int softness. This integer
is going to be some number from zero to 100, indicating the softness of the skin. After
that it’s time to extend the organic class. After that, we need to create our constructor.
Next, we need to create our getters and setters. And after that, let’s override the get details
method. Inside this method, let’s also print a skin color. Let’s say skin color. Plus this
dot get color. That’s all of our organs. Now we can create our patient class. Inside my
package. Let’s create a patient class. For this patient, first of all, I’m going to have
a string for its name. After that, we are going to have an integer for the age of the
patient. After that we can create our organs. For example, I’m going To save private eye,
let’s say left eye. Let’s do the same for the right eye. Let’s also have a heart. After
that may be a stomach and a skin. Okay, that’s all of our fields for the patient. Let’s create
the constructor. And let’s create all of the getters and setters. We don’t need any special method inside this
patient class, inside our main class, which we need to create, we can instantiate it.
Let’s create our main class. First of all in here, we are going to create our main method.
After that, we can create our patient object. But before that, let’s minimize this project main. Let’s say patient, let’s name the patient is equal to new patient. First of all, we
need a naming here, let’s say Brad. After that, we need the age, let’s say 28. After
that, we need the left eye which I can say new I. For this, I first of all, we need a
name, let’s say left eye. After that, we need the medical condition, which I’m going to
say short sighted. For the color, let’s say blue. For the is open Boolean, let’s say true,
we need the same thing for the right I knew I let’s just change the name to right i. Also,
let’s change the medical condition to normal. After that, we need the heart, let’s say new
heart. For the medical condition, let’s say normal. And for the rate of the heart, let’s
just say 65. I think the next organ was a stomach. So let’s say new stomach, the main
would be Islamic. The medical condition, I’m just going to enter a name of some medical
condition, I’m going to say p UD public services in case if you’re wondering. And for the ease
empty, let’s say false. After that, we need a skin let’s say new skin for the color of
their skin. Or First of all, we need the name, let’s say a skin for the medical condition,
let’s say burnt. After that, what do we need, we need the color and also softness for the
color, let’s say right? For the softness, let’s say 40 out of 100. Okay, that’s our
patient object. Now we need to create the logic for our application. First of all, I’m
going to print the name and age of the patient. So let’s say print name. Plus patients that
gets me after that do the same thing for the age. We are going to receive the user’s input.
So we need a scanner. Let’s say a scanner scanner is equal to new scanner. Let’s fast
system that. Also, now that we know about object oriented programming, we know how this
scanner is working. This is just the class with this new keyword we are instantiating it inside the constructor of this scanner, we need this system.in whatever that is. So
this way we have created an instance of this scanner. After that I’m going to define a
Boolean, let’s say Boolean. Let’s name it short finish. And initially I’m going to set
it to false. Later on, we will see that how it This one is going to be useful. After that
I’m going to create a while loop let’s say while should finish is not equal to true,
continue looping. Inside the while loop, I’m going to show a list of different organs.
For that I can say choose an organ. In here I have two options. I can use this system
dot out dot print lines line after line. Or the better option is to use backslash n. Let’s
see that as well. First of all, let’s go to the next line in order to have a cleaner code.
In here I can say backslash n, this backslash n moves the cursor to the next line. The other
one that I’m going to use in here is called backslash t. This backslash T will add a tab
to our text. Later on we will see exactly what these two are doing. Okay, the first
organ is left eye The next one is right I After showing this list to the user, I’m going
to receive the user’s input. So I’m going to say int choice is equal to a scanner dot
next int. We have seen this in previous videos. So I’m not going to explain in here, you’re
just going to receive the user’s input. After that, depending on this choice, I’m going
to create a switch statement, let’s say switch on choice. And let’s create our different
cases. In case the user’s choice is one, first of all, we are going to print the details
about the left eye. So let’s say patient that gets left I don’t get details. Next, I’m going
to give the user the option to open or close the eye. For that, I need to check that if
the eye is opened or closed. So let’s say if patient that get left eye that is opened,
we need to close it, or at least we need to give the option to close by, let’s bring something
in here. Let’s say backslash, D, backslash D in order to have two tabs. After that, let’s
say close or close the next we need to get the user’s input in case if it’s one, we are
going to close the eyes. So let’s say if a scanner dot next int is equal to one, then
we need to say patient dot get left eye dot, close the eyes or close. But in the else case,
we are just going to continue to the next record. In the else case of the first if statement,
this finding here in case the eye is not opened, we are going to show the option to open the
eyes. So let’s say else, we need the same logic, we can print something backslash t
backslash t Vaughn opened it. Next, we need to get the user’s input. Let’s say if scanner
dot next int is equal to one, we are going to save patients that get left eye dot open.
In the else case, we are just going to continue. By continue I mean, once again, we are going
to show the list of different organs. Okay, that’s our first case. We also need a break
in here. But let’s write the second case. The second case is the right eye. We basically
can copy and paste all of these logic. But we just need to change the left eye to right.
So for example, in here, let’s say get right. Okay, we are done with our second case. Let’s
also write the case for the heart. Let’s say case three. First of all, let’s show the details
of the heart, let’s say patient that get heart dot get details. After that, we are going
to give the user to change the heart rate. for that. Let’s say backslash t backslash
T one, change the heart rate. Once again, we need to listen for the user’s input. So
let’s say if the scanner dot next int is equal to one, we need to ask for the new heart rate.
So let’s say enter a new heart rate. After that, we need to save that heart rate. So
let’s say int is equal to a scanner dot next. After that, we can say patients that get heart
dot, let’s say set rate. And let’s pass our new heart rate. But in the else case, like
before, we are going to continue. We also need a break in here. Let’s quickly add that
and also after changing the heart rate, it’s better to show some message. Let’s say heart
rate change to plus patients that get heart dot get rate. Okay, that’s our third case.
The fourth one was for the stomach. So let’s say case four. First of all, we need to print
the details of the stomach patients that get the stomach that get details. For the stomach,
we had the digest option. So in here, let’s show that option. backslash D, backslash D.
Let’s see digest. Let’s see if the scanner dot next int is equal to one.
If that’s the case, let’s say patient dot get stomach dot digest. But in the else case,
let’s just continue. We also need a break in here. Let’s quickly add that the fifth
case is for the scheme. Let’s say case five For the scheme, we didn’t have any option,
we just need to print the details, let’s say patient dot get scheme, dot get details. Of
course after that we need to add a break. In here, I’m going to add a default case.
And if the user enters any other number than these five, we are going to create the application.
For that we are going to change the value of this short finish Boolean to true. After
that, we need a break. Okay, I think our application is ready to test let’s see how did we do.
In here, we can see the name and age of the patient which is good, we can also see the
list of our organs. Once again, it’s good. Let’s select left eye, we can see the details
of the left eye. And also we have this close the eye option. Let’s put another number beside
this one, let’s say to once again, we can see the list of our organs. Let’s see if we
can close the left eye. Let’s put one in here. And as you can see, the left eye has been
closed. Okay, let’s check the right eye. It’s working the same. Let’s enter two in here
in order to show the list of organs. And let’s select our heart, we can see the hearts details,
which is good. Let’s see if we can change the heart rate. Let’s select one in here,
the console is waiting for the new heart rates, let’s say 75. And as you can see, heart rate
has been changed to 75. Let’s check it once again, in order to make sure of that, let’s
say three. And as you can see, the heart rate is 75. Let’s go to in order to show the list
of organs. And let’s select four in order to see their stomach, we can see all of the
details of the stomach. Let’s digest some food. And as you can see digesting has begun.
Let’s check the skin which is five, you can see that we are getting the details of the
skin. But we don’t have any option. That seems to be good. And if you enter any other number,
then these five we should create the application. Let’s enter seven in here, for example. And
as you can see application has been finished successfully. Okay, it seems like our application
is working fine. Let’s just delete the getter and setter methods that we said that we will
delete if we didn’t use them. Let’s check the classes one by one. First of all inside
the iclass. Everything that is grayed out It means that we never used that, for example,
we never used this set color method. Okay, it seems to be good. Inside the heart class.
I think we have used all of them. Okay, that’s fine. Inside the organ class, we didn’t need
this set medical condition method. And also we didn’t need this setname method inside
the patient class. Let’s see what do we have in there? And I think we didn’t use any of
the setters of this patient class. Let’s just delete them. Okay, that seems to be good.
Let’s see, what do you have inside a scheme class. And in here, we never use these setters
and this get softness method. Let’s delete them inside the stomach class. Let’s delete
this set empty method. And in order to make sure of everything, let’s just run our application
one more time. Okay, it seems like we don’t have any compile time error, which seems to
be fine. Okay, that was our challenge about object oriented programming concepts. I will
upload the source code at the links that you can see on the screen, feel free to check them and also, I’m more than happy to see your feedback about the code. In the next
video, we will talk about collections in Java. See you in the next video. In previous videos,
we have seen simple arrays in Java. For example, if we wanted to store a list of different
names, we could have said something like this because I’ve set a string with a pair of a
square bracket. After that, we could have named our array. And after that we could have initialized our string array like this inside a square bracket in here, we needed to pass
the size of our array for example five. The other way was to pass our elements at the
time of instantiating our array for example, we could have said a pair of curly braces
and inside those curly braces because I’ve passed our elements. Let’s pass few names in here.
After that, if we wanted to have access to these elements, we could have say something like this, we could have said print, let’s say names with an index of let’s say two for
the third element, which is Brad in this case, if you’re on our application right now we should see Brad. Let’s run it. You can see that Brad has been printed but if you remember
I said that using these kinds of simple arrays in Java has a lot of limitations. For example,
the first limitation is that their size is immutable, it means that their size cannot
be changed. Right now we have five elements inside our names array. If we want to add
another item inside this array, we cannot do that. For example, we cannot say something like names with an index of five, which indicates the sixth element, we cannot assign a value
to that, you can see that in here we have a red warning. And if we try to run our application,
we get an exception. Let’s see the exception. The exception in here is array index out of
bounds exception. Also, when working with simple arrays in Java, you don’t have that much options. Later on, we will see that when we use different kinds of collections, we
can operate all sorts of operations on our collections in which we do not have them in
simple areas. If we want to add a sixth element to our array, we can create a new array, and
after that, we can copy all of the elements from inside this array. Right now for the
situation that we have in here. If we want to add another item to our array, we can create
another array with the size of six. After that, we can copy all of these elements to that array. And after everything, we can add this new item. For example, in here, I can
say a string array. Let’s name it new names, is equal to new string array with the size
of six. After that I can create a for loop let’s say for int i is equal to zero i less
than names dot length, and i plus plus. And inside that for loop, first of all, I need
to add all of the names to this new names array, I can say new names with an index of
i is equal to names with an index of oil. This way, we can copy all of the elements
from inside this names array to this new names array. After copying all of the elements.
After the for loop, I can say new names with an index of five is equal to Jerry. This way
we can get around this problem. For example, if I comment this line and run my application,
maybe after that, print the six element inside our new names, we have a solution. But as
you can see, this solution is not that much effective. Let’s quickly print this. You can
see Jerry has been printed in here. But as you can see, there are a lot of codes involved. And beside that if your array has a lot of elements, for example, a million elements,
this process can be really time consuming. Besides that it can be resource consuming
if you are downloading this array from the internet. So this solution in here is not
effective. Instead of copy and pasting the whole array, we are going to use collections.
Let’s delete all of these codes. In Java, we have multiple kinds of collections. The first of which I’m going to talk about in here is called ArrayList. Let’s begin to see
that so we type array list with capsule a. After that inside a pair of anchor bracket,
or as some might call them diamonds, we need to define the type of our ArrayList object
for example, I can say is string. After that we need to name our ArrayList. For example,
I can say names, is equal to new ArrayList. As simple as that we have an ArrayList This
is one way of defining your ArrayList. There is also another way, let’s quickly see that so instead of saying ArrayList string, we can say list, you can see that the icon in
here is different. It’s an interface. We will talk about interfaces later on in the course.
But for now just know that there are two ways of defining your ArrayList. Let’s see how can we use this list to create an ArrayList we can say list of strings for example. Once
again, let’s name our list. For example, let’s see students is equal to new ArrayList. There
is a slight difference in here. But I just wanted to show you both ways of defining your ArrayList. Okay, let’s delete this line for now. Right now our ArrayList is empty. And
if we want to add some elements to this ArrayList, we can simply say names.ad,
we can use this add method in order to add elements into our ArrayList. For example,
let’s add one. You can use this add method as many times as you want. If I want to add
another element, I can simply do that. So the first benefit of using array list is clear.
Now the size of array list is mutable, it means that we can change the size of array
list. After adding some elements to your array list. You can get those elements by using
the get method. Let’s quickly say that, for example, let’s print the first element I can
see names that get you can see that this get method requires an index. Once again, indexes
in Java start from zero so if I pass zero in here, it means that I’m going to get the
first element. Right now if I run my application, I should see my Sam printed into the console,
you can see that Mason has been printed. Also when using array lists, you can also get the
size of your array list. For example, if I want to print the size of my ArrayList, I can say something like this, I can say names dot size. Let’s run the application and see
what would be the result, you can see that the size of our ArrayList is two, you have all sorts of options when using array lists, you can also clear all of the elements inside
your ArrayList. For example, I can say names dot clear. This clear method removes all of
the elements inside your ArrayList. After clearing, if I once again get the size of
my ArrayList this time the size should be zero. And as you can see down in here, it’s
zero. If you want, you can also remove one element from your ArrayList. Let’s quickly see that I can say names dot remove this remove method requires an object, it means that we
need to pass the whole element. So let’s say may sound for example. But before that, let
me quickly comment this line because we don’t want to clear our array list. And after removing
my son, let’s print the first element inside our array list. Let’s say names that get and
let’s pass zero as the index. As you can see, the first element right now is Sarah, it means
that my Sam has been removed successfully. Let’s see what other options do we have on our ArrayList. Let’s like names by typing dots, we can see the list of options, we can
also check that if an option exists in our ArrayList. For example, if I use this contains
method, once again, it requires an object, let’s say Mesa, let’s bring this whole line
of code. This contains method returns a Boolean. So right now if I run my application, because
we have removed my sample we should see false, we can see that that’s good. But if we change
this to Sarah, we should see through. And through this, you can also check that if your
ArrayList is empty or not. For that you can print something for example, you can say names.is
empty, we have this method once again, it’s returning a Boolean. Right now we have zero
in our ArrayList. So it shouldn’t be empty. And as you can see we are receiving false.
But if we remove Sara before this line of code we should see through, let’s quickly do that names dot remove. And let’s pass this time you can see that through has been printed,
you can also get the index of some element from inside your ArrayList as well. Let’s
quickly see that before everything I’m going to comment all of these lines of code because I want to have a clear console.
down in here. Let’s say print names that index of and in here I can pass my object for example,
I can pass Mesa. Right now Mason is the first element so we should see zero printed, you
can see that it’s zero. But if we don’t have that element inside our ArrayList. For example,
if we pass Brad in here, we should see negative one. And as you can see negative one has been
printed. When you’re using ArrayList you can only have objects as the type of elements
inside your ArrayList. For example, in here, I cannot say integer, you cannot use primitive
data types when you are creating a new ArrayList. As you can see in the error in here, the error
says that type argument cannot be primitive type. If you want to have an ArrayList of
different numbers, you can use the equivalence Java class. For example, in here I can say
integer with a capital I. And now I have an ArrayList of integer. Right now I am getting
an error because I’m trying to add some strings to this integer array list. But you get the
idea of we can use this integer class in order to have an ArrayList. So in Java equivalent
to every primitive data type, you have a class as well, we have seen this integer. Let’s
also check others, we can say Boolean similar to that we can say long, we can also say double.
And you can check the others yourself. But let’s change this one to a string right now. The other option that you have when you’re using ArrayList is sorting different items.
For example, if you want to sort different elements inside your array list alphabetically, you have that option. And the way to do that is like this. Let’s see that down in here
we can see names that sort you can see that this sort method requires some comparateur
later on in the course we will see this comparateur In fact, we will use this sort method in order
to source different elements in our array list. But for now, I’m not going to talk about
this I just wanted to say that you have this option. Okay, let’s delete this and let’s move on from this part. Similar to simple arrays in Java. If you want to eat
To read through all of the elements in your array list, you can use for loops. Let’s quickly
see that as well. You can say for int i is equal to zero, i less than named that size,
this time, i plus plus. Inside this for loop, we can say, print names that get with the
index of AI. This way, we can print all of the elements in our array list. But before
that, let’s comment this line. And let’s run our application, you can see that we can see
both of our elements printed. Okay, that’s enough talking about array lists. Let’s also
talk about another kind of collection in Java called maps. maps are useful for when you
have some key value pair data. Let’s define a map and we will talk about what I mean by
key value pairs. You can define a map like this, you can say map you can see that map is an interface. We will talk about interfaces later on in the course I believe in the next
video. But for now, let’s move on. You can also see that inside these anchor brackets
inside these diamonds, we have these K and V these two stands for key and values. So
in here, if we say anchor bracket, for the first element, we need to pass the kind of key the data type of our keys. For example, I can say string. And after that, we need
to provide the data type of our values. For example, I can say a string once again, for the data type of my values. Right now I’m getting a red warning. And that’s because
it seems like the ID hasn’t imported maps into my class, I can press ALT plus Enter.
And if we take a look at above, in here, it seems like we have imported maps into our
class successfully. After defining the data type of your keys and values inside your map,
you need to name your map. For example, I can say contacts. In here, I can say new maps.
But if I do that, you will see that there are a lot of methods that I need to be worried about, there is a better way of defining your maps, let’s press Ctrl Z in order to undo
the change, I can say new hashmap. This time, you can see that this hash map is a class.
And if you want we can instantiate our map this way, this time, we do not have all of
those extra methods. I don’t intend to talk about hash maps in here, the topic is a bit
technical, it’s about the way that we are going to iterate through our maps elements.
It’s way above the talk that we have in here, I just wanted to say that you can instantiate your maps as different hash maps. Okay, now you have an empty map, which the data type
of different keys is a string. And the data type of different values is a string as well.
If you want to put some elements to this map, you can say something like this, you can say
contacts that put this is the method that we are going to use in order to add some elements to our map, you can see that right now this port method is waiting for a key and also
a value for the key. For example, I can pass a name, let’s say me, Sam, and for the value,
let’s pass his email, let’s say me [email protected] This way we can add elements to our map, I
think if I change the name of my contacts map to let’s say email list, that would be
much more better. In order to change the name of some variable or class in Java, you have
an option, you can select the whole name, you can right click on the name of your variable,
you can go to this refactor in here, and you can use this rename or alternatively, you
can use the shortcut, which is shift plus f6. Let’s select that. And in here, I’m going
to delete this whole name, you can see that once I’m deleting this, it’s deleting the name in both places. So let’s say email list. And let’s press enter. I think this name is
much more suitable. Let’s add another element to our map. Let’s say email list dot put,
let’s say Brad. And let’s say [email protected] Now that I have some elements in my map, if
I want to have access to those elements, I can say something like this. For example,
let’s print this statement. Let’s say email list dot. And once again, you can see that
we have this get method which requires an object for the object in here we need to pass the key. For example, if I pass Mesa, as the result, I should see the value of Mesa. In
this case, I should see the email. Let’s run the application. And let’s see if we can print the email of maysa. You can see that the email has been printed successfully, like array
lists. When you are using maps, you have all sorts of options. For example, if you want to get the size of your map, you can say something like this you can say email list dot size.
Let’s run the application and let’s see if we can get to two has been printed. If you want to remove an item from your map you can say email list that remove once again this
remove required As an object, in this case, we need to pass the key. Let’s pass Brad.
And let’s print the size of our map once again. This time after removing Brad, you can see
that the size is one. You can also check that if some key exists in your map or not. The
way to do that is like this. Let’s bring that first of all, we can say email lists that
contain keys. Similarly, we have contains value, which you can guess it will check that
if some value exists in your map. Let’s try the first one contains key. And let’s pass
Brad in here, because we have removed breath, we should see faults. And as you can see,
false has been printed. But if I change this to Mesa, we should see through. Okay, that
seems to work fine. We can do the same thing for the values. Let’s say email list dot contains value. Let’s pass the May Sam’s email may [email protected]
Because it does exist in our map we should see through and through it is you can check
other options. yourself. If you want. We can say email list dot once again, you can see
that we have this clear option, it will clear all of the elements of your map you have this
is empty method, it will check that if your map is empty or not. The other option that
you have in here is this values, you can see that this values method will return the collection.
If you want to copy all of the values of your map, you can use these values and you can
save it inside a collection. But I’m not going to do that in here. But to just show you I’m
going to say that there is an interface in Java called collection this morning here in
which is the generic type of all of the collections available in Java. But more on that later
on when we source different items inside our ArrayList like ArrayList, you cannot have
a primitive data type as the data type of your key or your values when you are using
maps. For example, in here, I cannot say int if you take a look at the warning, it says that type argument cannot be of primitive data type. Similarly for the values, let’s
say int, once again, you can use the equivalent Java class for these primitive data types.
But you cannot just use primitive data types. Okay, let’s roll back everything. Besides maps and array lists, you have other kinds of collections in Java as well, I don’t want
to talk about them in here, because I think that for the Android course, these two are
enough and you don’t need to know about others. But if you’re curious, you can always check
this Oracle webpage, you can see that we have sets, lists and other kinds of collections.
But honestly, these are not going to be helpful for our Android course maps. And array lists
are just fine. As the data type of key values inside maps, and also values inside the ArrayList.
You can also have different classes. Let’s quickly see that as well. In my source folder
inside the package, I’m going to create a new class called student. Let’s quickly see that inside this student class, I’m going to define two new fields, let’s say private
string name, and private integer ID. Let’s have a constructor and some getters and setters. Now that I have created this
class, I can pass it as the data type of my ArrayList. Let’s quickly create another array
list down in here. I can say ArrayList of different students This time, let’s name this
ArrayList students, let’s say is equal to new ArrayList. Now you can put as many students
as you want inside this ArrayList. For example, let’s say students that add let’s say a new
student, let’s say may sound with the idea of 15, maybe. So beside the strings, other
kind of customized datatype is agreeable with the collections in Java. Okay, just before
I finish off this video I’m going to talk about for each loops in here. If you remember when we talked about loops in Java, I said that there is another kind of loops called
for each loop, I said that we are going to talk about for each loops when we know about
object oriented programming and also collections in Java. So I think now it’s a good time to
talk about that you can create a for each loop like this, you can say for instead of saying int i is equal to zero, I’m going to pass the datatype of the list that I’m going
to iterate over. For example, in this case, I’m going to say a student. After net I need
a name. For example, I’m going to say s and after the name, I need a column. After that
I need the list or ArrayList that I’m going to iterate. For example, in this case, I’m
going to say students, let’s review everything once again. In here. First of all we have
passed the data type of the object that we are going to look into the name of that specific object is as you can name it, whatever Do you want after the name, you should have a
colon. And after that, you need to pass the collection that you’re going to look into
inside this for loop, you can use this s in order to have some sort of operation on your object. For example, if I want to print the name of my students, I can say, print s, that
gets me right now our ArrayList has only one student, let’s quickly add another, let’s
say students that add a new student. Let’s say Sarah, with the idea of maybe 18. Let’s
run the application and see what would be the result. As you can see down in here, Mason, and Sarah has been preempted. So this is in here is like I in a for loop. But instead
of being the index of the item, it’s the item itself from time to times for each loops can
be useful and this is how you can create them. Okay, I think that’s a good point to stop the video in here. In the next video we are going to talk about some small concepts. For
example, we will talk about this static keyword that we have seen in the declaration of our
main method. Besides that, if we had time, we will talk about inner classes, we will talk about interfaces and abstract classes See you in the next video. In this video,
we are going to talk about some small concepts that we need to know about. The first of those
concepts is this static keyword that we have seen so far in the declaration of our main
method. I’m going to talk about that in another class. So let’s create another class in my package. Let’s name this class test class. You have seen inside the main class that we
can have this static keyword for our methods. But besides that, we can also declare our
variables as a static as well. For example, in here, I can say public, let’s say static.
After that I can say a string. Let’s say name, you can see that it’s working fine as well.
I’m not sure if you have noticed or not. But as soon as I declare this a string as a static,
the style of this name has changed to italic. If I remove that you can see the difference.
But what does it mean to have a static variable or a static method? Well, when you define
your variables and methods as static, those methods and variables belong to the object
itself and not to the instances of that object, we will see what I’m talking about. But before
that, let’s quickly have other variables in here. For example, I can say public int, age, maybe another one public, let’s say a string, a skin color. These variable
doesn’t mean anything, I just want to have some variables. Let’s also create a constructor in here. Notice that when I use the intelligent generator to create my constructor, I do not
see my static variable in this list. Once again, that’s because the static variables do not belong to the instances of the object ID belongs to the object itself. Let’s select
these two. And let’s create getters and setters. But as you can see, we do have a getter and
setter for this static variable. Okay, now that we have this class, if we want to create
an instance of that class, for example, from inside our main class, if you say test class,
let’s name it test class is equal to new test class. The constructor in here is waiting for an age and also a skin color, let’s say 25. Right? This way, we can create our instance
of the class but how do we assign a value to the name variable farming site artists
class? Well, you can use the setter methods. For example, you can say test class dot set
name, this method and you can pass a name for example, Mason, but there is a warning
in here this highlighted warning. What does that mean is that you can set a value for
this name variable even without instantiating your class. For example, in here, I can say
test class with a capital T dot, let’s say name. Or alternatively, because the setter
method for this name is static as well, I can say set name. Let’s use the first one,
I can say test class dot name is equal to for example, Mesa, we can see that even without
instantiating this object, we can assign a value to this variable. In order to make sure of that let’s move these two line of code to after assigning a value to the name variable.
I can also delete this setname method safely. Now, if I want to have access to the name
variable of this test class, I can say something like this. I can say name plus test class
this time with flirty. That let’s say getname. Let’s run the application and let’s see what
would be the result. You can see that message has been printed even though we have set the
value of this name variable before instantiating our object so whenever you use a static variables
that variable will be the same in all instances of your class. For example, if I have another
instance of this test class, for example, if I say test class, second test class is equal to new test class, maybe another age and another skin color,
let’s say 30. Black. If I print the name variable of the second test class, the result would
be the same. Let’s say second test class dot get name, or for that matter, we can say name.
Let’s run our application, you can see that my Sam has been printed. Besides variables,
methods can be as static as well, let’s quickly define a static method inside our test class,
let’s say public static void. Let’s name it print something. And inside this method, let’s
just print something. This static method like a static variable belong to the object itself
and not to the instances of this object. For example, in here, once again, I can say test
class with a capital T dot print something, you can see that we do not need to instantiate
our object. If you’re on our application, you can see that something is printing. Okay,
now we know that static variables and methods belong to the object itself and not to the instances of that object. But why would we want to use a static variables and methods
there are a few reasons for that. First of all, you may want to have a constant variable in all instances of your class. For example, imagine that you are creating a class for
simulating different employees of a company inside that class you may have some information
about the employee, for example, the age, the name, email, and everything. But besides all of those, you may have a field called company name, you know that in all instances
of your employee class, the company name should be the same for that matter, you may want to declare your company name field as a static also, some say that using a static variables
and methods is very memory friendly, because there can be only one instance of that variable
or method in all of your application. So no matter if you have 1000 employees in your
application, the company name field in all of those instances will occupy only some spaces
as a string can so using a static variables and methods is very memory friendly. But there
is a downside when you’re working with this static keyword. Let’s quickly see that inside
our test class as well. Let’s quickly create another method in here, let’s say public void.
Let’s name it print, notice that this print method is not a static and because this is
not a static now, we cannot have access to that method from inside this print something
method for example, in here I cannot say print, you can see we have a red warning in here.
It says that non static method print cannot be referenced from a static context similar
to non static methods, you cannot have access to non static fields inside the static method
as well. For example, in here if I try to print the age of this test class, you can
see that we are getting a red warning in here. Once again, the warning says that non static
field cannot be referenced from a static context. So you cannot use non static fields and methods
inside a static method. Okay, I think that’s enough talking about this static keyword. Let’s quickly delete this class. Also, we need to delete all of these lines of code.
Okay, now we know everything about this main method. First of all, its access modifier
is public. It’s a static method, its return type is void, its name is main. And also as
the input of this method, we are receiving a string array called arguments. Because this
main method is a static, we know that there can only be one instance of this main method
in our entire application. We are not concerned about the voice in here, but we know that
in order to run Java applications, we need a main method, which is a static with this
specific syntax. Okay, that’s enough. Let’s move on. The next topic that I’m going to talk about in here is inner class. Let’s quickly see that in another class called test class.
Inside our classes, besides variables and methods, we can also have another class and
here is how we can define that class. We can say private, let’s say class, I’m going to
name this class test inner class. And here we have this inner class like before, inside
this in our class, we can have other methods and variables. For example, I can say private
string name in here. I can have a constructor for this class as well. Let’s quickly see
that you can see we can create the constructor we can create all sorts of getters and setters.
And we can treat this class as any other class in Java. But there are a few differences between
a class and an inner class. For example, if we have some fields in our parent class, that’s
Let’s define some, let’s say private int, age, and private string color. No matter what
the access modifier of these fields are, we can have access to these inside our tests
in our class. But we need to do that inside another method, let’s say private, void, print
the page. Inside this method, I can say print, let’s say age plus age, you can see that it’s
working fine. Even though the access modifier of this age is private, we can have access
to that from inside our inner class. The same is for different methods of your parents. Plus, no matter what the access modifier of those methods are, you can have access to
them from inside your inner class, there are some times that you may want to use inner classes for example, when you use inner classes, your code might be much more easier for us
to follow. The other reason for using inner classes is that you can have this private access modifier at the declaration of your class if you remember, we cannot change this
public access modifier when we create our parent class for example, in here I cannot say private you can see an error in here and if you hover over that you can see that modifier
privates not allowed in here beside private access modifier You can also have a static
classes. That is another benefit of using inner classes you didn’t have this option
when you created your parent class let’s quickly see that if I say static in here once again
modifier static not allowed in here. So within our classes, you can have private and static
classes in this course, we are not going to use inner classes that much. In fact, the first time that we are going to use it is when we know about async tasks. In most cases,
we are going to define our async tasks as inner classes. We will talk about that later
on in the course. Okay, I think that’s a good point to stop the video in here. I did want to talk about interfaces and abstract classes in this video. But I think that’s better to
have that discussion in the next video. So see you in the next video.
As I said at the end of the previous video, In this video, we are going to talk about interfaces in Java interfaces are like a contract between two parts of your application. Let’s
see what I mean by that you can create an interface like you have created a Java class, you can right click on your package by selecting new Java class. And then in here, by selecting
interface, you can have an interface Let’s name this interface card interface. If you
take a look at your package, you can see that we have created this interface. And if you take a closer look, you can see that the icon for this interface is somehow different from
the icon of classes in Java. This is the general schematic of an interface in Java, like classes,
interfaces, lives inside the package, they have this public access modifier, we can name
our interfaces, whatever we want. And also they have this interface keyword in their
declaration. Inside interfaces, we can define our abstract methods, let’s quickly see an
abstract method. I can say public, let’s say void. After that, let’s say print name. As
the input of this method, let’s just receive a name, let’s say a string name. And that’s
it, you cannot have a body for your abstract methods. For example, in here, if I put a
pair of curly braces, you can see that I’m getting a red warning. And the warning says that interface abstract methods cannot have body. So I can delete these two curly braces
and I can finish my sentence in here. By using abstract methods you are hiding the functionality
of your methods. For example, in this case, we don’t know what this print name method does. Instead, we are just defining some sort of contract. The contract in here is the signature
of our method, we know that the name of this method is print name, we know that we are receiving a string called name. And also we know that the return type of this method is
void. The signature of this method is a contract in which ferrovie implements this interface,
we are going to use this signature. Also if you take a look at this public keyword, you can see that it’s somehow grayed out. If you hover over that keyword, you can see that
modifier public is redundant for interface methods, it means that it’s not necessary
in here. And the reason for that is because all of the abstract methods are public inside
an interface. It means that you cannot have private or protected abstract methods. Inside
your interface. You can have as many abstract methods as you want. For example, if I want to define another one in here, I can say void. Let’s name this one to start. And I’m not
going to receive anything as the input of this method. Let’s define another one. Let’s name the spawn move. And also let’s receive an integer call. Speed, I created the first
method so that I can show you how you can create abstract methods. And I’m going to delete that in here. So this car interface has two methods start and move. But how can
we use this interface? Let’s quickly see that as well. Inside my package, I’m going to create
a Java class called electric car. And inside this electric car, I’m going to implement
that interface that I just created. In order to implement that interface, I am going to say implements at the declaration of my class. And after that, I need to provide the name
of my interface. In this case, it’s called interface. But once we do that, you can see
that I’m getting a red warning in here. And for this red warning, we need to implement the two methods that we have created inside our interface. In order to implement those
two methods, we have multiple options. First of all, we can click on this red light bar.
By selecting implement methods, we can implement the necessary methods. Or alternatively, if
you don’t see this red light, well, you can click on the error and by pressing ALT plus enter, you can see the same dialog or the other option is to come inside the declaration
of your class. And by pressing Ctrl plus i, you can see the list of necessary methods.
In this case, we have a start and move methods. Also, if you press Ctrl plus or you can see
the list of all of the methods that you can override. In here, you need to be a little bit careful because all of these methods are none necessary. The methods that are necessary
are these two methods. If you take a closer look at the icon of these two methods, you can see that the icon is somehow not completed. These are the two methods that we need to
implement or override. Let’s select them. And in here we have a body for this start
and move method, it means that we can define the functionality for these two methods in here. For example, in the start case, I’m just going to print something. Because it’s
an electrical car, I’m going to say electricity flow started. Let’s also define a field for
this class. I’m going to save private string name. We can also have a constructor by pressing
ALT plus insert, let’s create that constructor. And let’s also create getters and setters.
down in here, let’s also define some functionality for this move method. Once again, I’m going
to print something, let’s say this that getname plus moves at some speed.
Okay, now that we have created this class in which implements our interface, how can
we instantiate our interface for that we can switch to our main class. And inside this
main class, we can say car interface. Let’s name it car interface is equal to in here
I have two options. First of all, I can say new car interface like this for Java classes.
But if I do that, you can see that we are overriding these two methods. Sometimes this
might be our desired behavior. But in here, I’m not going to use this way. Instead, I’m going to say is equal to new electric car. This one, let’s also pass a name, let’s say
Tesla, you can see that we are assigning a class to an interface. But how is it working? Well, because when we have created our class, we implemented this interface. Now Java compiler is happy with assigning a class to this interface. Now that we have instantiated this interface, we can use its methods. For example, I can say car interface dots, let’s say start. And also let’s say car interface dot move, we also need to pass a speed, let’s pass 60. Let’s run the application, you can see that electricity flow started and Tesla is moving
at 60 miles per hour. You can see that even though inside our interface, we didn’t have any body. But we are printing some functionality. When we call the start method on our interface. If you want to make sure that this start method is happening inside your class. You can also print something else in here for example, inside this start method, let’s print the name of this class. I’m going to pass this if you remember I said that this refers to the current object. In this case, it’s referring to this class and right now if I print this,
it will print the address of our class. Let’s run the application. You can see the address of our electric car class. It means that this start method is happening inside our electric car class and not inside the interface. If you remember when we have talked about collections, we have defined array lists in two ways. One of those ways was to define our array list as list for example, because I’ve said list which once again you can see that is an interface. For example, the stuff a string, let’s name it names is equal to new ArrayList Probably
now you understand this syntax in here, it means that we have this ArrayList class in which implements this list interface. If you want to make sure of that and see it for yourself, you can press down the CTRL key on Windows, I believe it’s Command key on Mac. And by clicking on this ArrayList, you can see the documentation for your ArrayList class. Let’s scroll a bit above in here inside the declaration of our ArrayList class, you can see that we are implementing this list. Don’t worry about this in here, it means that we are going to
accept any kind of object but as you can see if you are implementing this Okay, let’s move on from here. Now that we have our interface, we can also have another class in which implements this interface. Let’s quickly create another class, let’s say new Java class. Let’s name this phone fossil fuel car. Once again, in here, I’m going to say implements. Let’s say car interface, we need to implement two methods. Let’s press Ctrl I, and let’s select our two methods. Once again, let’s define some functionalities for these two methods. For example, let’s
bring something in here. Let’s say explosion in cylinder cause the engine to start. Before I define some functionality for this move method, let’s quickly add a field above in here like before, I’m going to say private, the string name. Let’s create the constructor and also getters and setters. After that insight, the move method, I’m going to say this car is moving at some speed. Now that we have created this class, we can instantiate our interface inside our main class, like this, let’s delete this line of code. In here I
can say car interface. Once again, let’s name this one fossil car interface. Like before, I can say new fossil fuel car. Let’s pass Mercedes in here, for example. Once again, you can see that Java compiler is happy with this way of instantiating our interface because we have implemented this car interface inside both electric car class and also fossil fuel car class, we can instantiate our car interface in both of these ways. And after that, I can say fosu car interface dot start. And let’s see fossil car interface
dot move. Let’s also pass some speed in here and let’s run our application. You can see that both of these methods have been called in here, I have a point by defining two classes and implement the same interface in both of these classes. My point is that when you create an interface, that interface is a contract between different parts of your application. For example, in this case, I’m defining multiple points of cars. No matter what kind the car is, the contract says that discourse should have a start and also a move method. So probably
by now you can see the usage of interfaces. But there are a lot more two interfaces. For example, we can use something called callback interfaces. callback interfaces are extremely useful for when you create event listeners like clique listeners. And also they are useful for when you want to create some sort of connection between two different threads. We will talk about threads in the next video, don’t worry about that, we will see the use of interfaces when we want to create a communication between two different threads. Also, it’s worth noticing
that you can implement multiple interfaces at the declaration of your class. For example, let’s quickly create another interface in here, I’m going to name this one test interface. For this, let’s just have a method in here. Let’s say sorry, for private, the abstract methods cannot be private, let’s say void print name. And let’s receive a name. After I have created this interface inside, for example, my fossil fuel car, after implementing
the first interface, I can add a comma. And after that I can provide the name of my second interface. In this case, it’s test interface. But once again, you can see that we are getting the error somewhere inside the declaration of our class, I can press Control plus i and i can implement my method. So you can have multiple interfaces implemented at the declaration of your class. Okay, I think that’s enough talking about interfaces. Later on in the course we are going to use interfaces a lot and Dave users will be much more clear. So
hang in there if something is vague. Okay, let’s delete all of these extra classes and extra interfaces. Let’s also delete these lines of codes. Now let’s talk about abstract methods. For that I’m going to create a new class inside my package. Let’s say new Java class, I’m going to name this fun test abstract class. If you want to make your class abstract, you need to add another keyboard between the access modifier and the keyword class. Let’s add abstract in here you can see public abstract class. And immediately after I’ve added this
abstract keyword, if you take a look at your package, you can see that the icon for this class has been changed. This means that this is an abstract class. inside an abstract class like interfaces. You can have abstract methods, let’s quickly see them. For example, I can say public abstract, let’s say void, and let’s name this font print name. Let’s receive a name string name. Once again, like interfaces. When you create abstract methods, you cannot have a body. For example, if I put a pair of curly braces once again, I can see the
warning in here like before I can finish my sentence with the semicolon. The use of abstract classes is a lot like interfaces. But there are a few slight differences. For example, let’s see the first difference in here inside my package, I’m going to define a new class, let’s name this one test class. When we wanted to implement some interface, we could have said implements. And after that the name of our interface. But when we want to use abstract classes, we can say extends, let’s say extends test abstract class. Right now we are getting
the warning again by pressing Ctrl plus if you can see the necessary methods. So the first difference is between this extends and implement keyword. The other difference is that when you want to use abstract classes, you cannot extend multiple abstract classes. For example, in here, I cannot add a comma and add another abstract class in another word abstract classes does not support multiple inheritance. So that’s the second difference between an interface and abstract class. The other difference is that inside your abstract
class, you can also have non abstract methods. For example, in here, I can say public, let’s say void. Let’s name this method print. Let’s receive something in here. For example, let’s say a string text. And because this is not an abstract method, we can have a body for this method. We didn’t have this option inside an interface, we can have public non abstract methods inside an abstract class. Like every other class, we can also have fields and maybe a constructor as well. For example, let’s say private string name. Let’s create a constructor
for this class, you can see that all is working well. So the other difference is that you can have fields constructors and non abstract fields. If you want to see the use of abstract classes like before we can switch to our main class. And in here, I can say test abstract class. Let’s name it test abstract class is equal to new test class, the class that extends artist abstract class. After that, we can use the methods for example, I can say this abstract class dot, let’s say print name. In here, we need to provide a name, let’s
say Mesa, but we didn’t have any functionality inside artists class, let’s quickly define some functionality in here. Also know that I’ve switched to this test class. Because we have created a constructor inside our test abstract class, we need to create the constructor in here as well. But for the sake of simplicity, I’m going to delete all three of these, because I just wanted to show that these three options are available inside an abstract class. Let’s bring something inside our test class inside this print method. Let’s just print the name.
Let’s run the application and let’s see if we can see Mason printed into our console. You can see my Sam has been printed. So once again you can see that inside artist abstract class, we have this abstract method which does not have anybody instead we have an extended this class inside our test class. And we have declared the functionality inside this test class, you can see that there are a lot of similarities between interfaces and abstract classes. Sometimes the decision for using an abstract class or an interface can be hard
for that let me show you an Oracle web page in which may help you to decide better you can see the address in here. I think if you check this webpage, it might be helpful. They say that use abstract classes when your classes are related to each other. Or the other case is that when you think that the class that is going to extend the abstract class may use some common methods or fields or also it may require some access modifiers other than public such as private and protected or the other case is that when you want to
declare some fields that are non static and non final, this option is also not available in interfaces, but they say that Use interfaces when your classes are not related to each other, or in cases that you want to use multiple inheritance we can see that when we use interfaces, we can implement multiple interfaces. Once again, I suggest you take a look at this webpage, it might be useful. Okay, I think that’s enough for this video. In the next video we are going to talk about concurrency and threading in Java See you in the next video.
In this video, we are going to talk about concurrency. In Java. concurrency means doing things simultaneously and at the same time. For example, imagine that you’re working with your phone and you’re reading a book at the same time, you may want to listen to some music simultaneously, you may be downloading some files from internet This is called concurrency or doing things at the same time in programming. In Java. The first option that we have in order to create a concurrent programming is to create a thread a thread is a unit of execution
in your device’s central processing unit or CPU. Basically, different threads come together inside something called a process. All of these processes will be handled by your device’s CPU. By default, when you run your Java application or for that matter, your Android application, a thread called the main thread will be created. Also, some might call this main thread the UI thread or the user interface thread as well. Later on, if you want to do some background task, for example, downloading a file, you will create a worker thread inside your main
thread. The first option in Java that you have in order to create a thread is like this. You can say thread, let’s name this thread is equal to new threat. Like the finding any other Java class. Let’s finish our sentence in here. The constructor of this threat requires an interface and that interface is the runnable interface, we can pass that like this, we can say new runnable, you can see that this runnable is an interface. When you pass this runnable interface, this run method will be created. Basically this run method is the
place that you put the code that you want to be handled inside of worker thread. For example, let’s print something in here. And let’s see if we can have a complete application. For example, let’s say hello from another thread. In order to start this thread, we need to come down after the declaration of our thread and say thread dot start, let’s run our application. And let’s see if we can see any result, you can see that our message has been printed into the console. But how do we see the difference between the main
thread and this worker thread for that I am going to write a piece of logic in here. For example, inside this run method, I’m going to create a for loop. Let’s say for int i is equal to zero, i less than, let’s say five, and i plus plus. Inside this for loop, I’m going to print the value of this I and after that I’m going to asleep the thread we will see how we can asleep the current thread. First of all, let’s say print printing, let’s say plus I plus, let’s say in a worker thread. In order to asleep your thread, you have multiple
options. The first one and I believe the easiest one is to say something like this you can say thread with a capital T dot sleep. Inside the parentheses of this sleep method you need to pass some numbers in milliseconds. For example, if I want to sleep my tread for one second, I need to pass 1000 milliseconds this asleep method is going to cause our loop to wait one second for the next record for example in here we are going to print zero after that we are going to wait one second. And after that we are going to print one but right now
as you can see, we are getting a warning in here and the warning says that unhandled exception Java dot Lang dot interrupted exception. In the previous videos briefly, we have talked about exceptions. We will talk about them in this video later on. But for now, you just need to know that whenever an exception occurs, your application would crash. So because of that, we need to do something for this error in here. The easiest way to overcome this error in here is to click on the error and press ALT plus insert. After that, select
this surround with try catch, you can see that this code has been added and our thread dot sleep method has been moved to this try block later on. We will talk about this. But for now let’s move on from this part. And let’s write the same logic inside our main thread so that we can see that the code inside the worker thread and the main thread can be executed at the same time. So basically, I’m going to copy this for loop. Let’s copy it from here and inside the main thread outside of this run method. Let’s base the button
here. I’m just going to change this message to main thread. Let’s run the application and let’s see If this code is going to work, as you can see, we are printing the value of oil two times. Once in a worker thread, and once in the main thread, it means that the code inside the worker thread and the code inside the main thread are happening at the same time. In this situation, we say that our application is asynchronous, or it is concurrent. So once again, when you run your Java or Android application, one thread
will be created by default, in which we call that thread the main thread or the UI thread. After that inside that main thread, you can create as many worker thread as you want. The simplest way to create a thread is to say thread thread is equal to new thread. And after that, we can pass a new runnable to the constructor of your thread in which we will create this run method that will be executed in a background thread. But by this way of defining your thread, there are a lot of considerations that you need to be worried
about. For example, you need to be worried about cancelling your thread when the work is done. Also, there are all sorts of new concepts, for example, thread pools, deadlocks and interrupting your thread. I do not intend to talk about all of those concepts in here, because in Android, we have all sorts of options available for handling background tasks. In fact, later on in the course, we have a complete section dedicated to handling background tasks. In Android, we have options like async, task services, Job Scheduler, and work manager,
we are going to talk about all of them. But more on that in future videos. Okay, now let’s talk about this interrupted exception that is happening in here. Basically, whenever an exception occurs, your application will crash and your application will be closed by the operating system. There are all sorts of exceptions that can occur. For example, the ones that we have seen previously, were arithmetic exception, null pointer exception, and the one that we can see in here is interrupted exception. So in order to avoid crashes, you
need to do something about the exception. Let’s comment all of these and let’s see what we can do about the exceptions. Let’s create an application in which we will create an arithmetic exception. In here I’m going to define two new integers. For example, let’s say int, a is equal to two, and int b is equal to zero. Let’s bring something in here. Let’s say print a divided by b. If we run our application, right, now, we should see an arithmetic exception. You can see that
down in here. The thing that we can do about this exception is that to catch it whenever it happens, and we can do that by using a try catch block. Let’s quickly see that for example, I can say try. Let’s move this line of code to inside our try block. We can say try this in case some exception happens catch that exception. The kind of exception was arithmetic exception. So let’s say arithmetic exception, let’s name this exception E. And inside the catch block, we can do something else with our code. For example, we can print
something. In this case, let’s say B was zero. Now, if we run our application, we wouldn’t get an exception. Instead, we are printing this message. Sometimes this message might not to be useful. And in fact, in most cases, this message will be useful for debugging purposes, but it’s much better than a crash. The kind of exception that we are catching in here is arithmetic exception. If we do not know the kind of exception, we can pass the generic term exception. If you pass this exception, it’s going to accept all of the
exceptions. If we run our application, once again, we should see the same result. And here is the same result. The other kind of exception that we have seen so far was null pointer exception that would occur when we do some operation on a null object reference. For example, if we had a string in here, let’s say a string name is equal to Now, if we do some operation on our name inside the try block, for example, if we say name, dot equals, let’s say, Mesa, if we do that, we are going to get a nullpointerexception. Because we
have done this dot equals method on a null object inside the catch block this time, instead of saying B was zero, let’s say name was no. If you run the application, it’s going to work fine because we are passing the generic term exception, but we can safely pass null pointer exception. Let’s run the application. You can see that name was no the other kind of exception that we just saw was this interrupted exception. It’s going to happen whenever we interrupt the current thread. For example, imagine that inside a worker thread, you are
downloading some file at the same time, you may receive a phone call. If you don’t catch the interrupted exception, you may get a crash. We have all sorts of exceptions. For example, we have audio exception, which stands for input output exception We have class cast exception, we have all sorts of other exceptions in which we are going to talk about them whenever we face them. Also on our exception, we have a useful method for debugging purposes. And that method is called print stack trace, we can see the name of our exception dot print
a stack trace, we will talk about this stack in future videos when we talk about activities and fragments in Android. But for now, just know that this line of code in here is going to print something into our console that might be useful when you’re debugging your application. Let’s run the application in here. You can see the stack trace of your exception in here it says that the exception occurred in line 34, which is this line, and you’re also seeing the name of your exception, this is not an actual exception, you’re just seeing some
information about your exception, this in here might be useful for when you’re debugging your application. Okay, I think that’s enough talking about concurrency and exceptions in Java. In the next video, we are going to talk about Singleton pattern in Java. See you in the next video. In this video, we are going to talk about Singleton pattern. Basically, we use Singleton pattern when we want to make sure that we have only one instance of some class in our entire application. In some cases, we may want such a thing. And here is how
we can implement that pattern. For example, one case of using Singleton pattern is when you are using a database in your application. Don’t worry about the databases, we have a complete section dedicated to them. But in here, we are just going to create a simple Java class named database. Let’s quickly create that inside our source folder inside our package. And we will see how we can implement that patter. Let’s name this class database. For this database class, I’m going to define a field let’s say private string name. Also,
let’s create a constructor and also getters and setters. Right now, because we have this public constructor, we can create as many instance as we want from this database class. But if you want to make sure that you have only one instance of this class in your entire application, you can create another field in here, for example, I’m going to say private, static as the data type of this field, I’m going to specify the class that we are in for example, in this case, I’m going to say database. So
let’s say private static database, make sure to import the one that comes from our package or dot maker dot Singleton pattern. For the name of this field, it’s convention to name it instance. And that’s it. Now we can create a getter method for this field. And here is how we can create that field, we can say public, because we are going to use this instance inside this field, I’m going to say static, the return type of this getter method is going to be the class itself. So I’m going to say database. Let’s name this method. Get instance,
as you can see the suggestion in here and let’s have a body for this method. In here, instead of just simply passing this instance, I’m going to create an if statement, I’m going to say if let’s say now, is equal to our instance, if that’s the case, I’m going to create a new instance, I’m going to say instance, is equal to new database. Notice that we need a name for the constructor of this database. So I’m going to receive that name via the input of this method in here, let’s say the string name. And later on, let’s first this
naming here. So if our instance is now we are going to create a new instance, and after creating that we are going to return it, we can say return instance. But in the other case, in case if our instance is not not in the else case, I’m just going to simply return instance, right now if we use this get instance method, instead of this constructor, we are sure that we have only one instance of our database class in our entire application. For example, if we go to the main class, and if we try to instantiate our database, we
can have only one instance we can say database, let’s name the database is equal to database with capital D dot get instance, of course, we need the naming here. Let’s pass a name. For example, let’s say DB music. Notice that because we have declared this get instance method as a static, now we can use this database class with this capital D, it means that we are not relying on the instance of this database class. Instead, we are using the object itself. But right now someone can declare the database class as we did for any other class. For example,
in here, someone can say database. Let’s name this one test database is equal to new database because in our database class, we have the constructor, we can pass a name in here and if we do that, we have another instance of this database class. For example, let’s say DB movies. In order to restrict users from using the constructor, I’m going to change the access modifier of this constructor inside my database class, I’m going to change this public to private, so that this constructor would be available
only from inside this class and not from inside any other class like this main class. Right now we have an error in here and the error says that the constructor is private. So by changing the access modifier from public to private, now, we are restricting others from creating multiple instances of our database class. But because our constructor inside our database is private, we can use this constructor inside this get instance method, as you can see in line 10, in here, right now, because of this static keyword. And also because of
this, if statement that we have put in here, we are sure that we have only one instance of our database class. But there is also another point that we need to be worried about. And that’s when someone tries to create multiple instances of our database from multiple threads. In some cases, if someone tries to create multiple instances of our class asynchronously, they might be successful in doing such a thing. In order to make our get instance method thread safe, we have an option in here after this static keyword, we can add another keyword
in which we haven’t seen so far, we can say synchronized, this synchronized keyword will make our get instance method thread safe, it means that no more than one thread can call this get instance at the same time. So this is the whole Singleton pattern. First of all, you declare a static field of the kind of your class for example, in this case database, after that, you create a get instance method in which is a static once again and also synchronized. Inside that get instance method you are checking that if the instance
of your database is now or not, if it is not, it means that it’s the first time that you are calling this get instance method. So you need to create a new instance like we are doing in line 10. After that, we are returning the instance but in the else case, we are not going to create a new instance, we are just going to return the existing instance beside this get instance method. In order to restrict others from using the constructor, you need to change the access modifier of your constructor to private so that it would
be usable only inside your database class. We can also make these if statements shorter for example, in here, I can delete this whole else case. And I can delete this return instance from line 11 in here, and after the if statement, I can return that instance. So by the time we reach to this line 13 in which we are returning the instance, we are sure that our instance is not not and we are returning either a new instance or the existing instance. Okay, I think that’s enough talking about the singleton pattern later on in the course, you will see
that this pattern might be useful from time to time. Before I finish off this video, I’m going to talk about one more methods available in every class in Java. And that method is called to string right now we don’t have any to string method inside our database class. But if we switch back to the main class, and if we say print database.to string, you can see that we have such a method. And if we run our application, we should see some information about our database class. Let’s quickly run it and let’s see if we can see any result.
You can see that we are getting the address of our database class. If you want, you can change this for example inside your database class, you can override the two string methods down in here inside the scope of our class, we can press Ctrl plus or and from here we can select this two string method. Right now we are returning the super statement. If you want to change that we can delete this whole statement. And we can prepare a string in here. For example, we can say a string text is equal to let’s say database class. After
that, let’s add a line. And after that, let’s print the name of our database. Let’s say name plus the start name. After creating this text, we can return for example, we can say return text. Now that we have overrided, this two string method, if we run our application, we should see some other results printed into our console. From time to time this two string method can be useful as well. It’s available in every Java class and basically it will print some detail about that class. Okay, I think that’s enough for this video. And
for that method. I think that’s enough talking about Java. Our Java section is done and hopefully we have learned a lot in the next video we are going to have a quick challenge in order to make sure that we have learned everything that we have talked so far. But before I finish off this video, I’m going to say that with Java, you can do a lot have things for example, besides writing Android applications, you can use Java in order to create desktop applications for Windows, Linux and Mac OS operating systems. And also with the help of spring framework,
you can use Java in order to create enterprise web applications. There are much more to Java language. For example, you can use Java in order to create applications for quantum computers. You can also use it for data analysis purposes, and literally everything else that you want. In this course we have touched the surface of Java language. But if you want to know more, I’m currently recording a new course for Java language. And I’m sure that you will find that course helpful beside the Java course I plan to record a complete course for a spring
framework in order to create web applications. By the time you’re watching this video, those courses may be released, so make sure to check them. Okay, I think that’s enough. In the next video, as I said, we are going to have a quick challenge See you in the next video. Here is our final challenge for the Java section, I want you to simulate your phone’s contacts and messages applications. Here is how our application is going to work. First of all, we are going to greet the user with some message. After that, we are going to show three options
to the user, one for managing the contacts, another one for handling the messages, and another one for creating the application. In case if the user selects the first option, which is managing the contacts, we are going to show these options, one for showing all of the contacts another one for adding a new contact. Another one for searching for a specific contact. One more for deleting a contact. And the last one is to go back to the previous menu, which was these three options. But in case if the user selects the second option,
which is handling messages, we are going to show these three options. The first one is to see a list of all of the messages. The other one is to send a new message and one last one in order to go back to the previous menu in which is our initial menu, which has three options. In case if the user selects these third option, we are going to create the application. We haven’t talked about databases and permanent data storage. But as a hint, if you want to store contacts and messages, you can use a static variables. Okay, pause
the video in here and go solve the challenge. After that, come back. And let’s see my solution for this application. Okay, let’s go to see how can we create this simple application. First of all, I have created my project, and I have created this package inside my source folder. Inside this package I have created this main class in which contains this main method. Let’s continue from here. First of all, I’m going to create a new Java class called message. Inside this message class, I’m going to have three fields, let’s say
private string text. Another one, let’s say private string recipient. This recipient in here is going to be the name of our contact. Also in here, I’m going to define an integer called ID, let’s say private int ID. This ID is not necessary for this application in here. But because in most cases, when you’re working with databases, you have a column called ID in order to identify every unique message. I want to be as close as I can to a real world application. I just wanted you to see this ID field inside a model in which
is a message. Okay, now that we have these three fields, let’s quickly create a constructor. And let’s accept all of these three fields. After that, let’s create getters and setters. I’m not sure that we are going to need all of these getters and setters later on. If we didn’t use them, we will delete them. Also inside this message class, let’s quickly create another method called get details. Basically, we are going to print the details of every message. So let’s say public void. Let’s name it get details.
Let’s say print. First of all, let’s say contact name. And let’s print the recipient. After that, let’s print the text itself. Let’s say message plus the text. We also need to add a plus in here. Sorry about that. And I think if we add a backslash n in here, it would be much better. After the text. Let’s also print the ID Let’s say backslash n, Id plus our ID. That’s it for our message class, let’s quickly create the contact class as well. New Java class, let’s name this phone contact. Inside this contact class, I’m going to have
four fields, let’s say private string, name, private string. Number, you may think that this number should be an integer, or maybe a long, but because different numbers can have different formats. For example, they can have some parentheses or maybe some hyphens. I am saving this number as a string. But if you want, you can always use integers or long. After name and number, I’m going to save another field in here, let’s say private string email. After all of these, I’m going to have another field in here, let’s say private ArrayList
of different messages. Let’s name this phone messages. So basically, I’m going to save different messages for every contact inside an ArrayList. Okay, now that we have our four fields, let’s quickly create our constructor. But in here, I’m going to create two constructors, because at the time of creating a new contact, that contact does not have any messages. For that, I’m going to create another constructor in here, which will not accept an ArrayList of different messages. This time, let’s select the first three items. But because if I don’t
do anything about this messages, this ArrayList will be now inside this constructor, I’m going to see these dots messages is equal to new ArrayList. So at the time of creating a new contact, I’m saving the messages array list as an empty array list. Okay, now that we have two constructors in here, let’s quickly create getters and setters. And also after that, let’s create a get details method. Let’s say public void, get details. We can also use two string methods. But in here, I’m just more comfortable with this get details method.
let’s print all of the information about each contact. Let’s say name plus the staff name. After that, let’s print the number and email. Okay, that’s it for our contact class. Let’s switch to main class. And let’s write the logic for our application. First of all in here, I’m going to create an ArrayList of different contacts. But I’m not going to do that inside this main method because we are going to have multiple methods and we need to have access to our contacts are released in different methods for that I’m going to
define my ArrayList above in here in the fields of this main class. I can say private, static, because the methods that I’m going to use later on are static methods, I need to create this ArrayList as a static, so let’s say private static array list of different contact. Let’s name it contacts. But this is not a good place to initialize our contacts ArrayList because this is not the place that our code will be started. We can initialize our contacts ArrayList for example, inside this main method. But before I do that, I’m going to also define
a scanner in here because we are going to need the scanner in multiple methods. So let’s say private static scanner, let’s name it the scanner. After that inside the main method, let’s initialize our contacts ArrayList. Let’s say contact is equal to new ArrayList. As you can see in here, I’m going to greet the user with some message, let’s just print something. After that, I’m going to show these three different options to the user. Because I want to have a clean code. I’m going to show these three options inside another method. Let’s
name that method show initial options. But as you can see, right now I’m getting a red warning and that’s because I need to create this method down in here. So let’s say private, static void, show initial options. Let’s bring the options in here. After that, we need to get the user’s input. And for that we can use the scanner. So let’s initialize our scanner. Right now we have declared our scanner above in here, but we never initialize that. It’s a good place to initialize our scanner in here. We can say a scanner is equal to new
scanner. Let’s pass the system that after that, we can say it’s choice is equal to a scanner dot next int. Let’s create a switch upon this choice. Let’s say switch on choice. In case the choice is one, we are going to manage the contacts. Once again, let’s do that in another method, let’s say manage contacts. Don’t worry about this read warning, we will create this method later on. Let’s also add a break in here. But in case and the choice is to, we are going to handle the messages. So let’s say manage messages. In case the
choice is three, or for that matter, any other number, we are going to create the application. So in here, I can say default. In the default case, I’m just going to break out of the application. Let’s minimize this project main for now. And then in here, let’s create our two methods. First of all, let’s manage our contacts. Let’s say private, static void, manage contacts. Once again, in here, I’m going to show a list of options. So let’s print those options. One, once again, we need to get the user’s input for that we can use the scanner, let’s
say int choice is equal to a scanner dot next int. Notice that we are not initializing our scanner in here. And that’s because we have done that inside this short initial options method. So by this time, we have an instance of our scanner. After that, once again, we need to create a switch on our choice. In case it’s one, we are going to show all of the contacts, let’s create that method, show all contacts. And also we need to break. Let’s also write the rest of the cases. In the default case, I’m going to go back to the previous
menu. So I’m going to once again run this show initial options method, so that we can see all of these options once again. So in the default case, I’m going to say show initial options. After that in here, we need a default as well. I’m saying default in here, because I’m going to go back to the previous menu in case if the user enters any other number. Okay, let’s create these methods one by one. First of all, let’s show a list of all of the contacts. I’m going to do that down in here. Also, instead of writing the declaration
of your method, you can use the help of your ID. For that, you can click on the red arrow in here and by pressing ALT plus insert, the first option is to create this method, you can see that we have created this method successfully, the void is fine for the return type. And down in here, I can’t create the logic for this method. In order to show a list of all of the contacts, I’m going to simply create a for each loop. Let’s say for contact, let’s name it see inside our contacts, or at least, let’s just say C dot get details the method
that we have created inside our contact class. After the for loop once again, I’m going to show the initial options. For that I’m going to save show initial options. We are going to quit the application when the user tells us that’s all we need to do inside this method. Let’s just minimize it for now. And let’s create the second method which is add new contacts. Once again, we can use the help of our ID. Inside this add new contact method we are going to receive some users input. But before that, let’s print something you
Let’s say adding a new contact. After that, let’s say please enter the contacts name. Once again, we are going to use our scanner, let’s say a string name is equal to a scanner dot next, after we get the contacts name, we are going to get the contact number. So once again, let’s bring something in here. Let’s say please enter contacts number. Let’s see string number is equal to a scanner dot next, let’s also get the contacts email string he made is equal to a scanner dot next. After that, we need to make sure that the user enter
something as the name number and email of our contact. And don’t just press enter and leave us with empty strings. For that, I’m going to create an if statement in here, let’s say if name dot equals an empty string. Let’s say or if number that equals an empty string. Or if email is equal to an empty string. If each one of these is true, we are going to show some message to the user we are going to ask to enter all of the required information.
Let’s say please enter all of the information. After printing this line of code, I’m going to recall this whole method I’m going to start from the beginning of this method. For that I’m going to say add new contact. I’m calling this method from inside the method. This is called a recursive call, you can see the icon at the left side in here. If you hover over that you can see the recursive call calling a method from inside that method. So by this way of calling our method, we are starting once again from the beginning of this method,
but in the else case, we are going to create our contact object, I’m going to say contact, let’s name it contact is equal to new contact. And after that, I’m going to pass name, number and email. If you remember inside our contact class, we had two constructors. And in here we are using the second constructor, we didn’t pass any messages ArrayList to this constructor in here. After creating our contact object, we can say contact our ArrayList dot add. Let’s pass our contact. As simple as that. Now we have added a new contact after this
if statement. Once again, I’m going to show the initial options. So let’s say show initial options. Because once again, we are not going to create the application unless the user tells us Ok, we are done with our second method. Let’s see what else do we have in here. Let’s create this search for contact method. The Void is fine for the return type. Inside this method first of all we are going to ask for the name of the contact. So let’s bring something let’s say please enter the contact name. Let’s say string name is equal to the scanner dot
next. After that we need to make sure that we have received any so let’s say if name dot equals an empty string. We are going to print some message we are going to ask for the contact name once again. Let’s say please enter the name and after that once again we are going to use a recursive call. We are going to say search for contacts once again. But in the else case, we are going to search for this contact inside our contacts ArrayList. For that once again I’m going to create a for each loop. Let’s say for contact. Let’s
name it see inside our contacts are a list. Let’s say if c dot get name is equal to the name that we just received. If that’s the case, let’s print c dot get details. Let’s also define a Boolean before this for loop. We will see why I am defining this Boolean. Let’s say Boolean. Let’s name it does exist. Let’s initialize it to false in this if state Wherever you have found the contact first of all I’m going to change the value of this does exist to true, let’s say it does exist is equal to true. And after the for loop,
I’m going to create an if statement, I’m going to say if does exist is not equal to true. Then we are going to print something I’m going to save there is no such contact. After all of these logics, we are going to show the list of options once again. So let’s say show initial options. Okay, that’s it for our search for contact method. Let’s also create this delete contact method. Once again, in here, we are going to ask for the contacts name. So let’s say please enter the name. Like before, let’s say a string name is equal
to a scanner dot next, next, we need to create our if statement. If named, that equals an empty string, we are going to ask for the name once again. And we are going to start from the beginning of this method. In the else case, we are going to look into our contacts ArrayList. So let’s say for contact see inside our contacts list. Before I write the logic, let’s also define a Boolean above in here like we did for the search for contacts method. Let’s say Boolean does exist. Let’s initialize it to false. So in here, let’s say if I don’t
know why I said as in here, let’s quickly change this one to see. So let’s say if see, that gets name is equal to the name that we have received. If that’s the case, first of all, we are going to change the value of this does exist. Let’s say it does exist is equal to true. After that, we are going to remove this contact from our contacts ArrayList we can save contact dot remove. This remove method requires an object which I’m going to pass see the object that we are currently looking in this iteration. After the for loop, I’m
going to say if does exist is not equal to true then we are going to say there is no such contact. And after all of these logics, we are going to show the list of all of the initial options. Okay, that’s it for this method. Let’s minimize this one. So far, we have created the logic for the first option in our initial options, we have created the whole logic for this manage contacts. But right now in the logic for this manage contacts option, we have one or two possible points for multiple bugs, we are going to fix those
two and also we are going to write the rest of the logic for this application in the next video. See you in the next video. In the previous video, we have created the logic for this manage contacts option. But if you remember I said that we have some issues right now with this manage contacts logic. And in this video, first of all, we are going to fix those issues. The first issue is inside this add new contact method. In here we are adding the contact into our contacts array
list. But before that, we need to make sure that we do not have a contact with this name saved on our device for that before adding this contact to my contacts array list. I’m going to check that if I have such a contact on my device. So first of all, let’s create a Boolean in here. Let’s say Boolean does exist. Let’s initialize it to false. After that, I’m going to create a for each loop. Let’s say for contact see inside my contacts I have a list. Let’s see if c dot get name is equal to the name that we just received.
Inside this if statement, I’m going to change the value of this does exist to true so let’s say does exist is equal to true. After the for loop I’m going to check that if this does exist is equal to true or not. Let’s say if does exist is equal to true. Then we are going to print something we are going to say we have a contact with this name. After that we are going to start from the beginning of this method. So I’m going to call this add new contact method once again. But in the else case, I’m going to add the contact to
my account. Tech’s ArrayList, so I’m going to call this line of code from here. And I’m going to paste them inside this else case. Also, after adding the contact to our contacts array list, I’m going to show some message to the user. So let’s say name, added successfully. Okay, that’s the fix that we needed inside our add new contact method. Let’s also change the show all contacts method a little bit as well. So inside this for each loop, after printing the details of a contact, I’m going to print another line. And this line is going
to be just a separator that will indicate the difference between different contacts. Nothing special in here. Okay, let’s move on. And let’s create the method that we haven’t created in here, this manage messages method, by pressing ALT plus insert, we can create that method. First of all, we are going to show a list of options. So let’s print something. After that, we need to receive the user’s input, I can say int choice is the call to a scanner dot next int. And let’s create our switch statement on this choice. In case it’s
Vaughn, we are going to show a list of all of the messages. So let’s say show all messages, we will create that method in a minute. But in here, let’s add a break. And let’s write the other cases. In case it’s true, we are going to send a new message. So let’s say send a new message. And let’s add a break. In the default case, I’m just going to show the initial options. After that, in here, we need a break as well. Okay, let’s create this show all messages method. In here, first of all, I’m going to save all of the messages
from all of the contacts into an array list. So let’s say array list of different messages. Let’s name it all messages is equal to new ArrayList. After that, I’m going to create a for each loop. For contact, see inside my contacts or in this. Let’s see all messages that at all, we haven’t used this add all method, but in here, we are going to do that. I’m going to say C dot get messages. So by using this add all method, we are adding all of the messages of one contact to our all messages ArrayList. After the for loop, I’m
going to check that if the size of these all messages is zero or not. So let’s say if all messages that size is greater than zero, I’m going to show the details of every message. So for that, I’m going to create another for each loop in here. Let’s say for message, let’s name it m inside out all messages, at least. let’s print m dot get details. And after that, let’s also print some separator. But in the else case, I’m going to say that you do not have any message. After everything after the if statement, I am going to show
the initial options. Okay, that’s it for this show all messages method. Let’s also create the other method, which is send a new message. In here, first of all, I’m going to get the contact or recipients name. So let’s ask the user that, let’s say Who are you going to send a message. After that we need to get the name of our contact. So I can say string name is equal to scanner dot next. After that, we need to check that if this name is empty or not for that, I can say if name that equals an empty string. We are going to ask the user
to enter a name. So let’s say please enter the name of the contact. After that we are going to have a recursive call. So in here, I’m going to say send a new message. But in the else case, I’m going to check that if we have such a contact in our contacts ArrayList. For that I’m going to define a Boolean in here Boolean does exist, is equal to false. After that, I’m going to create a for each loop. Let’s say for contact see inside our contacts or at least if see that gets name, dot equals the name that we just received.
Let’s say it does exist is equal to true. After the for loop, I’m going to check that if does exist is equal to true or not. First of all, let’s write the else case. In the else case, I’m going to say there is no such contact. But inside the if block, if we do have such a contact, we are going to ask for the message itself. So let’s say what are you going to say? Let’s say a string text is equal to a scanner dot next. After that, we need to make sure that this text is empty or not. Let’s say if text that equals an empty
string. If that’s the case, let’s say please enter some message. And after that, we are going to start from the beginning of this method. So let’s say send me a message, which once again is a recursive call. But in the else case, we are going to create our message object. And after that, we are going to add it to the contact that exists in our contacts ArrayList. But if you remember, inside our message class, we had a field called ID. Basically, whenever you are receiving these messages from a database,
or maybe a web server, this ID is unique to every message. In here, I’m trying to be as close as I can, and to a real world application. So I’m going to make this ID unique. In order to have a unique ID for every message, I’m going to define a field above in here inside my main class I am talking about here. And after that, we will increment the number of this ID by one every time that we create a new message. For that I’m going to say private, static integer. Let’s name this phone ID. And let’s also initialize it to zero. After
that down in here, when we create our message object inside this else case, down in here, I can say ID plus plus. So because this ID is a static integer, whenever we increment its value, it’s going to be unique for every message. So right now we have the name of our recipient, which we have received above in here. And also we have the message body, which is this text. And also we have generated this ID which is unique. These are the three things that we need in order to create our message object, I can say message, let’s name
it new message is equal to new message we need the text. After that we need the recipient, which is name. And after that, we need the ID. Next, I need to find the proper contact to add this message to his or her array list of different messages. For that I’m going to create a for loop. Let’s say for contact see inside our contacts array list. Let’s say if see that get name is equal to the name that we received. If that’s the case, first of all, we need to get the array list of different messages of this contact. For that I can say
array list of messages. Let’s call this ArrayList new messages is equal to c dot get messages. After that, we need to add this new message to this new messages ArrayList so let’s say new messages that add new message Sorry, I have used this add all methods went to remove this all. After that we are going to save the current contact that we are looking into. because later on we are going to remove this contact from our contacts array list. And we are going to update his or her array list of messages. And after that, we are going
to add it once again to our contacts ArrayList. So for that I can say contact. Let’s name it current contact is equal to see. Next we need to say current contact dot set messages. And we need to pass our new messages are released. After that, it’s time to remove the C from our contacts or at least we can say contacts that remove and we can pass the C object. Now that we have removed the previous contact, we can add the new one by saying contacts dot add. Let’s say current contact. So this way we have added this new message to our
specific contact and we have updated our contacts ArrayList Okay, that’s the entire logic that we need in order to add a new message just after everything after we are done with this method. We need to show the initial options once again. Let’s minimize this method. And let’s see what else do we have? I think we have covered everything. And we are ready to test our application. So let’s run the application. In here we are seeing the greeting message, which is fine. After that we are seeing the
initial list of options. If we select one, we can see the options for managing different contacts. Right now we do not have any contacts in our ArrayList. So if we select one, you can see that we are not seeing anything, it’s better to print some message in here in case if we do not have any contact in our phone, for example, that says, you do not have any contact on your device, but we will write that later on. For now let’s just see the other options. Once again, let’s select Manage contact. This time, let’s try to add a new
contact, you can see that the console is waiting for a name, let’s say may Sam, the console is waiting for a number. Let’s add some number. After that, we need to provide some email, let’s say may [email protected] And as you can see Mason added successfully. If you want to make sure of that, once again, you can check the list of all of your contacts by saying show all contacts. And in here you can see the details of Mason. Let’s also add another contact. Let’s say Sarah, this thing. Let’s add a random number. After that an email.
This time let’s try searching for a new contact. So in here, if I say three, you can see that the console is waiting for the name of our contact. Let’s type Brad in here, you can see that we are seeing this message there is no such contact in your phone. Once again, let’s try searching for contact this time. Let’s try Sarah. You can see that we are seeing the details about Sarah. Let’s also try removing one of the contacts. Let’s say for in order to delete a contact, let’s say me Sam. In here we are not seeing any message indicating
if that we have deleted Maxim successfully or not. But if we check the list of our contacts, we should see only Sarah. And as you can see, we have only Sarah in our contacts list. Let’s also try this go back as well. So let’s say manage contacts once again. This time if I put any number beside one through four, for example, if I say six, you can see that we are going back to the main menu. Also, let’s try adding a new contact with empty values. Sorry about that, I need to go back and select one. Let’s say to in order to add a new contact.
Let’s don’t provide anything for the name. You can see that the scanner is waiting for some input. But even if the scanner moves to the next line, we have write the logic in order to prevent adding an empty contact. Okay, that’s good. Let’s add many samples again. Let’s add some number. And some you mean. Let’s see what we have inside the messages option. In order to see a list of all of the messages. Let’s press one, you can see this message you don’t have any message. Once again, let’s go to the messages option. I’ve entered
two in here and let’s try sending a new message. Who are you going to send a message? Let’s say Sarah, what are you going to say? Let’s say hello. It seems like we are getting a crash and the crash happened because of this exception. Concurrent modification exception. Okay, let’s take a deeper look into our code. And let’s debug our application for the first time. It seems like the exception occurred when we try to add the new message. Also, you can see the line 99 in here, if you click on that, you will be navigated to that line.
Yes, we have this exception inside the add new message or send a new message inside this for loop. I think the error in here is happening because first of all, we are using this C to assign a new value to this current contact. And immediately after that we are trying to remove this contact from our contacts ArrayList. So this causes the concurrent modification exception. If you want to get over that exception, you can simplify this if statement. For example in here I’m going to delete all these three lines of code. So by this point, we have this