Java Tutorial for Beginners | Java Programming Tutorial | Java Basics | Java Training | Edureka


Hey everyone,
this is Neel from Edureka and welcome to today’s
session on Java Tutorial. Now, we’ve all heard of Java either from our friends
or from the Internet and Java is something that
you would quite often hear across from different sources as well. So you may all be wondering what Java is and what does it exactly do, and we’re gonna understand
what Java is, how it functions, as well as I’m gonna help you build a good fundamental knowledge in Java as part of today’s session. Now, keeping that in mind, let’s move forward and
look at today’s agenda. Now, we’ll start off with
an introduction to Java. Then I’ll help you understand why you need to learn Java. And then we’ll be discussing
the various features of Java. After that, we’ll be looking
at the Java Development Kit. And then following that, we’ll understand how does Java work. Once you’re clear on these concepts we will move into the core Java and we’ll understand the
various data types in Java, the various operators
that you can use in Java. I’ll also help you understand the control statements in Java and we’ll be discussing
about arrays in Java. Finally, to give you a
good fundamental knowledge, we’ll also be discussing about the object-oriented
concepts in Java. Now are we clear with today’s agenda? Okay, Prashant says yes, I’ve
got a confirmation from Aaron Ann, Henry, Divya, Barath,
Dhiti, that’s great to see guys. So one thing I’d like to clarify right in the start itself is, at any point, if you’re not clear, if you have any doubts or any questions, please put it across in the chat window so that I can clarify
then and there itself. Now let’s move on have
an introduction to Java. So Java was created by James Gosling in 1995 for Sun Microsystems. So Java was originally
planned to be named as Oak, but that name was taken. So they went after and named
after their favorite coffee, which was Java. Now, Java is a platform-independent
programming language. Now, when I say platform-independent
programming language, what they basically means is that once you’ve written your code, you can run it anywhere as such. So there’s not gonna be
any specific dependencies or any changes that you
need to perform on that code once you’ve brought to a
different system as such. Now there are mainly three
key features of Java. So Java follows a concurrent approach. Now when I talk about concurrency, it basically means that when
you have a lot of statements that need to be executed, rather than executing
them one after the other, you can concurrently execute them and thereby increase the
overall efficiency as well. So this saves you a lot of time as such and this can be achieved using Java. After that, Java follows
a class-based approach. Now, here when I say class-based approach, it basically means that everything that you’re gonna be writing
as part of your Java, it is gonna be present inside a class. Now, don’t worry too much
about what are classes or how it is gonna help
us, we’ll be talking that towards the later 1/2 of the session, but just understand that every code that you’re gonna be writing is gonna be written
inside a class as such. After that Java follows
an object-oriented style. So to those of you who are not familiar with the different styles of programming, that are mainly two style that
programming languages follow. One is the procedural style and the second is an
object-oriented style. So Java actually follows
an object-oriented style where everything is
considered to be an object. So the variables that
you’re gonna be using, the functions that you’re gonna be using, the operations that you’re
gonna be performing, everything is gonna be
done using objects as such. So are we clear with
respect to the first slide? Okay, so I have my first
question here from Prashant. Prashant has asked me why
is object-oriented style so important? Now, Prashant to help you understand this, I’ll just give you a comparison
of object-oriented style to the procedural style that
I had just talked about. Now, basically, in procedural style, what you do is that you
follow a top-down approach that is all the statements
that you wish to execute are written in a sequential
order one after the other and then you go on executing one by one. So here when you look at
object-oriented style, firstly, it follows a bottom-up approach. And I had already mentioned
you everything here is considered to be an object. So when object basically means that everything can be related
into the real world instance. So when we talk about an object, definitely it can be related
to a real world instance. Let’s say you have an animal, so you would be having
an object for animal present in your programming language, or let’s say you’re gonna be writing something that is gonna deal
with your employee database. Then here, every employee
would be considered to be an object as such. So apart from that, Java also made sure that through object-oriented
programming style, your code and your data
is highly secure as well. So the future of security
is highly improvised in object-oriented programming style. So I hope with that, you’re clear with respect to why
object-oriented programming style is important. Okay. So, Prashant wants to know some
other programming languages that follow object-oriented approach. So Prashant, the other
programming language that follow object-oriented
approach is C++, then you have Visual Basic,
.Net, then you have C#.net. Now C# is a good competitor for Java. So, these are other programming languages that make use of object-oriented style. Are you clear Prashant? So now that we got an
introduction to Java, let’s go and solve the
elephant in the room, why we need to learn Java. Now, this is something every
one of you would be wondering and let me help you understand that with the upcoming slides. So, TIOBE is a company that mainly deals with the codes written
by a lot of organization. What they do is they cleanse and they improve the efficiency of code. So even if I’ve to give you a number, they deal with about 500
billion lines of code every day. So what they have done is that
they had conducted a survey based on the usage of
different programming languages in the industry as well as
they had also considered the searches for these codes across multiple domains as such. So the keyword search for these programs were taken from Google,
they were taken from Yahoo, YouTube, and then this
index was generated. So this index is something
that is released every year. And you can see here the popularity of each of the programming language. So here you can see how Java
has dominated this field from early 2000s till
the present 2017 as such. You can see there is no close
competitor to Java as well. This in itself should give you an idea of how popular Java is and how widely it is being
used in the industry. Now apart from this, let me
give you a better understanding of how Java is using different industry. So here I’ve just picked eight of the popular domains as such. Now in eight of these domains, let me give you an understanding of how Java is being used in them. So when we talk about
the financial service, here, your server side applications
will be written in Java. Now Java is a highly preferred language to write server-side applications and financial service domain is something that mainly uses
the server-side applications. Apart from that, when you
see even the retail domain. Now the billing applications that you see when you go to a store, this could be a normal store,
it could be a supermarket, it could even be a popular outlet as well. So their retail
applications’ billing system is completely written in Java. Apart from that in your banking domain, your transaction management, as well as your transactional
programs are written in Java. When you look at the IT industry, definitely you can understand Java as a programming language. So you have a lot of applications that are developed using Java and Java is again, one
of the most preferred as well as the most used
programming language in the industry today. So a lot of applications
either are written in Java or make use of some
features present in Java using the Java APIs. Then when you look at the
scientific and research community, there your calculator as well
as your operational programs are written in Java. So using Java, they are able to process as well as manage huge amount of data. Now again, when we say
huge amount of data, we’re all familiar with how
scientific community deals. So they’d be using numbers that are way too big for normal
applications to handle it. So all those numbers, all those data, is gonna be handled by Java quite easy. When we look Stock Market, now this is something
that is quite useful. So in the stock market,
there are a lot of algorithms that are written in Java
which help organizations to understand better as to which company they need to make their
investments as well. And when you look at the
most popular domains today, it is the big data as well
as the Android domain. So here your Android
programs make use of Java and your Android operating system itself highly makes use of Java and
various Java APIs as such. When we look at the Big Data domain, your Hadoop mainly uses Java and you even have a concept of MapReduce, which involves Java as such. So I hope with this, you’ve got an idea of the various domains where Java is being used. And these are not the only
domains where Java is being used. I’ve just selected some of the few domains but you need to understand
that Java is a very big ocean. So it’s not something that is just restricted
to certain domains. Java is being used across
different platforms in different domains. Everywhere you see, you can find some amount
of Java present there. But when you look at it in a positive way, Java is also an ocean of opportunity. When it’s being used
across so many domains, when you’re generating multiple
applications daily on Java, then it also brings in a good amount of job
opportunities for you as well. And this is something that you all you need to concentrate on. Here are some of the technologies that make use of some extensively. So let’s start off with the
continuous testing field. So when we talk about the
continuous testing field, this basically means
your automation testing. Now this is something that you
have not heard quite often. And in the automation testing field, Selenium is basically one
of the most widely used. So Selenium extensively makes use of Java as part of their core programs. So you’re gonna be writing
a lot of codes in Java as part of your Selenium. Then, again, we’ve
discussed about Android. Here, your Android applications that you find in your Google Play Store, as well as your Android operating system makes use of Java and
various Java APIs as such. We’ve also discussed about how big data and the Hadoop field makes use of Java as part of their core features. Then when we look at different frameworks, now you have Spring, Hibernate, Thymeleaf, these are some of the popular frameworks and they also extensively use
Java as part of their cores. Now Spring and Hibernate is something that we’ll be discussing more in our upcoming sessions where we’ll be extensively deep diving into these two frameworks. Now again, when we look
at web development field, here when you talk about these two, Angler as well as Node.js so these are basically scripting language. Here what you’re what
you’re gonna be doing is that you’re gonna be
using JavaScript extensively as part of their core as such. Okay, so any questions still here? So I have a question here from Barath, Barath has asked me what is a framework. So Barath, a framework
basically is a structure that you will be following for
developing your applications. So once you have a structure, you get an idea of how the
application should be developed. And again, Spring, Hibernate, are some of the most popularly-used
application frameworks across the industry. So when you have to
develop an application, you will be following these frameworks to have a standardized
structure for your application. So are you clear Barath? Okay, so Barath is clear. So any other questions? Okay, so Divya says no. Aaron says no. Anna is clear, that’s great to see guys. So again, I’d like to
remind you at any point, if you’re not clear, if
you do have any questions, you can put it across in the chat window, so I can help you clarify
it, then and there itself. So let’s move forward and talk
about the various features that Java has to offer. Now, when we talk about
the features of Java, this basically is also called
as the buzzwords of Java. So these are the most essential features which have made Java the most
popular programming language of the industry today. So we’ll be talking about
each of them one by one. Let’s move forward and discuss that. So the first feature that
we will be discussing is Java is simple. So when we say Java is simple, it basically means that
anyone can learn Java and it is very easy for
programmers to use it and make the code highly efficient as compared to other
programming languages. Now after that, you have the portability. So I’ve already discussed the
same with you in the start. Your Java application, once it is written, can be executed anywhere, it can be across multiple platforms, it could be across
multiple operating systems, they could have separate configurations, but again, your Java program
would not have any problem in executing across these platforms. After that, Java follows an
object-oriented approach, we’ve already discussed, everything in Java is
considered to be an object and every operation that
you’re gonna be performing is through these objects itself. Now, when we talk about the
security feature of Java, Java does not use the
concept of pointers as well. So if some of you are familiar with C++, there’s this concept of pointers, which basically means you’re pointing to a specific memory location. So this is something a lot of
programmers face hassle there. So that concept is completely
removed from Java as well. Now apart from that,
your Java Virtual Machine that is the environment, in which you’ll be
running your Java programs also checks the code, whether
there’s any threat present, or if there’s any error present in it before it goes on to execute your code. Again, when you look at
the web development side of Java as well, you have
this concept of applet which ensures that the security
of the code is met as such. So once you’ve written your code, it cannot be read by any other person. What you’ll be doing is you’ll be giving them an intermediate file that is known as byte code. So this byte code is not
a directly readable file. So with that, your code
is also completely secure. So no one can actually
directly read your source code. Okay, so when we talk about the
distributed feature of Java, this basically refers to the
remote method invocation. Now you don’t have to
worry too much about this, we’ll be talking about this
in our upcoming sessions. But I’ll just give you an overview here. So you’re gonna be
writing a lot of programs but they’re gonna be linked to each other and not everything may be
present on the same system that you’re executing your program. So through the remote method invocation, what you can do is if the dependencies are directly accessible over a network, your Java program will
be able to access this, take all the necessary
information that it wants, and then it will be able to execute it. And this will seem like an easy process, Java will make it look
like all the programs and dependencies are
present in your same system but it’s quite complex operation, but through Java’s distributed feature, it becomes quite easy
for you to achieve this. After that, you have the
dynamics feature of Java. So when we say dynamic, it basically refers to
the information gathering as well as the dependency
association at runtime. So a lot of information
that your program request a lot of dependencies or classes or packages that your program request is only associated to your code and just before it’s
gonna be executed as such. So there’s a lot of runtime features that comes into picture when you’re gonna be
executing a Java program. After that, you have the robust feature. So this basically refers
to how your Java program can be suitable across
multiple environments as such. So your Java code is basically checked both at compile time as well as runtime, after that, your Java memory allocation. So this is something
that is quite important. Memory allocation and memory
releasing is done by Java in turn itself. So you don’t have to
worry about the hassle for allocating memory for your variable then once you’ve done with the variable you need to release that memory. Here, Java does everything for you. So that completely relieves your burden of memory management. Then you have the high
performance feature. Now Java achieves its
high performance feature through the creation of byte code. Now the byte code basically
is an intermediate language that is present between
your high level language, that is your English, and your assembly language,
which is your machine language that will be consisting of ones and zeros. So once you’ve actually converted your high level language to byte code, it becomes very easy to translate it to machine level language. So once you have the byte
code, it will be very easy for you to run it across
multiple environments and it will be very easy for you to achieve high performance as well. So any questions with respect
to the features of Java? Is there any feature that
you’re not clear with that you want me to re-explain? Okay, Prashant says clear, Anna is clear, Barath, Divya, everyone’s clear. That’s great to see guys. So moving forward, let’s talk about the Java Development Kit. So here there are mainly three components that you need to understand with respect to the Java
environment as a programmer. You have your Java Development Kit, you have your Java Runtime Environment and you have your Java Virtual Machine. So these are three essential components. I’ll just show you how these
are related to each other and give you a good understanding with respect to them as well. So here are the three components
that I had just mentioned, you have your Java Virtual Machine, your Java Runtime Environment and your Java Development Kit. So your Java Virtual Machine, so basically this is one
of the competence of Java in which you’re gonna be
executing your byte code. So whichever machine has
your Java Virtual Machine, then that machine can directly
execute your Java code. All you need to do for
executing a Java code is that you need to have your Java Virtual Machine present there. So let’s say you want to give a program which you’ve developed in
Java, then all they need to do is that they need to have
the Java Virtual Machine present in the system and then
they can directly go ahead and execute that program. But then you have your
Java Runtime Environment. That is the environment in which you’re gonna be
running your Java programs. So there you’re gonna be
having your Java class files as well as your other files as such. Now, to give you a better
understanding of the other files, you can look at the image here. So basically these are the other files that you’re gonna be having as part of your Java Runtime Environment, which are all these files are required, the JRE will directly linked
to the JVM as required. So all these will be present in your JRE and after that, you have your JDK. JDK basically the first year
Java Development Environment. This is something all
developers are required to have. So in case if you want to
write your program as such, or you want to create various programs, then you need to have
your Java Development Kit present on your system. So again, when you talk about
your Java Development Kit, it basically has these
following development tools. So as you require, you can use either of these
tools as part of your code. So are you clear with respect
to the Java Virtual Machine, the Java Runtime Environment
and Java Development Kit? Okay, so Divya is asking me I already have Java
installed in my system, do I need to reinstall it? Now, Divya, unless you’ve
specifically downloaded and installed the Java Development Kit, I believe you will be having
the Java Virtual Machine installed in your system. So this is basically
used to run the Java code present on different browsers. So I think that is what
you’re referring to. Yeah, so that is your Java Virtual Machine that is installed in your system. So what we’re gonna be doing is that we’re gonna be downloading
the Java Development Kit and installing it on your system. But before we go ahead and do that, let’s try to understand how Java works. Now, let’s consider the
two main environments. First is a compile time environment, that is the environment where
you’re gonna be developing your Java code and then you
have the runtime environment where you’re gonna be
executing your program as such. So we’re gonna start off
with our Java source code that is gonna be of a .Java extension. So every code that you’re
gonna be writing and saving should be saved with an extension .Java. So this basically helps you to understand that this is a Java source code file. So once you’ve completed your
code, then you can go ahead and use the Java compiler
to compile your source code, check if there are any
errors present in your code, and then create a byte code. So once you’ve completed the
coding of your application, then you’ve translated to the byte code, then what you’re gonna be doing is that you’re gonna be
moving it either locally or through a network. That is you’re gonna be distributing it through the Internet. So once that is done,
basically whoever has to run it will follow the following
process to execute. So what happens is first is
that a class loader is called. Your application may use
objects from different classes. So their corresponding details are loaded from the Java class library. After that, the byte code is verified. So here again when we say
byte code verification, now you’re transferring your
byte code across the network. So there is a slight chance
that your code may get corrupted or some virus or some
changes may have happened through that transition. So your Java byte code verify checks if your code integrity is met, and if there are any errors
present in that code. So once that is done,
basically what happens is that you’re gonna first
send it to Java interpreter as well as a compiler as such. So this again, ensures
your code has no errors, it makes it easier for
your execution of the code because you’re running
it through an interpreter if it required or a compiler as well and then you’re gonna execute
it on your runtime system. So once you’ve bought
to your runtime system, then it interacts with
the operating system which in turn interacts with your hardware where memory allocation, data storage, everything happens as such. So are you clear with
respect to how Java works? Okay, so Divya is clear, we
got a confirmation from Anna, Barath has also understood,
Dhiti, Prashant, that’s great to see guys. So enough with the theory
part, let’s move ahead and install Java Development
Kit on your system. Now, to download the Java Development Kit what you need to do is
just go to your browser. Here, just search for JDK download. So this will directly
lead you to Oracle space where you can download the
Java Development Environment. So here you have your option to download Java Development Kit, that is JDK version 8u1.2.1. So this basically means you’re gonna be downloading Java 8. Now again to give you an
idea, in the industry, presently most organizations are working on Java Environment 7
but I again recommend that you start off with Java 8 itself because most organizations have already started migrating to Java 8. So all you need to do is
click on Download here, then you need to specify
which operating system is your choice. Before that, just accept their license and then you can choose the
corresponding operating system. Now the installation of Java is very easy. All you need to do is just
click next, next, next until you’re done with it. After that comes an essential process where you need to specify your
Java Development Kit’s path. So go ahead and install
Java on your system and then I’ll help you understand how to set the path variable. So now that you have installed
the Java JDK on your system, it’s time we go ahead and configure the path
of Java on your system. Now to do that, all you need to do is
go to your My Computer. Here just right-click and
select Properties option. So here you are getting system properties. Now all you need to do is click
on advanced system settings and here you have the option
of environment variables. Just click on that and
you will get this option, for setting environment variable. Now, if you do not have a
path variable already set, just click on new and
create a path variable, but if you do have an existing path file. So I already have multiple
programming languages set on my system, so
that path is already set, but in case, if you do not
have the path variable, just click on new and create. So then comes the question, what do you need to specify in this path? So here what you need to do is that you need to give the
location for your Java path, that is a JDK path. So go back to My Computers, to the location where
you have installed Java. Now in case if you have
not changed the path, it will be present in C:ProgramsJava and then here you have two
options: you have JDK and JRE. So go inside JDK and then inside them. So take this complete location, copy it, come back to
your environment variable and specify it here. So in case this there is any
other value, just put a colon and then put the new value, But if there is nothing said
you’re creating a new variable, then just directly specify this path here. Just click on okay. And then to check out if
your Java path has been set, just run your command prompt, that is. Here just type javac. So by this, what you’re doing is that you’re basically checking out if your Java compiler has been set. Just press on Enter and in case if you get
these values as such, then the path has been set correctly, but in case if you are
getting Java not found, then let me know I’ll be able
to assist you accordingly. Okay, so now that you’ve
all set your path, let me just show you how to
run and compile a Java code. So, to compile a Java code
first go to the directory where you have specified your Java. Now, it’s time I’ll show
you how to run a Java code. So all you need to do is cd. Go to the directory where
your Java code is present. I’ll run the hello world program that I had shown you earlier, so cd Java. It was present inside Java. Now, to compile your code,
you need to specify javac, followed by the class file in which you have stored your code. So I had stored it inside
hello.Java and once you type this, you can see a class file
would be created there. So I already had it created. Let me just remove and
actually do it again. So let me just remove this and let me just re-execute this command. So if you see here a
byte code gets created with the compiler command. So once you’ve done that it’s
time we execute this program. So to do that, all you
need to do is specify Java and then your hello.txt. So once you’ve compiled
your code, let’s execute it. So just specify Java and the filing. So you can see the code has been executed. But this becomes a tedious process when you have huge amount of programs that you want to execute. So to overcome this challenge,
what we’re gonna be doing is that we’re gonna be installing an IDE. IDE basically refers to an Integrated
Development Environment where you can basically compile
as well as see your code. Now the most popular IDE is Eclipse so let’s go ahead and
download and use Eclipse because this is a
standard development tool that you’re gonna be using
as part of your industry. And to download Eclipse, all you need to do is
go back to your browser, search for Eclipse download. So go to the eclipse download option. And here I’d recommend that you download Eclipse Neon itself. Now, there’s a direct option to download the 64 bit for Windows but let’s say if you’re using a 32 bit, you can go to the packages option and here you can choose
the corresponding package. Now, there are multiple
versions of Eclipse for Java developers. So I recommend that you download the Java Eclipse IDE
for Java J2E Developers. So this is a complete package and it will be very easy
for you to use this as well in our upcoming sessions. So specify your system and download it. You will be directly given zip file. Now, inside that file
once you’ve extracted it, you’ll directly have your Eclipse which can launch from there immediately. Let me show you how
the Eclipse looks like. So this is your Eclipse IDE. So to your left, you have
all your Java projects then you will have your code present here and this is your output feed. So don’t worry we be using the Eclipse IDE in our next slide, so
just hold on to that. Any questions with respect
to the installation or any problems that you have faced? All right, so now that all of you have also installed
Eclipse in your system, it’s time we move ahead. So we will be discussing
the various data types present in Java. So these are some of
the primitive data types that are present in Java. They can be mainly classified
into four different aspects. So first you have your integer, then you have your float,
character and Boolean. Now, integer basically is used
to store a numerical value. So this numerical value again, when I say, cannot have decimal points. For decimal points you have float. So any numerical value that does not have a decimal point value can be used to store int. So again, with an integer type itself, you have four different types. You have byte, you have long, you have short and you have int. So, based on the numerical value which you’re gonna be storing, you can use either of these. So to give you a better understanding, in a byte you can store a number that is between either minus -128 to +227. But at the same time, when you look at int,
it occupies four bytes, so within an integer you
can store a numerical value that is between 2100
crows to plus 2100 crows. So it’s a huge number itself so mostly we stick to integer itself but again based on your need,
you can even use long as well. So if you have a number that
is going beyond that as well, you can use long. So based on your need, you
can use a corresponding value. So here you can see
how we initialized int, num is equal to 56. So I’m creating a numerical
value of integer type that we’re gonna store a value 56. Now after that, you have the float type. Again, float type can be used to store decimal
pointed numerical value. So in case if you do have any value that is taking decimal point value, then you’re gonna be
using a float variable. So again, based on your
need of storing the number, you can use float or double. And you can see how I
initialized a floating number is equal to 568151.3285. So are you clear with
respect to integer and float? Okay, so Divya is asking
me what will happen if I stored a decimal
value inside integer? So good question Divya. So what happens is that
if you initialize a number to be integer, and you’re gonna be storing
a decimal value inside that, let me tell you this, so
if let’s say, if int num is equal to this value that is 56851.3285, then the decimal point
value will be discarded and only 56851 will be
stored inside that number. Are you clear Divya? All right. Okay, now apart from numerical values, let’s say if you wish
to store a character, then you have the character type. So you can see how you’ve used character to store a single character. So any of the alphabetical characters you can store it in part of this. Apart from that you can even
store special characters and numerical values also in this but they will be considered
to be a character value. Then you have the Boolean type. Again, Boolean is very straightforward. It can store either true
or false value accordingly. So any questions with respect to the primitive
data types present in Java? Okay, Prashant is asking me can we convert a number stored inside
character to an integer? Definitely. So, you can convert accordingly Prashant but that’s a process called typecasting, but for now, don’t worry about it, you can convert it accordingly. Okay, so Henry’s asking me can I store a word inside a character? No Henry, you cannot store a complete
word inside a character but for that we have another
type that is called strings, but we’ll discuss that
in our upcoming slides, so don’t worry about it. All right, so moving forward,
let’s see a use case, where you’re gonna be using
the different data types. So now let’s look at a use case. So John owns a department store. So he wants to create a bill which has to have the following fields but John does not know Java. So he wants you to help him out and create the corresponding fields with the correct data types. So you’re gonna have an invoice ID, you’re gonna have a product ID, you’re gonna have a product cost, you’re gonna have a quantity,
a discount, total price and whether the customer
has provided feedback. So can you guys guess which data type is gonna be associated to which field? All right, so most of
you made their guesses. Let’s see how many of you are correct. Your invoice ID is gonna be integer, your product ID is gonna be integer. After that, your product
cost is gonna be double. Now, some of you’ve guessed it to be float but again, we prefer
using double over float because it is more precise. After that, your quantity
is gonna be again integer, discount is gonna be double,
total price is gonna be double and where the feedback
has been provided or not, is going to be Boolean based
on true or false value. Now let me just show you how to declare these values
as such in the Eclipse. So the first thing that you need to do as part of your Eclipse is you
need to create a new project. So on the left, most I can present here you can select the Java project option. So here you need to specify
the name of the project. So I’ll call it Java and then you can directly click on finish and automatically a
new project is created. Now inside our project you
can see you have source as well as the Java system library. This is basically all the libraries to which you can access to as
part of your Java environment. So now I need to create a new class file. So right-click, and then go to
option and click classified. So I had mentioned to you earlier as well, everything that you’re gonna be writing is gonna be part of a class. So click on class and then you need to specify
the name of the class. Now one thing to remember is that as part of the naming
conventions for class you cannot use white spaces as such. So what I’m gonna do I’m
gonna make that retail shop, retail_shop. So don’t make any changes here as such but one thing is make sure
you have checked this option that is public static void main. This basically means
that inside your class your main function is gonna be. So just click on finish and you have your class file created here. Okay, Barath is asking me what
does the main function do? Okay, Barath basically every time that you’re gonna be executing your code. your code starts executing from that main. So wherever you’re gonna
specify in your main, there you could start executing. For now don’t worry about these concepts, public static void main, we’ll be discussing more about that in our next session about
classes and objects. For now all you need to do is I’ll show you how to
declare these variables. So first what do you want to do is that we want to specify integer type. Now for interior type all
you need to specify int. So just type int and system
will consider to be integer. So you can see it’s been
highlighted separately because these are reserved keywords, that is when you’re using these keywords, Java in turn understands
that this refers to integer. Now, in my integer, I
had my invoice number so I’m gonna say invoice_num. So this is a variable name. So whether I’m gonna use invoice
number as part of my code it basically means that
this is an variable that is of integer type. So if you have multiple
variables of the same type, rather than declaring them into loop, all you can do is put a comma and then start specifying
then next variable. So after the invoice ID,
we had the product ID, so that is product_id. And then the third integer
value that we had was quantity. So I’m gonna call it quant, and after every statement make
sure you put a colon here. So this basically tells you that this is an end of a statement. So in Java, it is very important that you make use of semi colon
at end of every statement. So this is a challenge most
programmers face initially when they forget to put semicolon at the end of their statement and they end up having a lot of errors corresponding to that. So every statement has to
end with the semi colon. After that we had double values. So for double what you’re gonna do is you’re gonna go to next line and you’re gonna call it double. Now, I had my cost that
was, so this is called cost. Then I had discount and
then I had my final price, so I will just call it price. So these were doubles. Now to specify a Boolean type you have to completely type Boolean B, that would be Boolean,
and then you had feedback. So this is how you define
different data types in Java. So by default, all the integers
would be assigned zero, the doubles would be again, 0.0, and your feedback would be set as true. So are you clear on how to
declare variables in Java? Okay, Divya is clear. Sandeep, Aaron, Anna,
that’s great to see guys. So now that we’ve seen the
various data types in Java, let’s move forward and look
at the various data operations that you can perform in Java. So you have mainly for different
types of operators in Java. So you have your arithmetic operators, that is to perform arithmetic operation, you have addition, subtraction, division, and finally, you have modulus. So this is something that
you may not be familiar with if you’re not from programming background. Basically, what modulus does
is that it performs division but rather than giving you the quotient, what it gives you is the
remainder of that division. So let’s say you’re gonna
perform five divided by three. Rather than getting one
what you’re gonna be getting is that you’re gonna be getting
to as the reminder value. And then you have the unary operator. Now the unary operators basically are increments and decrement value. So let’s say there is a specific value that you have initialized. So let’s say you have
an A that is equal to 10 and somewhere in your program, you want to increment it after
a sorted set of operations. So what are you gonna do is that you want to
increment in the value of A with the increment operation. Now don’t worry too much on this, we’ll be using the increment
operator in the loop statements and I’ll show you how it
plays an essential role as part of control statements. Then you have the relational statements. Now this is something that
you may be familiar with, you have lesser than, you have
greater than, or equal to, you have lesser than equal to, you have greater than,
you have not equal to. Again, not equal to is represented
with an exclamation mark and equal to sign. So when you do that, it
basically means it’s not equal to and for equal to it is double equal to because if you put a single equal to, it’ll consider to be an
assignment operation. So finally we have the logical operators. Now in the logical operators, you have three main operators;
you have AND, OR and NOT. So basically the NOT condition is used to negate the corresponding value. So let’s say there’s a condition that is true then if
you want to negate it, you’re gonna be using a NOT value. Now the two operators that
you need to emphasize on is the AND and OR operators. So here you’re gonna be working with different conditions as such, that is you’re gonna be
using the AND and OR operator on different conditions. Now let’s say both my
conditions are true, okay? So in that case, both of these operators will
give me the output to be true. But in case if one of
your condition is true, and the other is false. Now, in that case, your OR operator will give you
the output value to be true and your AND operator
will give it to be false. So unless and until all
your conditions are true, you will not get a true value to be output in case of an AND operator but in case of an OR operator, as long as there’s at
least one true statement, then you can get the output to be true. Now, these will be used in
control statements again. Now, don’t worry, you will be seeing that
in the upcoming sessions but I hope you’ve got a complete overview with respect to the different
data operators, okay? Divya is clear, so is Barath now. Okay, so let me give you an example here. So basically what you’re doing is that you’re taking
four different values, and then your correspondingly
assigning them an operation. So I had told you earlier equal to is used to
assign a specific value to that variable. So this is an assignment operation. So this is also another operation that you’re performing in Java. So what I’m doing is that
I’m assigning num1 to be 10, num2 to be 15, and num3 to be 25. So here, before you compute
the value, what happens is that there is a board
muscle that you follow. Now, this is something that
you have again in maths that you may be familiar with. First the bracket value is computed, then you’re gonna be
seeing the pu-th-re-ds. So you have division
first, multiplication, addition, then subtraction. So let’s look at this
operation present here. Now let me help you understand
how it happens here. Now, before any automatic
expression is computed, you’re gonna be following the board muscle that is you’re gonna first
consider the brackets then you can do be checking for if there’s any divisions
present and multiplication, additions and finally obstructions. So if you look at the
operation present here inside the brackets you
have an addition happening. So you’re gonna add 10 plus
15, you’re gonna get 25, then you’re gonna come up. Before you subtract 25 from 25, you’re gonna perform
the division operation, that is 25 is gonna be divided by five, which will give you the output to be five. So from 25 five is gonna be subtracted and you’re gonna get the output to be 20. Now again, when you see you’re using an increment operator here, now my original value
of num1 was set to be 10 and I’m using the increment operator, then it is gonna be increased to 11. After that, you’re
using a logical operator where you’re comparing, that is, is num3 greater than
equal to num1 plus num2? So 10 plus 15 is equal to 25. Therefore, it’s gonna give
you an output to be true. Then you’re checking is
now to equal to num1. This is why you’re
using a double equal to. If you use a single equal to, the value of num1 will
be assigned to num2. So in case if you want
to compare two values, you’re gonna use a double equal to. So num2 is not equal to num1 so it’s gonna give you an output of false. And finally, you’re using a
logical AND operator here. So is num2 less than num3? Yes it is, 15 is less than 25. And then you’re gonna check,
so one of my condition is true. Then when you come to the
right-hand side of the operator is num1 greater than num3? No, that is false therefore I have two and I’m gonna perform AND
operation with false value and this is going to
give me a false output. So we had discussed this earlier. Until and unless all
my conditions are true, I will not get an output to be true in case I’m using an AND operate. Finally, I had initialized num4 to be true and then negating that
value which is giving me output of false. So I hope with this you’re clear with respect to the data operations. Okay, so moving forward,
let’s look at a use case where you’d be using
these operators as such. So Matthew has come to
John’s retail store. Now Matthew wants to
purchase a few products from John’s store and he’s
given John the list of products that he wants. So John wants two quantity of itema, he wants a single quantity of itemb and he wants three quantities of itemc. So already the prices of these
products have been displayed. Item A cost 200, item B costs
75 and item C costs 500. Now at the time Matthew
visited John’s shop, there was a 10% on all
the products as such. Now from your total cost,
you need to subtract 10%. Apart from that, there was also a 5% service
tax applicable on this, so you need to add a
5% service tax as well. So let me show you how it is done. So now let’s go ahead and write a code to compute this value. So I have three items which
have a corresponding value. So what I’m gonna do is that I’m gonna call
them to be my variable. Since they don’t have a decimal value, I’m gonna make them integers, so in itema, and then I’m gonna initialize it with the corresponding value. So item A cost 200. So I have given the corresponding value. Then I had item B, which is equal to 75. And then we had item C,
which was equal to 500. So again, once you’re done
in declaring these variables, put a colon. And then we need another variable which will store the total price. So I’m gonna call it double price. So you can see here we are
not only declaring variables, we are also initializing them
with the corresponding values. In case if you want to
set a corresponding value for a variable before
you go ahead and use it, you can initialize it at the
time of declaration itself. So that’s exactly what we have done here. So now finally let’s go
ahead and compute our price. So price is equal to, now
let’s try the expression. So I’m gonna use multiple parentheses here so that I can perform all the
operations in one single code. Matthew wanted two quantities
of item A, so itema into two. Then he wanted a single
quantity of item B. Finally he wanted three
quantities of item C, so itemc into three. So with this, we have computed the total
value of these products. But we’ve seen that
there was a 10% discount associated with these products. So I need to compute that value. And then to that value, I
need to add a 5% service tax. So again, price is equal to price minus 0.1 into price. So here what you’re doing is that the existing value of price, you’re gonna subtract it
with 10% of the price value. So this value once it’s completed, will be updated and stored inside price. So always you need to remember that the expression to the
right-hand side of assignment is computed first and then
the corresponding value is stored inside the variable to the left. And finally we need to
add a 5% tax to this. Now to do that, just do 0.05 into price. So with this we’ve also added the tax. So it’s time we print
out this value for it. Now, to print any command in Java, you’re gonna use the following syntax, that is, System.out.println. And then inside quotes, you can specify what you want to print. Now, there’s another
simple way of doing this. All you need to do is just type this out and then press Ctrl + Space. So this will directly convert
your sys out statement to system.out.println. So this is basically
a shortcut in Eclipse. So here I need to print my price value. So all you need to do is specify price. So with that, just save the code and then it’s time we run
it, right-click on the space and then we have an option here run as. So select a Java application and then you can see finally
the output that is computed. So with this, I hope you’re clear on how to use the various data operators and how we can print a corresponding value out to your screen. Okay, so I’ve got a
confirmation from everyone. So let’s move on to the next topic that is the control statements. Now the control statements are
basically it’s the statements that will define how the
flow of your program code is gonna go ahead. Now, there are mainly three types of control statements in Java: you have the selection statement, you have the iteration statement and then you have the jump statement. Now inside the select
statement, you have if and else then you also have switch case. Now, these are two types
of selection statements that you will be using
based on your requirements. After that, you have your
iteration statements, or as they’re better known,
you have looping statements. So there are mainly three
types of loops in Java. You have your while loop,
you have your do while loop and you have for loop. And finally we have jump statements, that is break and continue,
which will be used to jump to a specific point in your program. Now don’t worry, we’ll be discussing about
each of them in detail. So now that you’ve got an overview of the various control statements, let’s move forward and look
at each of them one by one. We’ll start off with the if statement. Now, the basic syntax for
the if else control statement is that you have if
followed by a condition, then in case if the condition is true, the statements which have to be executed. If the condition is to be false,
it’ll go to the else block and then execute those statements here. So it’s not restricted that you can only check
one condition with if else. You can have a multiple if else statement. So you can check if condition one is true, then you can execute statement one, else if you can check condition two. If condition two is true, then you can go ahead and
execute statement two, again, else if condition
three and so forth. So you can have a laddered if else. So I’ll just give you an overview
of how the if else works. So you always start your program and then you check whether it is right. Now, if it is found to be
true, that is, if it’s raining, then you need to take an umbrella. But if it’s not true, then
you’re gonna go play football. So are you clear with respect to how an
if else statement works? Okay, Prashant says yes, so does Barath. Henry is clear, Divya, Aaron,
that’s great to see guys. Now moving on ahead, the
next selection statement that you’re gonna be talking about is the switch case statement. Now, when come back to
the if else statement, the switch case is quite straightforward. So here what you’re gonna do is that you’re just gonna write switch and then the expression here. So you need to understand
here in case of a switch, putting in an AND condition
would become problematic. Here you need to specify a
direct expression as such. Now to give you a better idea, let’s say if you want
to compare two numbers, then you’re gonna use
it if else statement. But let’s say if there’s a specific value that you want to check against, then you’re gonna be using a switch case. So how switch case works
is you specify switch followed by the expression,
then you come inside the loop and then you start writing
the different cases. So your case one will
check against the value that the expression will be
taking and then if it is true, it will execute statement one and then it’ll break from
that switch case statement. But if it is found to be false, then it’ll check the next case. Now, if the next case is found to be true, it will execute statement two and then it’ll break from that. So I hope with this
you’ve got a general idea. So to give you a better understanding of how the switch case works,
let’s look at an example here. So basically, you’re tossing a coin here. So if it’s heads, then Tom wins, and then it’s the end of the game. But if it is false, and it’s true, then Adam wins and the game ends. But let’s say you’ve taken
a wrong input from the user, then you’re gonna go to
the default statement and then print invalid input and come. Now, the default
statement is the last case that is gonna be executed if your expression does not
meet any of the case values that you have specified. So are you clear with respect to how the if else statement works and how the switch case statement works? All right. Okay, now let me give you an example with respect to the if
else and switch statement. Let’s go back to our
pricing system itself. So here let’s say you want to
provide an additional discount to the customer if he’s
purchased over 1,500 rupees. So what you’re gonna to do is that you’re gonna write
an if else statement here. So if my price is greater than 1,500, then what are you gonna do is that you’re gonna print congrats, you have a 25% discount coupon. Now in case if it is
not greater than 1,500, then what I’ll do is I’ll change to else. Then here you’re gonna
specify the else condition. I’m just gonna say thank
you for shopping with us. So basically what we’re
trying to establish here is that if the customer
has purchased products worth more than 1,500 from John’s shop, then he’s gonna get an
additional 25% discount coupon that he can use next time. So again, when the next
time that he comes, instead of giving him a 10% discount, he can win that 25% discount. But in case if he’s not
purchased more 1,500, then he does not then
the corresponding message does not get executed. So I just run this program again. So you can see he’s purchased 1,866 so therefore he’s getting
a discount of 25%. So here let’s say I’m
gonna reduce the price that he’s paid, prices is
equal to price minus 500. This is just to show you
how the else statement will get executed. If I rerun this code again, then you can see only
thank you for shopping us is being printed. So originally, he’s
had purchased for 1,800 but when we are checking the condition we are subtracting for 500. Therefore it is going
to the else condition. So are you clear with respect to how the
if else statement works? All right, that’s great. Now moving on, we’re gonna talk about
the various loops in Java. Now the purpose of having
loops in our program is to help us reduce the code redundancy. So what you want to do is
let’s say you want to perform a certain set of operations
multiple number of times. Then you’re gonna put it
inside a loop statement. So therefore you don’t
have to write the code multiple number of times,
the loop will execute it for any number of times that you wish. Now when we talk about
the various loops in Java, there are mainly three types of loops. You have the do while, you
have while and you have for. Now in the do while loop, what happens is you execute
the set of statements inside the loop once, then you check if the condition is true. If it is true, then you’re gonna go back and execute the condition
till it becomes false. So basically a do while
loop executes at least once and then only checks the condition. So it is an exit control loop where you’re controlling when
the controller should move out from the loop statement. Again, similarly, when
you look at while loop, while is quite similar
to the do while itself but the differences is in while
the condition is first set and then you enter loop
statement directly. So you mainly use why loop when you’re not sure of
the number of iterations you have to perform. Now, you use do while and while loop mainly when you’re not familiar with the number of iterations
that has to happen. So what happens is it runs for an uncertain
the number of times and once you’ve met your condition, then you come out from the loop statement. The only difference
between do while and while, is that do while executes at least once before the condition is checked whereas while checks the condition and only enters the statements. After that you have for loop. Now for loop is also an entry control loop but the difference being
you’re gonna use the for loop when you know the number of times you’re going to repeat something. So let’s say you want to execute a certain set of statements 10 times then you can put it in a for loop and specify it has to run for 10 times. So I hope you’re clear with respect to three
types of loops we have. Okay, Divya is clear,
so is John, RJ, Santush, Prashant, Anna, that’s great to see guys. So please let me know at any point if you’re not following me or if you do have any doubts, let me know, I’d be able to clarify
them and there itself. Now, moving forward let’s
look at the do while loop. Now the syntax for the do while loop is quite straightforward. You’re gonna specify the keyword do first, and then you’re gonna specify
this corresponding statements that you need to execute multiple times. So once the statement gets executed only then you come to the while part where you’re gonna check the condition. If the condition is true, then you go back to the do
statement inside the loop and then you execute that statement. Once you’ve executed that statement, again, it comes out to the
while, you check the condition, and then again, if it’s
true, it’ll keep going back and doing the same until
the condition is false. So you need to understand
how the cycle works so let me give you an example for that. So you always start to a point and then you execute the block of code that you want to execute,
this runs at least once. Then what happens is you check if the condition is true or false. Now, in case if it is false,
then you accept the loop. But if it’s true, you’re gonna
go back to the point of do and then you’re gonna
repeat the same execution of that statements. So are you clear with respect to how a do
while statement works? All right, so after that we’re gonna look at the while statement. Now if you remember with
the do while condition, you had a first do then
you execute the statement and then you check the condition. But here what happens is first itself while checks if the condition is true. Only if it is true does it enter and then execute the following statements. So every time before you enter the loop, you’re gonna check the
condition is true or false. So you’re gonna start the (mumbles) and then you’re gonna check
the condition straightforward. So if it is false, you
want to exit from that loop but if it is true, then
you’re gonna go back and keep repeating till
the condition turns false. I hope you have understood
how the while loop works and the difference between
do while and why loop. All right. Now finally, we have the for statement. Now the for statement as I’ve mentioned, is used when you have to run
it a limited number of times. So the syntax for for
is slightly different. Here you’re gonna have
three major concepts: you’re gonna have an initialization, you’re gonna have a condition, and then you’re gonna have an iteration. So basically what happens is you initialize available
here to the value that you want, then you’re gonna specify the condition based on which it is checking. And finally, you’re gonna
specify the iteration. So let’s say you want to
execute a statement five times, then you can initialize
the variable value to one, then you can check if it
is less than equal to five and then you can always iterate it using a incremental update. So for five times, the statement present
inside will get executed. So again, if you look
at the flow of for loop, you can start the code. You first begin by initialization, then you move forward
and check the condition. Now if the condition is false,
then you accept the loop. But if it is true, you will execute the
corresponding statements. Once you’ve completed the
execution of the statements, then you go ahead and iterate the value and go back to checking if the
condition is true or false. So are you clear with respect to how do
while and for loop works? Now, don’t worry, I’ll
be giving an example to understand each of these
loops one after the other. Now finally, you have the break state. So let’s say you had gone to run a loop for infinite number of times but there should be a point
when you need to leave out from that loop. And that is exactly
why the break statement comes into picture. Now, using the break statement, you can leave the control of the loop when a specific condition is met. I will show you a flow of this that’ll give you a better understanding. Now you start execution of your code, then you’re gonna check
the loop condition. So basically, you’re gonna
write a brief statement inside a loop or you’ve
seen it earlier also in the switch case condition. Now, in switch case statement, what happens is that
if a case is executed, then you break out from the
control of the switch case and that’s where you use break. So, what happens if you do
not use a break statement inside switch case is that
every corresponding case after the case you
wanted will get executed. So I’ll show you the
example for that as well. But for now, let’s try to understand this. So once you’ve checked your
loop condition, if it is false, you’ll exit from the
loop, but if it is true, you’ll come down and you’ll check if the
break condition is true. Let’s say it’s going to be true, then you’re gonna execute from that loop. But if turns out to be false, and you’re gonna execute the statements that are remaining in the loop, and then go back to repeating that loop. So every time when the
loop is getting executed, you’re gonna check if the
break condition is met. If it is, you leave from the loop and if the condition is never met, then you’ll exit from the loop as well once you’ve completed the
execution of that loop but opposite to the break statement is the continue statement. Now with continue, what
happens is that every statement that is after the continue
keyword does not get executed if the continue keyword is true. So let’s say you’re gonna
start execution of a program then you’re gonna enter
the loop condition. If it is false, you
gonna exit from that loop and if it is true, you’re gonna execute a
certain set of codes. Now in between these codes you
have your continue keyword. So if the continue condition
is considered to be false, then every statement after
that will get executed and you will go back to
execution of that loop. But let’s say it is true. Then what happens is that every statement that is after the continue keyword does not get executed in
that iteration of that loop. So every time a continue
keyword is found to be true, the execution of block
two does not happen. So I hope you’re clear with respect to how the break
and continue statement works. Now, to give you a better understanding of how the loops works, let’s
look at a use case here. Marry is a schoolteacher. She’s conducted final-year
exams for her students, for five different subjects. Now as there are multiple students, she wants to create an automated system that will take the marks
of each of the students, calculate the final score and then give a grade
distribution for these students. So she’s also given us a
grade distribution chart here. So if the school student
has scored below 40%, then he’s gonna get a poor grade. But if he’s scored 40 to
59, he gets an average. Similarly, 60 to 79 gets a good, 80 to 89 is getting very good, and 90 plus gets the excellent. So you’re gonna write a Java
code that makes use of loops, as well as control
statements to achieve this. So let’s see how it’s done. For this, I’m gonna create a new file. To do that, you can either
go back to your project, right-click and select
new option from there, or you can directly click on
the new class option here. Once you’ve set the
name, click on finished and you have your new class created. So I basically have
five different subjects for which I’m gonna take the marks. So I’m gonna assume the
marks are gonna be in wholes, so what I’m gonna do is that I’m gonna consider
the marks to be integers. So int we had maths, physics, then you had chemistry, English, and finally you had computer science. Now I need to store the
total marks on there so I’m gonna declare a double
variable that is finalscore. So now it’s time we begin
with that looping statement. So what I’m gonna do is
that I’m gonna start off with a do while statement, I’ll say do then open
parenthesis and then enter it. So always make sure when
you’re writing a code in Java or any programming
language for that instance, you follow the indentation because this will also help you identify where you’ve gone wrong and whether if you’ve missed
any parentheses or such. So always follow the indentation rule. So the first thing what I’m gonna do is that I’m gonna take
the subject-wise marks for each student. I need to take the maths marks first. So this statement will request
for the marks for maths. It will print out a statement that says enter marks for maths. Now, it’s time we go ahead and
take the input from the user. Now, before you go ahead and
take the input from the user, there’s one thing that you need to do is that you need to
create an input object. So I’ve already discussed this with you, everything that we do in Java
is through objects as such. Now, to create an object for taking input, I’ll show you the syntax. It’s is gonna be scanner
followed by the variable name, through which you’re gonna take the input. So in our case, I’m gonna
call it marks equal to new. Now new is a keyword through which you’re
gonna create a new object. So new and then we have to specify the original class to which it belongs, so that’s again, scanner here and then you need to specify System.in. Now this is one of the statements through which you’re
gonna create the object but before that you need
to import a certain package from the Java library. So to do that, just
type import java.util.*. Now this basically import
everything from Java Util package. Once you’ve done that, you
can use this kind of function. So to take an input from the user, what you need to do is
specify the variable in which you’re gonna take the input. So in our case, I’m gonna
start off with maths is equal to the object
that is used for input. So in our case, it’s marks.nextInt(). So what this basically means is that the next integer value that is being given by the user should be taken and stored inside maths. So are you clear on how to take
input from the user in Java? Okay, Prashant is clear,
so is Barath, Henry, Dave, that’s great to see. Now, I need to take marks
for the next five subjects. So rather than typing it out, I’m gonna just copy paste this. So maths, after that, you have chemistry, you have physics, you have English and finally you have computer science. Change the corresponding
variables as well. Now, this is for chemistry so
let’s rename it to chemistry. The next one was for physics then we had English and finally computer science. So with this I have taken the
marks from all the students. So now it’s time I go ahead and compute his final score. Now to do that what you’re gonna do is that you know right
final score is equal to. Then you have to add all the marks and then divide it by five
since we have five subjects. So let’s add each of these marks, that’s maths plus English plus chemist plus physics and finally computer science. All this divided by five and has to be stored inside finalscore. So any questions still here? Are you clear with respect to how we’ve
used input output statements and how we’ve taken the
input from the user? All right, now the next
part is to assign the grade. Now the first thing that we’re gonna do is we’re gonna create an
if else ladder for this. So if my final score is greater than 90, then he’s gonna get excellent. I’m gonna say else if, that is if the
first statement is not true, then check, final score greater than 80. So if you remember, the second statement was between 80 to 89. Now if it is not greater than 90, only then will it come to else if part. So here I’m gonna say very good. Again, I have another else if. Else if final score is greater than 60 he gets just good. Then you have another else if elif my final score is greater than 40 then I’m just gonna print average. Now after this I’m not gonna
write an else if ladder, I’m just gonna write else. The final result that is if
he’s got below 40% that is poor. Now, here what we’ve done is that we’ve used the if else ladder and specified which grade to get in case of a specific range. So now, what you need to do is you need to still specify
the while condition here. But before that, you need to understand when to exit from that loop. Now for that, what I want to do is that I’m going to
write an output statement, any more students. To know if there are any
more students present, I want my user to tell
me if it is true or not. So they’re gonna specify true or false. Now to store the true or false value I need a Boolean variable so let’s go back and declare
a new Boolean variable. So here let’s say result is equal to, again use our input variable
that’s marks.nextBoolean(), that is the next in Boolean value that is inputted should
be stored inside result. That is, while my results statement is always to keep
executing this statement. So when you say while result, it basically means till the
time my result value is false, keep executing these set of statements. So any questions here on how we view we’ve
used the if else ladder or how to use as a do while loop? All right, so let’s go ahead
and execute this program. So it’s asking me to
enter the marks for maths. Let’s say 85, then chemistry is 47, 56, 75, 92 and then you get a grade that is true. So in case if I say true, it’s gonna again ask
me the marks for maths. So again, let’s go with 75, 84, 92, 48, 56 and then you can see the
corresponding result. So finally, let’s say
if I want to say false, it exits from that program. So are you clear with respect to how to do
while loop is executing? All right. Now, same thing can be also
changed to a while loop. Now, all you need to do is that you need to remove
the while from here, go back to where you’ve
returned your do go there and then mention the while here. So here what is happening is that first year checking the result and then you’re gonna enter this loop. Now, before you go ahead
and then enter this loop, this value has to be assigned to be true because until and unless
the statement is true, it will not enter this condition. So what I’m gonna do is I’m
gonna initialize it to be true and then I’m gonna begin the
execution of the statement. So are you clear with
respect of how do while and while differs between both? So here basically it will
run infinite number of times till you’re gonna enter a false value but in the previous case, it’s
gonna execute at least once and then it’ll ask if you
have any specific values and keep running till
you enter a false value. Now, in the do while loop,
it executes at least once, but the value will execute only if the condition is to be found true. Now, in the same case let’s say you know the total number of
students beforehand itself, then what you can do is that
you can use a for loop here. So what I’m gonna do is that
I know the number of students so I’m gonna remove this
last statement from here. And before I enter my loop, what I’m gonna do I’m gonna
ask the number of students. Once you have that, you’re gonna assign it
to a corresponding value. Since I don’t need this,
I’m gonna remove it. And let’s say here I have
another integer value that is student number. So again, let’s see student number is equal to my marks.nextInt(), that is the next integer value entered should be my number of students. So with this, you’ve already established the total number of students for whom you want to enter the results, then you’re gonna go ahead
and execute the for loop. Now, if you remember the command for for, it is for and then you
begin with your operation. So here you need to start
with initialization. So I’m gonna use a variable known as i, which will increment its value and always keep comparing
with the number of students. So let’s say int i equal to zero, because always the numbering
in Java programming, be it the numbering of array,
be it the number of looping, everything is suggested
to start from zero. The counting system in Java,
or most programming languages, starts from zero in step one
so it’s always recommended that you start from zero. Now, for i is equal to zero,
i less than student number, and after the execution of each statement, I want incremental value
of i by one, so i++. So any doubt with respect to
the syntax of the for loop? Okay, Prashant is clear, so
is Barath, Sandeep, Santo, that’s great to see guys. So now let’s save this and
re-execute this code once again. So run as Java application. You’re gonna can ask
the number of students, let’s say two, enter marks for maths. So you’ve got yourself a first student. Let’s try it again. It’s got good. And then you can see it has
accepted from that loop. So our loop has run twice. So any questions with respect
to how the for loop works or the while, or the do while? Okay, so now that everyone is clear with respect to how the for
and do while loop works, so now let me show you another program where you’re gonna be using
both break and continue. Let me create a new class,
let’s me call it numbersequence. So here what I’m gonna do is that I’m gonna run
my for loop 50 times. But inside my for loop, I’m
not gonna print even numbers. And in case if my number crosses 20, I’m gonna break out from that loop, let me show you how it’s done. So let’s say for i equal to one, now in this case, my counting has to start from one because I’m gonna check from one to 50. i less than equal to 50 and i++. Now, inside this you need to specify both your break and continue statement. So first thing let me check
if the number is odd or even. So to do that, or if you do is say if i modulo two. So if my number divided by
two is gonna be equal to zero then that means that the
number is an even number because all even numbers
when divided by two gives me a reminder zero. So in that case I’m gonna
write a continue statement. That means don’t bother with
the statements after this, go ahead to the next iteration. If not, then else. I’m gonna print that number. So what happens here
is that it’s gonna run and print all the odd numbers till 50. Save it and let’s execute this. So you can see all the
odd numbers are printed but let’s say I don’t
want to print the numbers that are more than 20, then I’m gonna write another
statement if i greater than 20. In that case, break. So we need to change it to else if and then let’s run the second. So you can see here initially
when it was printing till 15 but at this point, when
it is greater than 20, it’s breaking out from that loop. So I hope you’re clear with respect to how the for loop works, how the while loop works,
or the do while loop works and how the break and continue statement are part of this programming style. So I’ve got a confirmation
from Divya, Prashant, Barath. So that’s great to see guys. Now, I hope you guys are trying out these codes simultaneously so that in case if you’re
facing any challenges you can let me know and I’ll
be able to assist you as well. So moving forward, we’re gonna
talk about the next concept that is arrays in Java. So remember, our good old friend John, John is facing a new problem now. Now John has 15 invoices that he wants to store with
the corresponding amount, but he doesn’t want us to
them as different number. Now, to solve John’s problem,
we have a data structure that is array. Now, an array is used to store
elements of the same type in a sequential order. Now, an they can also be single dimension or multi-dimensional and each array has mainly two components; now the first its corresponding index and the second is the
corresponding values. So when you have a sequence
of values to be stored, they will be stored in the value segment and the index is something that gets automatically incremented based on the number of
values you want to store. So the indexing starts from zero and goes on till n minus one
of the size of the array. So let’s say if you want
to store 15 numbers, then it starts from zero
and goes all the way to 14. Where let’s say, if you want
to extract a specific value from an array index that is, let’s say you want
the fourth value present there, then you need to specify
the corresponding index and the value can be extracted from this. Now, I’ve already told you
there are two types of array, you have the single dimensional array, and you have the multi-dimensional array. Now for initializing the
single dimensional array, you have this following syntax where you specify the
data type of the array, then the name and then you
use square parenthesis. This is to help Java understand that it is going to be an array. Then you need to specify the keyword new. I had mentioned you earlier as well. You’re gonna use new to
create new objects as such. So here array is also an
object and then again, the data type as well as
the size of the array. So below you can see
we’ve created an array with the name a of the size 12. The indexing starts from zero and goes on all the way till 11 and corresponding values
are being stored here. So let’s say if you
wanted the value of seven, then all you need to do is take a of six. Now when we talk about a
multi dimensional array, your data is gonna be
stored in a matrix format. So you can see the initialization is also slightly different. You again specify the data
type, the name of the table, then you’re gonna use two
sets of square brackets. Now this is to help Java understand that this is not a
single dimensional array, but a multi dimensional array. Then again, the new keyword comes followed by the size of the
multi dimensional array. So here you can see we specified
the number four and five. This is to help Java understand
that I need four rows and five columns in my table. So you can see here the array indexing starts on
zero to three for the rows and zero to four for the columns. So this is how your multi
dimensional array looks like. It’s similar to a matrix
that you use in mathematics. So I hope you’re clear with respect to how an array looks like and how to initialize one
because we’re gonna go ahead and help John out with
his invoice problem. All right, Divya has
given me a confirmation, so as Parat, Adam, Henry, Prashant. And let’s go back to our Eclipse. So here let me create a new class. I’m gonna call it arrayhelp. So we need a two dimensional array. So again, this is gonna have a mod. So what I’m gonna do is that I’m gonna make it
a multi dimensional array of double for. So double, I’m gonna say invoice, and then you need to specify that it’s a multi dimensional
array, so two square brackets and then equal to new double. So here you need to specify
the size of the array. Now John had told us he had
15 invoices and he also said that he wants to store the invoice number with the corresponding amount. So basically I’m gonna create
a new two-dimensional table which is gonna have 15
rows and two columns. Now, to do that, you need to first say 15 and the second case it’s gonna be two. So any doubts on how to create
a multi dimensional array? All right, so to access a
multi dimensional array, what you’re gonna need is that you’re gonna need
multiple nested for loops. So let me show you how it’s done. So let’s say for hint i equal to zero, my i should be less than 15,
that is the row number and i++. Now for j, equal to again zero, j less than two, j++. So here first I’m gonna
ask him to enter the value and then I’m gonna take the input. Now again, if you
remember how to take input you need to first import .util, Java.util.*. So this command completely loads the util and you can go ahead and
then create a Scanner object. New, C-A Scanner, and
this is a system input so System.input. So now it’s time I take
the input from the user and then store it into my invoice table. So invoice of i, that is my row and my column, is equal
to input.nextDouble(). So idea on how to take
an input for the array? Now, for printing out the output what are you gonna do is that you’re gonna use a system output in which you can directly print the value. Invoice of i, j, and then a colon. So let’s just run this program. It’s gonna ask me a set of values. So I’m gonna give random
set of values here. So let’s say 147.0. So you have your complete set of values that is being displayed here. So are you clear with
respect to how arrays work and how you’ve solved this separately? Now this is just a sample
value so don’t go by that. But again, I hope you got the
idea of how to use arrays. So coming back to our presentation, we have John store with it, and finally, we come to our final topic that is the object-oriented programming. Now, every one of you already heard about
object-oriented programming style, I have also discussed
about how (speaks off mic) to be an object in Java. But let’s just have a brief
overview of what are classes and what an object is here. After that, we move forward to discuss
the four main concepts of object-oriented programming. Now, Java is object-oriented and are class-based programming language. So if you’ve seen how Java is a class-based programming style and I already told you everything here is considered to be an object. But then you may have been wondering what is an object in itself. So basically, when you
talk about an object, anything that exists can be
considered to be an object. So let’s say you and I, are actually objects from the human class, or let’s say your dog or your cat, is an object from the
animal class as such, so I hope you get the idea. So in a class, you’re gonna define what are the basic functionalities each of the object should have but each object is unique
when compared to each other, like how you and I are not the same, but even though we are
objects from the human class. Now, to give you a better understanding, here you can see how a blueprint
of a house is depicted. Now, this basically is your class. This tells you how the
house should look like. But your individual house
in itself is an object. It may have the same
features as the class, but there will be certain
changes with respect to this and you cannot expect all the houses designed with the same
blueprint to be the same because there may be
slight variations as such. So, are you clear with
respect to what is a class and what is an object? Now, to call any programming language as an object-oriented
programming language, there are mainly for concepts
that it needs to cover. Now, the first concept that we will be discussing is inheritance. Now basically, inheritance is a mechanism through which one object
acquires all the properties from its parent object. So we’ve already seen
this concept actually. I just gave you an example as well. Now, if you see in the image here, it’s referring to same example where your superclass or
your parent class is animals. But when you look at the child class, it can be amphibians, it can be reptiles. It can be mammals, or it could be birds but when you look at their parents, it’s considered to be
animals as a super class. Now, through inheritance, you
can achieve code redundancy, and you can highly make
use of code re-usability. So let’s say there’s some common function or some common variable that you want to be present
across multiple classes, then all you need to do is specify that into the base
class or the parent class. Then all the children that you will be
inheriting from this class will have these same set
of variables or functions. Now, talking about functions, functions basically a set of codes. So these codes will perform
a specific operation and give you the desired result. Again, functions are quite handy when you have to perform
basic set of operations with different sets of values. Don’t worry, we’ll be talking
extensively about functions as well as these concepts
in our upcoming session but let’s just have an overview
of these concepts as well. After that, we have the
encapsulation concept. Again, encapsulation is
a highly useful concept in object-oriented programming. Now, through encapsulation,
what you’re gonna ensure is that now the variables
that are present in a class can only be modified or changed by a method present in the same class. So let’s say you have multiple classes that are linked to each other and one class is trying
to change the value of a variable present in the other class. And this is not directly possible, to do that you need to have
an object from the class in which you want to
change the variable name. So basically what you’re doing here is that your binding the code and the data manipulation together, that is if an object belongs to a class then only a method from that class can change the value of that object. So are you clear with respect to encapsulation
and inheritance? Okay, Prashant is clear,
Parat, Henry, Divya. That’s great to see. Now, moving on to the next concept that we’re gonna be discussing
about, that is polymorphous. Now, a polymorphism basically is ability of a variable or function, or even an object to take multiple forms. So here, we are not gonna
go in much in detail with respect to how a
polymorphism is applicable here, because one of the key
achievements that you’re gonna get through polymorphism is obtained
through function overload, and I’ll just give you an introduction of function overloading here. So let’s say you’re writing
multiple lines of code. And again, let’s go
into the example itself. Let’s say you want to
draw a specific diagram. Now you’re gonna have multiple
functions present already as part of your program. So the functions that
dealing with drawing, I’m just gonna call them
draw but based on the values that I pass to these functions it is going to draw different shapes. So let’s say in case of a rectangle, I’m gonna need two values,
that is length and breadth. Let’s in case of circle, I’m gonna need at least the radius of it. I hope you get the general idea. Based on the values that you pass, different functions will be called that serves different purposes. So this is achieved using
function overloading. Now, don’t worry if you’re
not clear with this, I’ll be talking about this
extensively in our next session. Now final concept that we’re talking about is data abstraction. Now basically, when we talk
about data abstraction, it means that you’re hiding the details and only showing the essential details. Now, if you look at the image here, basically, whenever we get a call, we just see an option to
either pick it up or cancel it. But in truth, there are a lot of codes that transfer every action that you do. But we do not need to worry about that, all we need to do if you
need to pick up a call is either swipe right, or if you want to cut the
call you’re gonna swipe left. And in Java, you’re gonna again, obtain abstraction through
abstract classes and interfaces. This is something I will be covering as part of our upcoming sessions as well. So I hope with this, you’ve
got a complete overview of the various object-oriented
concepts of Java and moving forward, let’s look a summary of today’s session. We started with an introduction of Java, then I helped you
understand why learn Java, we saw the key features of Java, or the buzzwords as
they’re called these days, then I helped you
understand how Java works and we discussed the various data types and the data operators in Java. Finally moving forward, we saw the various control statements and the arrays present in Java. So any questions with
respect to today’s session? So, all right, so the
recording of this class will be available in your LMS. I’ll also be sharing you the assignment as well as a solution to
the assignment in your LMS. So in case if you do have any sessions while you’re looking
back to the recording, you can always reach out
to Edureka Support Team that is available to you 24/7. Now, before you come
back to our next class, make sure you have simple
reading on what is a function and the various concepts
related to functions in Java. And with that, thank you
and have a great day. I hope you enjoyed
listening to this video. Please be kind enough to like it and you can comment any
of your doubts and queries and we will reply to them at the earliest. Do look out for more
videos in our playlist and subscribe to our Edureka
channel to learn more. Happy learning.

Be the first to comment

Leave a Reply

Your email address will not be published.


*