TRANSCRIPTEnglish

Java Tutorial for Beginners

12h 23m 25s150,605 words19,786 segmentsEnglish

FULL TRANSCRIPT

0:00

foreign

0:09

and in this video we're going to talk

0:11

about core Java in this particular

0:13

course which is a lengthy video you're

0:15

going to learn everything from the

0:17

basics of core Java to the end of core

0:19

Java now this is not the first time I'm

0:21

uploading the video on Java if you can

0:23

see from last few years you will find

0:26

multiple Java playlist and multiple Java

0:28

full-fledged video but we all know right

0:30

the quality of videos are going up every

0:33

day plus the number of features being

0:35

added to a language is going up and

0:38

that's the reason I'm going to re-record

0:39

the entire course now this is a

0:42

full-fledged course and that's why it's

0:44

so lengthy but you cannot finish the

0:46

entire course in one day even if it says

0:48

12 hours or 13 hours it will take time

0:51

now let's talk about Java now why Java

0:54

is so famous now if you see the

0:56

programming index the top five

0:57

programming language you will always

0:59

find Java there maybe at the first

1:01

position or at the fourth position you

1:03

will find Java why Java is so famous is

1:06

because Java has multiple features which

1:09

helps it to be used in the Enterprise

1:11

Market I mean not just Enterprise Market

1:13

it can be used for mobile development

1:15

web development and of course Enterprise

1:17

application and most of the big

1:19

companies if you go there they are using

1:21

Java as their preferred language now in

1:24

Java we have a technology called jvm and

1:27

it's not just Java we have multiple

1:29

languages running on jvm so even if you

1:31

are working on some other language let's

1:33

say kotlin Scala groovy ultimately

1:35

you're working on Java technology and

1:38

that's why if you learn core Java it is

1:40

much easier for you to jump to different

1:42

languages and I've also observed

1:44

irrespective of which entry you are

1:46

going for it can be for development or

1:48

testing and that doesn't matter which

1:50

language your working on that maybe

1:52

python C sharp C plus plus they might

1:55

ask you Java questions because because

1:57

most of the interview questions are

1:59

based on Java because they feel that if

2:02

you know Java that means you know other

2:04

languages as well or you can learn other

2:06

languages easily as for my experience or

2:09

did I forgot to introduce myself uh my

2:11

name is Naveen Reddy I'm a corporate

2:13

trainer software developer and currently

2:15

focusing one on blockchain but Java is

2:18

my first love so I'm working on Java

2:19

from the last 12 years I've made

2:21

multiple courses on Java on YouTube and

2:23

different platforms and yeah I can teach

2:26

Java any day anytime

2:28

yeah sometimes I have to watch my own

2:30

videos to realize the concept but the

2:32

thing is I love Java and with this

2:34

experience I'm making the entire course

2:35

I was talking about my experience right

2:37

in fact I have learned multiple

2:39

languages in this all 12 years maybe I

2:41

have worked on 12 different languages

2:42

you can name it maybe C plus plus C

2:45

python Java JavaScript uh go language so

2:50

I love most of the languages some

2:52

languages are learned just for the

2:53

projects but then when I was learning

2:55

those languages I was I was feeling hey

2:57

it's it's so easy it's because

3:00

I have learned Java yes people say Java

3:03

is difficult just to start with it is

3:05

difficult maybe if you want to learn

3:07

Java as your first language it's kind of

3:09

difficult because if you're coming from

3:11

other languages let's say python or

3:13

JavaScript and if you want to print one

3:15

statement you can actually see that in

3:17

one line uh that's not the case with

3:19

Java right you have to write five lines

3:20

of code but yes once you make a big

3:22

application the number of lines will

3:24

remain almost same okay so Java is

3:28

difficult yes just to start with but the

3:30

thing is Java is one of the most

3:32

readable language what I mean by that is

3:34

if you are looking at someone else code

3:37

you can read the Java code line by line

3:40

uh that's not true with other languages

3:42

okay so Java is great in that terms Java

3:45

is maintainable and that maybe that's

3:47

why they most of the industries are

3:49

preferring to use Java now Java has

3:51

multiple features we'll talk about those

3:53

features later but just to name them we

3:55

got Voda which is right once one

3:56

anywhere which supports multiple

3:58

multi-threads it has a collection API

4:00

concept you can handle the exceptions

4:02

and we are going to learn everything in

4:05

this particular course now just to go

4:07

for the history of java Java was made in

4:09

1995 by sun microsystem and it was led

4:13

by the team of James Gosling later on

4:16

Oracle bought Sun so now the trademark

4:18

of java belongs to Reichel yes in

4:21

between Oracle did some messed up things

4:23

with Java but yeah they are back on

4:26

track and the beauty is Java is getting

4:28

updated every six months not the major

4:31

updates but they are working hard to

4:34

make Java as the most powerful language

4:37

so I hope you were excited for the

4:39

entire course we are going to start from

4:40

the basic as I mentioned and you will

4:42

learn a lot of Defense stuff but as I

4:44

mentioned it's not about completing the

4:47

entire course in one day you can use a

4:49

timeline to jump between these sections

4:51

and also with this we also have a code

4:54

there so in the description you will

4:56

find the link GitHub link where you can

4:58

access all the codes and maybe it will

5:01

help it will be helpful for you to write

5:02

this code so let's start the journey I

5:04

know you were excited to write your

5:06

first Java code but the thing is to

5:08

write the Java code you need a place to

5:10

write the code right so basically you

5:12

need a editor of course notepad works

5:14

right we all love notepad apart from

5:16

notepad we have multiple softwares using

5:18

which you can write your Java code maybe

5:20

you you can use wordpad or you can use

5:22

notepad plus plus you can use edit plus

5:24

when it comes to programming there are

5:26

multiple editor options available

5:28

specifically for Java the one of the

5:30

most famous IDE now IDE stands for

5:33

integrated development environment so

5:35

basically we have editors where you can

5:37

only type the code or you can use a IDE

5:39

where you can type the code we can where

5:41

you can run the code where you can

5:43

compile the code where you can debug the

5:44

code and you can do everything in an IDE

5:46

now which is good now for beginners

5:49

using editor makes sense but then if you

5:52

are working on a real project you can

5:54

use a IDE now it is depend upon you

5:57

which idea you want to use in this

5:58

course we are going to use vs code

6:01

now there are multiple options available

6:03

we have Eclipse IntelliJ IDEA we have

6:05

netbeans in fact I have worked on a lot

6:07

on netbeans I just start off my career I

6:10

have worked on Eclipse as well for some

6:12

few years and then I've worked on

6:14

intelligent idea of one or two project

6:17

my personal favorite is intelligent idea

6:19

the only thing is the ultimate version

6:20

which is awesome is a paid version the

6:23

community version is good but it has

6:24

some limited features

6:26

now why we are using vs code is because

6:28

it's a very lightweight editor or an IDE

6:31

so you can do everything in vs code and

6:33

it's also lightweight

6:34

okay apart from that you also need a

6:37

compiler right so of course that when

6:38

you write a code of course you have to

6:39

compile your code so that your machine

6:41

can understand that and in terms of java

6:44

we have to download jdk which stands for

6:46

Java development kit now how do you get

6:48

these two softwares it's very simple

6:49

just go to your browser and search for

6:51

jdk download and also you can search for

6:54

vs code so these are two softwares we're

6:57

going to use

6:58

let's start with JK first in jdk we have

7:00

two versions We have a propriety jdk of

7:03

what I call and we have a open source

7:05

version in fact there are multiple

7:07

implementation for open source you can

7:08

choose any of them uh we have collector

7:11

we have different implementation you can

7:13

just export them but I'm going to use

7:15

Oracle jdk here yes at one point Oracle

7:18

made a Java paid but now it's free again

7:20

so we can use Oracle jdk here and you

7:23

can just click on the Java downloads

7:25

and you can see it will give you

7:27

multiple options the thing is

7:29

at the point of this recording Java 19

7:31

already launched Java 20 is going to

7:33

release soon and with every new version

7:35

in fact Java is following this six

7:37

months uh updates now so every six

7:39

months you will get new updates right so

7:42

with every new update you will get some

7:45

features but the basic language will

7:46

remain same so irrespective if you are

7:48

watching this when we have Java 25 don't

7:51

worry Java is Backward Compatible and

7:54

the basic language features will be same

7:57

of course W minor 2x in the new updates

7:59

new features

8:00

but overall it will be same in case if

8:03

there is something new happening which

8:05

is changing the basic concept uh we are

8:07

going to re-record that video in the in

8:09

this series

8:11

okay now which one it wasn't to choose

8:13

here should we go for Java 19 or Java 17

8:16

the thing is with every six months you

8:18

get new features right but not every

8:20

version is LTS LTS stands for long term

8:23

support so there are some versions which

8:25

get long-term support now Java 17 is one

8:28

of them so instead of downloading Java

8:30

19 we are going to go for Java 17

8:32

because it's LTS

8:34

so if I click on Java 17 here

8:37

and you can depend upon which OS you are

8:39

using I'm using Windows here or for the

8:40

kojo we are going to use Mac OS but then

8:43

the setup will change but the underlying

8:45

software will remain same the code will

8:46

remain same right so irrespective of

8:48

which OS you are working will not make

8:50

any difference for for the course

8:53

but for this video I'm using Windows

8:55

here and if you're using Linux you can

8:57

choose Linux distribution or Linux

8:59

package here

9:00

if you're using Mac OS you can download

9:02

Based on is it a mac Apple chip or it is

9:06

a Intel chip

9:07

and you can go for Windows as well if

9:09

you're using Windows and for this

9:11

machine I'm using Windows so you can

9:13

click on this download here and it will

9:14

take some time for the downloading

9:16

this setup is actually very simple you

9:18

just have to change one thing which I

9:19

will show you in some time and

9:21

straightforward in Mac

9:22

uh in Windows you have to set the path

9:24

okay we'll see how to do that

9:27

next is vs code and you can just click

9:30

on this download here now first of all

9:31

vs code is a very popular IDE now if

9:34

you're coming from some of the languages

9:35

you might have already used vs code so

9:38

it's quite easy

9:40

okay now depending upon which OS you are

9:42

using you can download one I'm using

9:44

Windows here I will click on download

9:45

and yeah it's starting soon and you can

9:48

see it is downloading here

9:50

now let's start with the jdk first when

9:52

you click on jdk

9:54

uh it will take some time the thing is

9:55

in my machine I already have this two

9:57

softwares so the first screen might

10:00

might look different for both of us

10:02

you can see from it says reinstall I

10:04

will say yes so here you can see it says

10:06

next in fact for you as well you have to

10:08

just click on next next everything is

10:10

smooth and depend upon your machine

10:11

speed it will change

10:14

the speed might change

10:16

okay that's jdk install but how do you

10:19

verify if you have jdk okay we'll do

10:20

that in some time but let's do the setup

10:22

for vs code again straightforward setup

10:24

uh say I accept you don't have a choice

10:26

here you can say next next

10:29

um

10:30

nothing to specify just body fight will

10:32

be there so just keep it and install so

10:35

by the time it is happening what I will

10:36

do is I will open my command prompt and

10:38

how do you check if you have Java

10:40

installed so go just type Java

10:43

hyphen hyphen version and it will give

10:46

you the Java version if it is coming

10:47

that means Java our JK is installed in

10:50

fact you have to check one more thing

10:51

which is Java C version for the compiler

10:54

and you can see it is getting giving the

10:55

version here but in case if you get

10:58

error that the Java C command not found

11:00

even if you have installed it that means

11:03

the path is not set how do you verify

11:05

the path here

11:06

it's very simple actually or you can

11:07

just go to search and search for

11:10

environment variables or if you type

11:12

environment it will give you the first

11:13

font just click on that and here if you

11:15

can see we have an option of environment

11:17

variables just click on this environment

11:19

variables and you can see we have user

11:22

variables path here you have to make

11:23

sure that you have a path variable or

11:25

you can edit times in system variables

11:27

if you want this to be applied

11:28

throughout the system different users

11:30

then you can use system variables or you

11:32

can use a user variable here

11:34

I will go for system variable make sure

11:36

that you have path in case if you don't

11:38

have a path click on new and set the

11:40

variable name as path and set the value

11:42

now what is the value here now the value

11:44

is your bin folder so what you can do is

11:47

you can go to your C drive go to program

11:48

files search for Java here you can see

11:51

we have Java and open this jdk click on

11:55

bin now this is a path because inside

11:57

this bin you have Java C so copy this

12:00

and you can paste it here in the

12:02

environment variables value

12:04

now since I already have pathway table I

12:07

will not create new if you don't have a

12:08

path variable create click on ok but I

12:10

will not do that because I already have

12:12

a path variable just double click it

12:14

it will show all the path variables for

12:16

my machine I already have it here if I

12:18

don't have it find the empty place or

12:20

click on new

12:21

paste and you're done

12:23

and click on OK for me it's only this I

12:25

will just remove this

12:27

and click on ok ok now once you do this

12:30

setup you have to make sure that you

12:32

restart your CMD

12:34

and now try for

12:37

Java C version and it works okay so

12:42

that's how you install jdk and make it

12:44

work now let's open vs code now once

12:47

your vs code is installed

12:49

you can see it's done for me I will say

12:51

launch so that's how vs code looks like

12:53

now this is a place where you have most

12:55

of the settings in fact I have already

12:57

used vs code in my machine with

12:59

different extensions and that's why you

13:01

can see them here but for you I don't uh

13:03

you will not be having this extension

13:04

you will be having only first four uh

13:06

now how do we check if everything is

13:08

working just click on Terminal here new

13:11

terminal

13:12

and try to do Java C here as well and it

13:16

works so your jdk and vs code is all set

13:20

now you can start the code now once we

13:22

have our setup done it's time to write

13:24

our first code that's right so finally

13:27

we are going to run our first Java code

13:30

here now for that we have to open our

13:32

editor so of course in the last video we

13:34

have discussed that we are going to use

13:35

vs code so let's open vs code here so I

13:38

will say code okay so our vs code is all

13:41

set to write our first code you can see

13:44

it will give you a welcome screen when

13:46

you do it and then it will ask you the

13:48

preference do you want a light color

13:49

dark color dark high contrast or light

13:53

high contrast I would say go for dark

13:56

because it's good for development it

13:58

will launch train your eyes at least so

14:00

let's close this get started and this is

14:02

how your vs code will look like okay

14:04

simple stuff right now what we'll do is

14:05

first of all uh where you will get your

14:08

folder so you will see your folders on

14:09

the left hand side so this is where you

14:11

will get your project all the files

14:13

which you want to work with so you'll be

14:15

having that here now of course in the

14:16

upcoming tutorials we'll be using a

14:18

different IDE and I want you to get used

14:20

to different IDs when you work with so

14:22

on the left hand side we can see we have

14:23

an Explorer here and then we if you want

14:25

to search something we have it here if

14:27

you want to add some extensions which we

14:30

are going to do later uh we'll add

14:32

certain extensions to work with at this

14:33

point we don't need any extension so

14:35

just keep it blank I mean no need no

14:37

need to install anything here in my

14:38

machine I already have something on some

14:40

other accounts but on this account I

14:42

don't think so anyway and then if you

14:43

want to run and debug debug you will

14:45

find the option here now why do we run

14:47

of course we know what to why to run

14:50

right but why do we debug the

14:51

application yeah we'll see that later at

14:54

this point let's open our files of

14:56

Explorer here and this is where I want

14:58

to create my first project now how do we

15:01

create a project now project will be a

15:03

very broad term at this point what we

15:05

are going to do is we will create some

15:07

files and of course when you merge these

15:08

files and libraries you create a project

15:11

at this point we do are not working with

15:13

any external libraries at this point we

15:14

are just going for a simple files okay

15:16

hello world right so for that what we'll

15:19

do is we'll click on open folder because

15:21

all these files will be part of one

15:23

particular folder so we'll click on open

15:25

folder and it will give you certain

15:27

options where you want to save this so I

15:30

will save this in on my desktop itself

15:32

okay in fact on my desktop I want to

15:34

create one more folder so I will say

15:36

open folder again inside desktop I would

15:39

I want to create a folder here we'll say

15:40

this is I will have a short name so it's

15:43

a course create and open so you can see

15:46

we got our course folder here what I

15:48

will do is I will zoom in inside my vs

15:50

code so that you can see everything

15:51

properly and again the recommendation

15:53

would be to watch this on a bigger

15:54

screen so that you know you can actually

15:55

understand each and every line because a

15:58

mobile sometime you will be seeing a

16:00

font size and a small font okay I don't

16:02

want to welcome I don't want to see this

16:04

page every time I open my vs code so I

16:06

will just uncheck this and click on

16:08

close here and let's create our first

16:10

file and this is where your going to

16:12

write our first code right so we have

16:14

already seen how do we install jdk how

16:16

do we check the version on the terminal

16:18

can you do that in this so vs code

16:21

itself has a terminal here so you don't

16:23

have to open the external terminal of

16:25

course if you are using Windows you will

16:27

be having an option of command prompt

16:29

but let's click on Terminal here and you

16:32

can see we have an option of terminal

16:33

click on this one

16:35

okay this is where you can enter your

16:37

commands whatever commands you have the

16:39

same command will write here so we'll

16:41

say Java version and you can see I have

16:43

Java 17 in this machine and Java 18 is

16:46

already released but then Java 17 is the

16:48

LTS version so when you say LTS it means

16:50

long term support right so we'll use

16:52

that and we'll also check if my Java C

16:55

is working you can see javasa is also

16:57

there now Java C basically stands for

16:59

Java compiler as we discussed before so

17:01

we need both we need Java to run the

17:03

application we need Java C to compile

17:06

the code right okay cool so we got the

17:09

terminal as well I will just minimize

17:11

this a bit okay so now let's create our

17:13

first file so what we are going to do is

17:15

we'll click on this particular file here

17:17

and we'll name this as Hello dot Java

17:21

now this is something you have to

17:22

remember whenever you create a Java

17:23

project you have to make sure that you

17:25

give an extension or Java files

17:27

basically you have to give an extension

17:28

with DOT Java now different languages

17:30

have different extensions and when you

17:32

talk about C we say dot C when you say C

17:35

plus plus language we say dot CPP when

17:37

you say JavaScript we use dot JS in the

17:40

same way in Java we say dot Java simple

17:42

stuff right so you got our file here now

17:45

vs code will give you some suggestions

17:47

will ask you to install some extensions

17:49

for Java and we'll click on

17:50

recommendation okay it is asking us

17:52

tension pack for Java which will help us

17:55

it will give you some options and extra

17:58

stuff again this is just an advantage

17:59

which you get from vs code again not

18:01

required if you don't want to install

18:03

this extension I think I should go for

18:05

it or we also have intelli code for AI

18:07

assistance but yeah we'll stick to this

18:11

extension again in your machine you

18:12

don't have to install this there's no

18:13

compulsion you can still write your Java

18:15

code but maybe it will give you some

18:17

suggestions and stuff in between let's

18:19

see let's close this and let's write our

18:22

code so extension will be installing

18:23

behind this thing you don't have to

18:24

worry about it so basically how do we

18:26

write our Java code it's very simple

18:27

actually uh first of all the question is

18:30

what you want to do in this code do you

18:32

want to create a game or do you want to

18:36

build a rocket which will go to mass

18:37

they will not be doing those extra stuff

18:39

what I will do now is I will create a

18:42

very simple file which will print hello

18:43

world I know that's how you start a

18:45

programming language so it is

18:46

irrespective of which language you're

18:47

learning the first thing you do is you

18:49

print hello world so here I want to

18:51

print hello world now how do we do that

18:53

see uh if you in case if you are coming

18:55

from other languages let's say python

18:57

right so what you do is if you want to

19:00

print hello world in Python you can

19:01

write only one line and maybe Java is

19:04

not that famous for Simplicity at the

19:07

start of course when you build a bigger

19:08

project Java is much more structured

19:11

compared to the languages but then to

19:13

start with it's not that easy so just

19:15

bear with me okay so what we are going

19:17

to do now is if you want to just

19:19

experiment with Java if you want to play

19:21

some if you want to test some code or

19:24

stuff we have an option which was

19:26

introduced in Java 9. so basically you

19:29

can write your code in one line you can

19:31

get the output example so even before we

19:33

jump to hello world let me show you

19:34

something so so we have we have a

19:36

concept of J shell in Java so it was

19:38

introduced in Java 9 as I mentioned so

19:40

you can open your terminal and here let

19:42

me just increase the size a bit so what

19:44

I will do is I will click on J shell or

19:47

I will type J shell and it will give you

19:49

a j shell prompt okay now J in this case

19:52

is Java so you can it make it should

19:54

make sense now okay so what you can do

19:55

in Json is if you want to experiment

19:57

something example why do we write code

19:59

why do we build applications so to solve

20:01

problems right now it can be a very

20:03

simple problem as well example if you

20:04

want to add two numbers it's very simple

20:06

you can say two plus three and if I say

20:08

enter you can say we got five right so

20:10

we got the answer which is five what if

20:12

you want to do a subtraction you can do

20:14

that so 9 minus 6 and you can see we got

20:17

three so this is working but what if you

20:18

want to print something so in this case

20:20

what you can do is whenever in Java if

20:22

you want to do something we use

20:24

something called Methods at this point

20:25

you don't have to remember methods on

20:27

what the definition is just remove that

20:29

there is a concept called Methods using

20:31

which you can do something the only

20:32

thing you have to remember is if you

20:33

want to do something you need methods in

20:35

the same way if you want to do something

20:37

here I want to print something right so

20:39

we have to use a method called print it

20:41

is a inbuilt method in Java so you don't

20:43

have to worry about it just use it it

20:44

will work so whenever you call methods

20:46

you have to pass the values so I want to

20:50

say hey print but then it will say okay

20:52

I will print but what you want me to

20:54

print so you can mention that in this

20:56

blanket so in this round brackets you

20:57

can mention I want to print hello world

20:59

now if you want to just print a number

21:01

let's start with that first so let me

21:03

just print a number which is six and uh

21:05

in Java there's one more thing you have

21:07

to put a semicolon at the end it should

21:09

work in this but then remember this at

21:11

the end put a semicolon that's how you

21:13

mentioned hey something is getting ended

21:14

so we're going to semicolon there and

21:17

now what you can do is uh let's try to

21:19

run this code now adults right adults

21:22

are very important in your life in your

21:24

program in life basically you still got

21:25

there it says cannot find a simple call

21:28

point it's not working okay cool no

21:30

problem so let me just clear the screen

21:32

now clearcom is not working ignore that

21:34

you will say so let me open JSL once

21:37

again and let's because I just want to

21:39

keep it on top so what I will do now is

21:41

if I want to print something and I want

21:43

to print a value 6 at the end I want a

21:47

semicolon as well now Java says if you

21:49

want to call print print belongs to

21:51

something right so it when I say it is

21:53

available inside Java so the statement

21:56

you have to always mention is system dot

21:58

out dot println see there will be a

21:59

point where you I will talk about what

22:01

is system what is out at this point it's

22:03

very difficult to understand so just

22:05

bear with me this is a statement which

22:07

you have to re which you have to use if

22:10

you want to print something on the

22:12

console okay so if I want to print

22:14

something here just say system.out.print

22:17

and whatever value you want to pass say

22:19

enter you can say we got 6. but what if

22:22

you want to print hello world I don't

22:23

want to print six so if you want to

22:25

repeat the same statement of course you

22:27

can type or you can click on the up

22:29

Arrow it will give you the option now

22:31

here if you want to print hello world

22:32

you have to put that in double quotes so

22:34

whenever you want to do something with

22:36

text see numbers are easy to use you can

22:39

simply say six but if you want to work

22:41

with a text you have to put that in

22:43

double quotes the way I've done it here

22:45

so in this double quotes you have to

22:46

mention hello world right that's how you

22:48

start a programming programming Journey

22:51

right I will say enter and you can say

22:52

we got hello world what if I want to

22:54

print something else which is very

22:55

simple you can say double quotes not

22:57

been ready the Disco and if I say enter

22:59

you can see we got the text so whatever

23:01

you want to print just put that in

23:03

double quotes and it should work and you

23:05

can see we are getting the output as

23:07

well so cool so we are able to do this

23:09

with the help of J shell but will it

23:11

work in this if I try to copy this in

23:15

fact why to copy when we can type it so

23:16

let's type system

23:18

dot out dot print Allen and here we'll

23:22

say hello world and let's give a

23:24

semicolon at the end it was working on J

23:27

shell but when you work with Java files

23:29

because this is how you build a project

23:31

right Json is not to build projects it's

23:33

just for the experiment if you are

23:34

learning a language which is good but if

23:36

you want to make it work you can do if

23:38

you want to build a big application say

23:40

dot Java file and write your code there

23:42

so let's compile this code now how do we

23:44

compile okay so there are two steps you

23:46

have to follow first when you write a

23:47

code you have to compile the code and

23:49

then you can run the code so to compile

23:51

the code you can say Java C in the

23:53

terminal and let me just increase the

23:54

font a bit so you have to say Java C and

23:57

then you have to mention the file name

23:58

so first you have to be make sure that

24:00

you are in a correct folder which is

24:02

course you can see that this is a folder

24:03

we have and then you have to mention

24:04

Java C and then the file name the file

24:07

name in this case is hello.java click on

24:09

enter oh we got an error okay so it says

24:13

something is missing you know something

24:15

else was expected and you are giving

24:17

this is not working so this is not how

24:20

you write applications or code in Java

24:22

file we have to do something else but at

24:24

this point we have understood right how

24:25

do you work with JSL so you can if you

24:27

want to do something with JSL just write

24:29

a code it will work if you want to add

24:31

two numbers it will work you can't do

24:33

that here we have to do something extra

24:34

so let's solve this problem so we are

24:37

not able to run this code right this is

24:38

not working we want to get Hello World

24:40

on the screen it was working with J

24:42

shell because J shell says it's just a

24:44

password statement I will work but then

24:45

when you build an application when you

24:47

run the application how exactly it works

24:49

behind the scene once we understand that

24:51

we can write this code so in your

24:53

machine you will be having something

24:55

called j v m Now by default when you say

25:00

Java is platform independent is because

25:01

of jvm because in your machine you will

25:04

be having jvm irrespective of which OS

25:06

you use it can be Windows Linux based Os

25:09

or Mac or whatever famous OS we have it

25:12

will have jvm installed and of course if

25:14

you have jvm you can run Java

25:15

application and if you don't have jvm

25:18

you can't run job application okay so we

25:21

got jvm here right now what jvm says hey

25:23

I will work but then there's one

25:25

question when you say jvm will work but

25:27

jvm works on which environment so

25:30

underlying we have your OS so this is

25:32

your OSS and then your OS is working on

25:37

a hardware right so you'll be having a

25:38

hardware it can be a laptop it can be a

25:40

desktopic in your phone doesn't matter

25:41

you have a hardware on top of that we

25:43

have an OS on top of that we have

25:45

something called jvm now jvm says okay

25:47

my job is to execute a Java code okay so

25:50

whatever Java code you create JV will

25:53

execute that but then jvm will not

25:55

accept the code directly see what

25:58

happens is Java goes in multiple steps

26:00

in Java application when you as a

26:02

programmer so let's say you are a

26:03

programmer here and when you write a

26:05

code you will not directly run write a

26:07

code which will run on jvm okay and we

26:10

don't even do that right we don't even

26:12

write a code which actually works on the

26:13

hardware and we don't even know the code

26:15

which works directly on OS Java says if

26:17

you want to make it work you have to run

26:19

your code on jvm but then JV will not

26:22

understand what you are talking about

26:24

and one more one more thing we have

26:25

talked about right Java is platform

26:27

independent so what does platform

26:29

Independence means it simply means that

26:31

you can run your application on any

26:33

machine okay so when you write a Java

26:35

code it will run on any machine

26:37

irrespective of the hardware or the OS

26:39

but the only thing it requires is that

26:41

machine need to have jvm so irrespective

26:44

of which platform talking about if it

26:45

has jvm your application becomes

26:48

platform independent but jvm itself is

26:51

platform dependent okay so example you

26:53

can't run jvm on iOS so we can't say

26:56

Java is platform independent when jvm

26:59

itself is is dependent so yes your

27:02

application is independent jvm is not so

27:05

jvm need to be built for a particular OS

27:07

and let's say if it is Windows in my

27:09

it's so in my machine it is Mac but

27:11

let's say if it is Windows we have jvm

27:14

available on Windows so your jvm says

27:16

hey I will run your code but you have to

27:19

submit me not the Java code but

27:21

something called a byte code so you have

27:23

to convert your code into a unreadable

27:26

format for the normal human bin and this

27:29

is called a byte code

27:31

because jvm only understands byte code

27:34

but can you write byte code of course

27:37

not you only understands English type of

27:39

languages right so whatever we have

27:40

returned the code we have a

27:42

returnsystem.out.print it's English like

27:45

language right so what you create is a

27:48

Java code so this is your Java file or

27:51

Java code and then you have to convert

27:53

this Java code so this is Java code

27:55

which is mode of readable this is not

27:57

something which you can read so

27:58

basically you have to convert this into

28:00

a Java code or byte code so for that we

28:03

have something in between here which is

28:05

called a compiler now since this is a

28:08

Java compiler we can call it as Java C

28:11

which is Java compiler okay so basically

28:13

you will write this code so this code

28:16

will be getting compiled into a byte

28:19

code and that byte code goes on the jvm

28:22

it will run okay makes sense right so

28:24

that means when you write something it

28:26

will go into byte code and then byte

28:28

code will run on jvm now the only thing

28:29

is see your project can be a big project

28:31

right here I'm writing only one file it

28:33

can be 10 lines of code it can it can be

28:35

10 files it can be 100 files it can be

28:37

thousand files out of all these files

28:40

there will be only one file with jvm

28:43

Will execute okay JV will start with the

28:45

first file and then of course that file

28:47

will depend upon the files and it will

28:49

execute so jvm says you need to tell me

28:52

what that file is okay so if you even if

28:55

you have thousand files which you want

28:56

to run in your project you have to give

28:58

the first file so in this case what you

29:00

will do is you will specify hey that's

29:02

my first file so when you run the code

29:04

you will specify the first file and also

29:07

that file need to have something called

29:10

a main method okay so as I mentioned

29:13

before right we have print in the same

29:14

way we have main so your file the first

29:17

file need to have a main method so when

29:20

I say first file it is not like you have

29:22

to follow a sequence create this file

29:24

then that's that file out of all the

29:27

files in your project the execution will

29:30

start from the first file which you

29:32

specify okay and that file need to have

29:35

main not just main it is looking for a

29:38

specific signature okay so when is the

29:40

signature it means the entire method

29:42

signature the method name what are the

29:46

values you're passing and all the stuff

29:47

and of course all this term you will you

29:49

will get used to in some of these

29:51

sessions don't worry so first you have

29:52

to make sure that you provide a main and

29:55

Main has a particular signature again

29:57

what each word means that we'll see

29:59

later so the signature is this it is

30:00

public static void main so you have to

30:04

remember always pass this method I know

30:07

there are four words but then this is

30:09

the thing you have to remember and Main

30:11

will accept some arguments some

30:13

parameters so you have to pass string a

30:17

now again we'll discuss this what this

30:19

string is what is public static void

30:21

we'll discuss that in some time but main

30:22

is looking for the signature so your

30:25

code will not work without the signature

30:27

okay so remember this so that makes

30:28

sense so what are the steps you have to

30:30

follow so the first step is as a

30:32

programmer you will be creating a Java

30:34

file okay now once your Java is ready

30:36

you will put that into compiler then

30:39

compiler will compile it then that code

30:41

becomes a byte file a byte code

30:43

basically then you will take this byte

30:45

code you will run that on jvm now once

30:48

you once your jvm says okay I'm running

30:50

the code jvm will look for this vertical

30:52

syntax it should be there in your code

30:54

and then you will get the output right

30:56

simple stuff okay now there's a space in

30:58

between here if you can see this is a

31:00

space I have left left that space

31:02

intentionally because we'll add certain

31:03

things there so at this point just

31:05

remember there's a space here there's a

31:07

box remaining here we'll we'll add that

31:09

box in in some time now once we have

31:11

understood all those things it's time to

31:13

expand this code now if you remember we

31:16

have to write everything in a main right

31:17

so this section should be a part

31:21

of remember what jvm will ask you for JV

31:24

will ask you for main so we have to put

31:27

that into main so what your Syntax for

31:29

main it's very simple it is public

31:30

static void main we have seen that of

31:33

course this words will Define that words

31:35

in the upcoming session and once you

31:37

understand all these terms you are Java

31:38

programmer right and in this will say

31:41

string a and that's it this is a syntax

31:44

to follow now there is one more thing uh

31:46

in Java now Java is object oriented now

31:49

what is object oriented means we'll

31:50

discuss that later but in simple terms

31:52

object oriented means everything should

31:54

be an object and to create object we

31:57

need to class okay again we'll discuss

31:59

all those things in the upcoming session

32:00

but a class is missing here in fact if

32:03

you compile this code this is what your

32:06

Java say will tell you JavaScript says

32:08

hey where is your class your class is

32:10

missing okay let's do that so let's put

32:13

that a class here and we'll give a class

32:15

name at this point we'll keep the class

32:16

name and file them same and we'll say

32:18

Open Bracket and close and we'll give

32:22

our indentation so basically we have to

32:23

follow this thing always give a tab so

32:25

every time you open a curly bag give a

32:27

tab it looks good okay there's no

32:28

compulsion it looks good and we love to

32:31

build stuff which also runs and looks

32:34

good okay this looks good right ignore

32:35

this accent these suggestions uh these

32:37

are the exercises coming from your vs

32:38

code but you know that part in fact is

32:40

it because it's happening of this

32:41

extension let me just remove this so

32:43

let's not use that extension because it

32:44

will give you a suggestion it's good for

32:45

development but not for teaching purpose

32:47

so it adds extra text there okay cool so

32:49

this is how you write your code okay we

32:51

got a class a file name and then public

32:54

static void Main in bracket you have to

32:56

mention the parameters again we'll

32:57

discuss that later and the only thing

33:00

important statement here is

33:01

system.out.print Hello World okay now

33:04

let's see if this works let's create the

33:06

screen and compile you can see there is

33:09

no error but the moment I compiled if

33:11

you can see on the left hand side we got

33:12

this extra file we got hello.class now

33:15

this is your byte code remember we have

33:18

talked about a byte code here this is a

33:21

file which got created automatically so

33:23

when you create a Java file when you

33:25

send it for the compilation it will

33:26

create the byte code and the extension

33:28

for the byte code is Dot class file the

33:33

extension for Java code is dot Java file

33:36

and now it is compiled done right now

33:38

it's time to run your code so remember

33:39

when I mentioned to run you have to use

33:42

jvm and for that you have to use a

33:44

command which is Java and you have to

33:45

mention the class name not a file name

33:48

this time just a class name the class

33:49

name is hello and say enter you can say

33:52

we got hello world so yeah that's how it

33:54

that's how it works so simple stuff

33:55

that's why we can able to complete our

33:57

code and we are able to run this code

33:59

now if you have done till now give a

34:02

round of applause or a part on your back

34:03

because you deserve it okay so this

34:06

makes sense right so there's a gap here

34:07

right so let's remove this Gap so what

34:09

we have here so basically you know when

34:11

you when you run your code basically

34:13

think about you're cooking something in

34:14

a kitchen right so where do you cook of

34:16

course in a kitchen right why not in a

34:17

hall or in a bedroom it's because in the

34:19

kitchen to cook something you need extra

34:21

things right example if you make a

34:23

Biryani I don't know if you know the

34:24

recipe even I don't know the recipe but

34:25

if you make a Biryani you have to add

34:27

extra ingredients right now those things

34:29

are kept in the shelf so in the kitchen

34:31

you will not just have that resources

34:34

which are there in the platform but also

34:36

you'll be having extra stuff now the

34:38

same way if you want to run your Java

34:40

application you need extra libraries and

34:42

you also need a runtime now runtime is

34:44

something which where you can run things

34:46

right now you can run something in a jvm

34:48

so that is part of run but then what if

34:51

you need some extra libraries you have

34:52

to Unix you need a environment as well

34:55

so Java provides you one more thing so

34:57

we have one more layer here which is

34:59

above your OS which is called JRE so

35:03

when you want to learn something you use

35:05

JRE to run it now JRE stands for Java

35:09

runtime environment and what is your jvm

35:12

jvm stands for Java virtual machine okay

35:17

so basically we have two things we have

35:19

a jvm and we have a JRE now jvm is a

35:23

part of JRE now what else you'll be

35:25

having inside your jvm you will be

35:26

having some libraries so jvm with

35:29

libraries is a part of JRE so whenever

35:32

you've done something it runs inside the

35:34

JRE and jvm is actually responsible to

35:37

run it okay and by default when you when

35:40

you want to run your application on any

35:42

platform it need to have JRE which will

35:44

have jvm now not every machine will have

35:47

Java C Java C is only for development

35:49

purpose right so we can imagine this in

35:51

this section uh so basically we have an

35:54

outer box which is your jdk which is

35:57

used by developers inside which you will

35:59

be having something called JRE and in

36:02

this you'll be having jvm so as a

36:04

developer you'll be installing jdk jdk

36:06

will have JD Jr you will have jvm but to

36:09

run something on any other machine let's

36:11

say if you have met the application you

36:13

want to run this on your friends machine

36:14

your friends machine will only have JRE

36:16

and jvm jdk is not required and that's

36:20

why we say Java is Vora which is write

36:22

once and run anywhere right so you just

36:25

write on your machine you can run it on

36:27

any other machine provided you have jrd

36:29

and jvm installed so that's how your

36:31

Java behind the scene I hope it makes

36:33

sense now okay so we were able to run

36:35

this code and we understood something

36:37

what is happening behind the scene right

36:38

so basically we know now that as a

36:41

programmer as a as a Java developer you

36:42

write our Java code which will be having

36:45

dot Java extension and that gets

36:47

compiled by Java C and then when you

36:50

when you compile the code what you get

36:52

in output is a byte code which will be

36:54

having dot class file and then you can

36:56

run the dot class file in your JV I mean

36:58

basically it's a part of JRE which will

37:01

have jvm as well as a libraries lot of

37:04

classes and then everything is a part of

37:06

a JRE that JRE you will be installing on

37:09

OS right so basically you need to have a

37:11

hardware of course you need a machine to

37:12

run right that machine will be having an

37:14

OS and then OS will be having JRE which

37:17

is which will have jvm but as a

37:19

developer you will need one more tool

37:21

which is jdk now when you install a new

37:24

jdk you get the updated JRE and jvm with

37:28

it okay so the version of jdk which we

37:31

are using is is 17 which is LTS which is

37:35

long-term support and in every new

37:37

version you get some minor extra

37:39

features right so even if you are

37:41

watching this after 10 years or 80 years

37:44

or five years you will be having new

37:46

version of java of course next five

37:48

years you will get 10 new versions and

37:50

every new version will have minor

37:51

updates but the base Thing Remains same

37:54

right so whatever you're learning is

37:56

actually from java one itself the

37:58

system.pinterland is same in Java one so

38:00

don't worry about the new updates the

38:03

code concept will remain same okay cool

38:04

now once you have this thing what next

38:07

we can do now think about this world of

38:10

course first of all why we build

38:11

software we build software so that we

38:13

can solve a problem now what problem we

38:16

are trying to solve uh so basically we

38:18

are trying to solve the real world

38:21

problems uh with the help of virtual

38:23

world example you wanted to buy

38:25

something so you've got Amazon which

38:27

delivers everything at home so you got

38:30

Amazon is online service you want to to

38:32

book a cab you got Uber so for every

38:35

problem we have a solution in the

38:37

virtual world right so banking

38:38

everything is happening online now when

38:40

you say you are replacing the real world

38:43

with a virtual world solution so

38:45

basically we need to work with one thing

38:47

the most important thing now is actually

38:49

data so basically we want to work with

38:52

data so how will you work with data so

38:54

basically you need data as well as

38:57

processing on it so whatever whenever we

39:00

build a project or when you build

39:01

application what we are doing is we are

39:03

doing the processing on data now this

39:06

data can be coming from user or

39:07

something so maybe you will accept the

39:09

data from the user and then you will do

39:11

some work with it maybe you want to

39:14

store this data now where exactly do we

39:16

store data so we store data in something

39:18

called database so we have a database

39:20

here in which you will be storing your

39:21

data so whatever data you're getting

39:23

from the user or from after processing

39:25

so let's say we got a user here user

39:27

sends us data we will apply a processing

39:29

on that so we will do some work in

39:31

between and then then at the end we

39:33

might store data in database now this

39:36

database is actually a permanent storage

39:38

or a persistent storage which you say

39:40

now when you say it is permanent that

39:42

doesn't mean you can't change it of

39:43

course you can you can replace the

39:45

values as well but then the data stays

39:47

there okay so if you just store it and

39:49

if even if you shut down your machine

39:50

the data will be stored example when you

39:53

copy movies or games on your laptop it

39:55

stays there right it will get deleted

39:57

when you say delete but the data stays

39:59

there so this makes sense right but when

40:01

I say you will receive data from the

40:03

user and then you will be doing some

40:04

processing on that where you will store

40:06

the data for temporary purpose of course

40:08

database is there for permanent storage

40:09

but during processing you have to store

40:12

your data somewhere and that somewhere

40:14

is variable so this is where you store

40:16

your data now what is variable so

40:18

imagine variable as a box and in this

40:20

box you will store your data now what

40:22

data you want to store your data can be

40:24

numeric it can be text it can be image

40:26

whatever you want to store so you can

40:27

store that data in this variable now I'm

40:29

not talking specific to Java in general

40:31

user you want to store data you will

40:33

store that in a box and that box will

40:35

have a type to it it can be of type

40:37

numbers it can be of types text or image

40:39

or video so here we are trying to store

40:41

data right so let's I want to store the

40:43

number which is five so this box will

40:45

have a number five and it will also have

40:47

a name to it maybe I can give a name

40:49

let's say a number it doesn't matter

40:51

what name you give of course there are

40:52

some restrictions on names but num works

40:55

you can use any name which you like

40:56

maybe you can create one more box here

40:58

and you want to store the number itself

41:00

but let's say a decimal value something

41:01

like 6.5 you can do that okay and maybe

41:04

you can have a different value to a

41:05

different name to it maybe you can say

41:07

marks or grades or maybe you want to

41:09

have one more box where you want to

41:11

store your name so I can do that I can

41:13

store my name as Naveen and it will have

41:15

a variable name so maybe I can say this

41:17

is user so user is Naveen so basically

41:19

what you do is you create a box and

41:21

inside the inside the Box will be having

41:23

values now this box are actually called

41:26

variables so we can name this box as a

41:29

variable okay and so what we got till

41:31

now we got thought that a box will be

41:33

called a variable and you will be having

41:35

a name to it you will be having a value

41:37

so name and value will be there apart

41:39

from that you know Java is actually

41:41

called strongly typed language now what

41:45

it means is whatever variable you want

41:47

to create so if you want to store the

41:48

data of course Java says hey we'll

41:50

accept the data but tell tell me also

41:52

what kind of box you want a different

41:55

type of data will require different

41:56

blocks okay that makes sense so I will

41:58

say hey Java I want to store a number so

42:01

I want to create a box for me which will

42:03

accept numbers so we can say okay this

42:06

makes sense number will go there we can

42:07

also tweet this as a number but this is

42:10

a text right so in Java we call them as

42:12

a string right so text will be having a

42:15

string so this box type is string what

42:17

about this box of course we say this is

42:19

number this is also a number but in

42:21

number as well we have a category inside

42:23

it so one of the categories I will

42:24

mention is this we can if you want to

42:26

store numbers we can store that in

42:28

something called in now int means

42:30

integer okay so we can store data in the

42:32

integer format but we cannot store 6.5

42:34

in it because 6.5 is a real number so

42:37

int will accept numbers but white is

42:39

called int why not natural numbers or

42:41

whole numbers is because maybe you want

42:43

to store negative values as well so

42:44

remember your maths concept so we have

42:46

negative numbers to positive values so

42:49

everything will be considered as int so

42:51

we can store minus 5 plus 5 that's your

42:53

choice okay so this makes sense what I

42:55

will do now is if I go back to the code

42:57

of course we want to print hello world

42:58

but we have done that now I want to

43:00

store value so that I can do some

43:01

processing on it now what kind of person

43:03

think I want to do maybe I can I want to

43:05

add two numbers maybe two plus two or

43:06

five plus six simple stuff right but

43:09

before that we'll see how do we store

43:10

data even if you don't store data you

43:12

can just go inside this system.print

43:15

whatever option you want to perform you

43:17

want to say three plus five that will

43:19

work so you can just go back here and

43:21

say compile and run so remember this

43:23

thing every time you make a change just

43:25

make sure that you compile it first so

43:28

the first step is comparison and then

43:29

running and you can see we got the

43:31

output 8 just ignore this percentage

43:33

symbol as of now I'll show this one more

43:35

thing I wanted to mention whenever you

43:38

print something let's say I want to

43:39

print this number here as well I want to

43:41

perform different options so you can

43:42

write multiple statements for different

43:44

operation you want to perform so you can

43:46

say eight plus seven so we are doing

43:49

addition here and the addition here

43:51

let's clear the screen okay I have to

43:54

first compile okay that's what compile

43:56

run and you can see we got the value

43:58

now the only thing you can see is the

44:01

result are bit that together right we

44:03

want to give a space of maybe I can

44:04

train 15 on new line so what you can do

44:06

is instead of using print you can

44:08

actually use something called print Ln

44:10

now print Ln means print new line so it

44:12

will print and it will go on new lines

44:14

so we can do that here as well so just

44:15

say Ln Ln at the End Print Ln which is

44:18

print new line and now compile run and

44:21

you can see we got the value we got 5

44:23

and 15. so from now we'll not be using

44:25

print we'll be using Ln println okay so

44:29

again println means it will print it

44:31

will go on new line so we got print Ln

44:33

now what else we can do here so I don't

44:36

want to perform the operation here

44:37

remove that I want to store that value

44:39

first so how do we store it of course we

44:41

need box right so I can say num1 I can

44:45

as I mentioned I can use any variable

44:46

name so let's say num1 or to start with

44:48

let's say num just to keep it simple and

44:50

then we can give a value to it let's say

44:52

value is three so remember you have to

44:54

mention the variable name and then we

44:55

have to mention the value for it and at

44:58

the end of course you can put a

44:59

semicolon so we can see we got num equal

45:01

to 3 and we got semicolon so in Java

45:04

after every statement you have to give a

45:06

semicolon you might be thinking hey why

45:08

don't we have a semicolon here it's

45:09

because that is a block so we don't put

45:12

a semicolon out at the end of a block

45:14

the reason is it signifies that's the

45:17

end here if you don't mention semicolon

45:20

how do we Define that that's the angle

45:21

so it's good to put semicolon at the end

45:24

and anyway Java will give you either if

45:26

you don't do that the next thing you

45:27

have to do here is we have to also

45:29

mention the type what type of num is

45:31

this so we can say int now we have to

45:33

say first you have to specify the type

45:35

and then you mention the variable name

45:37

maybe if you have learned some other

45:39

languages where you put the type

45:41

afterwards in Java we first put the

45:43

value or put the type and then we put

45:45

the name then we Ascend the value now

45:48

this equal to here is called assignment

45:50

operator because you are assigning a

45:52

value so what it will do is it will take

45:54

the value from the right hand side it

45:56

will assign the budget to the left hand

45:57

side okay remember this equal to will

46:00

take the value from the right hand side

46:01

it will assign the value the left hand

46:03

side okay so we got a variable which is

46:05

of type int okay so this is num which is

46:08

a variable and then we can print it now

46:11

let's only print the value that's it

46:12

nothing much I will say save now once

46:14

you click on save you have to compile

46:16

the code

46:17

and run the code and you can see we got

46:19

three Okay cool so now can I create more

46:22

variables we can actually so let me just

46:24

rename the value variable from num to

46:26

num1 and here as well I will print num1

46:29

okay and then I will create one more

46:31

variable which is num2 is equal to so

46:34

every time you want to get a variable

46:35

just mention the type which is int or

46:37

whatever type of variable you're

46:38

creating at this point we are working

46:39

with numbers so we'll say end num2 equal

46:41

to we're going to assign the value which

46:43

is 5 so we got 3 and 5 here what you can

46:46

do is you can actually perform the

46:48

operation here itself so against the

46:49

num1 plus num2 that works so instead of

46:52

adding values we can also add the

46:54

variables okay so now we are what we're

46:56

doing is we are saying num1 plus num2

46:58

and the addition will be getting printed

47:01

let's try so I can just go back here and

47:04

say compile run and you can see we got

47:06

eight you know what instead of adding it

47:09

inside this it's not a good practice

47:11

what you can do is we can create another

47:13

variable called result and that's right

47:15

variables are free to use of course

47:17

behind this engine is consuming a lot of

47:19

memory

47:19

but it's working right it's free as of

47:22

now but later on we'll focus on how can

47:24

you make your code more efficient so

47:26

that it will use less space right at

47:29

this point it works so we can say now

47:30

one plus so whatever option you want to

47:32

perform you can do that here and instead

47:35

of printing and this is doing addition

47:37

here we can simply print result so what

47:39

you're doing now is you're adding the

47:41

value and then you are storing that in a

47:44

result and then you are printing it it

47:46

should work let's try compile run and

47:49

you can see we got eight so this is

47:50

working so that's how you can you can

47:52

create variables so remember this thing

47:54

every time you want to create a variable

47:55

you have to you first need a name for it

47:57

at this point is num1 you have to also

47:59

mention the type of it at this point

48:01

this is int and you have to assign the

48:03

value for it in the upcoming videos

48:04

we'll talk about how can you create a

48:07

variable which will be having a name

48:09

with a different type and different

48:11

value so now we know what variables are

48:13

and how do we create them so basically

48:15

in the last video we have talked about

48:17

how to create a variable with the help

48:18

of the data type and the value as well

48:21

now what are the other options we have

48:23

so we have worked with eight right do we

48:25

have other options so basically we have

48:27

multiple data types to store your data

48:29

so let's talk about the first thing

48:31

which is uh data type in fact there are

48:34

two categories of data type which we can

48:36

normally work with one is called

48:38

primitive and the other one will be will

48:41

keep it question mark okay at this point

48:43

we are not just discussing about the

48:44

second one the first one is primitive so

48:45

basically in Java we have lot of inbuilt

48:48

data types available which you can use

48:50

and they are primitive now primitive

48:52

simply means it is simple basic to use

48:54

to work with so what about data you

48:56

normally have normally we work with

48:58

numbers we work with Point values we

49:00

work with characters we work with true

49:03

or false now for that we have this that

49:05

category so we can divide primitive into

49:07

four categories here uh the first way

49:09

one the first one is of type integers uh

49:12

the second one we have is float floating

49:15

point value basically so we can say

49:16

float and the third one is characters

49:19

and the fourth one is your bullion of

49:23

course when you divide this we have more

49:25

categories to it but in general we have

49:26

four we have integer float character and

49:29

Boolean now when you say you want to

49:31

store a range a negative range to

49:33

positive range numbers normal numbers or

49:35

natural numbers you can say in fact we

49:37

can we can call them as integer itself

49:38

because we are starting with a negative

49:39

value so that goes into integer whenever

49:42

you have Point values which that goes

49:43

into float value whenever you go with

49:45

characters like a b or any Regional

49:48

characters you have all across the world

49:50

that goes into character and then when

49:52

it comes to true and false we go with

49:54

Boolean now inside integer itself we

49:56

have multiple subtypes to it uh we have

49:58

a concept of byte we have short we have

50:01

int and we have long uh for float we

50:04

have another option which is of double

50:05

and of course we have float and then

50:07

carries characters are characters so in

50:09

characters we can have different type of

50:11

characters in Boolean uh the only values

50:14

we can have is true and false okay uh so

50:16

example if I go back to my code and if I

50:18

want to create a different video table

50:20

this time let's say float so I can just

50:22

come back here I can remove this section

50:24

and here I can say this we are creating

50:27

a float value now how do we get a float

50:28

value base so we can simply say float

50:30

and then we can have a value let's say

50:33

seven or we would name to it right so

50:36

name we can have is marks of course we

50:38

don't put in marks but let's go with

50:40

marks and then we can go with a value

50:43

now floating Point values would be

50:45

having a DOT right so we can say 6.5

50:47

something like this uh some float marks

50:50

equal to 6.5 that makes sense right okay

50:53

cool in the same way we have also

50:55

mentioned we have subtypes to integer we

50:57

can have a byte format but why do we

51:00

need all these extra formats for that

51:02

let's understand uh some basic concept

51:04

now basically when you talk about

51:06

integers itself so if you talk about

51:07

integer we have some sections to it so

51:10

integer will have different part so

51:12

interesting will have different sizes

51:13

example if you want to store a normal

51:16

number so in general we always go with

51:18

int and int has its own value okay so

51:21

when you talk about in the size of int

51:23

is is four bytes so we can say 32 bits

51:26

and then it has a range to it and you

51:29

can find the range on the screen uh we

51:32

also have a format which is let's say if

51:34

you want to store a bigger value a very

51:36

big value so in this case I'm I mean of

51:39

course the value goes beyond that range

51:41

we have to go for the long format now

51:43

long supports eight bytes now of course

51:45

when you have more budget it will occupy

51:47

more values and it will have a range so

51:49

of course you can find the range on the

51:51

screen now what if you want to store a

51:54

smaller number so of course int provides

51:55

you that range but then what if you want

51:57

to save memory you don't want to go for

51:59

end range in that case we can have a

52:02

short value now short is only two bytes

52:05

so if you want to have a limited range

52:07

you can find the range on the screen and

52:09

then we have the next range which is of

52:11

byte now byte is only one byte okay so

52:15

eight bits and whatever you want to

52:17

store you can store in that of course it

52:19

has its own range now how do we

52:21

calculate this range so we do with the

52:23

help of it goes from negative values

52:25

right so it is minus two raised to 8 2

52:28

plus 2 raised to eight minus one okay so

52:32

which is 2 s to 8 is what it is okay

52:35

starting with negative value right so

52:36

just two is to 7 and 2 is to 7 and the

52:39

range so how do we calculate version 7

52:41

is 128 so it starts from minus 128 to

52:44

plus 127 so 128 minus 1 127 that's how

52:48

you can calculate the range for all the

52:50

other numbers and you can of course you

52:51

have seen that on the screen so this is

52:53

how we can use integer so which one to

52:55

use so if you want to get a variable

52:56

which is a which will have a value let's

52:57

say five or six or maximum 127 you can

53:01

go for byte now how do you get that

53:02

variable it's very simple you use a type

53:04

which is byte you specify a variable

53:06

name let's say num and you assign a

53:08

value which is eight or any other value

53:10

which you want of course that should be

53:12

in a Range and that's how you can store

53:14

it now what if you want to have a float

53:16

value now float actually gives you two

53:19

different values or two different types

53:21

now it depends upon which programming

53:22

language you have worked with before

53:23

example if you have ever worked with CVA

53:26

by default we have a value which is

53:27

float so by default we have float there

53:29

now float only takes four bytes and it

53:32

has its own values you can store and

53:34

then we also have option of double which

53:36

takes eight bytes now what you think

53:38

which will be default value think about

53:40

it will it be float or double by default

53:43

in Java so default is actually double

53:45

not float okay now why double is because

53:48

see float are good when you have limited

53:50

values when you have limited Precision

53:52

because after your point value example

53:54

if you have value which is

53:57

126.5678 or whatever values you have so

54:00

float will have a limited Precision set

54:01

here but on the other hand double will

54:03

have a longer one right so double can

54:06

have more values so whenever you perform

54:08

scientific calculations or any kind of

54:10

calculation if you wanted to make it

54:11

fast and it should support maximum

54:13

Precision you can use double there right

54:15

and by default Java supports double so

54:17

if you want to create a variable which

54:18

is with double so you can simply say

54:20

double and you can create a variable

54:23

name num is equal to you can assign a

54:24

value which is 5.6 so whatever value you

54:26

want to assign but how about float now

54:28

if you try that with float it will it

54:30

may not work example if you say float

54:32

and if you say a variable name which is

54:34

num and equal to so when you try to

54:36

assign 5.6 it will give you error it's

54:39

because by default the moment you say

54:41

Point values it will be considered as as

54:44

double and if you want to say hey I

54:46

don't want to work with double I want to

54:47

work with float so explicitly you have

54:50

to mention hey okay this is actually

54:52

double value so you can this is actually

54:54

a float value so you can put a f there

54:56

right so 5.6 F that's how you define

54:59

it's a float value for double you don't

55:01

have to mention that now how about

55:02

character so when you when you work with

55:04

character and in other languages

55:06

character will have a less bits but in

55:10

Java it goes for two bytes so so in

55:13

other languages like C you have only one

55:15

byte it's because it normally occupies

55:17

the integer range it normally occupies

55:19

the character range ASCII values but

55:21

then in Java we thought hey can we just

55:23

have all the characters in the world not

55:25

just English all the languages so Java

55:28

actually follows something called

55:29

Unicode not ASCII by default it will

55:31

support Unicode and it has two bytes

55:34

which is 16 bits so it has a bigger

55:36

range right so this is a range it

55:38

normally works with so of course you can

55:39

work with any type of values you have

55:41

and any type of characters basically you

55:43

have and if you want to define a

55:44

character variable it's very simple you

55:46

say Cad and then you specify the

55:48

variable name let's say character or c

55:50

equal to whatever character you want to

55:52

assign let's say I want to assign a

55:53

character which is K now in this K in

55:56

this case if you want to work with a

55:58

character can we use double Quotes no

55:59

double quotes is for string right for

56:01

character we have to use single quote

56:03

and you have to put your character to

56:05

single quote and remember it will only

56:07

accept a single character and next we

56:09

have of type Boolean so if you want to

56:12

work with Boolean Napoleon will only

56:14

accept true or false okay no other

56:16

there's no subtypes here only true or

56:18

false now one thing you have to remember

56:20

now in other languages it may represent

56:22

it with the help of one and zero in Java

56:25

we don't do that so in Java it does not

56:26

it doesn't work with zero and one it is

56:28

true and false this Knight like true is

56:31

one and false is zero it's only true and

56:34

false it is normally used for conditions

56:36

if you want to check for the condition

56:38

based on that you will do something

56:39

that's what we can use two and false I

56:41

mean think about computers when you say

56:43

computer can think it's because of this

56:45

true or false okay and how do we Define

56:47

a Boolean variable so it's very simple

56:49

you use a keyword called Bull and then

56:52

you mention a variable name let's say B

56:54

is equal to you either assign true or

56:57

false okay you don't have to put a semi

56:59

a single quota double quote because they

57:01

are itself keywords so these are the

57:02

options we have now if you want to see

57:04

this in a code it's very simple you can

57:06

just go back here and say okay byte is

57:08

done let's create some variables with

57:10

different type so ain't we have already

57:12

seen so it's a number one this is how

57:14

you assign integer value what if you

57:16

want to go with a byte so you can say

57:17

byte B I will say b y equal to now can I

57:22

assign a value which is 129 you can say

57:24

it will give you if I do that if I go

57:26

back to my compiler compile you can see

57:30

it will give you error it says possible

57:31

lossy conversion from into byte so this

57:34

is int which is 129 it will if you try

57:37

to save that in a byte it will not work

57:39

but what if you say 127 that's the last

57:41

value you can store compile okay this is

57:44

a problem with result let me just remove

57:46

this section and compile no issue okay

57:49

so this is how you save byte value what

57:51

if you want to store shots you can say

57:54

short sh equal to

57:56

and you can assign value which is let's

57:59

say five five eight any even number

58:01

doesn't matter and then what if you want

58:03

to work with long so long L is equal to

58:05

now when you work with long remember

58:07

this thing after every number for long

58:09

you have to put a l at the end to

58:12

specify that's a long number the thing

58:14

we have done with float we have to put F

58:16

in the same way long needs l

58:18

and what if you can do here with float

58:21

so you can say float f is equal to

58:25

5.8 okay let's try with 5.8 and let's

58:28

see what happens if you try to compile

58:30

this compile okay you can see it is

58:32

giving you it says again possible logic

58:34

conversion from double to float because

58:37

by default 5.8 is double if you want to

58:40

mention hey that's not double that's

58:41

float you can put a f at the end that

58:43

should work you can work with double as

58:45

well Double D is equal to 5.8 same value

58:48

but this time it is double and then what

58:50

else we can work with we can work with

58:52

character as well so we can say care C

58:54

is equal to in single code you can

58:56

mention the character k then we can go

58:59

for Boolean which is Boolean in this

59:01

case and you can say B is equal to True

59:04

simple stuff right uh let's try to

59:06

compile this of course we are not

59:08

printing anything so we don't expect

59:09

anything on the console compile or

59:11

there's an error it says cannot find

59:13

symbol bull okay my bad it is Boolean

59:15

the only problem is when you work with

59:17

multiple language at the same time you

59:18

get confused somewhere so that that's

59:19

Boolean notable so in the notes as well

59:22

I need to change it so I will say this

59:24

is Boolean

59:26

cool and now let's compile this once

59:29

again compile okay there's no problem

59:32

and let's run this of course it will run

59:34

but then you will not see any output

59:36

right no output because we're not

59:37

printing anything so yeah that's about

59:39

this we have different type of

59:42

data types we can work with and all

59:43

these are inbuilt apart from this we

59:46

also we also have some more data types

59:47

which we'll see later they are not type

59:49

of primitive but this is primitive data

59:51

types and this is working okay awesome

59:53

so now we know what what variables are

59:55

what data types are what a size or range

59:58

of each data type here and you don't

60:00

have to buy hard them you will get used

60:02

to it the more practice you do with this

60:04

variables you will get used to when to

60:05

use what but what if I remove that

60:07

single code there let's try compile and

60:09

you can see we got another we can't even

60:11

put double quotes there it will give you

60:13

as well and you can see you can't even

60:15

put double code so you have to put

60:17

single code because that's how that's

60:19

what character represents and the

60:21

character can be numbered as well so you

60:22

can put a character here which is eight

60:24

which is the character itself right

60:26

digital also character no other

60:28

okay so when you talk about data types

60:30

and then values what kind of values we

60:32

can have so example if you talk about

60:34

this 9 or this eight these things are

60:36

actually called laterals

60:39

because they're literally the values

60:40

right so what we can do is we can play

60:42

with some literals here let's work with

60:44

integers first and we'll come back to

60:46

this so we are basically working with

60:49

literals now okay now what kind of

60:52

relatives we can have for example we can

60:53

have direct values we have nine here and

60:56

we can have any big value provided that

60:58

goes into the range so whatever range we

61:00

have it is a part of the range it will

61:01

work so we have all these are decimal

61:03

values right so it will have a base 10

61:05

what if you want to work with base two

61:07

something like a binary format can we do

61:09

this let's try it so I will say 0b 1 0

61:13

1. so basically if you want to put a

61:15

binary number like 101 which is five so

61:18

you can put B and then you can put a

61:20

zero let me just drive this works I will

61:22

say system dot dot println and then let

61:25

me print num1 let's see if this works

61:27

put your semicolon at the end

61:29

compile there is no issue so let's run

61:32

this code and let's see what's the

61:33

output and you can see we got five so

61:35

yeah that's the binary format Works what

61:37

if you want to work with hexadecimal

61:39

numbers in this case you can use x here

61:41

and let's put a number seven e so e also

61:46

supported right so let me just try

61:47

compile run and you can see we've got

61:50

126 I don't know if that's the correct

61:52

answer but you can verify there's one

61:53

more thing which will introduce later in

61:54

Java which is what if you have let's say

61:57

this number and trust me I always get

61:59

confused with number of zeros in any

62:01

amount even if I mention a password or a

62:03

pin or account number and whenever you

62:05

have multiple zeros I always get

62:07

confused so how do you count to the

62:08

number of zeros here of course you can

62:10

do it one by one but in Java we got this

62:12

feature where you can put a underscore

62:14

between numbers if you want to

62:15

differentiate them it was easy to count

62:17

right so you can see we are getting

62:18

zeros and then we got underscore even

62:21

this works let's try it out compiled one

62:23

and you can see we got the output of

62:24

course in the output it will give you a

62:25

normal number but as a programmer it

62:27

helps you for the number of zeros you

62:29

are entering that so these are basically

62:31

individuals right let's try with float

62:33

this time so let's say this float and we

62:34

are saying num1 and then I want to

62:36

assign a very different value can I

62:37

assign let's not let's go with double

62:39

just so that I can avoid that F at the

62:41

end and here I want to assign let's say

62:43

56. so basically when you try to assign

62:45

a integer value to float it will work or

62:48

double it will work because integer will

62:51

get converted into double automatically

62:53

when you save it okay uh okay that makes

62:56

sense can I have different value let's

62:57

say I want to store a very big number so

63:00

I can actually use Epsilon e in between

63:02

I can say 10 e 12 e raised to 10 and

63:06

let's see if that works compile run and

63:10

you can see we got the value as well

63:11

here and it will adjust automatically

63:13

based on its own implementation so it is

63:15

assigning 1 0 here so there's basically

63:17

12 into 10 raised to 10 or 12 into 10

63:20

into 10 to 10 and then it will I just

63:22

it's according to its own way and we

63:25

have one we have already discussed about

63:26

the Boolean it is only true and false

63:28

there is no one and zero there uh don't

63:30

try to assign

63:31

one it will give you error let's try

63:34

compile okay you can see we got there it

63:36

says int cannot be converted to Boolean

63:38

remove this okay how about character

63:40

let's try uh so when I say character

63:43

let's just see and I want to assign a

63:45

character here which is uh say in single

63:47

code let's say a now when you try to

63:49

print the character uh this will work

63:52

compile and run you can see we got a now

63:57

basically character can be also treated

63:59

as integer that's right we can actually

64:00

perform operations I can say C is equal

64:03

to C plus one so what I'm doing is I'm

64:05

incrementing C I'm enhancing the value

64:08

of it if it is a normal value value it's

64:09

a 5 it will become six okay what if you

64:12

want to do an increment of course we'll

64:13

discuss this later what increment means

64:15

but you can say plus plus to increment

64:16

the value and if you try that with

64:18

character compile it you can see that it

64:20

is getting compiled and if I try to run

64:22

this code we got a or because we are

64:24

printing it there my bad I will just

64:26

have to put that here so first we have

64:27

to increment the value then we can print

64:29

it so C plus plus implements changing

64:31

the value okay it's not a language I'm

64:33

talking about the operation on a c

64:34

variable and you can see we got B so

64:37

when you increment the value of a it

64:38

becomes C it becomes B cool so basically

64:41

all these things are called literals in

64:43

fact there's a concept of string

64:44

literals as well where you example when

64:46

you talked about hello world that's a

64:47

string that's string literal in this

64:50

video we'll talk about type conversion

64:51

and type casting so in the previous

64:53

video we have talked about variables how

64:55

to create a variable and then one thing

64:57

is for sure in fact two things every

64:59

variable needs a name so we have talked

65:01

about it and every variable needs a type

65:04

as well so if you want to get a variable

65:06

for numbers you will say ain't float

65:08

double or long in fact we have sub tabs

65:12

as as well for INT and then if you want

65:14

to store a character we can use care if

65:17

you want to store a Boolean value we

65:18

have Boolean option and then we also

65:20

have a string which we'll talk about

65:21

that later at this point we have a lot

65:24

of different types now there is one

65:26

confusion what if you want to change the

65:28

type of a variable can we really do that

65:31

so so unfortunately we can't we can't

65:33

change the type of a variable but then

65:35

can you assign a value to other type

65:37

example let's say we have a variable

65:39

which is of type byte so I'm using a

65:41

byte variable which is B and it will

65:43

have a value now of course we have

65:45

talked about the range of byte as well

65:46

you can have maximum 127 plus I mean

65:51

plus 2 127 this is the max value you can

65:53

accommodate here but can you assign a

65:56

integer value that's a question so what

65:57

if you have a variable of type int a and

66:01

the value of a is 256. now what will

66:04

happen if you try to assign this a value

66:06

which is 256 into B something like this

66:09

what if I say B is equal to a so what

66:11

I'm doing now is I'm assigning a value

66:13

of integer to byte now unfortunately

66:15

this will not work but what will work

66:17

the reverse will work what if you have a

66:19

variable which is a and then you are

66:21

trying to assign a value which is B this

66:23

will work so what happens is the type of

66:27

a is integer right it has a bigger range

66:29

and byte has a small range so the

66:31

compiler says hey you're trying to

66:33

assign a integer value to byte and at

66:36

this point you will lose you might lose

66:38

some data so basically we are trying to

66:40

convert it and it is narrowing it right

66:42

so we are narrowing the size of it on

66:45

the other hand if you talk about this

66:46

operation here we are widening it

66:48

because this the range of byte is small

66:51

and integer is a bigger one so of course

66:53

you can put a small items in a bigger

66:56

box but if you put a bigger item in a

66:58

small box it will not work right okay

67:00

cool so that makes sense right but then

67:03

what if you really want to do it you

67:05

want to convert a integer value into

67:07

byte see at this point if the value of a

67:10

is not 256 what if the value of a is

67:12

let's say 12. now the value of a is 12

67:15

which can be accommodated in b as well

67:18

so at this point even your compiler will

67:20

say hey I don't know what your value of

67:22

a is but I will not allow you to store

67:24

the value of a in B because a is integer

67:27

and B is byte so in this case you can

67:30

say hey compiler give me one second here

67:32

what you can do is you can say B is

67:35

equal to you can convert a you can do a

67:38

conversion by saying you can give a

67:40

bracket and you can mention the type in

67:43

which type you want to convert you can

67:44

say byte and you can mention the

67:45

variable name a so what you're doing is

67:47

you're converting integer value into the

67:50

byte format okay now this constant is

67:53

called casting so basically this is

67:56

actually a conversion but explicitly

67:58

you're doing it so whenever you do

67:59

explicit conversion it is called casting

68:02

so when I say explicit there should be

68:03

implicit as well right and that is

68:05

correct we have some conversions which

68:07

are implicit example if you talk about

68:09

this particular operation here this is a

68:12

implicit conversion basically you are

68:13

trying to store a byte value into a

68:16

integer variable so it becomes integer

68:18

when you assign it right so this is a

68:20

conversion okay so when you do it

68:22

explicitly that is casting when you're

68:23

disopening if it is happening

68:25

automatically that is conversion now

68:27

this makes sense right now is it means

68:28

you can do it with anything not exactly

68:31

there are certain types example you

68:32

can't store a character value into a

68:34

Boolean value okay because Boolean only

68:36

supposed to end false and character

68:38

supports all the characters so you can't

68:40

do those conversions but anything in the

68:42

range of integer float double actually

68:45

works example uh we have talked about

68:47

integer and byte if it is long as well

68:49

that completely works now what if you

68:51

have a scenario where you want to store

68:53

maybe of type a float value now let's

68:56

say you have an integer variable here

68:57

which is int let's say x is equal to and

69:01

you're trying to assign a float value

69:02

now float value is let's say 5.6 of

69:05

course I can create a float variable I

69:08

can assign this value to that or maybe

69:10

this is double because we don't have F

69:11

so let's say this this is double and

69:13

then you're trying to assign that to

69:14

integer now what will happen is in this

69:16

case if even this will not work so what

69:19

you have to do is you have to apply you

69:20

have to convert it so what I will do now

69:22

is let me just create a float variable

69:24

here so I will say float f is equal to

69:27

5.6 and remember you have to put F at

69:30

the end now if you try to put f it will

69:32

not work so what you will do is you have

69:33

to explicitly convert this value into

69:37

integer so what you're doing is you're

69:39

assigning a float value to an integer so

69:42

you're converting it now what you will

69:44

lose here so at this point you will lose

69:46

your point values so whatever value you

69:49

have in after that point you will lose

69:50

an example the value of five point which

69:52

is 5.6 so here when you convert that it

69:54

becomes 5 so in the X the value you will

69:58

be getting is only five cool uh so it

70:02

makes sense right so for float it will

70:03

cut the value but coming back to byte

70:06

now what will happen when you talk about

70:07

byte here what do you think we don't

70:09

have Point values so if you have a

70:11

bigger number example let's say this is

70:13

not one this is not 12 let's say we have

70:15

a bigger number here and the bigger

70:16

number is let's say

70:18

257. now what will happen when you

70:20

assign 257 because the range ends at 127

70:22

you can't go beyond that right so in

70:25

this case it will use a concept of

70:27

modulus okay or modular operator so what

70:30

modular will do is it will divide this

70:32

number so whenever I try to assign a

70:34

integer value to byte which which we are

70:36

doing here so when you are trying to

70:37

assign a integer value to a byte it will

70:40

convert that into modulo which is it

70:44

will divide the number a which is 257

70:47

and it will find the percentage okay so

70:50

257 percentage which is a modular symbol

70:53

and it will divide it with a range okay

70:56

which is 2 256. because a complete range

71:00

of byte is 256 right from minus 128 to

71:03

plus 127 and then the remainder you will

71:06

get here is one cool so the answer here

71:09

when you try to do it with the 257 it

71:11

will be one I will show you that in the

71:12

code so coming back to the code let's

71:14

try all this operation which we talked

71:15

about so what we'll do is we'll start

71:17

with a integer value we can say or maybe

71:20

we can start with byte so we can say

71:21

byte B is equal to and maximum you can

71:23

have is 127 let's take that value and

71:26

let's try to print it here so we can say

71:30

system Dot dot out Dot println and here

71:34

let's print the value of B so when you

71:36

save that and when you go back here when

71:37

you try to run this so we say Java C and

71:40

Hello dot Java comparison done let's run

71:44

this code I can say Java space I have to

71:46

run with the class file which is hello

71:48

and you can see we got 127. now what

71:50

will happen if you try to do that with

71:52

257 it will not work first of all it

71:56

will say either you can see we got data

71:58

it says incompatible types because it is

72:00

integer type now 257 okay so you can say

72:03

127 here now next what I'm going to do

72:05

is I want to create a integer variable

72:06

here which is int int n is equal to and

72:09

I can assign a value maybe let's say a

72:11

because we have used that so we'll say a

72:13

and then the value we are going to

72:14

assign is of B so can you assign a byte

72:17

value to integer because this is this is

72:18

where you're doing converting and if you

72:20

try to print the value of a let's try

72:22

that in fact there's one more thing

72:24

which is which came recently in Java so

72:27

basically you know we always do two

72:28

steps right first you compile the code

72:30

and then you run the code right so you

72:32

compile that with the help of java C and

72:34

when you compile it you see you say

72:36

hello.java and when you run it you say

72:39

simply say Java and hello you have to do

72:41

two steps right so when you're into

72:42

learning phase why to do two steps you

72:44

can actually do a single step again not

72:46

recommend it for a big project or not

72:48

even for a small project don't you don't

72:51

even try that in your company for

72:53

learning purpose we are doing it so

72:54

basically we got a shortcut we can say

72:56

Java space Hello dot Java directly maybe

72:59

it is getting influenced by the other

73:01

languages where you simply mention the

73:03

command and a file name no need to

73:05

mention compile and run Subway step

73:07

let's try this and you can say it works

73:09

so you can see we got 127 it is

73:12

perfectly working and just to show you

73:14

that things are happening behind the

73:15

scene uh the value is getting changed

73:17

you can see we got 125. so from now from

73:19

whenever you have single file will use

73:21

this but remember this is just a

73:24

shortcut ultimately behind the scene we

73:26

need to do this okay I mean when you

73:28

build a project first you have to

73:29

compile the code then run on the code

73:31

this is a shortcut and one more thing

73:33

this will work only in the recent

73:34

version of java I guess from java 14. if

73:37

you have an older version of java this

73:39

might not work so if it's not working in

73:41

your machine that means you have an

73:42

older version upgrade your jdk version

73:44

Okay cool so now what I will do here is

73:46

we can assign the value right but what

73:47

if you have a integer value let's say 8K

73:51

and now you are trying to assign or

73:53

maybe let's let's get a byte value byte

73:55

K is equal to a and let's comment this

73:59

section instead of saying B I want to

74:01

assign a value which is 257. now even if

74:04

you have a smaller value let's say 12

74:05

even this will not work because

74:08

ultimately the value the I mean the type

74:11

of a is integer right you can't assign a

74:13

integer to a byte format and if you run

74:15

this code you can say it will give you

74:17

error it will say incompatible types so

74:19

we got one way which is you can just

74:22

cast it with byte so we are doing a

74:24

typecasting here so when you run this

74:26

code now it will work you can say we got

74:28

12. now we got 12 because it is a in a

74:30

Range of bite what happens when you have

74:32

a bigger value which is 257 now this is

74:35

out of range right so you can come back

74:37

here run and you can see we got one as I

74:40

mentioned before it will perform a

74:42

modulus operation which is actually

74:43

finding the remainder so it will divide

74:45

this number by the byte range which is

74:47

256 it will find the remainder which is

74:49

one I hope this makes sense right so we

74:52

were able to do the byte conversion in

74:54

conversion this is working let's try

74:56

with float now so let's say we have a

74:58

float value which is f and the value of

75:00

float is 5.6 F at the end because when

75:03

you look at a float variable you have to

75:04

put at F at the end and then we can

75:07

create a integer value we can say int

75:11

let's say this time we'll go for T

75:12

doesn't matter what variable name you

75:14

use and then if you try to put F there

75:16

and if you try to print the value of T

75:18

let's see what happens you can see we

75:20

got error again the same thing it is it

75:22

says there might be loss of the value

75:24

from this conversion but then I want to

75:26

do it I want to convert that so we can

75:28

put a casting again and we can say float

75:30

here and if you try to run this code so

75:32

you're trying to convert that into

75:33

integer right what's wrong with me and

75:36

let's try now and you can see we got

75:38

five so we lost that point six there now

75:40

this concept of casting is actually very

75:42

important when you start working on the

75:44

oops concept object and the further

75:45

concept this casting will be required

75:47

there as well at this point we are doing

75:49

casting on primitive values but later on

75:51

we'll also work with objects

75:53

okay now we have talked about casting so

75:55

what is conversion so conversion is

75:57

automatic

75:58

or we can say conversion is automatic

76:00

conversion and casting is explicit

76:03

conversion so when you specify something

76:05

it is explicit cool now there's one more

76:08

concept which is type promotion so what

76:11

are what what do you mean by type

76:13

promotion so let's say we have a byte

76:15

value and the value for byte let's say b

76:17

or maybe let's say a and the value is 10

76:19

here okay and then we got a byte which

76:22

is B is equal to 20. and then when you

76:25

perform the operation maybe maybe this

76:28

should be 129 let's let's say this is

76:31

30. now when you perform an operation on

76:34

this let's say a into B now whenever you

76:36

do a into B it is what 300 right so when

76:39

you say 300 here it is going out of byte

76:43

now can you save that into integer can I

76:46

say int result

76:47

is equal to a into B now what is

76:50

happening here you're performing an

76:52

operation on buy so when you

76:59

know Java says hey we will promote you

77:01

so this value will be promoted into a

77:03

integer value okay and you can store

77:05

that into integer so this is called type

77:07

promotions let's run this and you can

77:10

see we got an error because we are

77:12

printing T here let's print the result

77:15

and run and you can see we got 300 so I

77:18

hope it makes sense what is Type

77:19

conversion and casting in this video

77:21

let's talk about operators that's right

77:23

you can perform operations on different

77:25

values in fact we have done that before

77:27

but let's expand the examples and more

77:29

operators so basically we can add two

77:32

numbers we can subtract two numbers we

77:33

can divide two numbers we can do we can

77:35

multiply two numbers and also we can

77:37

perform an operation so that you will

77:40

get a remainder of an operation okay so

77:43

when you divide a number and if you only

77:44

want a remainder we can do that as well

77:46

so let me show you those things with a

77:48

code here so what I will do now is first

77:51

of all let's create two variables and of

77:52

course we can name any variable name but

77:54

let's go with integer num1 and the value

77:57

it will have let's say seven and then

77:59

let's have one more variable here which

78:00

is let's say five uh num2 is 5 and then

78:04

I can perform some operation and I can

78:06

save that operation in a result so let's

78:09

say if you want to add two numbers we

78:11

can do that we can save the output in

78:13

result and then to perform the operation

78:15

we can use a plus operator that's right

78:17

we can actually use a plus operator to

78:20

add two numbers and then once you have

78:22

done that we can simply come back here

78:24

we can say system dot out Dot println

78:27

and here we can print the result and

78:29

just to see if it is working and as we

78:31

discussed before this is not a good way

78:32

of running a code you have to first

78:34

compile the code then run but we got a

78:37

shortcut I can simply use Java space

78:39

file name which with DOT Java say enter

78:42

and we got 12. so when you say 7 plus 5

78:45

we got 12. cool now what I will do is uh

78:49

so we can add two numbers what about

78:51

subtraction let's try that so I will say

78:52

minus symbol and run you can say we got

78:55

two so just talking so you can add two

78:58

numbers we can subtract two numbers how

79:00

about multiplication we can do that so

79:01

we can say star num1 into num2 come back

79:05

here one you can see we got 35. so 7

79:08

into 5 is 35 that work that is working

79:10

how about division uh let's try that so

79:12

the moment I said division we have to

79:14

use a slash okay so we don't have a

79:16

division in fact that's how we do right

79:18

we say underscore or something obviously

79:20

slash so yeah we got uh Slash let's try

79:24

and you can see we got one that's right

79:25

one is a question here right so when you

79:27

divide seven by five you get two things

79:29

one is a question which is one and then

79:32

you will also get a remainder now in

79:34

this case when you use slash you will

79:36

only get a quotient not a reminder

79:38

so how will you get a remainder in that

79:40

case you have to use a modulus operator

79:43

so we can use when you say percentage

79:44

symbol like this it is a modulus

79:46

operator that what it will give you is

79:49

only a remainder okay so if I run this

79:52

code you can see we got two to the

79:54

remainder here right now we can use any

79:55

complex numbers as well whatever number

79:57

you want to perform with and I would

79:59

suggest you to try certain examples so

80:01

that you will get the get used to these

80:03

operations now all this operation

80:04

actually makes sense right these are

80:06

very simple stuff to understand can we

80:08

use some other operators as well let's

80:10

try what I will do now is let me just

80:11

remove this result for time bin or maybe

80:13

I can just comment this part I also

80:15

don't need this num2 let me only focus

80:17

on the first variable which is num1 now

80:20

let's say I want to uh add this number

80:23

one by two okay so basically the value

80:25

of number is seven I want to increment

80:28

the value by 2. so what we can do is we

80:29

can say num1 is equal to so basically we

80:32

have to use num1 which is number one in

80:35

this case and we'll add a value to it

80:37

which is two and then whatever new value

80:40

you will generate after this will be

80:41

assigned to num1 okay that looks cool

80:44

right uh let's try this I will say num1

80:47

print num1 and let's run this code you

80:49

can see we got nine so this is working

80:50

so seven became nine so just walking

80:52

right now when you know that you are

80:55

adding the number by itself okay uh what

80:58

you can do is you can just use a

81:00

shortcut

81:01

now what that shortcut says is let me

81:03

just comment this section here and let's

81:05

write it fresh here so I will say now

81:07

one

81:08

see you want to increment the value of

81:10

num one by two right you want to add

81:11

Itself by two so you can say I want to

81:14

add myself

81:15

but how much value so I have to say

81:17

equal to 2. so what we are saying is we

81:20

want to increment num1 by 2. even this

81:23

should work let's try or we got an error

81:25

because I forgot the semicolon

81:27

and let me just clear this run and you

81:29

can see we got nine so this is working

81:31

cool now it's not just tradition we can

81:33

do that for any other operation maybe

81:35

you want to subtract a number by two so

81:38

we can say subtract equal to two so it

81:41

will reduce the value by two you can see

81:42

God we got five we can multiply with

81:45

some other number maybe I want to

81:47

multiply the same number with another

81:49

number and assign it so we got 56 7 into

81:52

8 to 56 we can divide as well and so you

81:55

can use slash and all the operators

81:56

which you have awesome so this is

81:58

shorthand now going back to the addition

82:00

operator and then let's say if you want

82:01

to add it by two we can simply mention

82:03

the value 2 right we can also add it by

82:06

one so in this case if you say add it by

82:08

1 the value of num1 is 7 and then you

82:11

are adding it by one you can see we got

82:13

eight now in fact there's a shortcut for

82:15

this as well what you can do is instead

82:18

of using num plus equal to 1

82:21

you can also say num1 plus plus even

82:24

this is possible let's remove the space

82:26

in between so even this is possible you

82:28

can say num1 plus plus which is

82:30

incrementing so what we are doing here

82:32

is something called increment and if

82:35

you're on this chord you can say we got

82:36

eight so this operation let me just make

82:39

it one so all these operations are

82:42

similar okay so why I'm saying similar

82:44

why not same I will discuss that in some

82:46

time but this is similar

82:48

and maybe if you have done that before

82:50

as well when we talked about characters

82:51

you know we remember C plus plus we've

82:54

done that so yeah we are incrementing it

82:57

as well now not just incrementing we can

83:00

do decrement as well so instead of

83:01

saying num plus plus we can also say num

83:03

minus minus so what we are doing now is

83:06

decrement so when you say plus plus that

83:09

is increment let me comment this part

83:10

and here I will say num minus minus now

83:14

why you have to comment this but I want

83:16

to run this so we can say num minus

83:17

minus let's run this code if say this

83:20

works and you can see we got six so

83:22

that's how you can perform this addition

83:24

subtraction multiplication division and

83:27

increment decrement in fact we have also

83:29

talked about modulus which is for

83:31

finding the remainder okay now that was

83:33

about increment decrement right we have

83:35

talked about that now instead of using

83:38

plus plus we can actually use something

83:40

called let me just write it in comment

83:42

so instead of saying num1 plus plus we

83:44

can also say plus plus num1 you know

83:46

just to simplify this example instead of

83:48

saying num1 everywhere let's say num so

83:50

that maybe you will not get you will get

83:52

confused by that one extra symbol there

83:54

which is one uh just ignore all this

83:57

operation just focus on this part which

83:59

is numb and numb also remove this also

84:01

remove this let's see if this is working

84:03

okay this is working so basically we can

84:05

also say instead of saying num plus plus

84:07

we can also say plus plus num now what's

84:10

the difference between these two even

84:11

this also work let me just try that and

84:13

show you you can see we got eight we are

84:15

incrementing the value even this will

84:16

give you eight but what's the difference

84:18

what's the difference between writing

84:20

this afterwards and writing before the

84:23

variable so basically we have a

84:25

different names to it so whenever you

84:27

perform operation after the variable it

84:30

is for increment or decrement so when

84:32

you do it post we say it is post

84:34

increment and when you do pre before the

84:36

variable it is called pre-increment okay

84:39

so when we are doing it afterwards we

84:41

say post increment when we say before it

84:43

is doing pre increment now you might be

84:45

thinking hey ultimately the value is

84:47

same right so even if you pull even if

84:49

you do this or this both will increment

84:52

the value then why we have to defend

84:54

Syntax for it why not just simply one

84:57

the answer is it behaves differently

84:59

when you try to assign a value when you

85:02

try to fetch the value let me repeat

85:03

these two operations will behave

85:06

differently when you try to fetch the

85:07

value okay let me tell you what I'm

85:09

talking about let me show you here let's

85:11

come back here and let's say I have a

85:12

different variable which is ain't and

85:14

then I'm storing that in result and what

85:17

I'm storing is num or maybe I will say

85:19

plus plus num okay now think about this

85:21

and maybe you can pause the video or

85:24

whatever you want to do just think about

85:26

the what will be the value of result

85:27

okay I will say result here to print and

85:30

maybe you have done your thinking let's

85:32

run this and you can see we got eight so

85:34

what is happening here is the initial

85:36

value of num is 7 and then you are

85:38

saying plus plus num that means you are

85:40

incrementing it that incremented value

85:43

will be assigned to result okay and then

85:46

you got result which is eight okay so

85:48

initially I've also mentioned that this

85:50

two will be incrementing so we can also

85:52

say num plus plus even this should be

85:53

doing the same thing right let's try oh

85:56

there's a Twist here so plus plus num

85:58

and num plus plus are not same I mean it

86:00

works same with their individual

86:02

statement but when you try to fetch the

86:04

value from them when they are performing

86:06

the operation they behave differently so

86:07

let's understand what is called

86:09

pre-increment and what is called post

86:11

increment so whenever we use assignment

86:13

operator with this operation what it

86:15

will do is it will check hey we have an

86:17

option which is num plus plus but that's

86:19

a post increment so first it will fetch

86:21

the value of num let me repeat it will

86:24

first fetch the value and then increment

86:28

in fact it looks like one statement but

86:31

we have two statements here right in

86:32

fact we have this statement here num one

86:34

equal to num plus one so what it will do

86:37

is it will first fetch the value of num

86:40

which is 7 in this case and then it will

86:42

increment the value of num and it will

86:44

make it eight okay but then what is

86:47

assigned result is 7 itself and then you

86:49

are printing that seven here okay but

86:51

what will happen when you say let me

86:53

just write it here so we'll say end

86:55

result is equal to plus plus num in this

86:58

case what will happen is it will first

87:00

increment and then fetch the value okay

87:03

so it will first increment itself and

87:05

then it will fetch the value and that is

87:07

what is going to result I hope this

87:09

makes sense right so this is how your

87:10

code works when you say S Plus num and

87:13

num plus plus in fact we'll do some more

87:14

operations with this once we get started

87:17

and it will make much more sense later

87:19

so in the operations we have talked

87:21

about other thematic operators till this

87:23

point so whatever operation we have done

87:24

it is for the arithmetic purpose right

87:25

addition suppression multiplication

87:27

division but what if you want to compare

87:29

two values what if you want to have

87:31

compound conditions so we are talking

87:33

about operators right so we have talked

87:35

about arithmetic operators where we can

87:37

do calculations we can add two numbers

87:38

we can subtract two numbers in fact we

87:40

have also seen how do we increment a

87:42

value and decrement right now it's time

87:45

to enhance and go towards something

87:47

called relational operators in Real

87:49

World what we do is we basically have uh

87:52

we compare We compare things right so it

87:55

can be comparing two values and if you

87:57

can check with the help of less than uh

87:59

you can check with the help of greater

88:00

than you can also compare with two

88:02

values right if they are equal or not so

88:04

we use something called equal to there

88:06

right uh what if you want to negate

88:09

something what if you want to find a

88:10

negative eight example uh if you have a

88:13

True Value maybe you want to make it

88:15

false so in that case we also have to

88:17

move from True to false right so

88:19

basically we want to implement this with

88:20

the help of operators in programming

88:23

language now specific to Java if you

88:25

talk about we have different operators

88:27

we can use less than to compare with the

88:30

help of less than or less than uh the

88:33

first value should be less than second

88:34

value if that is true of course you will

88:35

get True Value we can also check for

88:37

greater than we can also check for equal

88:39

to now the only difference is when you

88:42

talk about equal to in programming

88:44

specifically for Java equal to means

88:46

assignment operator example let's say if

88:49

you have a value which is 5 and you want

88:50

to assign that to a particular variable

88:52

let's say x so you say x equal to 5. so

88:56

this equal to symbol here actually

88:58

becomes your assignment operator but

89:02

what we want here is we want to compare

89:03

two values right so that's why we use

89:05

double equal to so in programming in

89:07

Java we use double equal to come to

89:09

compare two values what if you want to

89:12

check if two values are not equal in

89:14

that case we can say

89:15

exclamation and equal to okay that's

89:19

that's what you say it is not equal to

89:20

so this is comparing for equal and this

89:24

is comparing for not equal and this is

89:26

of course less than this is greater than

89:28

and you can also combine example if you

89:30

remember in mathematics we use the

89:31

symbol Which is less than equal to in

89:34

the same way of course we don't have

89:36

this key on the keyboard so we have to

89:38

use less than uh

89:41

so we have to use less than equal to for

89:43

this particular combination uh we can

89:46

also say greater than equal to right so

89:47

we can use all this combination which

89:49

comes up as a part of a relational

89:52

operator now basically when you compare

89:54

this to what your example let's say if I

89:55

want to compare if 7 is less than 5 so

89:59

this will return u a value which is true

90:02

or false so it will be either true or

90:04

false of course we got a static value

90:06

here which is seven and five so the

90:08

output for this will be of course false

90:10

but if you say 5 is less than 6 yes

90:13

that's true so that's how you define uh

90:16

that's how you compare two values and

90:18

that's how you get the output the point

90:20

to remember here is the output which

90:22

you're going to receive is a Boolean

90:23

value okay now how do we Implement that

90:26

in a code so let's go for that so let's

90:29

say uh we have this our we have our code

90:31

here and in this what I'm going to do is

90:33

I have two values let's say integer I

90:36

can say x equal to 6 and then I got Y is

90:40

equal to 5 and now I want to compare

90:42

these two values the way you can do that

90:44

is of course when you compare these two

90:46

values you will get a Boolean value so I

90:48

can say Boolean uh against the result is

90:51

equal to now how do you store a value

90:53

and how do you compare so first of all

90:55

storing is easy you can just assign the

90:56

value but how will you compare in that

90:59

case you can say x is less than y and of

91:02

course that's available on keyboard so

91:03

you can directly use it

91:05

and what I want to do is I just want to

91:07

print uh the output here

91:11

so I can say system Dot

91:14

out dot println just for printing stuff

91:17

and then I can just say result now the

91:19

moment you say result it will print the

91:21

output and of course you have to compile

91:23

this code and the way you can do that is

91:25

by saying Java C

91:27

demo.java enter okay there's no issue

91:30

with the compiling because the syntax

91:32

looks good and now I will just run this

91:34

I will say Java demo enter and you can

91:37

you can see we got false now why we got

91:39

false is because the value for x is 6

91:42

and the value for y is 5. so the first

91:45

value is lessons is not less than second

91:47

value and that's why we got false

91:49

greater than simple here it actually

91:51

makes sense because that's a

91:53

oh remember every time you make a change

91:55

first you have to compile the code and

91:58

then you can run the code and you can

91:59

see we got through this time so

92:01

basically that's how you can compare two

92:02

values and you can print it but what if

92:05

these two values are equal so let's say

92:07

we got 6 and 6 and now if we try to run

92:10

this code of course compile and run we

92:13

you can see we got false

92:15

okay but then they are equal right so

92:19

maybe I want to check if two values I

92:21

mean if the first value should be either

92:23

greater or it should be equal

92:26

so we can put that uh equal to symbol

92:28

here as already discussed and now I can

92:31

just compile and run and you can say we

92:34

got true so this is working

92:36

we can also check for less than even

92:37

that works because it's equal right and

92:40

anyway we have equal so it should work

92:42

so we got true

92:43

uh we can also check if two values are

92:46

not equal example let's say I just want

92:48

to make sure that X and Y are not same

92:51

and the way you can do that here is by

92:54

saying not equal to so when we say not

92:56

it simply means it will uh when you say

92:59

exclamation it simply means dot so not

93:01

equal to Y and with this if I try to

93:05

compile and run you can say we got true

93:07

so even this is working but what if you

93:10

don't want to check for less than and

93:11

greater than you want to check if the

93:13

two values are exactly equal so in this

93:16

case normally in mathematics we use

93:18

equal to but that will not work here so

93:21

you have to use double equal to so you

93:22

are comparing x with Y and they should

93:25

be equal clear the screen compile and

93:31

run and you can see we got false the

93:34

reason we got false is because we have

93:36

both the different values so if I make

93:38

it same now and if I run this code

93:40

compile

93:42

and run and you can see we got true so

93:44

basically uh we we have different

93:46

operators and this will be used a lot in

93:49

the upcoming sessions uh so it doesn't

93:51

matter what type of programming you do

93:53

see when you say your computer has a

93:55

brain you know we have to decide based

93:57

on some values of course I have just

93:59

checked it with integer you can try it

94:01

with float as well in fact maybe I can

94:03

try with double so I can simply say

94:05

double here and I can say this is

94:08

8.8 and I can say double which is let's

94:12

say six maybe nine point eight and I can

94:15

just check if they are less than equal

94:17

to and of course I want you to try

94:20

different values there

94:21

and compile No it should run and you can

94:24

see we got true but if you check with

94:26

the greater than symbol this should give

94:28

you false

94:31

and we got false so basically whenever

94:33

you have a primitive value uh this

94:35

actually works uh maybe once we start

94:38

with string we also try to compare the

94:41

string values and let's see how that

94:43

turns out to be but this is your this is

94:45

how you do your traditional and

94:48

traditional operator checking with the

94:50

help of relational operators we got less

94:52

than greater than equal to symbol which

94:54

is double equal to not equal to that's

94:56

one yeah so this makes sense right but

95:00

what if you have two conditions example

95:03

let's say I got this values and now I

95:06

have int a equal to 8 and int b equal to

95:09

6. so I want to check if I mean I want

95:13

this x to be less than y and I also want

95:17

this a to be less than B I want to check

95:21

both the operation

95:23

and the thing is when we say I want to

95:26

check both the things should be true

95:28

then only it should print true how will

95:30

you do that how will you combine two

95:32

different uh operations or two different

95:34

checkings

95:35

so we are done with the arithmetic

95:37

operators and relational operators right

95:39

then we had one question what if you

95:41

have two relation operators and which

95:43

will result in true or false right uh

95:46

and then you want to combine them so you

95:48

want one true or false out of two or

95:51

three true or false so what I'm saying

95:53

is let's say I have two variables here

95:54

which is x y and also we have one more

95:57

commission which is a and b and then I

96:00

want to check if I mean I want X to be

96:03

less than y and I want a to be greater

96:05

than b uh this might result in two and

96:08

true and false or it might result in

96:10

true and false what I want is the output

96:13

of I I want is I want output of this two

96:16

combined can be either true or false but

96:19

I want the combined output irrespective

96:21

if they are a true or false individually

96:24

so how do I combine this

96:26

now to combine these two different

96:28

conditions we can use a logical operator

96:31

in between example uh this logical

96:33

operator can be and or I mean and the

96:37

second option is R so it can be and and

96:39

all now what exactly it means

96:41

see if you want that your output to be

96:44

true so let's say if you are out if you

96:46

want your output to be true

96:48

so you can use if you can say Hey you

96:51

know I want both of this to be true then

96:53

only I want true or you can say if any

96:56

of this can be true I want it to be true

97:00

now this is very important in terms of

97:02

computation So when you say your

97:04

computer is smart it's because of a

97:06

computer can decide based on multiple

97:07

conditions so to do that we use

97:10

something called a logical operators so

97:12

what we do is uh again this concept is

97:15

coming from mathematics is not a

97:16

programming concept it is coming from

97:18

mathematics

97:19

where you can use something called a and

97:22

operator we can use odd operator or we

97:25

can use a not operator which is which is

97:27

the exclamation

97:28

so the and operator in programming we

97:31

represent that with the help of and and

97:33

the odd operator so we use Ampersand for

97:35

and and we use a pipe for odd so we can

97:40

use Ampersand which is and we can use Pi

97:42

property which is or or we can use the

97:45

exclamation mark for not

97:47

okay so how do we decide what could be

97:50

the output of two operations so let's

97:53

say the first operation gives you true

97:54

the second operation also gives you true

97:56

so maybe you can come we can do that

97:58

with the help of these two conditions if

98:00

both are true then what should be the

98:02

output so the output will be true again

98:05

I'm talking about in terms of and

98:07

operation if one is true one is false it

98:11

is false if one is false I mean the

98:15

first one is false second is true again

98:17

it is false and if you want if you have

98:20

both of them as false again it will give

98:22

you false now when you want such type of

98:25

output when both if both are true then

98:28

only should be true then we can use

98:30

something called and and operation here

98:33

but what if you have a different set of

98:35

my set of things in your mind uh let me

98:37

just divide this and the second set

98:40

would be let's say if the first one is

98:42

true and second one is also true you

98:45

will get true if the second one is true

98:48

so the first one is true second one is

98:49

false it will give you true if the first

98:53

one is false second is true it will give

98:56

you true and if both are false it will

98:59

give you false if you have a condition

99:02

or if you have a requirement where if

99:04

any of them is true then it should be

99:07

true so example if you can see if any of

99:10

one so of course both are two that's

99:12

great but if even if you have one true

99:14

that's true even if you have one true

99:16

that's true even if you have one true

99:18

that's true right but if if you have

99:21

both false then it will be false now if

99:23

you compared with this and when you have

99:25

both true it will give you true if any

99:27

of them is false you can see if there's

99:29

a false you will get a false here so

99:32

this is your and and odd so that's how

99:35

you can combine we have one more here

99:37

which is not now not basically used to

99:41

reverse the thing example if you have

99:43

true and if you want the output to be

99:45

false and if you want if you have false

99:47

and if the output you want is true then

99:49

you will apply a not operation now apart

99:52

from this we have one more which we have

99:54

not talked about

99:56

which is xor again we'll talk about that

99:58

in some time but then we have three

100:00

basic operations here we have and or and

100:03

not so basically how you combine this if

100:05

you want to do that in programming of

100:07

course you can also use and operator or

100:09

pipe or you can use not directly the

100:12

other option you have is something

100:13

called a short circuit so instead of

100:15

using and you can use double and instead

100:18

of using or you can use double or and

100:20

not is as it is we don't have to do Nots

100:23

there and now what's the use of this so

100:25

first of all let's talk about and so

100:27

let's say if you have X

100:29

rest X is greater than y and if you say

100:32

and here and you can say a is less than

100:34

b so we have these two conditions here

100:36

and I want both them to be true and

100:38

that's where you want the output to be

100:40

true so example if you have a Boolean

100:41

value which is R here so you can say R

100:43

is equal to X is greater than y and a is

100:46

greater than b if in this case if both

100:49

are true the output is going to be true

100:52

right but if you what if you use R here

100:55

so if I remove this and if I put up R

100:58

here if any of them is true it will give

101:01

you true

101:03

as simple as that okay now why do we

101:06

call it a short circuit the reason is

101:09

let's talk about R in R see the point is

101:13

if even if you have one true the output

101:16

would be true right so if the first

101:18

condition itself is true it will not

101:20

even check for the second one

101:22

Beauty right I mean you're saving your

101:25

time that's why it's called short

101:27

circuit you are saving the time if you

101:29

are taking a shortcut

101:31

and you will say hey then what happens

101:33

when you have and here so let's say let

101:36

me put

101:37

and now in this case even if so let's

101:40

say first one is true of course it will

101:42

check with the second one but what if

101:44

the first one is false if the first one

101:47

is false it will not even check with the

101:49

second one because the output is going

101:51

to be false right so that is why it is

101:53

called short circuit okay so from this

101:56

point we are always going to use short

101:57

circuits and an odd instead of using a

101:59

single Ampersand so it can be you can

102:01

say double Ampersand for and double pipe

102:04

for or and of course not will remain not

102:07

so example let's say if you already have

102:09

a Boolean value which is R so I can say

102:11

R and if if you want to save that value

102:14

in s so whatever value of R you will get

102:17

you will save that in s example if R is

102:18

t s will be t but what if you put a

102:22

Ampersand here so but what if you put a

102:25

exclamation here

102:27

so in this case if this is true if R is

102:31

true it will give you false it will give

102:33

you a reverse value and this is very

102:35

beneficial when you start working with

102:37

the uh project where you have future you

102:40

have a data and if you want to do

102:41

something related to the data So based

102:44

on the data you will change some you

102:45

will do something you will change the

102:47

condition all the stuff

102:48

okay this thing looks cool here but how

102:52

will you implement this in the actual

102:54

code like that's what we're going to see

102:55

now so if you can see uh I of course I

102:58

have to go with the same variables I can

103:00

say int uh X is equal to let's say seven

103:03

and in fact you know uh let's let's do

103:06

it on your line and I will say int Y is

103:08

equal to 5. so we got these two values

103:10

and then apart from this I want two more

103:13

variables let's say A and B so I will

103:14

say int a is equal to 5 again and end B

103:18

is equal to nine I'm just getting random

103:20

numbers here and what I want to do is I

103:23

want to save the data in a Boolean

103:25

variable so I will say Boolean result

103:27

and let's see what the value you're

103:29

getting

103:30

so first I will declare it here and I

103:31

will also print the value for this

103:34

okay so basically we are trying to print

103:35

result here but then I want to perform

103:38

some operation on this so I can say if x

103:41

is greater than y okay I'm just checking

103:44

for one thing and now you know we have

103:46

done this multiple times I will just

103:48

compile this code and run and you can

103:50

say we got true because the value which

103:52

of X which is 7 is greater than the

103:55

value of y which is 5. but then I want

103:58

to put one more condition here and I

104:01

want both of them to be true if I want

104:03

to get the output as true so what you

104:04

can do is you can use Ampersand double

104:06

Ampersand here and you can say a is

104:09

greater than b now if you can see the

104:12

first condition is true okay this will

104:14

give you true but the second one the

104:17

second operation here will give you

104:18

false now what do you think is the

104:20

output think about this and let's see

104:24

now the output which I'm expecting here

104:26

is false let's try and yeah we got false

104:29

is because the first one is false

104:32

so the first one is true second one is

104:33

false so in this case you both need to

104:36

be true example if I make this as less

104:38

than of course you should not check

104:40

change your condition you should change

104:41

your values but just we are learning

104:43

into we're doing we are learning right

104:45

so it it's fine it's fine

104:47

now let's compile this code and run and

104:50

you can see we got through since both

104:51

are true it's true

104:53

but what if you use a pipe here now pipe

104:57

simply means R right so even if you are

105:00

so let's say that both the conditions

105:02

are true now and both these operations

105:03

are true

105:05

run and you can see we got true but what

105:08

if let me make the first one as

105:12

false right so you can say we have 7 is

105:15

greater than 5 so this will be false but

105:18

the second one is true right but still

105:20

if you compile this and

105:24

run and you can see we got true right so

105:27

this is how you use and and all when you

105:31

have multiple uh relation of course you

105:33

can add more you can also add one more

105:35

pipe here by saying the value of a

105:38

should be greater than one

105:40

that's your choice how many pipes you

105:41

want to add compile and run and you can

105:46

see it works uh so this is how you use

105:48

and and or what about not operator here

105:52

so example let's say

105:54

I will only say a is greater than b now

105:57

what do you think what the output is so

105:59

if I compile this code

106:01

and run and you can see we got false

106:03

that makes sense right because the value

106:05

of a is smaller than value of B

106:09

but what happens just before printing

106:11

result I'm saying not so whatever result

106:14

of whatever value of result is we are

106:16

just we are just doing the opposite of

106:18

it if this is true it will give you

106:20

false if this Falls it will give you

106:21

true

106:22

and if I compile and run you can see we

106:25

got true it's because this was false the

106:28

Oliver once was and now when I say

106:30

exclamation result it will give you true

106:33

so as I mentioned before it's very

106:35

important once we go forward towards

106:37

conditions I mean if else for loops so

106:40

this multiple conditions or multiple

106:42

operations which will result in true or

106:45

false will be very help very helpful in

106:47

this video we'll talk about conditional

106:49

statements now basically what happens

106:51

you know when you say your computer can

106:52

think of course we have a lot of

106:54

different Technologies now we have AIML

106:56

right which which can help your computer

106:59

to think right uh we are saying AI will

107:02

change the world but if you go to very

107:04

basic of AI or any computer I can say AI

107:08

is an advancement in terms of computer

107:11

which can think but if you can go to the

107:14

base of it we have one important thing

107:16

which is the flow is depend upon true or

107:20

false example let's say let's say when

107:22

you execute a code so what happens is

107:24

whenever you execute a code the flow

107:26

goes in this way let's say there's a

107:28

flow going on and suddenly there's a

107:30

point here so that let's say at this

107:32

point so this is a flow of the code and

107:34

at this point you decide okay if the

107:38

condition I mean if based on something

107:40

if it is true I will go this way if it

107:44

is false I will go this way or maybe

107:46

based on some other condition I will go

107:49

this way so what happens you know your

107:51

flow of your execution actually changes

107:54

based on what you decide at that point

107:57

and even if you think about Tesla cars

108:00

which is uh maybe I need driverless cars

108:03

but which moves and based on some

108:05

conditions the speed will increase the

108:07

based on some condition the speed will

108:09

decrease based on some condition uh the

108:11

the car will take a halt but if there's

108:13

a object in front of a car at that point

108:16

the car will stop right so this is a

108:18

point where his decide hey it's time to

108:21

increase this speed of a car so maybe

108:23

you can say based on some condition

108:25

let's increase the speed of a car uh

108:28

decrease the speed of a card or stop the

108:30

car so based on some condition you

108:33

decide the another flow now how will you

108:36

do that in programming and that's where

108:38

we have this option of conditional

108:39

statements so what you do is you write

108:41

statement statement statement and then

108:43

you decide I will execute this bunch of

108:45

statement

108:46

based on some conditions so here we'll

108:48

be having a condition if the condition

108:51

is true

108:52

and of course we have talked about

108:54

Boolean values we have seen relational

108:56

operators logical operators if you get

108:58

true you will execute this particular

109:00

block here I mean this block if it is

109:03

false based on something I will execute

109:05

this set of lines right so should we

109:09

execute this lines or this lines will be

109:11

based on your condition now the way you

109:14

can do that in programming with the help

109:16

of if else now this is similar to most

109:20

of the languages if you are coming from

109:22

other any other language maybe you have

109:23

seen this if not don't worry I will talk

109:25

about if else in this particular video

109:28

to understand that let's head towards

109:30

our code here and as you can see uh we

109:34

have we have a demo code and basically

109:38

by using the same file here I want to

109:40

basically do something based on a

109:42

condition so let's say I have a value of

109:44

x here which is 8 okay a simple value

109:47

maybe I want to print hello or buy based

109:51

on some conditions so so I will simply

109:53

say

109:54

system.out.print and then I will print X

109:57

here

109:58

okay in fact I want to I don't I want to

110:00

point hello

110:02

and then I also want to print buys I can

110:04

just reuse the code which I've written

110:05

here

110:06

with a proper indentation yeah and then

110:09

I want to say bye now of course if you

110:11

execute this particular code here if I

110:13

say demo dot Java and run you can say we

110:16

got hello and buy both what I want to do

110:19

is I don't want to execute both the

110:21

statements I want to execute hello based

110:23

on some condition if that condition is

110:26

false I don't want to execute hello I

110:28

want to execute the buy statement

110:31

so at this point I want to decide now

110:33

based on what

110:34

so for this example I want to test based

110:37

on the value of x so I can say if the

110:40

value of x is let's say greater than 10

110:44

maybe I'm just taking an example here if

110:47

the value of x is greater than 10 in

110:50

that case I want to print hello

110:53

so and then if it is false

110:56

I want to execute the Buy

110:59

okay

111:01

so maybe at this point let me just

111:03

comment this section let's say I just

111:04

want to print hello only when the value

111:07

of x is more than 10 okay and to start

111:11

with let me set the value of x as 18

111:13

which is greater than 10. so what I want

111:15

is at this point so we can draw them

111:17

this we can say okay the value of x is

111:19

18. so if you go for the condition so if

111:22

is a condition here or it's a condition

111:24

statement it will check the point here

111:27

which you mentioned or with the

111:28

condition which you mentioned if it is

111:30

true it will execute this statement or

111:33

it will not execute the statement now of

111:35

course that's true so if you go back

111:37

here and compile and run you can say we

111:40

got hello

111:41

so this is basically the Syntax for if

111:43

so how it is syntax we have to use a

111:45

keyword called if and then in the

111:48

bracket in the round bracket we have to

111:50

mention the condition see ultimately if

111:52

is not concerned what type of operation

111:55

you are doing here if is just concerned

111:57

is it true or false so even if you give

111:59

a true here

112:01

if he's basically happy okay so if don't

112:04

want a operation that if says you give

112:07

me true or false I will execute example

112:09

if I say false here

112:11

and if I compile run you can say it will

112:14

it is not printing hello why it is not

112:16

printing address because we are saying

112:18

false so when if C is false here it says

112:21

okay I don't have to execute the block

112:23

whatever is written under if okay so

112:27

there's a space here if you can see

112:28

again that is optional even if you

112:30

remove that that perfectly works because

112:32

Java is not dependent on any indentation

112:35

uh by any chance if you're coming from

112:37

python in Python we do that we give a

112:39

tab to specify that it's an it's a part

112:42

of the eve Block in Java we don't do

112:44

that

112:46

okay I could have simply used a meme

112:48

from Avengers you know it says we don't

112:51

do that here okay anyway so we you can

112:54

see we got false and we are printing

112:56

hello that perfectly works I mean of

112:58

course it is not working because we got

113:00

false but if you make it true it will

113:02

surely execute compile run and you can

113:04

see we got hello

113:05

right now uh what if I just want to go

113:08

back to my condition the value of x

113:11

should be greater than 10 and for this

113:13

as well

113:14

of course we want the value to be true

113:17

and it should print hello and it's

113:19

working but what if the value of x is

113:22

not 18 it's 8 which is false right 8 is

113:25

not greater than 10 so it will give you

113:27

false and that's where you will not see

113:29

hello and you can see we don't have

113:31

hello

113:32

okay so that's how you can use if of

113:34

course you can put multiple conditions

113:36

here if you remember we have talked

113:38

about logical operators so we can say uh

113:41

the value of x should be greater than 10

113:43

and I want one more operator and so I

113:47

want to condition basically and I want X

113:50

to be less than 20. so I want the value

113:53

to be in between 11 to 20. I want 20 to

113:58

be included so I will say equal to

114:01

so the range now okay we can also

114:03

comment something in programming with

114:05

the help of double slash uh which will

114:07

not be read by the programming or the

114:10

compiler

114:12

so the value accepted value for a x now

114:14

is from 11 to 20. uh because we are

114:17

saying greater than 10 which is the

114:19

value above 10 which is 11 and less than

114:22

equal to 20 which is including 20 okay

114:25

and now I'm saying and here

114:28

and let's make it 18 again so if you can

114:30

see it will surely work and we got hello

114:33

right so we can basically put multiple

114:35

conditions but what if the value of x is

114:38

8 or maybe I can say 28 which is not

114:42

less than 20. so this is true X is less

114:45

than x is greater than 10 that is true

114:47

but X is less equal to less than equal

114:49

to 20 no that's not true so this will

114:51

result in true and this will result in

114:55

false so what's what do you think the uh

114:57

end result would be so true and false

115:01

will give you false remember we have

115:02

talked about that in some table in fact

115:04

that table used is called as a truth

115:06

table okay

115:09

so now what I would do is if it is not

115:12

printing hello I want to print by so I

115:15

can just remove this

115:16

and if I compile this code and if I have

115:19

this code you can see we got by hello I

115:21

mean the hello is not there because the

115:24

condition is false

115:26

but what if I make it 18 now of course

115:30

that's true so what I want to do is I

115:32

just want to print hello I don't want to

115:34

print buy because if you run this code

115:36

and if you see the output we got hello

115:38

and buy both we don't want a buy in this

115:41

case

115:42

so what you can do is if you want either

115:45

this or this you can use uh one more

115:49

thing here one more qubit which is

115:50

called else so normally if else works

115:52

together you can you can also write if

115:54

alone

115:55

or you can say if else okay so there's a

115:59

compulsion to have if when you when you

116:00

want to use else okay else cannot be

116:02

alone

116:04

okay that works and now if I uh compile

116:07

this code

116:09

and run you can see we got hello but

116:13

what if you make it 28 at this point we

116:16

it will only go for

116:20

compiled one you can see we got by so

116:22

that's how basically we can use if else

116:25

so we have if we check for the condition

116:27

and then we can also write else in fact

116:30

in the next video in fact let's try to

116:32

do one more example here so let's say I

116:34

have the value of x as let's say 5 and I

116:38

have a value which is Y which is which

116:40

is seven okay so we got two values and

116:43

maybe these two values are coming from

116:44

the user so what I want to do is I want

116:47

to print the maximum of these two so if

116:50

x is greater than y I want to print the

116:53

value of x if Y is greater than x I want

116:57

to print the value of y so how do we do

116:59

it so basically I want to print the

117:01

value of X and Y so I can say system

117:05

dot out Dot println and I want to print

117:08

the value of x

117:09

and let me just reuse the code

117:15

I want to print the value of y but if I

117:17

just try to print this without com

117:19

without checking any condition it will

117:21

print both what I want to print is I

117:23

want to print the greatest number from 5

117:26

and 7. so in this case of course this is

117:29

a very simple examples and I'm just I'm

117:31

on purpose taking simple example is

117:33

because at this point it's important for

117:35

you to understand the concept not the

117:38

complex operation right so what I can do

117:41

is I want to print either I want to

117:44

execute either this statement or this

117:45

statement so whenever you have this

117:47

condition where you want to execute a

117:49

one statement or some block of

117:51

statements always go for if else

117:54

so I can say if and I can check if x is

117:58

greater than y that's what you are

118:00

checking right if this is true I want to

118:02

execute this particular statement

118:04

else

118:07

I want to execute this statement that

118:09

simple right so that's how we can use if

118:12

else and if I compile and run you can

118:14

say we got seven

118:16

but if you would change the value let's

118:17

say if I make the value of X as 8. of

118:22

course the output should be eight and

118:23

that's what we got

118:25

now one thing to remember here whenever

118:27

you have one statement under if or else

118:31

you don't need the curly brackets and

118:34

you can say we don't have it but what if

118:36

you have multiple statement example

118:38

let's say here uh the way we are

118:40

printing X let's say I also want to

118:42

print uh thank you okay in double quotes

118:45

I will say thank you now you can see we

118:48

have two statements inside if so I want

118:50

to execute this two

118:52

when the when this condition is true the

118:56

moment you have two more than one

118:57

statement you have to make sure that you

119:00

put the curly bracket so you open the

119:02

packets and you closed it and this thing

119:05

was very very important whenever you

119:06

have if or in future when we work with

119:09

loops and some Advanced concept as well

119:12

at that point if you have multiple

119:14

statements you have to put curly

119:16

brackets if it is only one statement you

119:18

can skip it

119:20

okay so yeah that's it from this video I

119:23

hope you enjoyed it before that

119:25

uh let me just

119:26

go and you can see we got the output so

119:29

that's it from this video in the next

119:30

video we'll try to expand this logic

119:32

more because in this video we were able

119:34

to compare two numbers what if you have

119:36

three numbers

119:37

that will be challenging right and if

119:39

you want to you can just try with three

119:41

values without watching the next video

119:42

and let's see how you do it see you in

119:45

the next video so we were able to

119:46

compare two values now it's time to

119:48

compare three values so let's say we

119:51

have int x equal to 8 into y equal to 7

119:54

and int let's say Z is equal to let's

119:58

say five or six let's see and I want to

120:02

print the greatest of these three now do

120:05

you what do you think what could be the

120:06

solution here

120:07

see one thing we can do is we can

120:09

compare all the values but at one point

120:11

you can compare only two values right

120:13

example we can say x is greater than y

120:15

we can't simply say x is greater than x

120:19

y and J okay that will be invite syntax

120:22

let me just show you that if I try to

120:24

compile this code you can see we got the

120:26

error so what could be the solution of

120:30

course we can't do this but we can do

120:33

one more thing here can we say when when

120:36

we are saying let's compare for X so at

120:39

one point you can compare if x is

120:41

greater than y and J both

120:44

and we have done that right so in one if

120:47

condition we can say x is greater than y

120:50

and x is greater than Z okay but then

120:53

don't you think these are two different

120:55

conditions and if you want to combine

120:57

them and I want both of them to be true

120:59

in this case we can use and and we have

121:01

seen this right

121:02

so basically we are doing an and

121:04

operation here for this condition and

121:07

this condition so if this returns true

121:09

and this returns true it will execute X

121:12

in fact you know just to simplify this I

121:14

just I have put the

121:17

curly package in the last video let me

121:19

just remove that

121:21

just to keep it simple

121:23

and now you can see we got this

121:25

condition so if this is true it will

121:28

execute X that means X is greatest that

121:31

makes sense right but what if in fact

121:33

let me just run this code if this works

121:35

properly

121:36

uh clear first and then compile

121:40

and

121:42

run you can see we got 8 because 8 is

121:45

greatest

121:46

but what if the value of Z is nine and

121:50

if you compile this code now and run it

121:53

says seven oh that's weird that means

121:55

the code is not complete see at this

121:57

point if if is false I mean if by any

122:01

chance if this is false that means we

122:03

now we have to go for Y and Z we have to

122:06

compare them how will I do that

122:09

this was simple how will you compare the

122:11

gain because in else we don't put a

122:13

condition right

122:15

but what if we can so what if we can say

122:18

else and again we can put an if

122:22

and in this if we can say Y is greater

122:25

than x

122:27

and or not this and the logical and

122:32

and then Y is greater than Z so

122:35

basically what we're doing is in the

122:37

first case we are checking for x if x is

122:39

greatest the second case we are checking

122:42

for y if Y is greatest at this point let

122:45

me just make y the greatest which is 17

122:47

so of course it should print 17

122:50

run you can see we got 17 but what if

122:54

okay first of all let's do a draw it on

122:56

here so what happens is

122:59

it starts from this condition if the

123:01

value of x is greater than y

123:04

if that is true it will check for the

123:06

next one and as you can see here itself

123:08

it is getting false so the moment you

123:10

get false here it will not execute the

123:13

block what it will do is it will go next

123:15

it will say okay we got else here

123:18

it will execute the else block but an

123:20

else is not a simple else it is else if

123:23

one more condition here which is

123:24

checking for y if Y is greatest it will

123:27

print y but what if Y is not greatest it

123:31

is z here but still if you compile this

123:34

code you will get nothing you know why

123:36

is because we are not printing Z

123:37

anywhere so what you can do is you can

123:40

you can do one more else here for Z so

123:43

if this two are false of course you will

123:46

execute the else block I can just simply

123:48

copy this and paste it here in Aisha of

123:51

saying copy paste I can I can say code

123:53

reuse that makes much more sense and I

123:55

can say Z and now if I can compile and

123:59

run you can see we got nine

124:03

now this makes sense right so if both

124:05

these conditions are false it will

124:08

execute the S block okay and that works

124:12

now you can just change the values in

124:14

your machine first of all copy the code

124:16

and whenever you change the values it

124:18

will surely work but there's one more

124:20

thing

124:22

if you can think about this when you

124:25

execute this one and if this gives you

124:27

false don't you think the x is small now

124:30

compared to other two values

124:33

so in that case when you check for y why

124:37

we have to even compare a y with X and

124:40

if x is smaller right so now we just

124:42

have to compare y with C we are just

124:46

saving one in one of one operation here

124:48

and it's very important when you say one

124:50

one operation you are saving time you

124:53

are saving resources so try to try to be

124:55

a efficient programmer not just a

124:57

programmer and that's what you do you

125:00

check what is required which is not

125:01

required and remove all the unnecessary

125:04

code and once we have done this compile

125:06

the code and run you can see we got nine

125:08

and you can change the values it will

125:10

surely work so what we have done in this

125:12

video is we have talked about if else if

125:15

else and that's how it works now once we

125:18

have talked about conditional statement

125:20

in fact in that we have talked about if

125:22

and then we have also seen how do we use

125:23

if else and then we have worked with if

125:26

else if now for certain use case we have

125:29

a shortcut for it and it is called a

125:31

ternary operator now if you want to use

125:34

if else on the same line or if you have

125:36

a shorter condition and if you can write

125:38

out that on one line why not so what I'm

125:41

saying is let's say if you want to check

125:43

if a given number is even or odd how do

125:46

we do it it's very simple what you do is

125:47

you uh first of all you take a number

125:49

let's say another number is 4 and if I

125:52

want to check it this is even odd so

125:54

using IF else how you do it is we say if

125:56

and we check for the condition if n mod

125:58

2 is equal to equal to 0 that means it's

126:01

an even number right so what you do is

126:03

you come back here and you can you don't

126:05

need curly brackets because I want to

126:06

add only one statement and I want to

126:09

store that in a variable now basically

126:11

we can use a Boolean type here or we can

126:13

use a string as well well so let's say

126:16

I'm using a string okay and based on

126:19

this string or maybe let's let's keep it

126:21

simple so what I will do is I will not

126:22

print even or what I want to do is I

126:25

want to set the value which is in X or

126:27

maybe I will put that in result and I

126:30

want to put the result by default it

126:31

will be 0 but based on this if the

126:34

number is even or odd I want to set the

126:37

value for result if the value if the

126:39

value is even I want to set the value

126:42

for result as 10 if the value is let's

126:45

say odd I want to set the value for

126:48

result as 20 okay so for even it is 10

126:51

for odd is 20. how do we do it so in the

126:54

if condition you can do you can say

126:56

result is equal to 10. so if that's a

126:59

even number user result equal to 10 and

127:01

if in the else part you can set result

127:04

as 20. so what we are doing here is

127:07

based on some condition I'm assigning a

127:09

value now this is one way and of course

127:10

at the end I can print the value of this

127:13

as well I can say I want to point result

127:16

nothing fancy came simple stuff and then

127:19

I want to compile this on once I will

127:21

say Java C demo dot Java and Java demo

127:25

and you can see the output is 10 why the

127:27

output system is because the phone here

127:29

is a even number what if you make it

127:31

five now we are making it odd right so I

127:33

can just go back here compiled one you

127:35

can see we got 20. so this is working

127:37

now what you can do is you can convert

127:39

this code into something called a

127:41

ternary operator now how it how it works

127:43

let me just comment this section

127:46

see ultimately what we are doing is we

127:48

are saving the value in result variable

127:51

in both the thing we have the same thing

127:52

right

127:53

if it is if is it true then we will set

127:56

the value for result if it is false for

127:58

the else part also as a result so can we

128:00

do this can we say result is equal to

128:02

the result can be 10 or it can be 20

128:04

right but how will you do it how will

128:06

you check for the condition so what you

128:07

can do is you can directly run a

128:09

condition here so you can see if n mod 2

128:12

is equal to equal to 0 if this is true

128:15

then we have to do something or or will

128:17

do something so we can use something

128:19

called a ternary operator here now how

128:22

it looks like so turning up it looks

128:23

like this let me just write that in

128:24

comments so we have to use a question

128:26

mark and a colon so what is this

128:28

question mark and colon so whatever

128:30

condition you have it might return a

128:32

true or false so you can put a question

128:35

mark you can ask hey is it true or false

128:37

if it is true then execute the part

128:40

which is after the question mark which

128:43

is 10. if it is false you have to

128:45

execute or you have to take the value

128:47

which is after the colon okay I will

128:51

just give a space here so that it will

128:52

be much more readable right so what we

128:54

are doing is we are saying for this

128:55

condition if this is true in that case

128:58

we turn 10 if this is false in that case

129:01

return 20. so you can see out of all

129:04

these four lines of code we are doing

129:06

that in one line with the help of

129:08

tunnelio operator of course it is not

129:10

suitable for every evils it's not like

129:12

you will say hey from now I'm not going

129:15

to use zfl that's not that's not the

129:16

case this is suitable for some examples

129:18

this Theory it makes sense because we

129:21

have a very short assignment and

129:23

ultimately you are sending value to

129:25

result so this makes sense I can just

129:27

compile and run you can say we got 20

129:28

because the value is odd but if you make

129:31

the value as even let's say 4 again

129:33

and if I compile and run you can see we

129:36

got 10. so this is how you can basically

129:38

use a ternary operator now if you

129:40

remember what we have used if else if

129:43

you can also do that here I would

129:45

request you to try that put one more

129:47

condition here and try it out for

129:49

multiple use cases in this video we'll

129:52

talk about switch statement now to

129:54

understand switch statement we have to

129:56

go for an example so if you go back to

129:58

your code what we have here is let's say

130:00

we have a integer value and the value of

130:02

n is 1. now the value of n could be from

130:05

1 to 7 and might be you have guessed it

130:08

so when I say one two seven it might be

130:11

the number of days and you are right so

130:14

basically what I want here is if the

130:16

value of n is 1 I want to print Monday

130:20

if the value of n is 2 I want to print

130:24

Tuesday so based on the value of n which

130:27

can be from 1 to 7 I want to print the

130:30

day of the week how do we do it so of

130:33

course you can just simply print the

130:35

values right you can say hey I want to

130:37

print Sunday and you can just uh copy

130:40

paste it or maybe copy pasting is not

130:42

the right word you can say uh code reuse

130:45

so just ignore this xci just just

130:47

suggestion given by the vs code we can

130:51

say this is Monday and we can just paste

130:54

it multiple times so this is Tuesday

130:57

this is Wednesday Thursday Friday and

131:04

Sunday you know what I want to keep

131:06

Sunday as a last day because that's what

131:08

we say right weekend the Mondays the

131:09

videos will start from Monday okay so I

131:12

want to print this based on the value of

131:15

n at this point if you just run this it

131:18

will print it will print all the values

131:21

I don't want to print all the values I

131:23

just want to print uh one day based on

131:26

the value of n so of course you can do

131:28

one thing you can just check with the

131:30

help of efl so you can check if the

131:32

value of n is 1 you can just print

131:35

Monday if the value of n is let's say

131:39

you can say else a fear if the value of

131:42

n is 2 in that case I can print uh

131:46

Tuesday right we can do that at least

131:50

let's do between Monday and Tuesday

131:51

because that's what you're putting into

131:52

effects and if you run this code with

131:55

that changes you can see this printing

131:57

Monday it is not printing Tuesday

131:58

because Tuesday is in if else and you

132:01

can apply that in all the conditions

132:03

here and all the statements you can

132:05

simply put if else and you can check for

132:07

n is equal to 3 and then list goes on

132:10

right this is one way

132:13

there is another way using which you can

132:15

do this in fact you know what I can do I

132:16

can just simply complete this uh code

132:19

and here it should be 3 4

132:23

5

132:24

6. you know for seven it actually makes

132:27

sense to not even mention the condition

132:29

because if none of it is matching of

132:31

course it will be Sunday right uh so if

132:33

there's no office that Sunday okay so

132:37

basically this is what you can do here

132:39

uh but don't you think there should be a

132:41

good way or better way of doing this so

132:43

instead of doing if else everywhere and

132:45

at the end of else we have a good way

132:48

which is called a switch now what switch

132:50

will do example when we talk about

132:52

switch it will switch between the ports

132:54

right uh example in networking concept

132:57

as well we use something called switch

132:58

so here what you can do is we can use a

133:01

switch now based on a value it will

133:05

execute a particular case and that's why

133:07

most of the time we say it's a switch

133:09

case statement

133:10

okay just follow me so what I can do is

133:13

instead of using this if else I can

133:15

simply rewrite everything in a switch

133:17

block I can say switch switch and in

133:21

this switch you can pass the value which

133:22

is n in this case and then you can write

133:24

everything in this block okay how do we

133:26

mention that in this blog I have written

133:28

Capital switch my bad what you can do is

133:31

you can put all this thing inside this

133:34

block the only changes instead of saying

133:36

if

133:37

we we don't for the condition we don't

133:39

fully check for the condition we just

133:40

match we try to match the case it can be

133:42

something like this we can say case one

133:44

colon so whatever value you have passed

133:46

if this is matching with the case

133:48

example if n is matching with a 1 it

133:51

will execute this statement same applies

133:54

here in fact you know I just want to

133:56

give a tab to everything else and then

133:59

here I can just say case to

134:03

for this and let me just complete this

134:05

thing for all

134:06

okay and then of course the number has

134:08

to be changed so we can say this is

134:09

three this is 4 this is 5 this is 6 and

134:13

this is seven okay so we are trying to

134:16

match with all the cases what I will do

134:18

is I will start with seven and let's see

134:20

what happens the moment you make it

134:22

seven we are basically trying to match

134:24

the cases so what happens is when you

134:26

execute this it will go to the switch

134:28

statement and switch says okay I got the

134:30

budget which is seven and now with this

134:32

7 I'll try to match with all the cases

134:34

here so it will check if the value which

134:37

is passed which is 7 is it matching with

134:39

one no it will not execute the block is

134:41

it matching with two no is it matching

134:43

with three no this goes on it goes on it

134:46

matches with seven the moment it matches

134:48

it will execute this statement which is

134:50

print Sunday and it will do that so if

134:53

you run this code example if I compile

134:55

this and run you can see we got Sunday

134:58

simple stuff right and this one looks

135:00

much better than the efls because we are

135:03

checking for the condition multiple time

135:04

here we are matching the case Okay so

135:07

this looks better right and it also

135:09

works better compared to e-fills but

135:11

those of you there's only one issue here

135:13

the issue is what if the value of n is

135:17

2. now what do you think the output

135:18

would be think about this and maybe in

135:20

your mind you already have an answer so

135:22

what I will do is I will just compile

135:23

this code and run this code oh that's

135:27

weird we were expecting Tuesday but we

135:29

got all the values the reason is when

135:31

the moment your switch case matches with

135:34

a case example it will check do is it

135:37

matching with one no it will not execute

135:39

this block is it matching with two yes

135:42

the moment it matches the case it will

135:45

execute the block for sure and it will

135:47

just try to complete the entire switch

135:50

statement it will execute all the cases

135:52

it will not try to match again because

135:54

it's already matched oh that's an issue

135:57

right so what I want is I can simply ask

136:00

my switch hey you know once you match

136:02

with one of the case just come out of

136:05

the switch block and the way you can do

136:07

that is by using a special keyword

136:10

called break now break is very important

136:13

because this will be used multiple times

136:15

for different concept what we are doing

136:17

with break here is it says simply come

136:19

out of the block now once you match with

136:21

any of the case just come out and of

136:24

course for that you have to put break in

136:26

all the cases I will just copy this I

136:28

will try to reuse the code here and

136:30

paste it everywhere okay now what we are

136:33

doing is the moment it matches with the

136:34

case it will jump out let's try this now

136:37

compile run you can see we got Tuesday

136:40

it is not printing all the other blocks

136:42

because the moment you print Tuesday it

136:45

will simply jump out because we are

136:47

saying break that looks cool right now

136:49

now there's one more important thing see

136:51

for the last case we are still checking

136:53

right or maybe we can have one more case

136:56

what if the value is 8 is not in one

136:59

coming in any of the case here in that

137:01

case what you can do is you can do

137:03

something called default case if none of

137:05

the cases are matching you can do

137:07

default and you can print enter a valid

137:11

number I mean you can print any

137:12

statement you want I'm just saying enter

137:13

the valid valid number and you can see

137:16

their value here is 8 so it should print

137:18

enter the value valid number and that's

137:20

what you got enter the valued project

137:22

number so this is how basically you can

137:24

use this switch statement

137:26

now with the recent versions of java you

137:30

can actually avoid using break there's a

137:32

different syntax we can use but again

137:33

that will see in a separate video of

137:36

advanced Java concept where New Concept

137:38

has been introduced in new versions

137:39

right but at this point this perfectly

137:42

works and in most of the companies they

137:44

are still into Java 8 so let's stick

137:45

with the syntax so try it out with

137:47

different examples and you will be good

137:49

to go one of the most important thing

137:52

about programming is you can do certain

137:54

things repeatedly of course we have

137:57

talked about different things now in

137:59

normal software development basically

138:00

you work with data and that's why we

138:02

have variables so basically you can

138:03

store your values in normal application

138:06

you decide based on some values and

138:09

that's why we have seen if else we have

138:11

seen internally operator we have talked

138:13

about switch statement but then there

138:15

are certain use cases where you might

138:17

want to repeat the same thing multiple

138:19

times in fact there are a lot of memes

138:21

available which says you know in our

138:23

school time when when teachers used to

138:25

say write is 100 times

138:28

what if you can just do that with the

138:29

help of software write the same sentence

138:32

multiple times

138:34

not manually but automatically it should

138:36

reiterate it should iterate itself

138:39

and the way you can do that in

138:40

programming is with the help of loops

138:43

Let me Give an example here

138:45

so let's say I have a code here and I

138:47

want to print something let's say I want

138:49

to print here hi and I want to print

138:51

this High multiple times not once but

138:54

multiple times example if I run this

138:56

code now it will only print hi once

139:00

if I want to print that multiple times I

139:02

can just copy this and I can just paste

139:04

it multiple times here let's say four

139:06

times and if I type it four times of

139:08

course it will print the output in four

139:11

times we don't want four times what we

139:13

want is we want to write this once and

139:16

it should call itself multiple times

139:18

the way you can do that is by removing

139:21

this part

139:22

and by doing something maybe I can just

139:24

say uh I can just write in comments I

139:27

can say repeat this statement

139:31

four times maybe it's something like

139:33

that of course this will not work

139:35

because comments are normally ignored by

139:36

the compiler but how do you do that how

139:39

do you mention Java hey I want to run

139:40

this multiple times

139:42

and that's where the concept of Loops

139:44

comes in a picture

139:46

so we have a concept like Loops which

139:49

will reiterate the same thing and there

139:51

are multiple Loops available in Java

139:52

with different languages we have the

139:54

same thing or we can say similar thing

139:56

so we have something called a while loop

139:58

we have do while loop and then we have a

140:02

for Loop now mostly we use a for Loop

140:04

but then we'll start with while and do

140:06

while and they have their own use cases

140:08

it's not like just because we have for

140:10

Loop will not be using while and do

140:12

while they have their own use cases now

140:14

just to give you a hint

140:16

maybe you want to repeat certain things

140:18

multiple times but then what will be the

140:20

condition maybe you want to print that

140:21

100 times

140:23

that means there is a finite number five

140:25

times ten times eight times fifteen

140:28

times twenty times 25 times 100 times

140:31

or maybe you want to do that do

140:34

something based on some condition

140:36

example uh do something till I press the

140:40

enter button do something till the

140:42

system is alive okay something like this

140:44

so maybe you want to do something finite

140:47

times or maybe you only do something

140:48

based on some condition uh and for that

140:51

we have different types of Loops

140:52

available

140:53

so what is loop basically if you want to

140:55

do the same thing multiple times you use

140:56

Loop and there are multiple Loops

140:59

available while loop do while loop and

141:01

for Loop and there are multiple versions

141:03

of for Loop available but we'll see the

141:05

simpler version which we call normal for

141:07

Loop and yeah that's what we have will

141:11

explode all these Loops we have talked

141:13

about the need for Loops right and so we

141:16

have decided that if you want to repeat

141:18

something Loops can be a good thing and

141:21

we also talked about there are different

141:22

types of Loops right we have while loop

141:24

do while loop for Loop in this video

141:25

let's try to understand the first Loop

141:27

which is while loop so we'll go over

141:30

this go for the same statement here if

141:31

you can see we are Printing High and

141:33

again as I mentioned before ignore this

141:35

x this is a suggestion given by your vs

141:37

code uh this is what we are typing just

141:40

high and what I want to do is I want to

141:42

print this High multiple times so what

141:46

we can do is we uh we can say Hey you

141:48

know Java I want to print this multiple

141:51

times Java will say okay

141:53

the first thing you do is you put this

141:55

in a block of course it's not compulsory

141:57

when you have one statement but just to

141:59

understand just to get started we are

142:00

putting that in a block now and then of

142:03

course we have to follow into

142:03

indentation as well and we are doing

142:05

that now what we can do is we can say

142:07

Hey you know Java I want to run this

142:10

five times so Java will say okay if you

142:13

want to repeat you have to use a keyword

142:15

called while now in English as well we

142:18

normally use while to say repeat

142:20

something or while it is true do that so

142:23

here we can say while it is what in this

142:27

bracket you have to mention is it true

142:29

or false now basically that's how Wild

142:31

works so if you say true it basically it

142:34

will just keep printing this High

142:36

because we are saying true it says okay

142:38

while I'm true I will keep printing it

142:40

so that's why if you just run this code

142:42

by just by mentioning the

142:45

while

142:47

you can see we are getting high I just

142:49

keep printing okay it's it's not

142:51

stopping if you can see if I'm trying to

142:52

scroll down it is continuously happening

142:55

and maybe it will uh fill my Ram it will

142:59

crash okay I don't think so but let me

143:01

stop this execution otherwise there will

143:03

be an issue so what we can do is I don't

143:05

want to continuous this for a long time

143:08

I want to do that for a particular

143:10

condition maybe I want to say I want a

143:13

counter which will start from one and it

143:15

will stop at five oh when I say counter

143:18

it means I need a variable where I can

143:20

store the value so what we can do is

143:22

here I can just come up and I can say

143:24

int I is equal to let's say one now most

143:28

of the time when you use Loops you will

143:29

be using a variable name I it's not

143:32

compensative we can use any variable

143:33

name but it makes sense right because

143:35

normally when you go inside the while

143:38

you are executing something you are

143:40

changing the values of the eye

143:43

you basically increment it right so it

143:46

makes sense to call it as a iterator

143:47

which will iterate so I iterator makes

143:51

sense and that's why most of the time

143:52

you will say i j but as I mentioned you

143:55

can use any variable of your choice so

143:57

we are using I here which is I is equal

143:59

to 1 and then what we can do is every

144:02

time you run this loop after Printing

144:05

High you can just increment the value of

144:07

I

144:08

oh but then how do we increment and we

144:10

have seen that right we have a concept

144:12

of I plus plus so I plus plus will

144:14

simply increment the value of I okay

144:17

that makes sense and oh how do I check

144:22

for the condition see it makes sense now

144:24

because I is 1 and every time you run

144:27

this Loop the in the I value will

144:28

increase right so if you run this while

144:30

for let's say five times it will

144:32

increment the value of I let me show you

144:35

what I will do is I will print hi as

144:37

well as I will print uh maybe you can

144:40

you can print both the things I say on

144:42

the same line you just have to give a

144:44

space after high and I can give a plus

144:46

symbol so when you want to concatenate

144:48

two different things High and a number

144:51

we can use a plus operator yeah plus

144:53

Works in that way it works for addition

144:56

as well as it works to concatenate two

144:58

things

144:59

and here I'm writing I now let's see

145:03

what's the output is again I have not

145:04

changed the condition so it will print

145:06

infinite times

145:07

but I just want to see what happens if I

145:09

compile this code

145:11

and run you can see the value of I is

145:14

incrementing continuously

145:16

and you can see it so fast depend upon

145:18

your machine speed as well and let me

145:19

know when you try on your machine how

145:22

fast it is so what I want to do is I

145:24

want to stop this somewhere so we have

145:26

to apply a condition how do I stop it so

145:28

we have to say if I is less than equal

145:31

to 4 so what it will do is it will check

145:34

for the condition remember we have

145:35

talked about this in relational

145:36

operators it will continuously check for

145:39

the condition so what how it runs is

145:41

initially it will set the value of i as

145:43

1 and then in the while loop it will

145:45

check if the value of I which is 1 is

145:49

less than 4 yes it will continue it will

145:52

go inside the loop it will print high it

145:55

will also print the number which is 1

145:56

and then it will increment the value of

145:59

I what next it will go back to while

146:02

loop because and again it will check for

146:04

the condition because that's a while

146:05

right it will continuously happening it

146:07

will again check for the condition where

146:09

it says okay I value now it's 2 so 2 is

146:12

less than 4 yes and it will continue at

146:14

at the end when the I value becomes 4 it

146:18

will check 4 is less than equal to 4 yes

146:20

it will print High and 4 as well so we

146:22

got four iterations and then it will

146:25

increment the value of I which is it

146:27

will make it five it will go back here

146:30

and it will check is 5 less than equal

146:33

to 4 false and that's where it will jump

146:36

out of the loop so remember this thing

146:39

the moment your while gets a false here

146:41

it will not execute this block cool

146:43

right now after saying all those things

146:45

let's try to compile and run and you can

146:48

see we got high one high two High three

146:50

and high four that's how while loop

146:52

works now with this I have a question

146:54

for everyone here and the question is

146:56

what if at the end I'm just saying as

147:00

out and I'm saying buy and with this by

147:04

I'm printing the value of I what do you

147:05

think what will be the output of I

147:07

okay try to guess it I'm just running

147:09

this code to show you the output compile

147:12

run you can see we got all the values

147:14

here but with by you got five you know

147:17

why is because

147:19

we got false here when the value of I is

147:23

5 and that's why you got the value here

147:26

now since I is declared outside the loop

147:29

the value will be maintained okay now

147:32

one more thing here I want to run this

147:34

in a debug mode let me show you how it

147:35

works in debug mode so what I can do is

147:37

I can go to run

147:39

and I can say start debugging okay so

147:42

there are some options okay for

147:43

debugging you have to basically you know

147:45

uh you have to set the checkpoints what

147:48

I will do is I will just set the

147:49

checkpoint here so basically debug is an

147:52

awesome feature in programming where you

147:54

can set the checkpoint this is where you

147:56

want to iterate one by one so normally

147:58

when you run the code it will directly

148:00

give all the output right what if you

148:02

want to see the steps so it makes sense

148:04

let me just clear this and again I will

148:06

go into run

148:07

and get into the debug mode okay so you

148:10

can see uh we are in debug mode now and

148:14

here on the left hand side we got

148:15

variables we got local a and I we have

148:18

two variables you know why we got a is

148:20

because of this a variable here but it

148:22

is at this point we want to ignore that

148:23

we want to focus only on I variable and

148:26

the value of I at this point is one you

148:29

can see that I is one but now if I say

148:33

step into you can see we got

148:35

system.winder it will print the high and

148:37

I the value of I which is one uh okay I

148:40

want to go step into now it will

148:42

increment the value of I you can see we

148:44

got the output as well we got i1 and

148:47

then when I say step over it will go for

148:50

the next step and you can see before

148:52

executing it the value of I is 1 but

148:54

once I execute that statement the value

148:56

of I now becomes 2 okay so that it went

148:59

back to while loop and this will keep

149:01

happening for I value 2 I value 3 I

149:04

value 4 as well the moment your value of

149:07

I become 5 here you can see the value of

149:09

I is 5 now it is checking and it falls

149:12

it it will jump out of the loop it will

149:14

print by with the value 5. so that's how

149:17

your while loop Works in fact what you

149:20

can also do is we can run a nested Loop

149:23

here now how an estate Loop looks looks

149:24

like let's say I want to print i1 and

149:28

then I want to do something else maybe I

149:30

want to print hello three times after

149:33

every high so let's say I'm Printing

149:35

High here and then I want to print hello

149:37

as well

149:38

and I don't want to print the value of I

149:40

anymore

149:41

or maybe let's print the value of I

149:42

let's see what happens if you keep the

149:44

value of I here and then incrementing it

149:47

here as well that makes sense but now

149:49

what I want to do is I want to

149:52

after printing this High I want to print

149:54

hello five or three times now in that

149:56

case I can simply say hello and I can

149:59

write the same statement multiple times

150:00

right that's how the loop works right so

150:03

I will say hello hello and you can see

150:05

we got hello three times but don't you

150:07

think we are doing the same thing

150:08

multiple times so it makes sense to put

150:12

that in a loop so I can simply say while

150:15

oh but what should be the condition can

150:18

I say I is less than equal to 3 no right

150:21

because see I want this hello three

150:24

times for every high so high we are

150:27

getting four times and for every High I

150:30

want Hello three times

150:32

so the same value will not work in that

150:34

case what you have to do is you need to

150:36

work with a different variable and I

150:39

will create one more variable here which

150:40

is called J and the value of J initially

150:42

will be 1

150:43

and then every time you execute this

150:46

while loop where it prints High

150:48

it will jump inside here it will set the

150:51

value of J as 1 it will set the value it

150:53

will check for the condition where J

150:55

should be less than equal to

150:57

3 and then every time you do that let me

151:00

open the curly brackets

151:02

and we can put this hello statement in

151:05

here so we don't have to put this thing

151:08

now I can just simply say hello and of

151:11

course I want to print J plus plus as

151:13

well and remember this thing we are

151:16

saying J plus plus in the inner while

151:18

loop and then we are saying the I plus

151:21

plus in the outer loop okay if you want

151:24

to understand this with an analogy uh

151:27

just go with an example of days in a

151:30

week so we have multiple days in a week

151:32

right so we have Sunday Monday Tuesday

151:33

Wednesday Thursday Friday Saturday

151:35

Sunday I've already mentioned you know

151:38

we we basically want Sunday twice a week

151:41

but anyway so if we talk about these

151:42

days every day we have 24 hours right so

151:45

if you say this is 1 pm on Monday again

151:48

the Monday gets over the clock will

151:50

start at zero right then zero one two

151:52

three four five six and then it

151:54

continues right as the date changes the

151:57

clock repeats in the same way once your

151:59

eye is incrementing it's like a days

152:01

Monday Tuesday the inner loop you can

152:04

imagine that's as a number of hours it

152:06

will keep incrementing but after three

152:08

it will go back to one because it will

152:10

complete the inner loop it will go back

152:12

to Outer Loop and again this is again

152:14

the value of J will become one let me

152:16

show you how it works let me just

152:17

compile this code compile and

152:21

run you can see we got high uh we are

152:24

still printing one here you know what I

152:26

will do I will also print J so that you

152:28

can see the value of J as incrementing

152:31

it

152:32

and let me give a space here as well let

152:34

me say clear

152:36

compile run so you can see we got high

152:39

one and then it is going for the inner

152:41

loop now so once you execute this High

152:43

it will go for the inner loop where we

152:45

are saying J is less than 3 so basically

152:48

it will print this statement three times

152:50

because the value of J is less than

152:53

equal to 3 so it will iterate three

152:55

times

152:55

and then again you have to go for the

152:57

outer loop so the the moment this is

152:59

false it will execute I plus plus where

153:01

it will make I2 it will go back to the

153:05

upper loop again it will say okay now

153:07

the value of I is 2 is 2 less than equal

153:10

to 4 yes it will go inside it will print

153:13

the value of high which is high 2. again

153:16

it will make the value of J as 1 and

153:19

that's why it is incrementing again

153:22

cool right so this is basically called a

153:25

nested while loop

153:27

so that's it from this video where we

153:29

have talked about uh how to create a

153:31

while loop how to use a nested Loops

153:33

using uh while loop now once we have

153:36

seen while loop it's time to move

153:37

towards a do while loop in fact before

153:40

we proceed first of all I want to remove

153:42

this breakpoint because we don't want to

153:45

debug anymore and I want to remove the

153:47

inner loop because we want to focus only

153:49

on the Outer Loop now

153:51

using this Loop we were Printing High

153:54

four times and then we are printing buy

153:57

as well let me also remove buy for now

153:59

and with this I just want to run this

154:01

code once again to see what happens

154:03

compile

154:05

run and you can see we got the same

154:07

output right

154:08

okay now what I want to do is uh when

154:10

you talk about while what happens is

154:12

when you talk about this line which is

154:15

line number seven it will assign the

154:17

value of i as one wait go for the next

154:19

line which is line number nine here

154:21

it will check for the condition if it is

154:24

true it will execute this block that

154:27

means if this condition is true then it

154:30

will execute otherwise if this condition

154:33

is false it will not execute the block

154:36

that actually makes sense right that's

154:38

that's how the loop should work but

154:40

there are some certain situation where

154:42

you want to execute this block even if

154:45

your condition is false at least once I

154:47

mean you want to earn this at least once

154:49

maybe something like uh you want to send

154:51

a message to your friend and if the

154:55

service is not available from your

154:57

friends and at least you should be able

154:59

to send a message once at least

155:01

okay so that you can get the reply the

155:04

user is not available or something like

155:05

that so what I want to do is I want to

155:07

execute this block at least once even if

155:10

the condition is false now when a second

155:12

condition is false what it means is

155:13

example is the value of I is 5. now when

155:16

you make the value of i as 5 and if you

155:19

run this code you can see will not get

155:21

any output is because the condition is

155:24

false

155:26

so what we can do is can we just cut

155:28

this part from here and put it after the

155:32

block

155:33

we can do this and we can just also give

155:36

a semicolon here because that's how you

155:38

end this statement normally when you

155:40

write a while at the start the curly

155:43

brackets becomes a statement but then if

155:45

you write while after the block we have

155:48

to end it first by the semicolon and we

155:50

have to add one more which is do so we

155:52

have to say do while

155:55

now what it does is it will execute the

155:58

block at least once even if your

156:00

condition is false now if you can see in

156:03

this situation first of all let me just

156:04

go with one again and let's see what

156:06

happens if you're on this code compile

156:08

run you can see we got the normal output

156:10

and this the same output we got when we

156:12

had a normal while loop as well but here

156:16

if you make this condition false I mean

156:18

if you say I is 5 now which is a false

156:22

con which will give you false here

156:24

but if you run this code or compile this

156:26

one you can see at least you will get

156:28

the output once this is important so if

156:32

you want to get the output even once for

156:34

even if your condition is false you will

156:36

be using two while so yeah that's the

156:38

main difference between while and do

156:39

while otherwise it works the same way it

156:42

executes uh check for the condition

156:44

again execute check for the condition

156:46

and list and the flow go on now once we

156:49

have talked about do while loop and

156:51

while loop let's talk about the next

156:52

type of loop which is a for Loop now if

156:55

you go back to while loop so let me just

156:57

remove do here and let me put this back

157:00

here and okay let me remove the

157:03

semicolon as well there is one more

157:05

thing if you talk about this while loop

157:06

if you can see we have three things here

157:08

the first one is we have initialization

157:11

we have a condition and then we have a

157:14

increment so basically we are doing

157:16

three things on three different lines

157:18

and if we talk about any finite Loop so

157:20

when I say finite Loop it means it is

157:22

starting somewhere it is ending

157:23

somewhere and we know when it is going

157:25

to end there are some Loops where you're

157:28

not sure when it was going going to end

157:30

example when the user says enter it will

157:32

end maybe the network goes off it will

157:35

end so based on some condition it will

157:37

end maybe you are trying to read a file

157:39

so in a file you let's say you have a

157:42

thousand characters and we don't know

157:44

how many characters we have so when you

157:45

open the file and when you try to read

157:47

each character so the the loop will end

157:50

when the characters end in the in the

157:52

file so we are not sure that's why I'm

157:54

saying you it can be uh not a infinite

157:57

Loop but somewhere we cannot determine

157:59

when the loop will end so at this point

158:02

we can determine right but anyway so we

158:04

have three things here we have a counter

158:05

we are initializing it we are checking

158:07

for the condition and then we are

158:08

incrementing it so what if we can just

158:11

write all these things in one line what

158:14

I'm saying is can we just pick up this

158:17

thing from here cut and paste it here

158:19

itself so on the same line we are

158:22

initializing the variable

158:24

and on the same line I'm give I can give

158:26

a semicolon I can say I plus plus

158:29

and of course we don't have to do that

158:30

here now

158:31

the only thing is while loop does not

158:34

support this syntax while it says I will

158:36

only for check for the condition

158:38

but then if you want to use this type of

158:40

syntax you have to use another type of

158:41

loop which is called a for Loop so in a

158:43

fourth Loop you can mention three

158:45

different statements in one line mind

158:47

you these are three different statements

158:49

why is because we are using semicolons

158:52

in between so this is one statement here

158:55

this is another statement and this is

158:57

another statement

158:59

and you can see I'm just pointing High

159:01

here the same the same code

159:03

I just want to check if this works

159:05

compile

159:07

run oh we have not got any output oh is

159:11

because the value of I is still 5 let's

159:13

make it one and by doing that I can just

159:16

compile this code and run you can see we

159:18

got all the values make sense right so

159:21

how a for Loop works so we have to set

159:23

the initial value which we have which we

159:25

have done here and then we have to check

159:27

for the condition and then we have to

159:29

increment or decrement so let's say I

159:32

want to print that a decrement order I

159:34

mean I want to print 4 3 2 1 in that

159:37

case I will start from 4 and then I have

159:40

to end

159:42

the eye should be greater than and equal

159:44

to 1 and I plus plus so the I minus

159:48

minus because we are decrementing it

159:50

compiled one you can see we got high

159:52

fourth High 3 High 2 and high one so

159:55

this works okay can we do something more

159:57

here actually we can uh what if you want

160:02

to start let's let's again go for the

160:04

increment part so let's say I'm going

160:06

for one and it should be less than equal

160:08

to 4 and then I'm saying I plus plus

160:12

see most of the time we'll start with

160:13

one itself right so that's how your uh

160:16

normal number system works as a human we

160:18

do that right but computer normally

160:21

start there counting from zero it's

160:23

because of the binary format uh because

160:25

then by the way we have zero to one in

160:27

fact in the number system also we start

160:29

with Zero versus zero to nine right uh

160:31

in fact once we start with the array

160:33

concept there as well the number starts

160:35

with zero so in general in computers the

160:38

number system starts with zero so we can

160:41

also say let's start with zero

160:43

but the question is if I start with 0

160:45

where I have to end it is it four or

160:49

five or three because we want to print

160:51

only four times right so let's keep it

160:53

four it which is equal to 4 and then we

160:56

also have 0 here so let's try to compile

161:00

and run you can see we got uh five

161:03

values it starts from zero and it is

161:04

ending at four

161:06

okay that is an issue how do I stop it

161:10

at three I want to print 0 1 2 3 that's

161:13

it four four iterations so in that case

161:15

what you can do is you can stop it at

161:17

three now this is something which will

161:19

take time for you if you're doing this

161:21

for the first time when you're starting

161:22

with zero you have to end before that

161:25

you have to end before the count so if

161:27

you want to do it four times you have to

161:28

end it at three okay now let's try this

161:30

I will say compile and run you can see

161:34

we got four times now one of the common

161:36

syntax you might be seeing in most of

161:38

the examples or if you're if you're

161:40

referring a book or online content you

161:42

can also stop this as four but you have

161:45

to say less than four because you're not

161:47

including 4 here you're saying less than

161:49

4 not equal to 4. so this is one of the

161:52

common syntax you can say it starts with

161:53

zero and it ends less than four and now

161:56

if you try this you can see we got four

161:58

values make sense okay

162:01

uh now let's try something else so Loops

162:04

normally work well when you try to print

162:06

some pattern or when you try to do

162:08

something repeatedly so what I will do

162:09

is let's try something

162:12

so what I would try is I want to try

162:14

this one I want to try this one I want

162:16

to try to print the number of days

162:19

I will say one two three four five

162:22

six and seven because we have seven days

162:24

and also every day I want to print

162:27

uh 24 hours

162:29

that looks good right we have talked

162:31

about this example right so what I will

162:33

do is first of all I want to print all

162:34

the days so I will say one two

162:37

seven including seven so I have to say

162:39

equal to and then here I will just print

162:42

the number I will say okay I will say

162:44

day one two three four uh imagine day

162:46

one means Monday day two means Tuesday

162:48

and and so on so if I try to do that I

162:52

will say compile and run you can see we

162:55

got all the days now with days I want to

162:58

print the number of hours

162:59

okay I know that will be tricky or maybe

163:03

we can we can actually print from uh 9

163:06

00 am to 6 PM because that's that's most

163:10

of the work hours right so let's say if

163:12

I want to build a calendar in fact I

163:14

don't want to print till seven five

163:16

makes sense because uh we have sixth and

163:20

seventh day as holiday so let's try this

163:23

I will compile and then we got five days

163:26

in this five years I want to print from

163:27

nine a.m to 6 p.m how do I do that so I

163:32

can simply print with the help of

163:34

statements I can say uh this is one

163:38

or 9 am

163:40

right or I will simply say nine and then

163:43

I can say 10 I can just print this once

163:46

again so this is 10 and so on right I

163:49

can print it till 18 because 6 PM is 18.

163:52

if you go for 24 hours of clock uh but

163:54

then don't you think we are doing this

163:56

thing repeatedly I have to basically

163:57

type so many lines

163:59

so of course we can use a loop here

164:01

right so what if we can use a inner loop

164:03

we have talked about nested Loops here

164:05

we are doing it with four so I can say

164:07

for and I have to give a count right so

164:09

I can say J we'll start with from where

164:12

now this is tricky should we start J

164:14

from one we can so I can say one and J

164:17

less than so many hours we are doing

164:19

here we are doing nine hours right so I

164:21

can say nine and J plus plus right and

164:25

then I can just give a loop open and

164:29

loop close and then we can print the RS

164:32

here now what hours are to print I

164:34

cannot print nine because nine is not

164:37

nine it will print nine every time okay

164:39

example if I show you this

164:41

let's say clear in fact what we'll do do

164:43

you know before printing nine I will

164:44

give a tab so that it will give some

164:47

indentation before it Sprints this is

164:50

what I was saying you can see we got day

164:51

one and we got nine hours here but then

164:54

it is printing 999 I don't want to open

164:56

999. maybe I want to put an attendance

164:58

here or maybe a calendar where we can

165:00

Mark whatever what we are doing going to

165:02

do

165:02

so what we can do here is I want to

165:05

uh say okay I should not be printing

165:08

nine after this space

165:10

I can say I want to print let's say

165:13

value of J but J will print from one to

165:15

nine let me show you what I'm saying

165:18

clear

165:19

compile

165:21

run so you can see it is printing one to

165:23

nine I don't want to point one to nine I

165:24

want to print from nine to eighteen in

165:27

that case can I simply add this with

165:29

eight because it will start with one so

165:31

one plus eight is nine right which it

165:33

will start from nine and every time it

165:36

will increment the value of J so you

165:38

will get some other values I will just

165:40

compile this and run

165:43

oh something went wrong okay now what is

165:46

happening here is we should have talked

165:48

about this before but it's if we are

165:50

never late right so what what is

165:52

happening here is we were expecting it

165:53

will print nine but what is happening is

165:56

when we are saying that we are adding a

165:59

string with a number it will coin

166:03

calculate right so here we were

166:05

expecting that J and 8 which is 1 and 8

166:08

will get added to get nine but actually

166:11

what is happening is it is getting

166:12

concatenated so we have to mention hey

166:15

you know uh instead of adding a string

166:17

to a number the number what you can do

166:19

is you can put a bracket here so I want

166:21

this to resolve by itself I mean this

166:24

should be solved in a separate way so

166:27

whatever number you are adding here 1

166:29

plus 8 will be a separate thing and then

166:31

you are concatenating that with a string

166:33

let's try that compile and run oh it is

166:37

working I guess no it is not working

166:40

actually oh okay we cannot add six right

166:44

because it's the 17th R which gets over

166:47

Okay so let's say we are printing for 9

166:49

hours which is from nine to five

166:51

okay which also includes the the fifth

166:54

uh the fifth 17th R okay that's tricky

166:59

okay in fact you know what should I do I

167:01

should actually print uh colon here or

167:04

maybe a dash hyphen from this R to this

167:07

R this is what happens when you take the

167:09

example randomly so I will say J plus

167:11

nine so what I was saying is this you

167:14

know uh from nine to yeah this makes

167:16

sense now so you can see we can we have

167:18

actually built a calendar for this week

167:20

so day one from nine to ten we can do

167:22

something here from 10 to 11 we can do

167:24

something here we can set up we can set

167:26

a meeting calendar something like that

167:27

uh so what I'm doing is we are printing

167:29

J Plus 8 which is nine and then J plus

167:32

nine which is ten for the first

167:35

iteration as the value of J increases it

167:37

will have different values here

167:39

okay make sense right so this is what uh

167:42

we have achieved in this example so

167:45

that's how basically your for Loop works

167:47

okay this is nested formula we have seen

167:49

that as well

167:50

okay now once you understood this let me

167:52

just comment this part I don't want to

167:54

do that I have a question for you now

167:56

think about this and let me know what

167:58

could be the sequence here

168:01

see when we write this in three uh in

168:02

the same line these three things how do

168:05

you think they're executing is it

168:06

something like this is getting executed

168:08

first and then this one and then this

168:11

one and later on after executing all

168:13

these three statements it is executing

168:16

this statement what do you think okay

168:17

let me give you the answer so the moment

168:19

you execute the for Loop let me just you

168:22

know this is the best time to run a

168:23

debug mode so I will say Run start

168:26

debugging taking some time to start the

168:28

debug okay so I will say step over so

168:30

you can see it is executing the uh for

168:33

Loop but we have not studied that yet so

168:35

let me start the for Loop the moment you

168:37

start off for we can see the value of I

168:39

is one it just check for the condition

168:40

as well

168:42

but it is not it has not incremented yet

168:45

because the value of I is still one so

168:47

that means these two things will be

168:50

executed first and then it will execute

168:52

the block

168:54

and then if I step over it will execute

168:56

the next iteration

168:57

in fact you know what I will do for the

168:59

next iteration I will say step into

169:02

the value of I is 3 okay it is

169:05

not doing that but anyway so it is

169:07

executing the body and then it is

169:08

incrementing okay

169:11

so that's what I wanted to show you cool

169:13

uh so Point remember it will execute the

169:16

this statement first it will it will be

169:19

done only once and then it will check

169:21

for the condition then we execute the

169:23

block and then it will increment it I

169:25

mean whatever you want to do in that

169:27

situation in that portion

169:29

in fact if you want to uh skip any of

169:33

this step you can actually so that what

169:34

if you want to skip this from here it's

169:36

your choice you can just keep it empty

169:38

the only thing is since you are skipping

169:40

that if you want to make it work you

169:42

have to say I plus plus of course you

169:43

have to increment somewhere otherwise it

169:45

will be an infinite Loop you can

169:46

actually skip the instruction part from

169:48

here and you can write it outside as I

169:51

mentioned your choice it's just that you

169:53

have to put the semicolon that's

169:55

compulsory you have to have two

169:56

semicolons there

169:57

and now let's

170:00

compile

170:01

run you can see it it works so that's

170:04

how you can work with fall I hope you

170:05

enjoyed so we have basically seen three

170:07

Loops right while loop do while loop and

170:09

forth Loop now the question is which one

170:11

to use in fact doing the discussion of

170:13

this loose we have talked about it which

170:16

one to use but just to summarize things

170:18

see if you have of course all the loops

170:21

works in the same way you can use any

170:23

any of them at any point but the thing

170:26

is if you know how many iterations you

170:28

want to go for example let's say if you

170:30

want to print from 1 to 100 you normally

170:32

use for Loop if you because we know the

170:34

numbers right because in photo we

170:36

mentioned all the three things the

170:37

initiation value condition and the

170:40

increment decrement part but what about

170:42

reading a file because in reading a file

170:44

we don't need a counter right because

170:46

when you read a file how it doesn't

170:48

matter how many characters you have you

170:50

have to pick up each character right so

170:51

there is no initial point it's just that

170:54

you will read the file the moment you

170:55

have end of file you will stop the loop

170:58

so using a while loop there makes sense

171:01

can I use for loop as a while loop yes

171:04

we can if you remember we have seen a

171:06

code where you can just give two

171:07

semicolons and in between the semicolons

171:09

you can put the condition even that

171:11

works

171:12

how when to use do while loop

171:14

now whenever you use a while loop and if

171:16

you think hey even if the condition is

171:18

false you still want to execute

171:19

something at least once dual makes sense

171:22

but expert my experience most of the

171:25

time you are going to use for Loop if

171:27

you want to read a file re-database

171:29

while loop makes sense and I have I have

171:32

hardly used do while loop in my entire

171:34

career but yes there there might be some

171:36

use cases where you have to use Dubai

171:37

Loop so which is the best Loop in terms

171:39

of frequency of use it will be for Loop

171:42

in terms of the usage for database files

171:46

Network while loop makes sense once we

171:49

have talked about the basics of java

171:51

it's time to move towards some Concepts

171:53

now at the start itself we have

171:55

mentioned that Java is object oriented

171:58

programming now what do you mean by

172:00

object oriented programming so when you

172:02

talk about object in real world

172:04

everything is object right in fact if

172:06

I'm using uh a pen that's an object if

172:09

I'm using a mouse that's an object if

172:11

I'm using a seller type I don't know why

172:12

I have sellotape on my table but that's

172:15

an object right so everything around you

172:17

is an object and the weird thing is in

172:20

the programming world even they treat

172:22

humans as objects but that's fine

172:25

everything is an object now everything

172:28

has two things in fact every object has

172:31

two things

172:32

an object knows something an object does

172:34

something example let's say if you talk

172:36

about this pen

172:37

this pen is an object right so this pen

172:40

knows something so when I say no

172:42

something it simply means it has some

172:44

properties some physical properties

172:45

right you can see this pen right so we

172:48

have a a color for this pen I'm very bad

172:51

with Colors by the way but we have a

172:52

color for this pen there's a height uh

172:55

there's a kind of pen it also has a

172:57

brand I'm not sure which company pen it

172:59

is it doesn't matter so there are some

173:01

properties for the pen right and the

173:03

same way if we talk about this uh this

173:06

remote okay this is this has some

173:08

properties height color uh the number of

173:10

buttons so this is what a remote knows

173:14

and also a remote does something example

173:16

I can on my AC by pressing some button

173:18

so it has a behavior as well so

173:20

basically every object knows something

173:22

every object does something and that's

173:25

what we are going to create in Java as

173:27

well now whatever project you are making

173:29

you will be having uh different objects

173:33

now specific to Java from now it's it's

173:35

also about thinking okay so it's not

173:37

just about coding something even when

173:40

you start making something in your mind

173:42

you have to treat everything as an

173:44

object example let's say if you want to

173:45

add two numbers the first thing you

173:47

should which should come to your mind is

173:48

okay I need an object which can add two

173:52

numbers

173:52

getting a point right everything you

173:54

have to think in object way so if you

173:57

come back to programming if you look at

173:59

this example of course we don't have an

174:01

object here in this code but you have to

174:04

remember whatever I want to do you have

174:06

to do with the help of object so what we

174:08

have this discussed till now is there's

174:10

a concept of object oriented programming

174:13

so basically we have certain things here

174:16

we have a concept of object and this

174:19

object will have a property and this

174:22

will have Behavior so every object will

174:25

have a property and they also have a

174:27

behavior the properties can have

174:28

multiple properties and multiple

174:30

behaviors example let's say if I as a

174:33

human if I'm an object I know something

174:36

I know a lot of things I know how to

174:37

talk how to walk how to dance how to uh

174:41

type code right and I behave based on

174:46

what I know so I can dance because I

174:49

know how to dance so that's your

174:50

property but you know how to dance

174:52

that's a behavior when you're dancing

174:54

when you're talking when you're walking

174:55

right so I know my age as well I know a

174:58

lot of different so whatever I know here

175:00

is properties what I what I do is

175:02

behaviors okay but how do we create

175:04

objects so in in Java if you want to

175:08

create an object we have to first create

175:10

a class okay now why we need object and

175:14

Clause concept so first of all we have

175:15

talked about object everything has to be

175:17

done with the help of object in real

175:19

life we can't do anything without

175:20

objects so that's how it works so in

175:23

Java as well if you want to do something

175:25

you have to create an object

175:26

but how will you get an object and who

175:28

creates this object

175:30

so let's talk about it now if we talk

175:33

about the concept of object who creates

175:35

object in real world example let's say

175:37

if you talk about this table when I'm

175:38

with I have my systems now this table is

175:41

created by a carpenter okay so when you

175:43

go to a carpenter by saying Hey I want a

175:45

table Carpenter will say okay uh I will

175:47

give you a table but tell me the exact

175:49

dimensions okay uh maybe take the

175:52

carpenter the actual guy who is making

175:54

this table he is very technical so we

175:56

have to explain in technical details and

175:59

that's why we have sample sign between

176:00

who says okay you give me the details

176:03

okay I will forward it to the carpenter

176:06

so basically you give the details to the

176:09

to that person 30 percent and then that

176:11

third person gives a detail to the

176:12

carpenter because Carpenter understands

176:15

One technical details and you understand

176:16

on the later terms I can say hey I want

176:19

a table where I can keep my computer

176:20

where I can keep my monitors my keyboard

176:24

mouse mobile phone and all the all the

176:27

different things which I have on the on

176:28

the table here

176:29

so that person in the middle person will

176:32

explain everything to a technique or the

176:33

carpenter okay that's the dimension a

176:35

person wants uh this is the height which

176:37

I want based on my height I want this

176:39

table to be and uh so all the details

176:42

right so got the point so how do you

176:44

give that design so as a novel person

176:46

you give a design in a particular way

176:48

and in programming you can give that

176:51

design in a class file so you have to

176:54

create a class file which will act as a

176:57

blueprint for the carpenter or for the

177:00

for the for the sum for someone who can

177:02

create object for you in Java now who

177:04

creates objects in Java it is your jvm

177:06

again what is jvm what is JR will have a

177:09

digital discussion there but at this

177:10

point understand this we have a jvm who

177:14

will say okay it's my job to get the

177:16

object but you give me a blueprint okay

177:19

so basically you create a class file now

177:21

this class file gets compiled to create

177:24

a byte code now that byte code goes to

177:27

the jvm and that's where you get the

177:29

object

177:30

okay so in the last video we have talked

177:32

about what are classes and object right

177:35

and we have seen the theory of it in

177:38

this video let's try to create a class

177:39

and we'll also create an object of it

177:41

now first of all where do we get a class

177:44

and what class I want to create as I

177:46

mentioned before in Java what you want

177:48

to do you have to do that inside a class

177:50

so in that class you'll be having

177:52

methods you'll be having variables how

177:54

do we do that that will say in some time

177:56

but the first question is where we have

177:59

to create a class now the class can be

178:01

created in the same file so example in

178:03

demo.java we already have our class

178:05

right

178:06

we have a demo class and here with this

178:09

class I can create another class here

178:11

and we can surely do that so what I can

178:13

do is I can create a class here and I

178:15

will name this class as calculator now

178:17

basically imagine what I want to do here

178:19

is I just want to add two numbers so

178:21

even if you want to add two numbers

178:23

we'll do that with the help of a class

178:25

now what if you don't use a class and

178:28

normally how you do it so let's try that

178:29

first I want to just add two numbers and

178:32

I will remove this extra comments from

178:34

here now how do we add two numbers it's

178:36

very simple you use variables let's say

178:38

int num1 comma num2 so we got two

178:42

variables here and then we have to give

178:44

them some value as well so I can just

178:45

come back here I can give them values I

178:48

can say num1 is equal to 4. and num2 is

178:51

equal to 5 simple numbers right in fact

178:53

you know what works well is to have this

178:56

in two different lines again coding

178:58

standards if you have variables on

179:00

different lines it is much much easier

179:01

to read and much easier to maintain the

179:03

code as well now once you have these two

179:05

variables if I want to add these two and

179:07

of course when you add these two numbers

179:09

you will get the output you will get the

179:11

result

179:13

save results somewhere so I will say int

179:16

result equal to num1 Plus

179:19

num2 so you can see we got these two

179:21

values and then we are adding them

179:23

okay so in result we got the value and

179:26

of course you can print this value so

179:27

you can simply come back here and you

179:28

can say result print result that

179:30

perfectly works now the twist here is I

179:33

don't want to add these two values here

179:35

of course this code will work I just

179:36

want to see if this works before we do

179:39

something

179:40

I will say compile and run so you can

179:42

see we got nine but I don't want to add

179:44

these two values here so how do we

179:46

represent that in a separate class uh

179:49

it's something like let's say if you

179:50

want to call someone YouTube you will

179:52

not manufacture a phone by yourself you

179:55

will just buy the phone now this phone

179:57

company they will Design the phone they

179:58

will make the phone example if you talk

180:00

about iPhone in let's say you know

180:02

nowadays they don't mention that on the

180:04

back of the phone but initial days they

180:06

used to say uh okay let's talk about

180:08

some other phone first you know most of

180:10

the phones doesn't matter how costly it

180:12

is uh so they it is mentioned it is

180:15

where it is made uh in the previous the

180:18

previous uh manufacturing how was China

180:20

so most of the phone was having made in

180:23

China right uh but then if you think

180:25

about Apple they are smart in this case

180:27

they say designed in California and

180:30

assembled in China so even they believe

180:33

designing is more important than

180:34

assembling and that's what you have to

180:36

do here because manufacturing of your

180:39

object which you do here will be done by

180:41

jvm what we are concerned about is

180:44

designing of it so when you design it

180:47

you can just put come back here and you

180:48

can say class and I will just name it as

180:51

a calculator

180:52

I can just open the curly packets and

180:54

close

180:55

so this is basically where you will

180:57

design your stuff you will put your

180:59

things example if you talk about

181:00

calculator and if I mentioned that

181:02

before every object will have two things

181:04

properties and methods you will do that

181:06

in this particular calculator

181:09

now what I want to do is I want to add

181:11

these two values right

181:12

so if you want to add that's a behavior

181:15

right that's an action action will be

181:17

done with the help of methods so here I

181:19

will say public so how do we get a

181:21

method so we have to specify the access

181:23

to it which is public I want this to be

181:25

called from anywhere else and I want

181:27

this to return me a value so once that

181:31

is called it will perform some operation

181:33

and it will give back the value so I

181:35

will say ain't that's the written type

181:37

so whatever it returns we have to

181:39

specify the written type of it as I

181:41

mentioned before Java is a statically

181:43

typed language which means you have to

181:45

specify

181:46

for all the data whatever data you're

181:48

working with when you create a variable

181:50

you have to mention whatever variable

181:51

you want to create when you return you

181:53

have to mention what type of data you're

181:54

going to return so that's end and what

181:56

could be My Method name so method will

181:58

also have a name so here I will say the

182:00

method name is ADD and this ad will

182:03

perform an operation now at this point

182:05

you can see I'm not passing any value

182:07

I'm just saying add and when it calls I

182:11

just want to print in ad I just want to

182:13

see if this get really get called okay

182:15

so at this point I don't want to add I

182:19

just want to see if this is getting

182:20

called and also we are returning a value

182:23

right and that's what the errors it says

182:25

it says the method must return a result

182:27

of type int okay uh so I have to return

182:31

something so irrespective what type of

182:33

calculus I'm doing I have to return

182:34

something

182:35

's a return so that's how you've written

182:36

the value you have to use a written

182:38

keyword and then you will give uh any

182:40

value it doesn't matter any integer

182:42

value so I'm at this point I'm giving 0

182:43

because we are not concerned about this

182:46

value but the question is how do I call

182:48

this ad okay so this is basically a plus

182:50

a class will have methods a class can

182:53

also have variables what if you want to

182:54

get variables here it's very easy just

182:57

outside the method and inside the class

182:59

you can just mention the variables at

183:02

this point I just want to get a variable

183:03

a just to show you that how do we get a

183:05

variable we are not going to use this a

183:07

anymore but let's let's keep it there

183:10

so in a class this is how you design

183:12

your class you do you put two things

183:14

there you put your variable and you put

183:16

the method the variable name is a the

183:18

method name is ADD makes sense right and

183:22

here we are just adding these two values

183:24

putting that into result but we are not

183:26

going to use this anyway so we'll sub

183:28

comment this part and comment see the

183:30

ultimate AIM now is how do we call that

183:34

add method

183:35

that's tricky so in order to call the

183:38

add method

183:39

can we directly do that can we just

183:41

directly call a method like this so

183:43

that's a method right now if you're

183:44

coming from some other languages it

183:46

might be supported where you can simply

183:48

mention the method name and it will work

183:51

at this point you can see it is not

183:53

working we are we are getting the error

183:54

itself in fact there are two type of way

183:56

you can check the address you can

183:57

directly see in the IDE if you're using

183:59

it otherwise you can just compile the

184:01

code to see the exact error it says

184:03

cannot find symbol okay that's weird the

184:05

ad method is there right but it still

184:07

says cannot find symbol you know why

184:10

because if you can see in the error

184:13

itself it mentions that the symbol add

184:16

which is a method it's not it cannot

184:19

find that method in the location demo

184:23

oh okay so when you call AD is not able

184:26

to find that in the same class

184:28

but it's not in the same class right in

184:29

a different class

184:31

how do I mention that hey you know this

184:33

ad is it belongs to a different class

184:34

for that basically what it says is the

184:38

design is not enough basically what you

184:40

need is also the object example let's

184:43

say if someone comes in the world by

184:45

saying hey no we have this amazing idea

184:47

and we wanted to buy this idea nobody

184:49

will buy the ideas why do you buy the

184:51

devices no one will give you a uh I mean

184:54

you will not buy a phone

184:56

odd idea which using which you can call

184:58

someone it says you know this device can

185:01

call but then you don't have a device to

185:03

call uh so basically we need an object a

185:05

physical object in the world of

185:07

programming we need a virtual object

185:09

so for this class which is a design I

185:13

want the object the instance which I can

185:15

use

185:16

how do I do that how do I create the

185:18

instance now for that you have to come

185:20

back here and you have to say Hey you

185:21

know I have a calculator class and I

185:25

want to create an object of it so

185:26

basically imagine calculator as a type

185:28

example if you want to get a variable

185:30

like num1 you specify a type right which

185:32

is int so here as well imagine Calcutt

185:35

is a type and then you are creating an a

185:37

reference of it let's say calc

185:42

they are also variable the calc is also

185:45

variable the difference between these

185:47

two is this num1 and num2 they are

185:49

called primitive values or primitive

185:51

variables is because ain't and all those

185:54

type uh flow double they are primitive

185:57

this calc is of type calculator so we we

186:00

normally call them as a reference

186:02

variable

186:04

so that's done uh let's say we have this

186:06

calc calculated calc and then using this

186:09

calc I can say calc dot add

186:12

but still it will not work the reason we

186:15

are just creating a reference we are not

186:17

getting an object yet

186:19

okay how do we get an object

186:21

now first of all who creates an object

186:23

and every time you get an object in this

186:25

world you know we are talking about

186:27

carbon neutral we are talking about how

186:30

can we reduce the e-fest in the same way

186:33

uh every time you buy a new phone

186:35

basically you are creating an e-vest

186:36

because when when you buy a phone the

186:39

companies are manufacturing new phones

186:40

okay so basically you are increasing the

186:43

E-Waste in the same way in jvm as well

186:47

every time you get the object it will

186:49

consume some space

186:51

okay how do we do that first how do we

186:53

create an object how do we consume that

186:54

space now for that you have when you

186:57

want to consume the space you will

186:58

simply say Hey you know I want to create

187:00

a new device so you have to say new

187:03

but whatever device it will ask you in

187:06

that case you have to mention I want to

187:07

create a object of type calculator and

187:11

you have to also give the round brackets

187:13

now this is how you create the object

187:18

okay this is how you get an object of

187:20

course we'll talk more about this uh how

187:23

do we I mean what's the difference

187:24

between this line and this part but at

187:27

this point just to keep it simple

187:29

this is how you create the object first

187:32

you mention the class name then you

187:33

mention the reference variable name or

187:36

variable name using which you can access

187:38

the methods of calculator and then you

187:40

send new keyword that's how you consume

187:43

this space but how much space you need

187:45

what what are the variables you have to

187:46

initialize that can be done with the

187:48

help of this thing now it has a special

187:52

method in Java called Constructor but

187:55

what is Constructor we'll talk about

187:56

that later how it is different from

187:58

methods we'll discuss that in detail

187:59

later

188:01

but at this point with this line we got

188:03

the object and once you got the object

188:05

you can simply call the add method your

188:07

job is done

188:09

but will it really work let's try so

188:11

what I can do is I can just clear this

188:13

and I can go back here compile

188:15

and run you can see we got in ad

188:18

basically you were able to call in ad

188:22

that's great

188:24

okay but if you can see

188:27

this add method actually returns a value

188:30

so don't you think we should accept the

188:32

value as well if someone gives you

188:34

something you have to accept it as well

188:35

so here I can say end result so whatever

188:38

it returns I will put that into this

188:42

result variable and I will just print it

188:46

and let's see what happens so compile

188:49

run oh it works you can see it says in

188:52

add and we are also printing the value

188:54

of result which is zero y 0 is because

188:56

we are returning 0 but what if you

188:59

return something else maybe I'm going to

189:01

return based on the values which I have

189:03

here

189:05

oh that means here

189:07

I don't want to print something what I

189:10

want to do is I want to create a

189:12

variable called as let's say R is equal

189:14

to I want to add num1 plus num2 and then

189:17

whatever addition I'm getting I will

189:19

just return the value now it will make

189:21

much more sense so I didn't show if

189:22

you're doing 0 I should I should be able

189:24

to return the value so now 1 plus num2

189:26

should be assigned to R and I want to

189:30

return the r but the only problem is I

189:32

mean let me compile this code to see

189:34

what happens compile oh we got errors

189:37

and you know I love errors because

189:39

errors will teach you a lot of things it

189:41

says num1 and num2 cannot find which

189:44

location in calculator and you have to

189:46

mention hey you know I was actually

189:48

talking about number num2 which belongs

189:50

to demo in fact the main method so what

189:53

you can actually do is uh can we when

189:55

you're calling ad can we just pass these

189:58

two values something like this four and

190:01

five right and then since you are

190:03

passing those values example let's say

190:06

if I ask you hey you know add two

190:08

numbers the obvious question from your

190:11

side would be what numbers give me the

190:13

values first right so I will say Okay

190:15

add 15 plus 20 then you will say okay

190:17

that's 35. so that means I have to pass

190:20

a value so when I call you to add values

190:22

I have to I have to give you the values

190:24

and then of course you have to accept

190:26

those values it's not like I am giving

190:28

you the values and you are not listening

190:29

to me so I have to accept those values I

190:32

will say N1 comma N2 different variable

190:35

names

190:36

I have to give int

190:38

and then here instead of adding num1 and

190:39

num2 I will add num n 1 and 2. the

190:43

variable name can be number random two

190:44

as well just to just to differentiate

190:46

between these two I'm just giving

190:47

different different variable names

190:50

so the idea here is whatever value you

190:52

are passing 4 and 5 will be assigned to

190:55

this N1 and N2 and that's how you can

190:58

pass the value

191:00

make sense right and anything else

191:03

let's try this let's see if this works I

191:05

will just clear this and

191:08

compile

191:10

okay I've done comparison two times my

191:13

bad so you can see we got nine so it is

191:15

working basically whatever value you're

191:16

passing it is getting passed here

191:19

but there's one more thing

191:21

when you have those values in a variable

191:22

why you have to mention numbers here we

191:25

are hard putting values not a good idea

191:27

so what you can do is instead of writing

191:29

4 here we can write number one and

191:32

instead of writing

191:33

5 here I can write num2

191:36

simple stuff so whatever value you have

191:38

here you are passing it here and then

191:41

that gets assigned to num1 and N2 and

191:44

then you're adding the value

191:46

let's try

191:48

compile

191:49

run it works

191:52

so what we have done is we have created

191:53

the class how to get a class it's very

191:54

simple you say class and you mention the

191:56

class name which is calculator and then

191:58

in that class you can create variables

192:00

you can create methods this is how you

192:02

specify what object knows right

192:05

variables data and what object does an

192:08

object can add two values

192:10

now how do we use this class basically

192:13

you have to create an object of it so as

192:15

you can see this is how you create the

192:18

object you say Hey you know jvm I want a

192:21

new object you see you you use a new

192:23

keyword there and then jvm says okay I

192:25

will give you the object but also tell

192:27

me design

192:28

and this is how you mentioned the design

192:30

you say calculator now this will give

192:32

you a new object of what type calculator

192:35

so you're assigning that to a variable

192:37

called calc of type calculator example

192:40

when you say 5 that's an integer type

192:42

that's why you say int num2

192:45

this is an object of calculator type so

192:48

you will assign that to calc which is a

192:51

calculator type variable now once you

192:52

got the object reference here you can

192:55

just use that reference and you can call

192:56

the add method by passing two values

192:59

return the value and print

193:01

simple stuff that's what we have done in

193:03

this video so till this point we have

193:04

seen some programming thing and we were

193:06

able to execute them we have talked

193:08

about classes variable methods and

193:10

object relation as well now it's time to

193:13

actually see what is happening behind

193:15

the scene because if you understand what

193:17

is happening behind the scene then the

193:19

further cooling will make sense right so

193:21

every time I introduce A New Concept we

193:23

can actually relate what is happening

193:24

behind the scene now to understand that

193:26

let's focus on some keywords okay if you

193:30

remember we when we started this videos

193:32

at the start of the video I mentioned

193:34

that to compile the Java code you need a

193:38

tool right in fact you have to do setup

193:40

as well and that tool is your jdk now

193:44

jdk here stands for Java development kit

193:47

maybe I can just write it here so

193:49

basically why do we need it why do we

193:51

need a development kit see the thing is

193:53

when you as a programmer when you type a

193:55

code you have to convert that code into

193:58

byte code because ultimately the what

194:00

runs on the machine is your is a

194:03

bytecode so of course you cannot type A

194:05

bytecode I mean you can don't try it but

194:07

the idea is you will type the Java code

194:10

you will compile it and it will convert

194:12

into bytecode which will run on a

194:14

machine but which machine now for that

194:16

we have one more term here which is

194:18

called a jvm now basically jvm stands

194:22

for Java virtual machine

194:24

so basically this is your machine which

194:26

runs the code so of course you have to

194:28

compile the code with the help of jdk

194:29

and you have to run the code on jvm now

194:32

apart from this there is some more

194:33

things in the picture so what happens is

194:36

when you want to run your code so what

194:38

code I'm talking about I'm talking about

194:39

the byte code here so assuming that you

194:42

have already compiled and you have a

194:43

byte code with you you need to get a

194:46

space okay so let's say this

194:49

is your space where you are going to run

194:52

your code and this space is basically

194:54

your j v m now this is where you

194:58

actually execute the code now why it is

195:01

called jvm is because we have a machine

195:03

and to make deep Java platform

195:05

independent we went for a virtual layer

195:08

on top of it so it doesn't matter on

195:10

which machine you have your jvm it will

195:12

work the idea is to have jvm on every

195:14

machine now jvm is going to run your

195:16

code and behind the scene computer will

195:18

convert that into its own away and it

195:20

will run

195:21

so basically you need a jvm to run your

195:23

code but then what happens is for your

195:26

code let's say this is your file here if

195:28

this is your file which you want to

195:29

execute most of the time your code is

195:33

dependent on some external file not your

195:36

files which you have created maybe you

195:37

have created five files apart from the

195:40

code which you have written you have

195:42

used some inbuilt classes some inbuil

195:44

libraries and that's where we need some

195:47

extra things here so maybe we need some

195:49

extra files from which you are going to

195:51

use in your code okay now question is

195:54

where you will find all this code and

195:56

that's where we have one extra layer

195:58

here

195:59

on top of it which is your JRE now JRE

196:05

stands for Java runtime environment so

196:09

basically we have a concept of JRE which

196:12

is Java runtime environment now in this

196:14

Jr you'll be having all the extra files

196:16

you need extra classes in the classes it

196:19

also checks your byte code basically it

196:21

validates your byte code or maybe you

196:23

want to load a class so here GRE will

196:25

help you but ultimately your code will

196:28

run on jvm so jvm is very important

196:30

because that's where that's where your

196:31

code runs but even to run your code you

196:34

need some extra files and that's what

196:35

GRE will provide you and the name itself

196:37

says right Java runtime environment so

196:40

it's an environment where you can run

196:42

your code and the beauty is jvm is a

196:45

part of GRE so you can't simply say hey

196:47

we don't need GRE we need if we don't

196:49

have a GRE you don't have a jvm right so

196:53

basically every time you when you say

196:55

you have Java in your machine to run the

196:57

code you actually have Jr with it so you

197:00

have a JRE and inside that you have a

197:02

jvm but then as a developer you install

197:05

jdk right right now jdk acts like a

197:08

upper layer so every time you install

197:09

jdk you also get these two as well so

197:12

the the top layer here is your

197:15

jdk so jdk when you install jdk you will

197:18

get the updated JRE and also you will

197:20

get the updated jvm but by default on a

197:24

client machine you will find jrd and jvm

197:26

which will have extra files and which

197:28

will have where you can run the code in

197:31

fact you might have seen this sometime

197:32

when you when you install some software

197:34

it will say Hey you know in this machine

197:36

I'm not able to find.net framework or

197:39

I'm not able to find Java it's because

197:41

the software will be running on those

197:44

particular softwares I mean the

197:46

application will run on dotnet platform

197:48

or on Java of course there are other

197:50

platforms as well just to give an

197:51

example we have dotnet Java and that's

197:54

why you know you'll have you install

197:55

extra softwares the same thing if you

197:57

don't have the updated version of GRE

197:59

and if you try to run some Advanced

198:01

software it might not work and that's

198:03

why you know sometime on Windows uh it

198:05

says Java is getting updated for the

198:07

same reason because to support extra the

198:10

new softwares so this is basically your

198:12

jvm Jr and jdk so jdk will have

198:15

everything JRE and jvm and in jvm you

198:18

execute the thing now in the next video

198:19

we'll try to expand more on jvm so in

198:22

the previous video we were able to work

198:24

with a calculator and basically we're

198:26

adding we were able to add two numbers

198:28

right so what we did is in this

198:30

calculator we have created this method

198:31

called add and then we have accepted two

198:34

parameters and then we were able to add

198:36

them and we were able to return the

198:39

value now of course the place from where

198:41

you are calling this method uh returns a

198:43

value and that's what you're saving in

198:45

result and you are putting it that now

198:47

in this video let's try to understand

198:49

more about methods of course in the

198:51

entire course we will be using different

198:53

types of methods we'll explain how this

198:56

method method is behaving this way but

198:58

at this point let's understand about

199:01

methods more so what I will do is just

199:03

to simplify this I will remove this

199:05

class and I will also remove the entire

199:08

section from here now if you can see C

199:11

by default even if you have one class

199:13

here which is demo by default we have

199:15

one method which is main now main is a

199:17

method which is a start of execution so

199:19

it defines hey this is where we have to

199:22

start the execution so by default we we

199:25

are anyway using a method right but then

199:27

if you understand the actual development

199:30

of any kind of product it can be a car

199:32

or a mobile phone or a software now we

199:35

normally create modules example if we

199:37

talk about a car we have different

199:39

modules there right we have wheels we

199:41

have uh doors we have Bonnet we have

199:44

engine right so all these things come

199:46

together to to form a car in the same

199:48

way if you talk about a big software we

199:51

break down this offer into small parts

199:52

and we try to create those as a

199:55

component now in Java we can create

199:57

components with the help of classes

199:59

right so we can say hey if you want to

200:01

build a car maybe let's create a class

200:04

called Wheels let's get a class called

200:07

uh engine let's create a class called

200:10

seeds right so all these things comes

200:12

together

200:13

now every class here is which whatever

200:15

class you work with let's say wheel

200:17

Class A Wheel class will also have its

200:19

own behavior right so we'll have some

200:21

data we have some Behavior so here if

200:24

you talk about Behavior if you want to

200:26

Define hey this is how the wheel will

200:28

move this is how the uh the engine will

200:31

work now all those things comes under a

200:34

behavior and we do that with the help of

200:36

methods just to simplify this let's say

200:39

I create a class and I will link this

200:40

class as ABC it can be any component or

200:43

maybe I can I can say this is a computer

200:45

of course even a computer is made up of

200:48

multiple components but just to simplify

200:50

things let's say we have a computer it's

200:52

a class okay and then in this class I

200:55

can Define the behavior what a computer

200:57

can do a computer can run games a

201:01

computer can play music so I can say hey

201:03

I want this computer to me to play music

201:05

in fact in my childhood this is what we

201:08

used to do when we used to get computer

201:09

to access uh I was around I was in my

201:14

11th standard when I got full-fledged

201:16

use of computers and at that point

201:20

uh when I say I I got to use computer

201:22

what the thing I will do of course we

201:24

can use paint we can use Ms docs which

201:27

is the word or we can use some amazing

201:30

media player to play music right so here

201:32

let me say uh this computer can play

201:36

music okay now how do we Define that's a

201:39

behavior we do that with the help of a

201:41

round bracket and then this play music

201:44

will do something so we have to open the

201:46

brackets and close this is where you

201:48

will Define what is a behavior of this

201:51

particular method

201:52

and every method we also have to specify

201:55

what type of access you're giving of

201:58

course we'll talk about access later but

202:00

at this point let's make it public I

202:02

just want it to be accessed from

202:03

anywhere and also a method will do

202:06

something right so that's what we are

202:07

doing here now this method will do

202:09

something and it will give you something

202:11

in return example let's say if I ask

202:13

someone here so let's say if I have

202:15

Hershey and if I say hey I want a pen

202:18

now at this point as a external method

202:22

I'm asking her to give me the pen right

202:24

so what I'm expecting from hush a pen

202:27

right maybe I want to just get some

202:30

information maybe I can just ask hush

202:31

hey can you just call someone to send

202:35

this information

202:36

so at this point if I am asking her to

202:39

call someone I'm not expecting anything

202:40

in return

202:42

so what I'm doing is when I say I don't

202:44

want anything in return is wider so of

202:47

course your method can give you back

202:48

something you can say a method can give

202:50

you uh integer value a method can give

202:53

you a pen as well so this method which

202:55

is play music is not giving you anything

202:57

it's just performing some operation

202:59

now of course we can write multiple

203:01

lines of code here to play the music but

203:03

just to keep it simple I will say music

203:06

playing or playing music it doesn't

203:08

matter so we are just writing this

203:09

statement here now this is my method

203:12

that's how you define the behavior we

203:13

can have some extra methods as well I

203:15

can say public and this is get me

203:18

a pen

203:21

okay this is a method name I have and

203:24

this method will give me something so I

203:26

will say turn so what it will return so

203:28

let's say I want I want to get a pen but

203:30

at this point we don't know how to get a

203:31

pen right so let's say I will I will

203:33

return a five value now if we talk about

203:35

this five this five is a number right or

203:37

maybe I can just return pen now if you

203:40

can see this pen is a string now string

203:43

is a special type in Java which is a

203:46

combination of different characters so

203:48

we have to put that in double quotes now

203:50

we are returning a pen so here we can't

203:52

say void because when you say y it means

203:54

you're not expecting anything into in

203:56

return but here we are doing that we are

203:58

returning a pen in a string format so we

204:01

have to say I am returning a string so

204:03

whatever type you written here you have

204:05

to mention that we're going to type here

204:07

makes sense right so basically we got

204:09

two methods and then it is also possible

204:12

that when I say hey get me a pen

204:15

hush will say okay I will get you a pen

204:17

but you have to give me money first of

204:19

course right no one will give you

204:20

something for free so I have to provide

204:22

the money here so if you ask a method to

204:25

do something it comes under your design

204:27

how you want to design it do you want

204:29

your methods to exp accept something

204:31

from you or what type of data you want

204:34

to return so here I'm saying this get me

204:37

pen will it will get some integer value

204:40

so let's say I'm passing 10. so normally

204:42

one or not 10 but available which is num

204:45

I will say cost

204:47

so basically you have to pass a number

204:49

let's say 10 so one pen will cost to 10

204:51

Rupees and then if you want to call this

204:54

get me a pen you have to pass the value

204:55

as well

204:56

make sense right now we have two methods

204:59

how do we create how do we call them so

205:01

from this main which is in some other

205:03

class if you want to access this method

205:05

the first thing you have to do is you

205:07

have to create an object of a computer

205:08

as I mentioned before every time you

205:11

have a class which is just an idea right

205:14

class is a blueprint an idea and if you

205:16

really want to use those ideas you

205:19

really need an object how can we use

205:21

something without the object right even

205:24

if you want to call someone you need a

205:26

phone with you a physical phone not a

205:27

virtual concept which using which you

205:30

can call someone okay so let's say here

205:32

we have to get object of computer so

205:35

we'll say computer comp of course you

205:37

can give any any reference name here we

205:39

can also say obj which is very common so

205:41

you can say computer object equal to new

205:43

computer

205:44

and that's how you create the object

205:46

right so this is a line normally we use

205:48

to create object but only these two

205:51

things are actually getting the object

205:52

here this is a reference variable and

205:55

now using this obj I can actually call

205:58

both the methods I can call uh play

206:00

music

206:02

so let's try with play music only and

206:04

let me clear this screen here

206:06

so we'll say Java C

206:08

demo.java and you can see we got the the

206:11

comparison done let's run this code I

206:13

will say Java space demo it says music

206:15

playing so this is working uh can I call

206:19

get me a pen let's try so I can say obj

206:22

dot get me a pen but if you can see get

206:25

me a pen is asking you to pass a integer

206:29

value so let me I will pass 10 here

206:32

and I can just run this

206:35

the only thing is if you say get me a

206:37

pen it returns a value which is a string

206:40

format that also means that when you

206:42

call get me a pen here it will return

206:45

you something it will give you something

206:46

so I will put that into a string Str

206:49

so I can say string Str of course you

206:51

can use any variable name but that's a

206:53

string we are not using integer double

206:55

here we are working with string and of

206:58

course we'll have a separate session or

207:00

separate video on string at that point

207:02

we'll understand that here what I will

207:03

do is I will just try to print this Str

207:05

just to see what it is printing I hope

207:07

it will print a pen and yeah that's what

207:10

we got we got music playing and we got

207:12

pen

207:13

cool now there's one more thing here

207:15

which is what if it in this cost so

207:19

let's say you're passing 10 that works

207:21

but then what if someone is passing

207:22

letter two now of course we'll not get

207:24

any pen for two rupees right of course

207:26

it at this point it will work because we

207:29

are not doing any any checking I don't

207:30

want to return a pen when the cost is 2.

207:32

so how do we do that I want the minimum

207:35

value for this cost to be let's say 10

207:37

Rupees in that case you can remember we

207:40

have talked about if else so we can

207:41

actually use if else here so we can say

207:43

if the cost is less than or the cost is

207:46

greater than equal to 10

207:48

then return pen otherwise don't return

207:50

pen right it's very simple if your if

207:52

the cost is more than 10 or equal to 10

207:55

then you have to return pen otherwise

207:56

don't return pen someone is calling this

207:58

method you have to return something in

208:00

this case you have to write else

208:03

return and you can say return nothing

208:07

you know the weird thing is at the time

208:09

of recording there is a phone called

208:10

Nothing phone so basically what you're

208:13

returning here or nothing phone ignore

208:15

my poor jokes but yeah so what that's

208:17

what you're doing here you are returning

208:19

nothing and you can see I am passing 2

208:21

here and of course the consumer will be

208:23

false it should return nothing let's try

208:25

compile run and you can see we got

208:28

nothing

208:30

okay so this is working and this is what

208:33

we wanted

208:34

you can do one more thing you can simply

208:36

say you don't even have to mention the

208:38

else keyword you can simply say return

208:40

nothing but then you will say it will

208:42

execute return nothing anyway right the

208:44

thing is the moment your code sees

208:48

return pen here it will just jump out of

208:52

this method it will not execute the

208:54

further statement what I'm saying is if

208:56

the value let's say is 12 not 2 12 of

208:59

course this will pass the condition and

209:02

then once it is true it will execute

209:04

return pen the moment you set return

209:06

keyword in a function or a method it

209:09

will return the value it will just go

209:11

back it will stop the execution

209:13

and that's what we will be getting here

209:16

so if I compile this code and run you

209:19

can see it says pen it's not printing

209:21

nothing but yes if the value is 2 in

209:23

that case this condition will be false

209:25

this one will be false and it will not

209:27

execute the return statement of if it

209:30

was execute return nothing and that's

209:32

why it will Point nothing now if you can

209:34

observe there is one difference between

209:35

these two methods here if you can see we

209:37

have get me a pen and we have Main

209:40

the differences of static keyword and we

209:43

have not talked about static gate so let

209:44

me talk more about the methods and this

209:46

is important for the further Concepts

209:48

and very interesting so let's say again

209:51

let's go back to our calculator to get

209:53

some more understanding of it so I will

209:55

say it's not a computer it's a

209:57

calculator of course in a computerizable

209:59

we can do calculations but just to keep

210:01

it simple let's go with calculator so we

210:04

got this class here and let me also get

210:07

the object here and there is no

210:08

compulsion that a class should have the

210:10

variables and methods we can keep it

210:12

empty as well that perfectly works so

210:14

you can see we got calculator obj equal

210:16

to new calculator and then here I want

210:19

to let's say add two numbers I know I

210:21

know you'll be saying we have done that

210:23

before but let me just do that once more

210:25

and you'll understand what I'm doing

210:27

so you can see I'm saying public end I

210:29

want to add two numbers why int is

210:31

because I'm expecting some written value

210:33

and it will accept two values one is N1

210:35

and 1 is N2 and here I want to return

210:38

the value of course you can simply

210:40

return the addition of it so you have a

210:42

choice you can create a separate

210:44

variable uh what I'm saying is you can

210:46

create a variable like result and you

210:48

can say N1 plus N2 and here you can say

210:52

return

210:53

result this works uh you know we can

210:56

actually do a shortcut here if you look

210:58

at this variable result is only used

211:00

once we have assigned the value and then

211:03

we are returning a value so can we just

211:04

do this can we just say return N1 plus

211:08

N2 we don't have to create a new

211:10

variable

211:11

okay so you will get used to it once you

211:13

start making progress you will get used

211:15

to it so you can see we got a calculator

211:17

which can add two values and then

211:19

returning the addition of that this

211:21

perfectly works right so what I will do

211:23

here is I will come back to my code I

211:25

will say obj dot I can call this add

211:28

method which will take two values three

211:30

and let's say four so it will return a

211:32

value right so I will say int R1 so int

211:35

R1 is equal to this addition and I can

211:37

actually print this value so let's

211:39

compile this code uh not running

211:41

directly first you have to compile the

211:42

code and run you can see we got seven

211:44

this works so what we are doing is we

211:47

are calling add by passing two values

211:49

which is three and four what if you want

211:51

to add three values that would be tricky

211:54

so of course you will say I mean think

211:56

about this if you want to add three

211:57

values the first thing comes to your

211:59

mind is it's easy we can change this

212:01

particular method which accepts three

212:02

values right and of course you have to

212:04

pass three values because if you pass

212:06

only two values uh your compiler will

212:08

feel bad compiler will say Hey you know

212:11

uh you have a method which accepts three

212:12

values and you're passing only two

212:14

values not fair okay so you will say

212:17

okay I will pass to I will pass three

212:18

values and that perfectly works I will

212:20

just compile this code and run and you

212:23

can you can see we got seven oh we got

212:25

seven because we are still adding N1

212:28

plus N2 we are not adding N3 so let's

212:30

compile and run you can see we got 12

212:33

this is working but there's only one

212:35

little problem here not a little problem

212:37

actually big problem what if now you

212:39

want to add two numbers sometimes you

212:41

want to add three numbers sometimes you

212:42

want to add two numbers again it will

212:44

not work

212:45

your compiler will say Hey you know we

212:48

are expecting three values and you're

212:50

passing only two now unfair right okay

212:54

how do we solve this problem maybe if

212:56

there is one solution which comes to my

212:58

mind is what if you can just create

213:00

another method which can accept two

213:02

values I can say public int add one

213:05

which will accept int N1 comma int and 2

213:10

and this will return N1 plus N2 now this

213:13

perfectly works the only thing you have

213:15

to do is you have to remember what

213:17

method you have to call because add one

213:20

adds two numbers and add as three

213:22

numbers of course you can make it more

213:23

effective by naming the method name as

213:26

add two numbers add three numbers okay

213:28

we we were able to solve that but the

213:30

problem is the moment you have more

213:32

methods you have to maintain the

213:34

documentation for those methods and also

213:36

it's very difficult to remember the

213:38

method names add method looks much

213:40

better right instead of adding two

213:41

numbers adding three numbers so can we

213:43

do this can we have the same method name

213:46

that can solve our problem like this of

213:50

course I have two methods but then but

213:51

they have the same names the only thing

213:54

which is changing is the parameters if

213:56

you can see this ad take three

213:59

parameters this ad takes two parameters

214:01

so that's right we can have the method

214:04

name of with the same name the only

214:06

requirement is they should have

214:07

different parameters example if you make

214:09

it same parameter here again it will not

214:12

work it will say it's a duplicate method

214:14

add we can't do that so basically we

214:16

need to have different parameters and

214:18

it's not like they have to be in numbers

214:20

they can also be of a type what I'm

214:22

saying is what if you want to have the

214:25

same method I will I can just reuse my

214:27

code here and instead of int I can also

214:30

have a double value

214:32

so I can say double N1 and N2 and I can

214:36

just simply return the only thing is

214:38

when you double when you have double and

214:40

int you return with double because

214:43

double plus eight will give you double

214:45

that's it so it's not just about the

214:47

number of parameters it's also about the

214:49

type of parameters so you can have

214:51

different number of parameters or you

214:54

can have same number of parameters but

214:56

of different type now this concept in

214:58

Java is called method over loading

215:01

because you have two methods with the

215:03

same name in fact three methods here

215:04

with the same name what is different is

215:06

parameters it is called method over

215:10

loading now likewise we also have one

215:12

more concept is called method overriding

215:14

where you also have the same name but

215:17

things work differently there it's more

215:19

about inheritance and thing at this

215:21

point we are focusing only on the

215:22

overloading part how about this see

215:25

every method will have a name and

215:26

parameters what if you have the same

215:28

parameter but different written type

215:31

even that will not work it has to be

215:33

with the parameter itself this return

215:36

doesn't matter what matters is the name

215:39

and the type now let's try to understand

215:42

what is there inside the jvm so what I

215:46

will do is I will just go back here and

215:48

let me draw a bigger jvm because that's

215:50

what you're focusing now if you talk

215:52

about the outer box for this that's your

215:53

jrd outer box for that is your jdk but

215:55

let's focus on jvm here and also name it

215:58

j b m now what we normally do here is

216:02

this is where you execute the code right

216:04

now question arise how exactly we are

216:06

going to execute the code of course

216:08

there is something called a runtime or

216:11

the runner which will do that here we'll

216:13

focus more on the memory level what

216:15

happens on when you create a variable

216:17

when you store a variable in your memory

216:19

or inside jvm memory we have we can

216:22

categorize into two parts the first one

216:25

is this this is how it looks like of

216:27

course I want you to guess so basically

216:29

this is a area where you can store or

216:33

your data and that has a feature which

216:36

is last in first out example let's say

216:39

if you send data which is five six so

216:41

let's say if you send data five six

216:43

seven now the first value which entered

216:45

was five second value is six third value

216:47

is seven now when you try to get the

216:50

value you will get 7 first because that

216:52

is last in first out now this is called

216:55

stack so basically you have a memory

216:57

which is called stack memory now apart

217:01

from this memory you have one more which

217:02

is you call as Heap now on purpose I am

217:06

drawing this type of image where it

217:08

represents that it expands right so this

217:11

memory here is your hip so basically we

217:14

have a stack and we have a heap memory

217:16

so in the stack you store data in a

217:19

different sequence and in Heap you have

217:21

a open space now before we jump into

217:24

this memory how it works let's focus

217:26

more on this code okay remember when we

217:28

talked about calculator and then we have

217:30

talked about these two variables now

217:32

here when you talk about this N1 and N2

217:35

variable the variable but they are local

217:38

variables okay just to reiterate those

217:40

are called local variables now what do

217:43

you mean by local and what are different

217:45

type of variables we have so if I create

217:47

a variable here let's say num now in a

217:50

calculator we have a num variable and

217:53

then in a method like add we have N1 and

217:56

N2 that's right the N1 and 2 are part of

217:58

add method now here the N1 and N2 become

218:03

your local variable and num here becomes

218:05

your instance variable that's right the

218:07

word is instance now what happens is the

218:11

moment you create this the moment you

218:13

have main now the beauty is every method

218:16

will have its own stack that's right

218:18

it's not just one stack every method

218:21

will have its own stack and if you can

218:23

see in this code we have two methods

218:24

right so of course you will be having

218:26

two stack what I will do is I will just

218:28

redraw this tag to accommodate more data

218:32

so what I will do is I will just zoom it

218:34

a bit so let's say we have a stack here

218:36

okay this is your first stack and a

218:38

stack will have two section okay and

218:40

basically it will have two columns and

218:41

multiple rows depend upon how much data

218:44

you want to introduce now if we talk

218:46

about this main this particular stack is

218:48

actually your main stack so I can say

218:49

this is my main stack now in this stack

218:52

you will be having your data so what

218:54

data I'm talking about if you go to the

218:56

main method if you can see we have a

218:58

variable a okay now that's also a local

219:01

variable okay so I can say hey I have a

219:03

variable which is called a here so I'm

219:05

just reading a there now what's a value

219:07

at this point there is no value for a so

219:09

I will just ignore that part maybe let's

219:11

not even mention that once we talk about

219:12

string then it will make much more sense

219:14

let's start with the variable here maybe

219:17

I can create some more variable just to

219:18

understand so I will say int data is

219:21

equal to 10. so what I'm doing is I'm

219:22

creating a variable which is a local

219:24

variable again inside the main method

219:26

and you can see we can say this is my

219:29

data variable and the value for that is

219:32

and straightforward we can assign the

219:33

value 10 because that's the value so

219:36

basically your stack will have a key and

219:37

a value a key would be name of a

219:39

variable and value will be 10 the value

219:41

itself now coming back to R1 here okay

219:45

let's ignore this obj as of now and mind

219:47

you this obj is also available so it was

219:50

a part of this tag so data obj R1 they

219:54

all are variable

219:55

so it says Okay I want to have R1 as

219:58

well so I got R1 variable but what is a

220:01

value for R1

220:03

and that's where it will basically call

220:05

the add method okay so it will call the

220:07

add method but we don't have our ad

220:09

method in the memory as of now right so

220:11

if you see in this memory you know where

220:13

we have ADD so basically what we have to

220:15

do is it will create a new stack out of

220:18

it so we got another stack and the name

220:20

for this stack would be add and of

220:23

course it will have two sections and

220:24

multiple rows so two column multiple

220:26

rows now if you see in this particular

220:29

ad we have two variables N1 and two so

220:32

of course we have to define those two

220:33

variables N1 and N2 what are the values

220:37

for these two variables we are passing

220:39

them right three and four the moment you

220:42

pass these two values it will go to N1

220:45

and N2 it will assign those values so

220:47

the value for n 1 is 3 the value for N2

220:50

is 4 okay that's good and then you are

220:54

executing add method so in add method

220:56

what we are doing is we are just adding

220:57

those two values uh three and four which

221:00

is seven it will return the value

221:02

whatever value you have returned it will

221:05

return the value with this particular

221:07

method to R1 so that's right we got R1

221:10

as 7. I hope this makes sense

221:13

cool uh so we have done the changes

221:16

there as well so R1 is now seven

221:19

what else

221:21

now what happens to this method ad we

221:24

have called it right so there is a

221:26

special method area where you have a

221:28

different stack of calling methods so

221:30

when main call add add will execute

221:33

First Once ad is over it will be gone at

221:35

this point let's not talk about the

221:37

function calling let's only focus on the

221:39

data areas so basically this is what we

221:42

do there right but what happens with the

221:45

object here now that's tricky and what

221:48

happens to this num variable where do we

221:51

put in a variable in the add stack in

221:53

the main stack no if you can see num is

221:57

not a part of add is not a part of main

222:00

the beaut is you can actually print the

222:03

value of or you can use the value of you

222:05

can use a variable num inside a method I

222:08

can actually print the value of num here

222:10

so maybe I can set the value as let's

222:13

say 5 and I can actually use this num

222:16

inside a method see using a particular

222:19

variable and the declaring the variable

222:21

inside a method are two different things

222:23

at this point we are using it we are not

222:25

declaring it inside a method N1 and N2

222:28

they are declared inside the add method

222:30

but not num and that's why if you if I

222:32

if I can show you if I compile and run

222:35

it still works you can see we got num

222:37

which is five so we can use it but the

222:39

question is where it resides not does it

222:42

resides in add stack or main stack the

222:45

the answer is and that's where we have a

222:48

heap memory there right now inside the

222:50

Heap memory what happens is just focus

222:52

on the line number 20 here on line

222:53

number 20 what we're doing is we are

222:55

saying calculator obj now if remember we

222:58

have talked about this multiple times

222:59

this obj is not an object now people

223:02

coming from C plus plus you might be

223:04

having this urge of calling obj's object

223:07

but no that's a reference variable but

223:10

that's a variable right now this

223:11

variable where it will get created it

223:14

was declared inside main so of course we

223:16

have to say o b j here

223:19

but the question is what will be the

223:21

value

223:22

now for that you have to understand what

223:24

is happening here now when you say new

223:26

calculator it will simply create a new

223:29

object where it will create the object

223:32

inside the Heap memory this is where you

223:35

will get your object

223:37

and inside this object you will be

223:39

having two things actually so let me

223:40

just draw the first object quite bigger

223:42

and later on we can draw it small so

223:44

this is your first object the object

223:46

will have two sections the first section

223:49

will have all the variables all the

223:52

instance variable not local variable you

223:54

know why because local variables are

223:56

part of a stack so the part of the stack

223:59

not of Heap but if you talk about the

224:02

instance variable like we have num here

224:04

they are part of the Heap memory and

224:08

this is where you can say variable name

224:10

or num the value for that is let's say

224:12

five we have assigned five right

224:14

and then this particular object will

224:17

also have methods and this in this Heap

224:19

we are saying this is an object of

224:23

calculator it will also have the method

224:25

declaration so it will have ADD method

224:26

but then it is only have the definition

224:28

of it the actual area which ad will

224:32

consume will be of Stack okay if that

224:35

makes sense of course we can have extra

224:37

methods as well at this point we only

224:39

have one now this particular object will

224:41

also have its own address let's say the

224:43

address is one zero one of course right

224:45

when you store the data it has to get

224:47

some address now that's one zero one of

224:50

course it will be a complex address at

224:51

this point I just want to keep it simple

224:53

one zero one now this one zero one will

224:56

be stored in the stack so every time

224:59

when you say obj dot add the actual

225:03

execution starts from from stack itself

225:05

you say okay someone is trying to call

225:07

example if you talk about line number 21

225:08

here we are saying obj dot add that

225:12

means we are using obj object to call

225:15

the admit method but it belongs to

225:17

orbital object right which is the object

225:19

here

225:20

so it will simply use this particular

225:22

address

225:23

it will jump to the object and it will

225:25

call the add method so can we say there

225:28

is a link between stack and hip yes

225:31

because of the address right and that's

225:34

why you can use this object multiple

225:35

times and that's important in future we

225:38

are going to talk about Anonymous object

225:39

as well and we'll discuss why this link

225:42

is important and it will make much more

225:45

sense at this point just imagine there's

225:46

a link every time you say open J dot add

225:48

you are going from Main looking at the

225:53

stack then you are jumping towards the

225:54

Heap to look for the object I hope that

225:57

makes sense the good thing is we can

225:59

actually create multiple objects I can

226:01

just copy this line or maybe I can say

226:02

code reuse and I can create another

226:04

reference which is obj1 and we got a new

226:07

object here that's right in total we got

226:09

two calculator objects so with this

226:12

change is what we are getting is we will

226:14

be getting a new object here let's say

226:17

the address is 105 and it will have the

226:21

method it will have it will also have

226:22

num variable and the value will be 5 of

226:24

course you can change it and you can

226:26

have ADD method okay this is num okay

226:29

not n but imagine that's num and you'll

226:32

be having obj one here because in main

226:34

we have one more Declaration of obj1 it

226:36

will have the address 105 and you can

226:38

imagine there's a link between stack and

226:40

Heap okay now when I say these two

226:43

different objects have two different

226:45

values or two different num variable but

226:47

they have the same values of now right

226:48

example if I come back here you know

226:50

what I will do just to simplify this

226:52

thing more I will just comment all the

226:54

other print statements

226:57

to see only one output so here what I

227:00

want to do is I want to print obj dot

227:01

that's right if you want to even use the

227:03

variable the instance variable you have

227:05

to use the object name which is obj1 in

227:07

fact the first object is obj the second

227:09

object is obj one dot num so you can see

227:14

we got two objects and we are printing

227:16

the value of it each num value and if I

227:18

run this code you can see we got five

227:20

and five you know why because that's the

227:22

value you have assigned now my question

227:23

to you is before printing the value now

227:26

if I say obj dot num is equal to eight

227:30

you can see I'm changing the value of

227:32

only one object what do you think will

227:34

it reflect on both the object or only

227:35

one object let's try so when I say

227:38

compile

227:39

and as one you can see it is impacting

227:42

only one object it's because there are

227:44

two different objects right example

227:46

let's say if I have this phone this is

227:49

Samsung and let's say I have the exact

227:51

other Samsung phone

227:52

and one of it so let's say if the glass

227:55

of one phone is is broken will it affect

227:58

the other other phone of course not

227:59

there are individual phones and damaging

228:02

one phone will not affect the other

228:04

phone

228:04

so that's the important point to

228:06

remember here so yeah that's it about

228:08

the data area so they have different

228:11

areas we have stack and Heap in this

228:13

video we'll talk about

228:15

adding now first of all why do we need

228:17

an array so in this video we'll talk

228:19

about the need of an array if we talk

228:21

about a variable so normally when you

228:23

create a variable it's very simple right

228:24

in a variable you can store a value now

228:26

this can be of type integers flow double

228:28

uh string doesn't matter you have a

228:31

variable and you can store values in

228:32

that example let's say if you talk about

228:34

let's say int variables so how do we do

228:36

that how do we work with into variable

228:38

so we say int I is equal to 5. so what

228:42

happens is you have a variable here

228:44

which is I which has a value of 5. what

228:46

if you have one more variable here which

228:48

is let's say J and it has a value which

228:50

is six maybe one more you have end K is

228:54

equal to seven so you can see we have

228:55

three variables here and you have three

228:58

values now first of all why do we need

229:01

three variables here is because we have

229:03

three values what if you can save all

229:07

these three values in one variable now

229:09

why I'm doing that is because the moment

229:11

you have more variables you have to work

229:13

with multiple variables you have to

229:15

remember those variable names uh you

229:16

have to document them instead of that if

229:18

you can save that in one particular

229:20

variable it will be easier for you to

229:23

maintain the data and also it's not just

229:26

three values you may you might have ten

229:28

values you might have 20 values you

229:30

might have 100 values you don't want to

229:31

create 100 variables right so for that

229:34

we can use a concept called array here

229:36

now how it works we can create a simple

229:39

variable let's say num and in this num

229:42

instead of storing one value I can store

229:44

three values so let let's say if I have

229:47

five six and seven I'm storing these

229:49

three values of course we can put comma

229:51

to separate them and then we can put

229:54

them in a curly brackets to represent

229:56

that this is a bunch of values I want to

229:58

store and I want to assign that to a

230:00

variable num now being a simple integer

230:03

variable it will not work you cannot

230:05

simply take a integer variable by

230:06

assigning four values reason the moment

230:09

you create a variable here it occupies

230:12

some space now if you come back to this

230:14

particular variable here of course this

230:16

will occupy some space now in that

230:18

limited space you can't store multiple

230:21

values right you have to mention

230:22

somewhere hey this particular num here

230:24

is not a normal variable it's a a

230:27

variable which can hold multiple values

230:29

and with the way you do that is by

230:31

mentioning a square bracket you

230:32

mentioned hey that's an array which can

230:35

hold multiple values now if you want to

230:37

imagine this you can imagine a array as

230:39

a tray of Cups so let's say from a

230:41

kitchen if you want to carry four cups

230:44

uh with you to the hall of course you

230:47

can carry one cup at a time or maybe two

230:48

cups because you have two hands or maybe

230:51

you can carry just four cups by dipping

230:53

your fingers in the cup but the ideal

230:55

way is you can carry a tray now that

230:58

tray becomes your array which can hold

230:59

multiple cups so that's your array so

231:03

why do we need to add it of course if

231:05

you want to store multiple values in one

231:07

particular variable we can use array

231:09

here the beauty is it's not just limited

231:11

to three values you can store 100 values

231:13

it's just that once you define the size

231:16

of an array you have to stick with it

231:18

example let's say uh if you want to

231:21

define a array let's say num1 and if you

231:25

want this to be an array and you don't

231:27

have initial values here we at this

231:29

point if we talk about these three

231:30

values you know what are the values are

231:32

but what if you don't know the values in

231:34

that case you can say new int array so

231:39

you can specify okay so I don't know the

231:41

values it's just that I want to create

231:43

an array this is how you do it I want to

231:45

create a array and you have to also

231:47

mention the size here let's say four so

231:49

this particular num1 will have four

231:52

values not more than that okay it's a

231:54

size is fixed you can't increase

231:55

dynamically now once we know why do we

231:58

need an array let's continue with the

232:00

example now once we know why do we need

232:02

an array let's start the implementation

232:05

so in this video let's try to create an

232:07

array now how do we do that in fact in

232:09

the last video we have seen our basic

232:10

syntax so let's use it so we can simply

232:13

create a we can mention the type of the

232:15

type of area we are using which is int

232:18

and we can give a name to it let's say

232:20

nums and then we have to put a round we

232:23

have to put a square bracket to Define

232:24

that's an array equal to and then if you

232:28

already have the values here you can

232:29

assign the values itself or you can

232:32

specify the values dynamically so here

232:34

what I will do is I will put a color

232:35

packet here and since I know the values

232:37

I can directly put the values here let's

232:39

say 3 7 2

232:41

four so let's say we have these four

232:43

values okay and now I got an array with

232:47

four values it's it's that simple the

232:49

only problem is how will I use those

232:51

values how will I print those values

232:53

example let's say I want to print all

232:56

the values here or maybe even this first

232:57

value let's say I want to print three

232:59

because at the start if you remember uh

233:02

when we when I mentioned that instead of

233:03

using four variables I can use an array

233:05

in the four variables it actually makes

233:07

sense because every value will have a

233:09

different variable name it's easier to

233:11

fetch here how will you fetch the first

233:13

element from an array now what happens

233:17

is if I want to represent that array in

233:19

a memory structure what happens is

233:22

see if you have one variable you can

233:24

create one box right but if you have

233:26

multiple variables you have to create

233:27

multiple boxes here so one two three

233:30

four now this is your array which is

233:34

names numbers I've got the same code

233:36

which you are doing and then in this

233:37

array you'll be having these four values

233:39

which is three

233:40

seven

233:42

2 4. now what it means is if you want to

233:45

fetch the value of course in one

233:47

variable you have four values now if you

233:49

want to differentiate between all these

233:51

values you have to use some numbers

233:52

right some index numbers uh the way you

233:55

do it for the table columns right so we

233:57

have numbers in the same way here we

233:59

have to give them numbers now if you are

234:01

assuming that this will be number one

234:04

two three four uh no that's not how your

234:07

computer works so add a numbering starts

234:10

with zero the point remember it starts

234:13

with zero zero one two three now those

234:16

are the index values in fact you know

234:18

what should I do I should convert that

234:21

into different color 0 1 2 3. now this

234:25

numbers in yellow color are the index

234:27

index numbers for the array okay so

234:30

three seven two four there are the

234:32

values but 0 1 2 3 is the index that

234:35

means if I want to fetch

234:37

the values I have to mention nums in

234:40

describe but you have to mention the

234:41

number which index value you want to

234:44

fetch I will say I want to fetch zero

234:45

now in this case if we do that let's

234:48

compile and run you can see we got three

234:51

this is working so whatever value you

234:53

have here you're able to print it here

234:55

what if I want to change this I want to

234:57

print let's say I want to print the

234:59

value at number one

235:01

so compile run you can see we got seven

235:03

so basically you can actually print this

235:06

values one by one

235:08

okay that sounds good

235:10

uh how about updating the value so let's

235:13

say I want to change the value 7 here I

235:16

want to set 7 to 6. so basically I want

235:20

to change the second value and I want

235:22

the value to be 6. in that case you can

235:25

say index off so when I say second value

235:27

it's actually one because the numbering

235:29

starts with zero so to say nums of 1 is

235:31

equal to 6. now that's how you change

235:34

the value and now if you try to fetch

235:36

the same value here let's compile and

235:39

run you can say we got six

235:42

I hope that makes sense uh so basically

235:44

you can assign the value you can print

235:46

the value as well

235:48

what if you want to make it dynamic

235:51

okay how do I do that I don't have this

235:54

values let's say and then still I want

235:55

to create an array in that case instead

235:57

of using this syntax here what you can

236:00

do is you can say new int and in this

236:03

square bracket you can mention the

236:04

number the size of the array which is

236:05

full now this is another syntax the only

236:08

thing is by default all the values of

236:10

this array will be zero so of course

236:12

you've got four boxes but the values are

236:14

zero and if I don't set the value here

236:17

let's check what is the current value

236:20

for the index number one which is zero

236:24

so all the four values are zero uh I

236:26

want to change the value so what I can

236:27

do is you can specify I want to change

236:29

the index value 0 to let's say number

236:31

four and the nums of index one so every

236:36

every index have to set the value

236:38

let's say eight and nums of 2 is let's

236:42

say three I'm just giving random values

236:45

okay it's not like I have all these

236:47

values prepared in my mind but let's say

236:50

for three the value is nine okay so we

236:52

got these values here now how it looks

236:55

like in the memory it's very simple you

236:57

got an array with four boxes so the

237:00

index value is zero one two three and

237:03

the actual value so before assigning the

237:04

value of the values was Zero zero zero

237:07

zero but now since we have assigned the

237:09

value of the number the value has been

237:11

changed the values are four eight three

237:15

nine okay yeah so for eight three nine

237:18

those are the values you have

237:20

now this is how you store the value now

237:22

if you can also fetch the value let's

237:24

see what happens so at number one uh

237:26

index number one we have value which is

237:28

eight and you can see we got eight so

237:30

this is working but what if you want to

237:32

print all the values now that will be

237:34

tricky uh let's try so what you can do

237:37

is if you want to fetch all the values

237:39

of course you can just print this

237:41

statement multiple times

237:43

right the only difference you have to do

237:45

is you have to say index of 0 index of

237:48

one index of 2 index of three and you

237:50

can simply fetch all the values right so

237:53

compile

237:54

and then you can see we got all the

237:55

values this is working yeah but don't

237:58

you think we are doing the same thing

238:00

multiple times it's just that the

238:01

numbers are changing that to in sequence

238:04

you know what we can do here that's

238:06

right we can use a loop so I can just

238:08

remove all the extra statement just by

238:10

using one statement I can put that into

238:13

a loop now the main question is when I

238:16

start a loop here the loop will start of

238:19

course you have to say int I but what

238:21

should be the initial value for I

238:24

it should be zero you know why because

238:26

the index value starts with zero and

238:28

then less than should I say equal to

238:31

we can we just have to say 3 there

238:33

because the size of the array support

238:35

you have to stop at 3 and I plus plus

238:37

that's one way or you can say less than

238:40

four whichever format you follow

238:42

okay and then just put that into a curly

238:46

brackets now we can just shift this

238:49

statement in the block so that it will

238:50

execute so basically we are using a

238:52

forward Loop to to print this now the

238:55

only thing is if you can say we are

238:56

mentioning 0 which means it will the

238:59

loop the loop will run for four times it

239:02

will simply fetch the index 0 I mean

239:05

zeroth element every time or first

239:07

element every time we have to change

239:09

this we have to make it I so that the

239:11

value of I keep changing here and you

239:13

will get new value at a time every time

239:15

so compile and run you can see we got

239:19

the values so this is how you can use an

239:21

array of course there are a lot of

239:23

things related to array how can we

239:25

create multi-dimensional are they can we

239:27

use a different kind of loop so till

239:28

this point we were able to understand

239:30

what is an array right so basically we

239:32

have seen this example where uh you know

239:35

we have an array which has four elements

239:37

right we have three seven two and four

239:39

now if you look at this can you say this

239:42

is array of elements of course it makes

239:45

sense right so we can say this is array

239:49

of elements now can I create something

239:53

like this let's say I want two different

239:56

arrays so I got this as a first array

239:58

let's say four elements and whatever

240:00

elements we can have here let's say five

240:03

two six one so we got this array and

240:07

then I also want one more array here

240:10

right let's say we got in fact we need

240:12

two molari and of course they will have

240:14

some values it doesn't matter I'm not

240:16

concerned about the values as of now we

240:18

have some values there okay and of

240:20

course every array need to have a name

240:22

for it so can I say this is uh N1 array

240:25

this is N2 array this is N3 array we can

240:28

give any name to an array right in this

240:30

case I'm just naming them as N1 N2 and

240:33

M3

240:34

okay so if you're going to have multiple

240:36

values as one array can we have multiple

240:41

arrays combined to form one big array

240:44

what I'm saying is can we just have an

240:48

array of array something like this that

240:52

looks weird but can I use a different

240:55

color just to make you understand this

240:57

what if you can just have an array

241:00

so this is a big array which has three

241:03

elements and this particular array the

241:06

bigger element let's say the outer array

241:07

we name it as uh

241:10

nums so this nums array will be having

241:13

three different arrays

241:15

that sounds good right now this is

241:18

called

241:19

multi-dimensional array so ignore my

241:21

spelling that but this is called a

241:23

multi-dimensional array now how will you

241:25

represent this it's very simple

241:27

uh if you want to represent this we can

241:29

simply say hey I have a nums array which

241:33

is multi-dimension which will have array

241:34

of array the way you can achieve that is

241:36

very simple you just have to put two

241:39

square packet because one square packet

241:41

represents that's an array now can we

241:44

say the nums array is of array of array

241:48

yes we can and we have to mention the

241:50

type as well so it will be of type

241:52

integer let's say and then you have to

241:54

say new and you have to mention again 2

241:57

square bracket now but what will be the

241:59

values here see if you have a single

242:01

array it's very simple you can just say

242:03

the size is 4 but here if we talk about

242:07

the size how will you define it so if

242:08

you compare this particular thing can we

242:11

say we have one big array which has

242:13

three internal arrays to it so can I say

242:16

this is three internal analysis and each

242:19

array has four elements let me repeat

242:22

in this big array nums we have three

242:25

small arrays and each array has four

242:28

elements okay that's how you define it

242:30

now how will you assign a value to

242:32

everything so what you can do is if you

242:34

want to represent that in a good diagram

242:37

here what I can do is I will just draw

242:39

that once again

242:40

so we have one big box and in this box

242:45

we have three sections and each section

242:48

has array

242:50

and each array has four elements one two

242:56

three one two three simple right now how

243:00

will you give numbers to it so outer

243:02

array will have number as well so it

243:03

will be 0 0 1 2 and each array inside

243:08

will be having their own indexing uh

243:11

let's say 0 1 2 3. now let's say if I

243:14

want to represent a particular element

243:15

here now what element so let me assume

243:18

it what element I'm talking about let's

243:20

say I want to Target uh this element in

243:23

fact let me just fill some values here

243:24

five two six one uh three two one seven

243:31

four eight one two random numbers okay

243:34

nothing in my mind I'm just giving

243:35

random values Here and Now I want to

243:38

Target let's say the first value five

243:39

how will you target five what will be

243:42

the output for how will you find this

243:43

value

243:44

so 5 here becomes 0 comma 0 simple right

243:47

because this is zeroth row zeroth column

243:49

uh if I want to represent this six so

243:52

this 6 would be 0 comma 2. if you want

243:55

to represent this 7 so what do you think

243:57

in your mind if you can think about this

243:58

this will be 1 comma 3 and if I want to

244:01

represent this 4 here this will be 2

244:04

comma 0 this 2 would be

244:07

2 comma three okay that's how you

244:10

represent the values here quite simple

244:12

right so let's try to create this in a

244:14

code now so what I will do is I will

244:16

just remove all the extras things from

244:18

here I don't want student anymore and

244:20

let me just simplify this stuff all the

244:23

code gone

244:24

and I'm I'm just assuming that you are

244:26

doing The Code by yourself after

244:28

watching each video and that's how you

244:30

do it right it's not just about watching

244:31

videos it's also about implementing it

244:33

uh so what I will do now here is I will

244:35

just create a array of nums but this

244:38

will be a two dimensional array or a

244:40

multi-dimensional array and I can say

244:42

ain't

244:43

here so we want three rows and four

244:46

columns now there are different ways of

244:48

saying there's three rows four columns

244:49

or I can say we have three rows and each

244:52

row has one particular array of size

244:55

four as simple as that and here I can

244:58

just come back and I can by default all

245:01

the values are 0 right and in fact let

245:03

me just do that let me print all the

245:04

values now how do you print it we can

245:06

use a loop here now the only thing is we

245:08

have to use two loops

245:10

or nested Loops we have talked about

245:12

nested Loop before as well but let's try

245:15

so what I will do is I will say int I is

245:17

equal to 0 and I less than equal to okay

245:21

I less than now what should be the value

245:22

it should be 3 for the Outer Loop and

245:25

then I plus plus and then for the inner

245:27

loop see the outer loop will hover

245:30

towards all the rows now we have to go

245:33

inside the row as well so to go to

245:35

inside the row we have to say I ain't I

245:38

mean J is equal to 0 and J less than 4

245:42

and J plus plus

245:45

and here let me print the value so I

245:49

will simply say nums of

245:51

so what is representing the outer loop

245:53

it's I which represents the outer box

245:55

and J for the inner inner box because

245:58

for every row we have to start from 0

246:00

again right example if you see here

246:04

uh it will start with 0 and then for

246:07

every row it will go for 0 1 2 3 right

246:11

once you reach 3 here it will jump to

246:14

the next row and again it will start

246:15

from 0 1 2 3 then it will jump to the

246:19

last row and then again it will be 0 1 2

246:21

3. right so if you want to represent a

246:24

number you have to say nums of I and J

246:29

because I represents 0 and J represents

246:32

every column here okay let me just run

246:33

this code let's see what happens I will

246:35

say clear

246:36

compile and run you can see we got all

246:40

zeros

246:41

maybe you wanted to print this in a box

246:43

structure what you can do is instead of

246:46

printing Ln because Ln will point on a

246:49

new line you can say let me print a

246:52

value and let me also give a space here

246:55

for after every number and then once you

246:57

complete the inner loop once you print

246:59

one row just come on the new new line so

247:02

I will say just print again it will come

247:04

on new line

247:05

let's see if this works compile

247:08

run oh we got it so we got a two

247:11

dimensional array right the only thing

247:13

is I want different values here so I can

247:16

assign but I can assign them manually I

247:18

think this is a good time to talk about

247:20

how do you fetch the random values so

247:22

you know we have this math class in Java

247:25

which will give you some random values

247:29

right so this is a inbuilt class which

247:32

will generate a random value and to

247:34

understand that let me just go to the

247:37

random function random function returns

247:39

a double value so it returns a double

247:42

value if I store that value in temp or

247:46

maybe random the variable name is also

247:48

random the only thing is will not get

247:50

integer value and also I guess the

247:53

random function gives you a value which

247:56

is in points decimal points greater than

247:59

or equal to yeah basically it will give

248:02

you value less than one so what I can

248:04

simply do is I can multiply the number

248:06

by 100 let's see what happens so

248:08

whatever value I receive I will just

248:10

multiply that by 100 again right I'm

248:13

just doing some experiment here now once

248:15

you multiply that we have to also

248:18

uh do a typecasting now we have to

248:20

convert that double value into int now

248:22

whenever you want to convert something

248:23

we can use something called a type

248:25

casting so double value will get

248:26

converted into int here and I don't want

248:29

this to be generated on only once so

248:31

let's change the value for I will just

248:34

cut this part I will use it later

248:35

initial value for random is zero

248:38

and I want to find the new value every

248:41

time to assign the values here

248:43

and I can simply use the same Loop right

248:46

let me just print that statement

248:48

so that I will have it somewhere

248:50

and I will just copy and paste it here

248:53

to assign the values this time I will

248:55

not print values I will assign the

248:58

values

248:58

so I can say nums of

249:01

I and J is equal to

249:07

this value this formula I think this

249:10

should work I I just I'm not sure what

249:13

will be the value it will assign but no

249:15

we don't even need this variable

249:16

basically

249:18

but how do I know what value it is

249:20

printing so let me also print the value

249:21

of what it is assigning so that I will

249:23

know what is happening so compile

249:27

and run oh it's assigning zero values

249:31

why I think it should be typecasted

249:35

after we do the calculation compile run

249:38

oh got it so you can see we got this

249:40

random values uh so what was happening

249:43

before is we were typecasting the random

249:45

values and then we were multiplying by

249:47

100. what you have to do is since random

249:50

will give you in point values like 0.12

249:52

or

249:53

0.74 so all these zero point values we

249:57

just have to multiply that by 100 so

249:58

that you will get a double digit number

250:00

at least and you will simply Typecast it

250:04

later of course it will have 72 points

250:07

on values I'm just typecasting it

250:09

and then yeah so let me just remove this

250:11

printing so that you will get the exact

250:14

box here

250:15

compile and run if you want single digit

250:19

I think multiplying by 10 should work

250:23

I'm just assuming yeah A1 multiplying by

250:26

10 works because we only want single

250:27

digit

250:29

yeah that's how you Generate random

250:31

values that's how you print it

250:33

the random values are fun yeah so one of

250:36

the library which we have used is math

250:38

okay we can do one more thing here so

250:41

while printing we have used a normal for

250:44

Loop right can we use uh enhanced for

250:47

Loop can we let's try so in enhanced

250:51

model what you do is you use a for Loop

250:53

and then from the array it gives you the

250:56

first element right now coming back to

250:59

our drawing board here if we talk about

251:01

this particular array or this array here

251:04

it will give you one value at a time

251:07

right so we can simply accept that in an

251:09

integer variable but what if two

251:12

dimensional array here if we talk about

251:14

hey nums I want the first element Norms

251:17

will throw the entire array to you it

251:20

will not throw one element it will throw

251:21

the entire array and that's what you

251:23

have to handle so here you can say ain't

251:25

I will say n

251:27

now n here is not a simple array or it's

251:31

not a simple variable it's an array

251:32

itself it is coming from nums okay so

251:36

let me repeat n is not a simple variable

251:39

it's an array a single dimensional array

251:41

so we have to say with square bracket

251:44

now once you get the array from that

251:47

array we have to get the first value

251:50

so we have to use one more Loop here and

251:53

we have to say m colon n now m is taking

251:59

the value from the array of N and then

252:01

we can simply print the value so I will

252:03

just remove this bracket I will say

252:05

print M because that's the element and

252:08

with that I also want to give a space

252:11

and now here I just want to write into

252:13

in the second line okay so both are

252:16

doing the same thing let me prove I'll

252:18

clear this

252:19

compile run so this is printed by the

252:23

first Loop this is printed by the second

252:25

Loop the enhanced for Loop so that's why

252:28

we use two dimensional array with

252:29

different types of Loops so let's say we

252:32

have talked about multi-dimensional

252:34

array right now in multi-dimension Array

252:36

of course you specify the number of rows

252:38

you want so if you look at the code here

252:40

we have specified what are the rows we

252:43

have and in each big array we have add a

252:47

small array which is of size 4 right

252:50

and then we were assuming that every row

252:52

or every array inside the array will

252:54

have the same size example if you look

252:57

at this three

252:58

areas here they have the same size but

253:01

what's the guarantee that they will be

253:03

having the same size what if you have an

253:05

array here which is of size three uh

253:07

here you have a size four here you have

253:09

a size two can we do that

253:11

can we have the internal arrays or the

253:14

columns basically having less values

253:18

it is possible so what I'm saying is

253:21

this 4 is not fixed we can simply say

253:24

hey I don't know the number of columns I

253:26

have

253:26

yes we can do that so the internal array

253:29

or the inside array can have different

253:32

size of elements now this type of array

253:34

is called a jagged array so before this

253:37

we talked about normal or regular arrays

253:40

where you have you have to specify the

253:42

size at this point we are not doing that

253:44

see we have to at least specify how many

253:46

areas we have but internally those are

253:49

those rows we don't have to specify how

253:52

many columns we have

253:54

that we can manage but how how we are

253:56

going to manage so what we can do is

253:58

once you specify that I I'm not sure

254:00

about the number of columns here which

254:02

is dag data then we have to individually

254:05

specify how many columns you have what

254:07

I'm saying is you have to say index of 0

254:11

is equal to for the first ad you have to

254:14

specify how many values you have so you

254:16

have to say new and three so that's the

254:20

first thing we have and then likewise we

254:22

can specify the other values I can say

254:24

copy paste and paste for the first index

254:27

I have the size let's say four for the

254:30

second index I have a lesser size two so

254:32

that's why we to manage this one now

254:34

this is your Jagged array okay I mean

254:36

this one this is your jack Daddy not a

254:38

normal array because individually for

254:39

every column we are specifying what's

254:41

the size of it now if you if you try to

254:43

imagine this in a new structure let me

254:46

draw it here so you got a box this is

254:49

the outer outer thing and in this you

254:51

have three rows and each row will have a

254:54

box now the first box will be of size

254:56

three the second box will be of size 4

254:59

and the third box will be half size two

255:01

okay so we have different size of array

255:03

in different at different location so

255:06

for zeroth index we have three for first

255:08

index we have two or sorry four for the

255:10

second index we have two that's what we

255:12

have done here okay now once we specify

255:15

that the thing is how will you save the

255:17

value inside this now the problem is the

255:20

number of rows are fixed but I don't

255:21

even want to mention the number here the

255:24

way you can solve that problem is by

255:26

saying

255:27

Norms of or maybe you can say nums dot

255:30

length this will this will show you the

255:31

number of rows you have but for every

255:34

row okay in fact you know what we should

255:37

do we should save that value somewhere

255:41

um or maybe we can say this nums of I

255:44

for every row

255:46

I want to know the size

255:51

so now basically we have we have

255:52

specifically the size of each array so

255:54

for the first array I have three values

255:56

for the second I have four values so for

255:59

every row how many what's the length we

256:01

have we have here that's what we are

256:03

specifying

256:04

and now while printing as well instead

256:06

of using normal for Loop let's use

256:08

enhance for Loop and the beauty about

256:09

now enhance for the paste you don't have

256:11

to mention the size anywhere it will

256:13

automatically detect where the values

256:15

are

256:16

okay so if I try to

256:19

compile this code and run you can say

256:22

this is the values you have the first

256:23

row we got three second row we got four

256:25

and the third row we got two okay now

256:28

this is the jagged arrays so once we

256:31

know about Jack Douglas let's go back to

256:32

normal array what we can also do is we

256:34

can skip this part now can we create

256:36

three-dimensional array we can every

256:38

time you want to increase the dimension

256:40

you just have to specify extra square

256:44

bracket and extra Loops for every every

256:46

square bracket so now you can imagine if

256:48

I want to draw this I don't know how do

256:49

we how do we draw this but let's try so

256:52

we have the outer box in which you

256:55

specify how many rows you have we have

256:56

three rows zero one two in this you have

256:59

an array of size 4

257:02

so one two three four and in each box

257:06

you have one more array here which is of

257:08

size five okay let me just do that in a

257:11

small let me expand this

257:13

and in this we have one more array of

257:16

size five okay uh this is index number

257:19

zero one two three four and then this is

257:24

zero one two three so if I want to fetch

257:27

this value here which has value X is six

257:29

if you want to fetch this six you have

257:31

to say zero comma zero comma two right

257:33

and of course in every box here we'll be

257:36

having the same thing we have the box

257:38

here which has four elements and each

257:40

box will be having multiple arrays yeah

257:43

so we have added here as well and if I

257:45

want to fetch let's say the third

257:47

element here we have to say one comma

257:50

one comma three okay that's how you

257:52

fetch it so the more array you create it

257:55

will create a box internally to it so

257:57

that's how the array Works uh now this

258:00

is not jacked anymore this is

258:02

three-dimensional array so I hope this

258:04

makes sense uh we have talked about Jack

258:06

that is and three-dimensional in one

258:08

video in this video let's talk about the

258:10

drawbacks of array of course we have

258:12

seen a lot of good things about array we

258:14

can store multiple values and then we

258:16

had some fun with the multi-dimensional

258:18

array and the 3D array Jack that is but

258:22

let's talk about some drawbacks what I

258:24

will do for that is let me remove the

258:25

entire stuff and let's create a single

258:28

array here so let's say when you create

258:30

an array of norms and let's say this is

258:32

of int type so of course you have to say

258:35

new int and you have to give a bracket

258:38

under this was for the size four now

258:40

first of all we

258:44

all beeping Java is an object okay the

258:45

moment you're saying new keyword that

258:46

means it's an object right it will be

258:48

created inside the Heap memory but what

258:50

it does is the moment you say this line

258:52

inside the Heap memory it will occupy a

258:55

space and the memory location it will

258:57

take is Conti it's continuous okay which

259:00

means it will simply occupy the space

259:02

continuously and the problem with this

259:04

is once you specify the size here you

259:07

cannot go back so once you say four now

259:09

you cannot expand the size it's fixed so

259:11

in future if you want to create or if

259:13

you want to expand the array you can't

259:15

see one of the solution what you can do

259:17

is you can at that point if you want to

259:18

say hey I want to increase the size of

259:21

the array just create a new array of

259:23

that size and copy all the elements

259:24

that's a solution but it will also

259:26

consume some time and there will be some

259:29

computation will be happening so if you

259:31

know the size is fixed you can go with

259:33

add a the second problem is every time

259:36

you want to search something it will

259:37

Traverse between the elements so for

259:39

searching for inserting value it does

259:41

consume a lot of time the last problem

259:44

which is we we mentioned about Interior

259:47

right but what if you want to have an

259:49

array of different types let's say if

259:51

you want to have integer string double

259:53

in the same average unfortunately you

259:56

can't do that I mean there's a solution

259:58

uh if you want to have object of student

260:00

integer string there is a solution of

260:04

object but that's not that's not an

260:05

optimal solution so again they are not

260:09

really a drawback what I've talked about

260:11

they that's how the array works but then

260:13

you know when you say something is

260:15

drawback you say that's a drawback

260:16

because there is something new which

260:18

came up which has all these features now

260:21

the concept which I'm talking about is

260:23

collections now this is not a good time

260:24

to start with collection it will come in

260:26

later but uh just because of collections

260:29

we are talking about some drawbacks of

260:31

adding but other Addies are great they

260:33

are fast you can store multiple values

260:35

but yeah there are some things which

260:37

might have been done to improve it but

260:40

that's where we have collections right

260:42

we'll talk about collection later and

260:44

it's not like just because of collection

260:45

will not be using ad anymore they have

260:47

different use cases every time you know

260:49

you have a fixed size and if you want to

260:51

store multiple values as they will be

260:53

the first preference so once we have

260:55

talked about array I mean basically

260:57

which we have talked about why do we

260:58

need array and how to create an array

261:00

let's do something more with that in now

261:03

in fact in the previous video we have

261:05

seen how to create the array and then

261:07

how to print the values now in this

261:11

video let's try to see if we can find a

261:13

better way of printing this value see

261:16

what is happening now is let's say I

261:18

mean of course we know the length of

261:20

this array here right which is four ah

261:23

it can also be five or any numbers I

261:25

mentioned before right it can be 100 200

261:27

or whatever size you want to mention and

261:29

you can insert the value now before I do

261:31

anything else I just want to show you

261:33

one more thing what if the value of the

261:34

size of array is 6 and you are assigning

261:37

only four values here now in that case

261:40

the other indexes will also have some

261:42

values right let's see I just want to

261:44

see what are those values before we

261:46

continue

261:47

so compile and run you can see we got

261:50

the values but then the last two values

261:52

are zero remember we have talked about

261:54

this by default the values for the array

261:56

or integer array will be zero

261:59

and that's what is happening Here and

262:01

Now what I'm going to do is I want to uh

262:04

see if I can get this value see the

262:07

thing is why I should know the length of

262:09

the array it might also lead to errors

262:12

example if I say 7 here by mistake and

262:15

if I try to fetch the value see this is

262:18

where it will give you

262:19

an exception now till this point we have

262:22

not talked about exception of we have a

262:25

separate topic what are exceptions at

262:27

this point just remember exceptions are

262:29

runtime errors okay so the address which

262:32

comes in runtime at compile time there

262:34

is no problem when you compile this code

262:35

it will not uh blame you it will not

262:38

shout but the moment you run this code

262:40

it will say hey something is wrong here

262:43

so that's exception now what is the

262:46

problem the problem is we have six

262:48

values index number star starts with

262:50

zero it ends at five not at six and here

262:54

it says index 6 is out of bounds so

262:58

basically we are trying to go beyond the

263:01

limit okay so how do we solve this

263:04

problem so instead of specifying the

263:06

number hardware values we can do one

263:08

thing you know this arrays they have a

263:11

property called length so we can simply

263:14

use this length as a variable or a

263:16

property which will show which will tell

263:18

us what's the actual length of the so

263:20

that you will not make any mistake the

263:22

only thing you have to remember is you

263:23

have to put that you have to make sure

263:25

that it's less than not greater than

263:27

okay and I will I will clear this I will

263:30

say compile and run you can see we got

263:33

the values and there's no problem the

263:35

only thing is the size is 6 and then we

263:37

are printing only we are assigning only

263:39

four values let me go back to four

263:41

values and you can see we got four

263:43

values here

263:44

now this is one of the good addition in

263:46

terms of the property which you can use

263:48

so that's about length here now what we

263:50

can do is uh let's say if I want to have

263:54

a string array or maybe I let me create

263:58

a array of student can we do that uh so

264:01

remember one thing so if I create a

264:02

student here and of course I can create

264:04

one object I can get multiple objects

264:06

here and let's say I have a integer I

264:09

have a student which has some values so

264:11

let's say integer roll number

264:14

okay and the string name and int marks

264:18

of course not a good way to uh maintain

264:21

the records we don't want to maintain

264:22

marks but just for the example okay so

264:24

we have student and then we have these

264:26

three variables okay now mind you these

264:29

are the instance variable because they

264:30

belong to a class not to a method I just

264:32

want to comment the entire section here

264:34

and what I will do is I will create

264:36

student object so let me create the

264:38

first object I will say student S1 is

264:40

equal to new student and I want to

264:43

assign all the values to the student so

264:44

I will say S1 dot roll number which is 1

264:47

and S1 dot name which is let's say

264:51

Naveen and S1 dot marks which is let's

264:55

say 88 okay so we got the first object

264:57

now maybe I want to create more objects

264:59

what I can do is I can just copy this

265:00

code and paste and let's say three

265:03

objects so we want three objects here

265:04

and I will say this is two this is three

265:08

and let's say well number two let's say

265:10

this is hush and marks let's say 67 and

265:14

if I say this is 3 let's say this is

265:17

kitten and let's say the marks is

265:21

97. okay so we got these Marks here

265:24

right and then basically they have your

265:25

objects now I I made one mistake here if

265:27

you can I don't know if you have

265:28

observed but if you can see I'm still

265:31

assigning the values to S1 don't you

265:34

think it should be S2 S3 uh so it should

265:37

be S2 S2 and S2 and there here we have

265:42

to say S3 S3 and S3 so basically we got

265:46

three objects here now what I will do is

265:49

I want to create an array of students

265:51

right we have created the array of

265:54

integers but then how do we do that

265:55

array of students it's very simple you

265:58

say student and then you have to say

266:00

students because we are creating

266:01

multiple students here that's a variable

266:02

name equal to now this students here is

266:06

not one variable it's an array so I have

266:08

to say array and then we have to say new

266:10

now in this case when you say we have

266:12

INT in the same way here it should be

266:15

student

266:16

right student array of let's say three

266:19

students will be having so all these

266:21

objects you know we got three objects

266:23

here S1 S2 and S3 they will become part

266:26

of this array example I can say student

266:30

students of 0 is equal to S1 so the

266:33

first object the first reference is

266:35

getting stored here in the first index

266:38

and likewise we have this is S1 S2 and

266:42

this is S2 S3 so you can see we got

266:45

three objects in the array so of course

266:47

you need these objects otherwise this

266:48

will not work now if you are thinking at

266:51

this point we are creating present

266:53

object no we are not getting three

266:55

student object most of the people have

266:56

this confusion that on this line we are

267:00

creating three student object now we are

267:02

creating an array which can hold student

267:05

references it will not create those

267:07

object by itself

267:09

you have to manually create the objects

267:11

and assign to an array now this thing

267:14

will make much more sense when we start

267:16

working with databases when you fetch

267:18

data about a student it will give you a

267:20

lot of data you can basically fetch

267:22

those data in a student array now what's

267:25

the advantage the advantages let's say

267:27

if you want to print all the details of

267:28

a student so what you can simply do is

267:30

you can use an array which will start

267:32

with 0 and I is less than equal to okay

267:35

I is less than students dot length which

267:38

we have seen and then I plus plus and

267:41

then here we can simply print but how do

267:44

you print the values so what we'll do is

267:46

you have to print one value at a time

267:48

you have to say S1 because if you try to

267:51

fetch the value of a student it will

267:53

give you a address format okay example

267:55

let me show you something at this point

267:57

uh let me just comment this section and

268:00

let me print the student object itself

268:02

and let's see what happens so if I try

268:04

to print the S1 value

268:07

you tell me what will happen what the

268:09

what will be the output

268:11

I mean think about that I will just

268:13

compile this and run you can see it is

268:16

not printing the value of a student it

268:18

is printing something else see in the

268:20

upcoming videos will solve this problem

268:21

at this point it's printing the address

268:23

and we don't know what this is so what

268:26

we can do is explicitly you have to

268:28

mention S1 dot name and you have to

268:31

maybe you can do a concatenation here

268:33

and you can say S1 dot marks I just I

268:36

don't want to print the roll number at

268:37

this point so I'm printing on you this

268:38

too and now if you compile and run you

268:42

can see we got the student details okay

268:44

so can we do it here so let me just

268:46

uncomment this section the only thing is

268:48

you have to print this particular

268:50

statement cut

268:52

and paste now the only problem is it

268:54

will always refer to S1 we don't want to

268:56

refer to S1 right the student value will

268:58

keep changing so I can say students

269:01

square bracket I right so this value

269:05

will keep changing right this will be S1

269:07

S2 S3 in every iteration the same can be

269:09

done here

269:11

so I can say students of I that should

269:14

make sense and let's see if this works

269:17

let me go back here compile

269:20

and run you can see we got the values

269:22

right so basically we can work with

269:25

normal objects as well uh before this we

269:27

have worked with integer of course you

269:29

can work with string as well but we just

269:31

went uh one step ahead by creating an

269:35

array of students okay so I hope you got

269:39

some idea regarding how do we get an

269:41

array of students in this video we'll

269:43

talk about for each Loop now why do we

269:46

need another loop when we already have

269:48

this particular Loop here

269:50

okay for that let's understand let's

269:52

comment the entire section from here

269:53

because I want to use uh the simple

269:56

example which we already have here right

269:59

so if you remember we have worked with

270:01

this array which has four numbers so

270:03

let's work with normal integers now if

270:05

you want to print all the values this is

270:07

what we did right we used a normal

270:08

variable normal for Loop in which you

270:11

have a counter I which starts from 0 to

270:15

the last value of the length of the

270:17

array less than that which is three and

270:20

we were able to print all the values the

270:22

only thing is why you need to do the

270:26

counter and then you have you should

270:28

also know the length of the array and

270:30

then you have to specify the index value

270:33

here to fetch the value I mean this

270:34

works but then can we have a better

270:36

solution for this and that's where we

270:39

got an amazing Loop which only works

270:42

with array and array type of data so in

270:45

future we're going to talk about

270:46

collection as well and that also it is

270:48

used but since we only know addeds at

270:50

this point let's try to use another type

270:53

of loop now think about this can we just

270:55

say hey for Loop you know what I want

270:58

you to do

271:00

iterate between all the values and give

271:02

me one value at a time and of course I

271:05

will decide what to do with that value

271:07

example here as well we are able to

271:10

fetch a particular value of uh array and

271:13

then we can do whatever we want to do

271:14

with that of course at this point we're

271:16

just printing it but we have an option

271:17

of doing multiple things here as well I

271:21

would say hey for Loop you gave me a

271:24

value not the index number

271:25

give me the value

271:27

so what Loop says okay okay let me try

271:30

so whatever value of order will give you

271:32

you can store that in n a variable but

271:35

then from where you will get the value

271:37

you will get the value from the nums

271:39

with which is the array so I can give a

271:41

colon and we can say nums this is the

271:44

Syntax for for each loop I know this

271:46

will be confusing at the start but once

271:48

you get used to it you will love it what

271:50

is happening here is if you compare with

271:52

the upper Loop in the upper Loop we are

271:54

using a counter here we are not using a

271:56

counter we are simply saying from these

271:58

nums I it doesn't matter how many values

272:00

you have four values 10 values 50 values

272:02

I don't care you just give me one value

272:06

at a time and that one value will go

272:09

into n now you can decide what to do

272:11

with that at this point just to keep it

272:13

simple we are printing it okay so you

272:16

can see I just got the value n and I'm

272:18

printing it no I no counter no increment

272:22

no need to check for the length no

272:24

exception right and uh this is a beauty

272:28

about it it will just iterate between

272:30

the nums and iterate till the last

272:33

element so if you had five elements it

272:35

will iterate five times if you have four

272:36

elements it will iterate four times and

272:38

every time it will give the value to n

272:40

and that's what you are printing it here

272:42

let me prove that let me just compile

272:44

this code and run you can see we've got

272:47

the values

272:48

this is better right you can also use

272:51

this with the thing here which we have

272:54

done for student I will just comment

272:55

this part

272:57

and uncomment the entire section from

273:00

here

273:01

everything is uncommented the only thing

273:03

is I don't want to use the novel for

273:05

loop I want to use the enhance for them

273:08

so normally instead of saying that this

273:10

is a for each Loop we say enhanced for

273:12

Loop okay so what we can do is we can

273:14

say for and here okay the only thing is

273:18

when normal photo we use ain't because

273:20

we want to maintain the counter if we

273:22

talk about the example which we have

273:23

done for normal integers here also we

273:25

use we are using int because the type of

273:27

values in nums is int but now we are

273:30

working with students right so here when

273:32

you say student let's say stud colon so

273:35

we have to mention the student type not

273:37

int because from the students array you

273:40

are getting a student reference and you

273:43

will get the values from students so it

273:45

doesn't matter how many students you

273:46

have it will fetch one student at a time

273:49

and then with that student you can print

273:51

the same thing

273:52

so whatever you're printing here you can

273:54

print that here the only thing is this

273:57

two represent one particular student at

273:59

the time so you can simply say student

274:00

dot num and you can just concatenate so

274:03

what we are doing here is we are doing

274:05

concatenation the student name and then

274:08

we are giving a colon so to do that we

274:11

have to use a plus operator in between

274:12

to concatenate and I can also say stood

274:16

dot let's say marks let's see if this

274:18

works now we are working with student

274:20

not with normal variables compile and

274:23

run that's it you got the output so you

274:26

got Naveen and you've got number so this

274:29

is your enhanced for Loop yeah some

274:32

people do say it's a forage Loop but

274:33

let's call it as enhanced for Loop

274:35

because in the upcoming in the latest

274:37

version of java not the latest one I'm

274:39

talking about Java 8 which came way back

274:41

it has a forage method so just to not

274:44

get confused let's call this as a

274:47

enhanced for Loop now one of the reasons

274:49

why people say it's a foraged loop is

274:50

because in other languages this type of

274:53

Loops are called for it that's why in

274:55

Java also people have a tendency to call

274:56

it forage but let's call it enhance for

274:58

Loop so I hope you got the idea and I

275:01

would like you to work on this practice

275:03

this otherwise it will be confusing so

275:05

practice this multiple times and you

275:07

will get used to it it's time to talk

275:08

about a very simple concept and complex

275:11

at the same time because to use it's

275:12

very simple but when you try to

275:14

understand what is happening behind the

275:16

scene that's where it gets complicated

275:18

so let's try to understand that in this

275:20

topic of string so in this we'll just

275:22

talk about what is string now basically

275:24

we have worked with string before

275:26

example whenever you try to create a

275:29

variable we can have we have to specify

275:30

the type of it right example we have

275:32

worked with integer float double Boolean

275:35

in the same way if you want to store

275:37

a character stream let's say if you have

275:39

multiple characters something like this

275:41

let's say uh you want to store a name

275:43

like Naveen now this Naveen is not a

275:46

single character right it's a bunch of

275:47

characters and when you want to store

275:49

this together of course you can use a

275:51

character array we will talk about

275:53

either later but then we can create a

275:56

character array here which is a array of

275:59

different uh different characters or we

276:02

can create a string here now how do we

276:04

represent that in a string now one thing

276:06

to remember whenever you talk about

276:07

string in Java you have to put that in

276:08

double quotes the way you work with

276:10

characters in characters you use single

276:11

quote here we'll be using double quotes

276:13

So if you want to store Naveen so you

276:15

have to first put that into double

276:17

quotes and then where you will store

276:18

this of course you have to you you need

276:20

a variable here right so what I will do

276:21

is I will create a variable called name

276:23

now this variable name needs a type now

276:26

what type of this this is of type string

276:28

now one thing to observe when you talk

276:31

about primitive data types we have end

276:33

float double character uh Boolean and

276:35

many more right now all those actually

276:37

starts with small letter right uh this

276:40

string s starts with capital which means

276:43

that string in Java is not a primitive

276:46

type it's a class basically if you go to

276:48

string class you will find string is

276:51

actually a class

276:53

okay that actually makes some sense but

276:56

then when you say class we have seen

276:58

this right whenever you work with

276:59

classes and if you want to create object

277:02

of it we use a new keyword right we are

277:04

not doing it here so this does not look

277:07

like a correct syntax it works but it's

277:10

not something we are used to right

277:12

whenever you work with class we normally

277:13

create a new object

277:15

okay this works but then why this will

277:17

work that we'll discuss later at this

277:19

point if you want to go in a general

277:21

sense if you want if you have a class

277:23

the way you can create a string is by

277:26

the way you can get object of string is

277:27

by saying new string and you can give

277:30

the round packet now this is how you

277:33

create a string variable a string object

277:36

now basically this is a variable right

277:37

reference variable which is referring to

277:39

a string object now what might be

277:41

happening behind the scene so it's very

277:43

it's not that difficult uh if you

277:46

imagine this as your jvm The Big Box

277:48

inside this of course you will be having

277:51

your stack memory right now this is your

277:54

stack and then of course in this uh you

277:56

will be creating a reference variable

277:58

which is name and then it will have some

278:00

address but this address or the object

278:03

will be created inside Heap memory

278:04

imagine this is your HEAP memory you

278:06

know we should make this a big one this

278:08

time let's say a bigger Heap memory so

278:10

that I can write some multiple things

278:11

there now the reason we are creating a

278:14

heap memory in another structural format

278:17

is because there is no fixed size to it

278:18

it may expand depend upon your

278:21

requirement so we got a heap here now

278:24

when you talk about this line line

278:25

number eight when you say new string of

278:27

course it will create a new object for

278:29

you you got object here right and it

278:31

will have let's say some address I will

278:33

say the address here is one zero five so

278:37

of course you will create a variable

278:39

which is name here and then the address

278:42

is one zero five so there's a link here

278:45

and of course in this particular box

278:47

there is no data it's it's empty right

278:49

okay uh this empty makes sense actually

278:53

but then if you try to print this let's

278:55

try let's see what it prints if you try

278:57

to print this data here I will compile

278:59

this code and run you can see it spin it

279:01

prints nothing basically it prints blank

279:03

thing

279:04

okay it's because it's blank now how

279:06

will you provide data now in this case

279:08

what you can do is in this new string in

279:10

the Constructor you can pass this string

279:12

whichever you want to example if I want

279:14

to store Naveen that's the D time

279:16

storing ignore this original this is

279:18

given by the IDE but this is our value

279:20

which is Naveen so inside this string

279:22

Constructor you are passing the data

279:25

which is Naveen right now if you try to

279:28

print this you can say we got Naveen

279:30

here okay so that means in this thing uh

279:33

we are creating the data which is Naveen

279:36

that's what we are expecting as of now

279:37

let's see if that works okay now there's

279:40

also a concept of hash and equals right

279:42

so what I will do here is I will try to

279:44

print the hash code for this one so if

279:48

you try to print the hash code and if

279:50

you run this code you can say this is

279:51

the hash code for this depend upon data

279:53

depending up on some calculation now

279:54

what else we can do here so example

279:56

let's say if I want to concatenate this

279:58

Naveen with hello so let's say I want to

279:59

print hello Naveen what you can do is uh

280:03

of course you can write a different

280:04

statement to print hello and then you

280:05

can print the name but then string by

280:09

default does not work with any other

280:10

operators like subtraction division but

280:12

only there's one exception which is plus

280:15

orbital so you can use string with plus

280:16

operator

280:17

okay so let's say I want to print hello

280:19

and Naveen both so in this case you can

280:20

print hello and then to concatenate to

280:23

mix this two you will use a plus

280:25

operator here so you can simply say plus

280:27

and now with this compile and run you

280:31

can see we got hello nabin I hope that

280:33

makes sense

280:35

so you can use plus to do to print this

280:38

what else we can do uh we have certain

280:40

methods here which we can use now since

280:42

it's a class of course it will have some

280:43

methods right so there are some handy

280:46

methods here example you can check uh

280:48

what is a character of a particular

280:50

location so let's say I want to know

280:52

what is a character at location two I

280:55

mean index number one

280:56

so if I pass index 1 here

280:59

it will give me a character at index 1.

281:02

so if you look at this Naveen it is n is

281:04

index 0 a in is index one then two then

281:08

three then four and if you try to

281:10

compile and run this you can see we got

281:12

a so yeah this is how you can basically

281:15

use certain methods there which is

281:16

character add or you can also

281:19

concatenate two strings okay so let's

281:22

say if you concatenate my name with I

281:25

will give a space and ready so basically

281:27

what I'm doing is I'm concatening Naveen

281:29

with ready here let's see if that works

281:32

okay so that we got Naveen ready so

281:35

that's how we can basically use this now

281:37

since strings are so important for the

281:40

programmers what we got here is uh

281:44

let me just remove let me just remove

281:46

this line yeah so string is so important

281:48

for programmers what we got in Java is

281:51

instead of writing this new string you

281:53

can actually you know directly write

281:55

Naveen here even that works so behind

281:57

this it will create an object you don't

281:59

have to worry about it so when you say

282:00

string name equal to Naveen it will

282:02

create the object for you you don't have

282:04

to worry about object creation of those

282:06

stuff so most of the time you will see

282:08

this syntax hardly you will say uh

282:10

string name new string and then Naveen

282:13

that's hardly anyone do that so this is

282:16

famous and it even this works you can

282:18

still try this out and that's why before

282:20

when we were using string we directly

282:22

assign the value it was working so in

282:24

the previous video we have talked about

282:25

string right now we talk about this

282:28

string

282:29

uh what might be happening behind the

282:31

scene example let's say if you store

282:32

this Naveen of course it will be getting

282:34

stored in the in the hip memory but now

282:36

what I want to do is before printing

282:38

Naveen here let's say I want to append

282:39

something so let's I will say name is

282:42

equal to I want to add my name with

282:46

let's say ready here I'm giving a space

282:47

so that we can differentiate these two

282:49

words so of course now when I say name

282:51

it is will it will be Naveen ready right

282:53

so basically we are trying to change the

282:55

value now if we talk about integer float

282:57

double basically you can change values

282:59

of it right and it actually makes sense

283:00

that's why we call them as variables

283:03

and here also we are trying to do that

283:04

but first of all let's check if this

283:06

works

283:07

so if I go back and compile and run it

283:11

works you can see that the new data for

283:12

the Naveen is Naveen ready right and

283:14

that works but what if I say that's not

283:17

the case you're not changing the

283:19

existing string let me repeat

283:22

you're not changing from Naveen to

283:25

Naveen Reddy so if you're thinking there

283:27

will be a variable here where you have

283:29

Naveen you're making it Naveen ready uh

283:32

no that's not the case that's not how

283:34

string works okay so what happens so

283:37

what I would what I will do is I'll just

283:38

remove this part now so what is

283:40

happening basically

283:41

you know since strings are used heavily

283:44

in Java now most of the time you try to

283:46

reuse the strings the same string you'll

283:49

be using multiple times example let's

283:51

say uh if if before I show you something

283:53

let's say I create a string here which

283:55

is S1 is equal to Naveen

283:58

and you can see we have a capital M this

284:00

time so this two data are actually

284:01

different

284:02

okay so I will say string S2 is equal to

284:05

and I will say Naveen again now what do

284:08

you think how many objects we are

284:10

creating behind the scene think about

284:12

this okay so if you're thinking we got

284:14

two objects actually not

284:17

we got two references we got S1 S2 of

284:20

course in your stack you'll be having S1

284:22

S2 here

284:24

but

284:25

in total you got only one object

284:29

I know that sounds tricky here right and

284:31

let me prove that point to you uh see

284:34

what I want to prove is the value for

284:36

this field and this field will be equal

284:37

the way the way I can do that is by

284:39

saying s out S1

284:41

is equal to equal to S2

284:44

if this is true then they are same so if

284:48

I run you can say it got true that means

284:51

the value for the addresses are same in

284:54

total we got only one object so here

284:56

what happens is we have a special area

284:59

inside your hip memory so this is your

285:03

HEAP that's your jvm and that's your

285:06

stack so inside your hip you will be

285:08

having a special area called string

285:12

constant

285:13

pool and by the name itself you can see

285:16

it says constant that's right every

285:18

string which you create which you use in

285:20

Java which is a string literal is

285:22

actually a constant you can't change it

285:24

example if I if I write something like

285:26

Naveen here I cannot change it it's done

285:28

it's Naveen everywhere okay so you can't

285:32

change the data so example what happens

285:33

is every time you try to use a variable

285:36

example if you execute the line number

285:38

eight it checks okay we need Naveen

285:40

variable do we already have Naveen in

285:42

the string constant pool no we don't

285:44

have it so let's create Naveen here so

285:46

it will create a string variable Naveen

285:48

it will have some address let's say one

285:49

zero one and then the name variable here

285:52

will get this address so this name

285:54

variable is actually referring to this

285:57

particular address

285:58

okay then it goes for the next statement

286:00

let's execute this part Let's ignore

286:02

this line as of now let's focus on line

286:04

number 12 and 13. so line number 12 says

286:06

S1 is equal to Naveen okay so it checks

286:09

do we have Naveen with capital N in the

286:11

string pull no we don't have it so it

286:13

will create Naveen here and let's say

286:15

the address is 1 0 3 so it will store

286:18

103 here okay now that makes sense right

286:20

and that's why you're referring to that

286:21

particular location

286:23

now what happens on line number 13 line

286:25

number 13 says string S2 is equal to

286:27

Naveen it will go to string constant

286:29

pool again and check hey do we have

286:30

Naveen with capital n yes we do have now

286:34

it will not create a new object for you

286:36

it will simply use this address one zero

286:39

three okay and that's the beauty yeah

286:41

that's how you're trying to save the

286:43

memory otherwise you have you could have

286:44

created multiple variables with the same

286:46

data so it doesn't make sense right and

286:48

that's why it uses a string constant

286:49

pool now my question to you is what is

286:52

happening here now when I say you can't

286:55

change the data that means you cannot

286:56

simply update this particular Naveen by

286:59

Naveen ready no that's not happening the

287:01

wind is still there it will be it's a

287:03

constant you can't change this now on

287:05

line number nine what is happening is it

287:07

is creating a new object which will have

287:11

a data which is Naveen ready it let's

287:14

say it has the address which is 105. so

287:16

on line number nine basically what you

287:18

are doing is you are changing this

287:21

address from one zero one to one zero

287:23

five you're just changing the address

287:26

you're not changing the actual data you

287:29

are basically creating a new object okay

287:32

now you might be thinking what will

287:34

happen to Naveen here the thing is now

287:36

this particular object is eligible for

287:39

garbage collection it will be removed

287:41

after some time just to get some memory

287:43

back okay so Point remember is once you

287:46

create this object you can't change it

287:48

and that's why we have some terms to

287:50

remember and the terms are so there's a

287:52

concept of mutable strings and immutable

287:57

string now what is mutable means mutable

287:59

means change something which can be

288:01

changed and immutable means unchanged or

288:05

you cannot change it

288:07

so basically by default strings are

288:09

immutable once you create the object you

288:11

can't change it and that's very

288:12

important remember it's immutable string

288:15

okay so whatever data you're creating

288:17

here is immutable but what if you want

288:19

mutable string and that's why we have

288:21

two different classes in Java to use

288:22

instead of using string you can use

288:25

something called a string buffer or a

288:30

string Builder now both are very similar

288:33

with one difference that difference will

288:34

discuss later but they both are similar

288:37

and they both provide you a way to

288:39

implement a immutable string okay in

288:43

this video we'll work with string buffer

288:45

so what we have is we have a string and

288:47

where we have worked with it right so

288:49

what I will do is remove everything and

288:51

let's work with string buffer so I will

288:54

say string buffer and let's create a

288:56

reference for it equal to new string

288:59

buffer now when we talk about string

289:00

buffer of course in this string buffer

289:02

you will have a string and which is

289:04

actually mutable you can change it now

289:06

to make it more efficient you know what

289:08

string buffer does string buffer will

289:09

give you a buffer size okay which is of

289:12

16 bytes so I can just try to print that

289:14

first I will say SB Dot ah so we can

289:17

know the capacity of this string buffer

289:19

even if it is empty it will give a

289:20

capacity of 16 characters and you you

289:22

got it there in fact you can also

289:24

provide some initial string here so

289:26

let's say uh if I say Naveen here now

289:28

Naveen is of five characters right this

289:30

is how you basically create a string

289:31

buffer in bracket you mentioned this

289:34

string so it will create a string of the

289:36

object and the beauty is you can change

289:38

this data okay now the thing is uh if we

289:41

talk about capacity what it will be so

289:43

capacity is should be still 16 right no

289:46

it actually changes if you can see the

289:48

capacity will become 21 it's because it

289:51

will store my name and it will also give

289:53

me a 16 space extra

289:56

now why it gives me that space is

289:58

because every time you change data and

290:00

if it consumes a continuous memory

290:02

location inside your hip memory and what

290:04

if there's no continuous location next I

290:06

mean what if there is no location

290:08

available there is no space available in

290:10

that case it has to relocate so to

290:12

reduce the relocation of the string it

290:15

gives you a buffer

290:16

okay so that's the capacity and now we

290:19

know how it works we can also print the

290:21

length of the string we can say SB dot

290:23

length and length is available in normal

290:25

string as well so if I compile and run

290:28

you can say the length is five so length

290:30

n capacities are two different things

290:31

okay now can I append data see the

290:36

ultimate thing was can I change my data

290:39

which is Naveen in this case the beauty

290:41

is string buffer will give you a lot of

290:43

different methods to do that you can see

290:45

we have a method called append so you

290:47

can use append and you can append any

290:49

data which you want to so let's say I

290:51

want to append ready here and of course

290:53

I want to give a space between Naveen

290:55

Naveen and ready so you can append and

290:57

you can print the data so you can print

290:59

SB as it is let's first we have to

291:02

compile the code run and you can see we

291:05

got nothing ready that works next what

291:07

if you want to assign string buffer data

291:10

to string is example if I do that you

291:12

can see it will

291:14

give you error it says type mismatch

291:17

cannot convert from string buffer to

291:18

string of course right STIs of type

291:21

string so in that case you can get this

291:23

string back uh do we have a method for

291:25

it let's see yes we do have two string

291:28

we can use that I think that's the only

291:30

method we can use

291:31

yeah so let's use two string so

291:33

basically you can use two string to

291:35

convert the string buffer data I mean it

291:38

will get the string format it was stored

291:40

in Str and you can print it

291:42

okay we can use that what else we can do

291:45

with string buffer if I come back here

291:47

if I say SB dot you can see we have so

291:49

many methods here okay so we have append

291:52

we have gas it will give you a character

291:55

we have done that okay so we can also

291:58

delete something we can delete a

291:59

character at an index so let's say I'm

292:01

going to delete a character at location

292:02

two if you try to delete that data

292:05

compile and run you can see we got oh

292:08

something is missing the V is missing

292:10

there because that is at index number

292:12

two

292:13

okay so the idea is we basically we can

292:15

change it and you can experiment it a

292:17

lot you can try to use different methods

292:20

we can delete

292:21

we can insert also in between let's say

292:23

I want to insert a Java at the start you

292:26

can do that so you can say insert you

292:28

can mention the index value where you

292:29

want to insert a desert location and the

292:32

data we want to which you want to insert

292:33

so I will say Java space now what it

292:35

will do is it will add Java at index 0.

292:39

I mean that is start itself so if I run

292:41

you can say it says Java of course you

292:43

can add Java in between as well let's

292:45

say I want to add after fifth location I

292:49

think that should work after the space

292:50

is six so let's try that compiled one

292:54

you can see Naveen Java ready so it

292:56

works okay so yeah that's how we can

292:58

basically use insert we have more

293:01

methods to explore and I would recommend

293:02

you to do that can we see one more we

293:05

can also get this substring let's say if

293:06

you have a very big string you can

293:07

specify the starting point ending point

293:10

using substring you can set the length

293:12

as well so if you set the length of the

293:15

stream let's say I want to set the

293:17

length of 30. so if you want to add your

293:20

data and if you want to have a specific

293:21

length which is study here it will save

293:23

the data which is whatever you have now

293:25

been ready will be stored and all the

293:26

other values will be null let's see so

293:28

after Naveen ready it will add some

293:30

extra data here okay so that you can

293:32

replace that later cool or we can do

293:34

that we can also set a minimum or

293:37

maximum data I think minimum capacity

293:40

method which we can use I think the

293:42

method name is ensure capacity yeah okay

293:44

so we can say we can we can set the

293:46

initial capacity to 100 so it by minimum

293:49

capacity will be 100

293:51

okay that's how you have different

293:52

methods now what's the difference

293:53

between string buffer and string Builder

293:56

so as I mentioned we also have string

293:57

Builder right there's only one

293:59

difference string buffer is thread safe

294:01

and string Builder is not now what is

294:04

thread safe means that will understand

294:05

once you talk about thread concept so at

294:08

that point also uh we'll talk about

294:09

difference between string buffer and

294:11

string Builder but whatever methods you

294:14

have in string buffer it's almost same

294:15

as a string Builder so if you know one

294:18

you know the other one as well okay so

294:19

that's about string buffer and some of

294:22

the methods of string in this video

294:24

we'll talk about static keyword from the

294:26

start itself when we have written the

294:28

first code we are looking at the static

294:30

keyword and the question Still Remains

294:32

right what is the static so we know what

294:35

is method is we know what is written

294:37

type we know it is class but again

294:38

problem gets stuck here in static itself

294:41

now to make you understand this let's

294:43

take an example here so let's say we

294:44

have a example of mobile phone and every

294:47

mobile phone has a brand so brand will

294:49

be spraying Services string brand and

294:51

and we can have a default value for it

294:54

we can have int which is price

294:58

and then

295:00

string Network

295:03

okay and we can also make it for a

295:05

specific brand as well but let's keep it

295:07

simple let's have a mobile phone

295:09

different brands different price

295:10

different network and we'll say name

295:14

okay now see name itself of a mobile

295:16

phone changes right so we used to say

295:17

normal phones then we got smartphones

295:20

and now we have foldable phones right so

295:23

we can have a different name to phones

295:24

nowadays every phone is called a

295:25

smartphone now because that's how that's

295:27

what we call initially it used to call

295:29

mobile then we used to call that phone

295:31

right so we have a name here as well

295:33

what do you call a mobile phone so now

295:35

it's a SmartPhone okay so if you want to

295:38

work with this mobile phone of course

295:39

you have to create an object right so if

295:40

I say mobile obj if I will say mobile

295:42

obj1 and equal to new mobile and of

295:45

course every mobile will have a

295:46

different parameter here what I will do

295:48

is I will just remove this network just

295:49

to keep it simple we just want three

295:51

variables here obj one dot brand I want

295:54

to set the brand here let's say the

295:56

brand is Apple uh since I'm using an

295:59

Apple phone here and then the price

296:01

let's say 1500 dollars

296:04

and what else we have we have a name so

296:06

what do you call a mobile phone so um

296:08

currently if you talk about Apple phones

296:10

or any phones nowadays we call them as a

296:13

smartphone okay I know this variable

296:15

doesn't make sense but just for the

296:17

example I'm saying a name is equal to

296:19

smartphone right now if you talk about

296:22

this phone here we have all these values

296:25

right now of course if I create multiple

296:27

objects

296:29

it should have a different value of

296:30

course every object is different so we

296:32

can have a different value to it if I

296:34

come back to Apple let's say the next

296:35

phone is of Samsung and the price is

296:38

let's say 1700 and then we got we also

296:42

call them as smartphone okay the only

296:44

thing is you have to change it here as

296:46

well object 2 obser 2 and then I can

296:49

print the values as well

296:50

um you know what I will do I will just

296:52

create a method which will print

296:53

everything I will say public wide so it

296:55

should print all the details here so it

296:57

should print brand so that I don't have

296:59

to print that every time and I will say

297:02

price

297:03

and I will print name as well now by

297:05

doing this what I'm doing is instead of

297:07

printing it here every value I can

297:10

simply call obj1 Dot show and obj2 Dot

297:14

show so it will print its own value not

297:16

other values and if I come back here

297:18

compile and run you can see we got the

297:21

values right nothing complex here the

297:23

thing is if we talk about these

297:24

variables here they are called instance

297:26

variable right now if you create a

297:28

variable inside a method that's a local

297:29

variable but this are your instance

297:32

variable and every variable for a

297:35

particular object is different right the

297:36

value will be different now what I want

297:38

is I want to have the name to be same

297:42

for all the objects see because what is

297:44

happening is in in your memory so let's

297:46

say we have a jvm a big jvm and inside

297:48

this you have a stack where you have all

297:51

the reference and locations and address

297:53

and then you have a heap memory so in

297:56

total you can see we got two objects

297:58

right one with mobile obj one and one is

298:02

object 2 and then of course they have

298:04

the address as well so this is one zero

298:06

one one zero three and of course this

298:09

will be one zero one one zero three and

298:11

you will be referring that right so this

298:13

is referred here this is referred here

298:16

now that's how we can basically use it

298:17

and every object will have a different

298:19

value but what if I want to have a

298:22

common value for all the objects the

298:23

problem with this is if I want to make

298:25

sure that the name variable should not

298:27

be a part of the object because that's

298:29

an instance variable now it will be a

298:30

part of the object and every object will

298:33

have a different value I want this name

298:35

to be common for all how do I do that

298:37

now if you make this variable as static

298:42

Now by doing that what we are doing is

298:43

we are making this name variable common

298:46

to all the object what I mean by that is

298:50

let's say after some time if I say obj1

298:53

dot name if I change the name to let's

298:56

say a phone here just a phone not a

298:58

smartphone anymore if you make this as a

299:00

phone and if you see the values it will

299:03

affect all the objects you can see we

299:07

have we are trying to change the name of

299:09

obj1 but we are actually changing the

299:12

name of obj2 as well you can say this is

299:15

also phone now the reason is the static

299:18

variable is actually shared by all the

299:20

objects so basically will not be having

299:22

individual static variable for every

299:24

object we have a common variable which

299:27

will be used by all the objects imagine

299:28

there is a common variable here name and

299:31

all the objects will be referring to the

299:33

same variable so if you change the value

299:36

for this it will affect all the variable

299:38

all the objects okay now this is why we

299:41

use static now the beauty is

299:44

see why we have to refer brand with obj1

299:46

price with obj1 is because every object

299:49

will have a different value for it now

299:52

don't you think name is a static

299:54

variable and it will be common for all

299:56

the objects and that's why you can

299:59

actually call name with the help of a

300:02

class name itself you can simply say

300:04

mobile dot name so static variables

300:07

should be called with their class name

300:09

not with objects you can call with

300:11

objects but avoid it and that's why you

300:14

can see it gives you warning as well it

300:15

says the static field mobile.name should

300:18

be accessed in a static way which is

300:20

using a mobile uh the class name which

300:23

is mobile okay so the point remember

300:26

here is static variables are actually

300:29

shared by different objects and if you

300:32

want to refer to a static variable you

300:33

can use a class name so there's a

300:35

different area in your jvm where you

300:38

have all the static variables and here

300:40

as well I should say mobile now the

300:42

thing is in non-static methods you can

300:45

use static variables there is no harm

300:48

okay and that's the point to remember in

300:51

the next video we'll talk about what if

300:52

you create a static method yes we can do

300:54

that you can create static method and

300:56

the question is can I use instance

300:58

variables there okay we'll see that in

301:00

the next video but here we talked about

301:02

what is a static keyword so static

301:04

keyword means you are making something

301:07

as a class member not an object member

301:09

so static this name here belongs to a

301:12

class not to the object and that's why

301:14

you are able to use this with the help

301:16

of a class name okay and then yours

301:19

you're saving memory as well because not

301:21

every object will have their own value

301:23

for it or their own copy for it so

301:25

that's about static variable now we know

301:27

what is static variable static methods

301:28

right now let's try to expand this more

301:30

to simplify this what I will do is I

301:32

will remove the static method for some

301:34

time and I will also decrease this code

301:37

let's only work with one object now

301:39

and here see normally when you work with

301:42

variables so we you now we know that

301:44

this is an instance variable and this is

301:47

a static variable right the name

301:49

now to instantiate the instance variable

301:52

what we do of course we can instantiate

301:54

the variables from here but then what if

301:56

you want to instantiate the variable on

301:58

this line if you want to give some value

302:00

not instantiate if you want to

302:01

initialize the variable what you will do

302:03

so basically we have seen one concept

302:05

right which is Constructor so using

302:07

Constructor we can say Okay public

302:10

mobile

302:12

and we can give some default values to

302:13

it right so by default the brand will be

302:15

empty string and by default the price

302:18

will be let's say 200 right so this is

302:21

the default brand default uh text and

302:25

default number right which we want to

302:26

give what about name I can also give a

302:28

default name to a name which is phone in

302:31

this case let's say we can do that we

302:33

can initialize this static variable

302:35

inside a Constructor and there's no harm

302:37

in that but the problem is every time

302:40

you create an object every time you

302:42

create one more object it will call the

302:45

Constructor right and don't you think it

302:46

makes sense for this two to initialize

302:48

every time but why name has to

302:51

initialize every time

302:52

I feel that the name variable which is

302:55

static should be initialized only once

302:57

what do you feel of course the same

302:58

thing right how will you do that so if

303:00

you mention that here of course every

303:02

time you call the Constructor it will

303:03

install we don't want to do that not to

303:05

achieve that we have a special block

303:08

called a static block now in the static

303:11

block we can basically initialize the

303:14

variable the static variable now the

303:17

good thing about this is this static

303:19

block will be called only once

303:20

irrespective how many objects you create

303:22

it will call the static block only once

303:25

so what I will do is just to demonstrate

303:27

that I will create one more object here

303:29

let's say

303:31

and I will not assign any value to it

303:33

just to default values so object to

303:35

Second object so it will call the

303:38

Constructor two times it will call the

303:41

static block only once to prove that

303:42

what I will do is I will just print

303:43

something I will say in

303:46

static block now the Syntax for static

303:48

block is very simple it's

303:49

static keyword and then you just open

303:51

and close there is no bracket or

303:53

something it's just a simple block okay

303:55

and in the Constructor as well I will

303:58

say in Constructor okay and now with

304:02

this let me just compile this code okay

304:05

there is an issue I have written one

304:07

extra okay I have removed the

304:10

country package from here so compile and

304:14

run so you can see it is calling the

304:16

static block only once and just calling

304:18

Constructor two times

304:20

there's one more issue why static block

304:22

is called first

304:24

even before the Constructor it is

304:25

calling setting block and that's weird

304:27

is it the sequence let's try let me just

304:30

put this after the Constructor let's see

304:32

if there's any make doesn't make any

304:34

sense anyway Java does not follow the

304:35

sequence but just to

304:38

confirm you can see the static block is

304:41

still getting called first now to

304:43

understand that let's go to the diagram

304:45

here see what happens you know with

304:48

there are two words we and we only focus

304:50

on one word every time you get the

304:52

object that two steps one your class

304:56

loads

304:58

and then your objects are okay so first

305:03

your class loads and then your object

305:04

gets instantiated so there are two steps

305:06

every time you create the object there

305:09

are two steps class loads and object

305:12

will then will get initialized the thing

305:14

is the class will load only once

305:16

so basically in your jvm you have a

305:19

special area here which is called a

305:22

class loader okay and this will have all

305:25

the class which are loaded and by

305:27

default of course you don't have any

305:28

class loaded right so the class will get

305:30

loaded from from your library to the

305:34

class loader and it will happen only

305:36

once so for the first object when it

305:38

sees hey there's object creation it will

305:40

load the class first and then it will

305:42

create the object now every time you

305:45

load a class it will call the static

305:48

block

305:48

now since glass loading happens first

305:50

and that's what you will call this edit

305:52

block first and then it will create the

305:54

object and that's why it will call the

305:57

Constructor after select block now

306:00

irrespective of how many objects you

306:01

create the class is already loaded right

306:04

so you can simply create a next object

306:06

without loading because it's already

306:07

loaded now my question is if that is how

306:09

it is working what if I don't create any

306:11

object I'm not creating an object now so

306:15

if I don't create the object of course

306:16

it will not call the Constructor but

306:18

what about static block it will not even

306:20

call the static block if you can see we

306:22

don't have any output oh okay so that

306:24

means if you if you don't create the

306:26

object it will not load the class as

306:28

well but what if you want to do that

306:30

what if you want to load the class how

306:34

would you do that now for that we have a

306:36

special class inside Java which is

306:39

called a class class yeah that's a class

306:41

okay I know Java has been weird we have

306:44

object class we have class class but

306:46

that's how it is designed so Java class

306:48

if you go there you can you can see it's

306:50

a class and it has a lot of methods to

306:52

it one of the method which we want to

306:54

use here is for name now fourth name is

306:57

a method which does one thing which

307:00

loads your class and you just have to

307:03

mention your class name what's your

307:05

class name or the class name is mobile

307:07

so in this you can mention the class

307:08

name that's it okay the only problem is

307:11

it throws an exception okay at this

307:12

point we have not talked about exception

307:14

but just ignore this line

307:16

uh we'll talk about this in detail later

307:18

ones which start with the exception

307:19

concept but as of now just to make it

307:21

work I'm just writing this line ignore

307:23

it

307:24

what's important is the class dot for

307:26

name will load your class if I go to

307:29

font name you can see for them is a

307:30

method and it's a type method so you can

307:33

directly class you can call this with a

307:35

class name

307:36

and this particular method

307:39

do they have any definition mentioned

307:42

okay so it says the system class loader

307:44

is used so basically it will load the

307:46

system class loader to load this class

307:49

okay so which class mobile class okay so

307:52

now if I just write this line it will

307:55

not create the object it will just

307:57

instantiate it compile run you can say

308:00

it says in New

308:02

static block okay that means you're just

308:05

loading the class so basically that's

308:07

how you load your class you can simply

308:09

use the this line to load your class

308:12

normally when you build a project you

308:14

don't actually use this feature yes

308:15

there are some situation where you use

308:17

it example when you start a jdbc we'll

308:19

talk about this one but we normally

308:21

don't use it I'm just using this just to

308:24

show you how to call a static block how

308:26

to load a class okay I hope this video

308:28

makes sense where we talked about static

308:30

block to so static block is used to

308:32

initialize the static variables

308:34

so that's it from this video in which we

308:36

talked about static Block in the earlier

308:38

videos we have talked about static

308:39

keyword static variable static methods

308:41

and yeah that's super static now once we

308:44

talked about static variable it's time

308:45

to talk about static methods now what I

308:48

will do is I will just try to remove all

308:51

this content from here just to keep it

308:53

simple and let's make a static static

308:56

method now the method which we have here

308:58

show is not a static method right it's a

309:00

instance method it's because we have not

309:02

mentioned a static keyword now whenever

309:04

you want to work with a non-static

309:07

method you need to create an object of

309:09

it of course if you want to call you

309:11

have to create object of Mobile in fact

309:14

you know actually we need those objects

309:16

here

309:17

and yeah let's keep it here just to use

309:20

it

309:20

now let me create a simple static method

309:23

so I will say public

309:24

uh static void show one again if you can

309:29

see I'm using a different method name

309:31

just to differentiate it and here if I

309:34

try to first of all I will just print in

309:37

static method

309:39

now how do we call the static method

309:42

to call show we have to get object right

309:44

you can't simply come back here and say

309:46

mobile dot so this will not work if you

309:49

try to call mobile.show it will give you

309:52

an error it says let's see the error it

309:54

says cannot make a static reference to a

309:56

non-static method that means you cannot

309:58

call a non-stick method with the help of

310:00

a class name and that's why you need

310:02

object here which we can see we're using

310:03

obj1 object two

310:05

but can I call show one from mobile

310:08

let's try and we can you can see that

310:10

there's no problem

310:12

so you can call a static method directly

310:15

with the help of a class name

310:17

okay that's what we are doing here we're

310:19

actually calling it with a class name

310:20

now the question is this looks cool

310:23

right starting method can be used

310:24

directly with a class name but can I

310:26

call can I use us variables here let's

310:29

try to do the same thing I will just

310:31

copy this line and paste it here let's

310:34

see what happens the moment you do that

310:36

you can say there's no problem with name

310:37

so that means you can use a static

310:40

variable inside a static method but you

310:43

cannot use a non-static variable inside

310:46

a static method

310:48

the question is why I mean think about

310:51

this

310:52

brand and price they are instance

310:54

variable that means it will be different

310:55

for different objects now when you call

310:58

show with obj1 we know that in this show

311:00

when you're printing brand and price

311:02

you're talking about obj1

311:04

when you call show with object 2 you

311:06

know that the brand and price will be of

311:08

object 2. but then when you call

311:11

mobile.show1 how do we know which Which

311:14

object you are referring to so this

311:16

brand belongs to obj1 or it belongs to

311:19

visual two and that's why it says hey

311:20

there's a confusion right let's not do

311:22

that let's not have a non-static

311:25

variable directly getting access in a

311:27

static method it's not possible uh this

311:29

time I have added a directly keyword

311:30

that that means entirely is possible now

311:33

how would you do that indirectly it's

311:34

very simple what if you can actually

311:36

pass the object itself you can say hey I

311:40

want to run show one for obj object obj

311:43

one object now since you are passing the

311:45

object you have to also accept it here

311:47

so you can say mobile obj

311:50

and now now since we have the object I

311:53

can refer that with the help of obj1 dot

311:55

brand

311:58

so basically we cannot directly use a

312:01

non-static variable inside a static

312:03

method but if you have object reference

312:05

then you can use it because now we can

312:07

differentiate so the brand which I'm

312:08

talking about is for obj one not for

312:10

object two so that's how you have to do

312:12

it so that's how you work with static

312:13

method now if You observe here from

312:16

start we are using this right the main

312:18

method is static why in this world we

312:21

have a main static is because if you

312:24

don't make main static if this was a

312:26

syntax the problem is

312:28

if you don't mention static that that

312:29

means main becomes a non-static method

312:32

which means if you want to call Main we

312:35

have to first create the object of demo

312:37

oh that means you cannot call Main

312:40

without the object of demo but the

312:42

question is

312:43

we all know that main is a starting

312:46

point of execution if the execution has

312:49

not started how can you create object of

312:51

demo okay so there's a deadlock here

312:53

right uh so to solve that problem what

312:56

we can do is we can make this variable

312:58

as static so to solve that problem we

313:00

can make this main method as static the

313:02

moment you do that we are saying hey you

313:05

know to call Main we don't need object

313:08

of demo problem solved right so yeah

313:11

this is how we can solve this problem

313:13

and this is what static methods are

313:16

uh anything else I think it looks good

313:19

in the next video let's try to see

313:22

something more about static there's a

313:24

concept of static block let's talk about

313:26

encapsulation now I know that's a new

313:28

term but when we started with oops

313:30

concept now encapsulation is a part of

313:33

oops so when I say let's Implement

313:36

object oriented programming so yeah we

313:38

have talked about class we have talked

313:39

about objects and the next step is

313:41

encapsulation here now by word what

313:44

encapsulation means you know we have the

313:46

concept of capsules where we have

313:48

medicines powder there so basically we

313:51

can imagine capsule as something which

313:53

is keeping it close keeping it tight and

313:56

no one from the outside world can use it

313:59

that makes sense right so what we can do

314:01

here is let's say let's design a class

314:03

okay

314:04

and let's name this class as human I

314:07

know we can use different names but

314:08

let's go with human here and I know we

314:10

are aliens but let's say let's say this

314:13

is human now here when you have this

314:15

human class a human will have some

314:17

parameters right example uh if you

314:20

remember we have talked about this

314:21

before as a human or as a person we know

314:24

a lot of different stuff so in our mind

314:26

you have a lot of different concepts or

314:27

you know a lot of things example you

314:30

know your name you know your where you

314:32

stay you know your favorite actor you

314:34

know your favorite trainer of course you

314:36

can name you can use my name there but

314:39

just kidding so we have different

314:41

information right now can you imagine

314:43

this that all the information are

314:45

actually stored in your brain as a

314:48

variable box I'm a big fan of pixel

314:51

movie which is

314:53

I don't know the exact movie name but

314:54

you know uh Pac-Man basically eats

314:57

people and then they become pixels so

314:59

you can imagine the data in your mind is

315:01

Cube okay and in that box you have the

315:04

data example your name is stored in a

315:07

box which name Naveen or which names

315:09

name is equal to and in the Box we'll be

315:12

having a value for mates Naveen let's

315:14

say if you are your favorite actor so

315:17

you'll be having my favorite actor and

315:19

the Box Inside the Box will be having a

315:20

value so what I'm saying is all the data

315:23

is stored in your brain in the format of

315:25

boxes variables

315:28

now of course uh you can use these

315:30

variables right so let's say my name is

315:32

Naveen and let's say if I'm walking on a

315:34

street and if someone is asking my name

315:36

I might

315:37

provide my name what if someone is

315:39

asking my email address I might provide

315:42

them my email address depend upon who

315:44

that person is what if someone is asking

315:46

for my uh children's name my wife name

315:49

my family name my Facebook password no

315:52

I'm I'm not going to share those things

315:54

it's because there's something called

315:55

privacy right and yeah I know a lot of

315:57

people don't believe in privacy but of

315:59

course no one should be able to get your

316:01

data directly right it's not like your

316:03

data is available for the entire world

316:05

to see it's there it's closed no one can

316:07

see the data of course they can see my

316:09

Facebook profile but no one can see the

316:11

data inside your brain

316:13

that's important if they want to know

316:15

your data they have to ask and you can

316:18

decide do you want to share or not in

316:21

the same way let's say we have this

316:22

human class and in this human class we

316:24

have a variable called age and we have a

316:26

string which is named that's it just two

316:28

variables and now if I want to access

316:31

those data of course I have to create

316:33

object of human here and let's say this

316:35

is the human object Neo human and once

316:38

you got this object with this object you

316:40

can fetch data right uh of course you

316:42

can set the values as well and for this

316:44

particular object I can say obj dot h is

316:48

let's say 11. and obj Dot name is let's

316:53

say Naveen I know that's not my age but

316:55

at this point it's okay so we got these

316:57

values and we got this object created

316:59

here

317:00

now what I want to do is what if someone

317:03

asked me hey human or maybe someone is

317:05

trying to directly trying to fetch the

317:07

data example if I say s out and if I try

317:10

to say obj dot name now what we are

317:13

doing is just by using the object anyone

317:16

can fetch data okay and this will work

317:19

okay it will not give any issues I will

317:21

say compile and run you can see we are

317:24

able to fetch data and

317:27

syntactically there is no issue but then

317:29

if you really think that we have to

317:31

implement software for the physical

317:33

world or the real world it should match

317:35

with the real world not all the data are

317:37

directly accessible and we want to

317:39

control it so one way to do that is what

317:42

you can do is you can make your

317:43

variables private now what do you mean

317:45

by this private keyword of course we'll

317:47

have a detailed discussion later at this

317:49

point private simply means this

317:51

particular variable which is age is

317:54

accessible only in the same class let me

317:57

repeat this very this age variable is

318:00

accessible in the same class the moment

318:03

you make it private it's private no one

318:05

can no one from the outside world can

318:07

use it of course I can use my data

318:09

because that's private to me but you

318:11

can't access my data okay that's

318:13

probably to me right the same thing can

318:15

be done for the the invariable here and

318:18

if You observe the moment I do that we

318:20

got an error here I mean of course vs

318:24

code helps you that vs code says hey you

318:26

know this variable age is not visible

318:29

you know why it's not visible because

318:31

it's private now going forward this is

318:35

what you have to do every time you

318:36

create a instance variable let me repeat

318:39

every time you create a instance

318:42

variable make it private no one from the

318:45

outside world should be able to use it

318:47

now you will say hey that doing that we

318:50

are actually Breaking All the purpose of

318:52

building a software right if no one is

318:54

able to access this data then why we are

318:56

even creating these variables that's

318:58

right the reason why we're getting this

319:00

variable is because we can store values

319:01

and someone should be able to access

319:03

them

319:03

they should be able to access them but

319:06

not directly okay so there should be

319:09

some indirect way yeah we have indirect

319:11

way so the way they should be able to

319:13

access this data is with the help of

319:15

methods example let's say if you want to

319:17

know my name you have to ask for it and

319:20

I will decide should I should I share

319:22

the data with you or not so if you say

319:24

hey what's your name I will say okay I

319:26

will ask my brain what's my name because

319:27

most of the time I don't remember my own

319:29

name so I will say hey what's my name my

319:31

brain will say okay your name is Naveen

319:33

and then from my methods which is my

319:35

mouth or my by using my handwriting or

319:38

by typing somewhere there is a way I

319:41

have to share that data with you right I

319:44

have some methods Behavior I can talk I

319:46

can write I can type Now using those

319:48

behaviors I can share the information

319:50

okay not like anyone can just come to me

319:52

by it they will break my head and say

319:54

okay I got the data now that's not how

319:56

it how it should work so how do I come

319:58

how do I uh restrict it so what we can

320:01

do is we can make it private we are just

320:02

restricting it the only way to access

320:05

them is with the help of methods so what

320:08

I can do is temporarily I will just

320:09

comment these two sections and I will

320:11

assign the value here itself and we can

320:13

do that I will say name is Naveen and

320:16

then here coming back to public I will

320:18

say public I want to get data I want to

320:21

say hey get age okay and of course you

320:24

can use any method name it doesn't

320:25

matter but let's say get age and this

320:27

will return a integer value so I will

320:30

say int here and this will return so

320:33

this will basically return the age now

320:36

the question is can I access age here

320:38

and the answer is yes you can you can

320:40

access the age variable inside a method

320:43

of a same class that's right this get

320:46

age is defined in the same class that's

320:49

why you are able to access that and if

320:51

you see age here or name here so we are

320:54

trying to get the data of another class

320:55

demo and human there are two different

320:58

classes okay makes sense now now we can

321:02

also do it for the name so for the name

321:04

I will say public

321:05

string uh string get name and this will

321:11

basically return a name

321:13

okay so we got two methods get age and

321:16

get name and then we are returning the

321:18

age and name now if someone wants to

321:21

fetch it I can simply say Hey you know

321:24

don't call name that will not work

321:25

because name is a variable which is

321:27

private you can access the methods you

321:29

can say obj dot get name that's how you

321:32

can fetch a value of course you can also

321:34

fetch the age what I can do is I can

321:36

just say plus I can give a colon in

321:38

between and I can say obj dot get age

321:42

okay now I think we should be able to

321:45

access it let's try and compile

321:49

run oh it's working can you see that we

321:51

got Naveen and Levin so the point

321:53

remember is every time you create a

321:56

variable which is private we are making

321:58

sure that it is restricted no one from

322:00

the outside world should be able to use

322:02

it so this age variable this name

322:04

variable will be used by their own

322:06

methods that actually makes some sense

322:09

okay but there is one little problem

322:11

here what if I want to assign the value

322:13

what if I don't specify the value here

322:15

cut and cut let's say we have the

322:18

variables but don't I don't assign the

322:20

value now of course by if you don't

322:21

assign the value default value will be 0

322:23

and a null string but I want to how do I

322:28

set the value now for getting the data

322:31

we have said get for setting data can we

322:34

say set let's try so here for age I will

322:37

say public now when you're setting the

322:39

value you are not getting in return

322:40

something right so you will say void and

322:42

you will say set age okay now it's just

322:46

that when you are setting the age you

322:48

have to ask hey what is the value so

322:50

maybe you can also accept a value here

322:53

maybe I can say a right and then here I

322:56

can say age is equal to a now what I

323:00

mean by that is of course you have a

323:02

method which is accepting a value

323:03

assigning that to age but how it will

323:05

work so let's come back here which the

323:07

the portion which we have commented I

323:09

want to set the age right so instead of

323:11

setting the age to the variable I can

323:13

say obj dot set age we are using a

323:16

method now not the variable and in this

323:18

particular bracket you can pass the

323:20

argument and let's say the age is 30. so

323:23

now we are passing the value now this

323:26

value will be assigned to this a and

323:29

from a a this will be assigned to age

323:31

can we do the same thing for string

323:33

let's try I will say public void sect

323:37

name and of course we have to also

323:39

accept the name but what's the format of

323:41

a format of name it is string name I

323:44

will say end this time and here I can

323:47

say name is equal to n so whatever value

323:50

you pass will be assigned to This n and

323:53

from n it will be assigned to name now

323:55

the same thing need to be changed here

323:57

so I will just cut this part and I will

323:59

say cut this part as well I will say set

324:01

name and in the double quotes I will

324:04

pass the name

324:05

and the name here is Naveen

324:07

and we will use a different name this

324:09

time I will set a d okay and now I will

324:11

just come back here clear the screen

324:14

compile Run Okay so that we got ready

324:17

and study so what we are doing is we are

324:20

assigning the value with the help of

324:22

methods here and we are getting the

324:25

value with the help of methods here

324:27

so that's how we can access this data

324:30

now what we are doing is we are making

324:31

your variable private and then the only

324:33

way to access them is with the help of

324:35

methods right so basically we are

324:37

binding our data with the methods the

324:39

only way to access that data is with

324:41

methods so no one from the outside world

324:43

can use your variables directly so

324:45

somewhere we are encapsulating the data

324:47

and methods and that is encapsulation

324:50

okay simple stuff right uh so yeah

324:54

that's it from this video let's try to

324:56

understand this more what is this get

324:58

and set methods uh can we use different

325:00

names or

325:02

only this name will work that we'll

325:03

discuss let's try to understand what

325:06

these methods are or can we change the

325:08

name say get Aid set age makes sense but

325:12

can we use some other names actually we

325:14

can instead of saying get age we can say

325:16

let's say we can say ABC and set age we

325:18

can say x y z we can do that okay there

325:21

is no harm in that the only thing is

325:22

when you call this methods of course you

325:24

have to change the name I will say this

325:26

is XYZ so when you set the value you

325:28

have to call XYZ and then when you fetch

325:31

the value you have to say ABC and it

325:33

surely works if I uh try to compile this

325:36

and run you can see it works now the

325:39

problem with ABC and XYZ or any name is

325:41

your method name should Define what user

325:44

what is your intention because most of

325:46

the time it's not just about writing

325:48

code it's also about reading code when

325:49

you write a code once it will be read by

325:52

multiple people in the entire lifetime

325:54

of a software why not use proper words

325:56

which explains what that method is doing

325:58

so instead of saying ABC if I say get

326:01

age each it is actually makes sense

326:03

right it is much more readable what

326:05

you're doing there right and as I

326:07

mentioned before it is not a compulsion

326:09

to use the same name but then as a

326:11

developer we have adopted this standard

326:14

whenever you want to get the value of a

326:16

variable the method name would be get

326:18

Space the other variable name the

326:20

variable name which is age to set the

326:22

value you will say set and you will use

326:24

the variable name which is age okay so

326:26

that's how we can use this so This is

326:28

called a getter so for the variable user

326:30

this is a getter and to when you want to

326:33

set the value so the method is called

326:35

Setter so we can say that's those are

326:38

Getters and Setters uh in fact the

326:41

beauty is if I delete the entire part

326:44

okay let me just do that I know we are

326:46

we need to type that once again actually

326:48

you don't most of the ideas have this

326:50

amazing feature you can just right click

326:52

and there's an option of source in fact

326:54

it doesn't matter which ID you are using

326:56

uh you will see the option of source or

326:59

insert code something like this so it

327:01

will say source action what do you want

327:03

you want to get you you have to say

327:06

generate Getters and suggest for me

327:08

click on that for both the variables and

327:12

click on OK you can say it will generate

327:14

the methods for you and you see that

327:16

variable names it's same get age set age

327:19

and then we got get name set name okay

327:23

and those are called Getters and Setters

327:25

and of course there is no compulsion to

327:27

have both the methods you can also you

327:29

can only have get name you don't want to

327:30

have set name that's your choice there's

327:32

no compassion that there should be both

327:34

it depends upon you how you want to

327:35

implement it for this example I want to

327:38

assign value to H name I want to get the

327:40

values as well and that's why I'm using

327:42

this show so only thing is for every

327:44

variable you need two methods Getters

327:46

and Setters okay so yeah that's it uh

327:48

but there's only one issue now if I use

327:50

the code which is given by the IDE you

327:53

can see this new keyword here we got

327:54

this in this video let's talk about this

327:57

because in the previous video we were

327:59

doing something and then suddenly we got

328:01

this keyword so it's important now to

328:03

talk about this keyword here now what I

328:05

will do is I will just remove this or

328:08

I'll just change this variable name back

328:09

to A and N and of course here we have to

328:13

say A and N right and let me remove this

328:16

this keyword from here just to see if

328:18

this works now if I remove this keyword

328:21

from there and you find on this code

328:25

compile okay there is some problem with

328:27

the variable names I think we are still

328:29

calling x y z so it should be set age

328:32

and we are still calling ABC it should

328:34

be get H the same thing which we have

328:37

done in the previous video

328:38

okay so if I save this and if I go back

328:41

and compile so compile done

328:43

running

328:45

done so you can see we got the values

328:47

right the thing is when you talk about

328:50

this variable like age and when you're

328:52

passing a here we are using two

328:53

different variables right

328:55

now the point to remember here is this a

328:58

variable here is a local variable this

329:00

age variable here is a instance variable

329:02

right if you want to understand that

329:04

with a diagram uh you can just go back

329:07

here we have a stack memory remember we

329:10

have talked about stack and Heap so we

329:12

got a stack here and then we got Heap

329:14

here now the moment you create the human

329:16

object we got this object here human so

329:19

in this we have a human object and of

329:21

course it will have some address let's

329:22

say one zero one so we got obj and the

329:27

address is 101 that's the first object

329:28

we got and there is a link here which we

329:31

can cannot ignore and then we have some

329:34

more variables you can see if you

329:35

execute and of course this was my main

329:39

stack uh now we have executing

329:43

which method so if you say set age we

329:45

are executing set age method right

329:47

Now set age will have its own stack here

329:50

let's say we have a set age stack here

329:52

you know what I just have to zoom it a

329:55

bit

329:55

and here let me create segments so we

329:58

got multiple segments here and of course

330:00

this will be linked with uh this

330:02

particular variable here and don't you

330:05

think this this box which is address

330:07

once the one is an object of human so it

330:09

will also have the instance variable how

330:10

many instance variable we have we have

330:12

age and name so we got age and we got

330:15

name here now if you look at the set Age

330:18

Now set age this particular stack uh is

330:22

of set age every method will have its

330:24

own stack and in this we got a variable

330:26

called a and the value for a which we

330:28

are passing is study so we got 30 here

330:31

okay and then the same value 30 we are

330:35

assigning to age that means we are

330:38

changing this variable and we are

330:40

assigning a value which is study there

330:41

if that makes sense okay so both the

330:44

value for a and age study and that's why

330:47

when you fetch data when you say get age

330:50

it will return the value of age in the

330:52

object

330:53

which is study in this case and that is

330:56

perfectly working

330:58

now the problem is what if I just make

331:01

this particular variable age as age I

331:03

mean a as h

331:05

now what I want is I want my local

331:07

variable and my instance variable both

331:10

to have the same variable name

331:13

what do you mean by that is if I remove

331:15

this a from here the variable name here

331:18

will be H so both the variable name the

331:21

local variable and the instance variable

331:24

have the same name

331:26

then it should work right what we are

331:28

trying to do is the age variable will

331:30

get 30 here so of course in this stack

331:33

we got 30 and if you see we are

331:35

assigning H is equal to age so I'm

331:38

expecting that this age will get the

331:39

value from this particular age it should

331:42

work right let's try if I go back to my

331:45

code and compile and run

331:48

oh can you see that the value for age is

331:51

still zero that means the assignment

331:52

which we are assuming that it will be 30

331:54

it is still zero why

331:57

the reason is

331:59

if you look at line number 11. and if

332:01

you see it we are saying age is equal to

332:03

age

332:05

the problem is by default now see this

332:08

age can be local variable this age can

332:10

be instance variable

332:11

right because the names are same so can

332:14

we do this can we say it is possible

332:17

that

332:18

the age here is actually a local

332:21

variable it is also possible that the

332:24

age is assigning value to itself

332:27

maybe this age is not even an instance

332:29

variable that means this age is

332:31

assigning value to itself and this age

332:34

here

332:35

which we have created it is still

332:37

zero and that's why we are getting 0.

332:40

how do we solve this problem so

332:42

somewhere we have to mention hey you

332:44

know this particular age here is not a

332:47

instance variable this is not a local

332:49

variable this is an instance variable so

332:51

remember this point the preference will

332:53

always give always be given to local

332:55

variable so in case of confusion it is

332:57

always the local variable we have to

333:00

mention by some way hey this is not

333:01

local variable that's an instance

333:02

variable

333:03

how do you mention that so in order to

333:06

say that's an instance variable can we

333:07

do this can we say

333:08

a human can we get object here itself

333:11

object one equal to new human and then

333:13

we can say obj1 dot h now what we are

333:17

doing is we are saying this age is a

333:20

instance variable and you might be

333:22

thinking hey how can we use age with a

333:25

object see we can do that because we're

333:27

in the same class okay we were not able

333:29

to do that here because we were in

333:31

different class but again the problem is

333:33

we are saying this age is instance and

333:35

this age is local this should work let's

333:37

see if this is a problem or we have

333:39

solved the problem let's run this oh it

333:42

is still zero what is happening let's

333:44

try to understand more now when you

333:46

execute this set age method what we are

333:48

doing is we are creating a new object oh

333:51

that's the issue we are basically

333:53

creating a new object okay

333:57

um how do you solve this problem we

333:59

don't want to get new object because the

334:00

moment you create new object what will

334:02

happen here is it will create a new

334:03

object let's say 103 and this will have

334:07

this will also have age equal to name

334:09

equal to and what value you are signing

334:11

here your assigning value which study

334:13

and then here in this set age you will

334:17

create obj1 which will have the address

334:19

103 and there's a link here

334:22

so the value which you are changing is

334:24

of this age not this one we want to

334:27

assign the value to this particular age

334:31

that means creating a new object is not

334:33

a good idea okay how do I clear this

334:35

let's remove this

334:37

in fact you know this is a problem this

334:39

is creating a new object

334:41

okay if I don't want to get a new object

334:43

how do I access

334:45

this obj oh I got an idea here what if

334:49

with this 30 if I can pass the object

334:52

itself can we will it solve the problem

334:54

with 30 if you can pass this particular

334:57

object and we can accept the object here

335:02

what type of object it is it is human

335:04

human obj

335:06

and then here we can simply assign the

335:08

value so what I'm doing is if you if you

335:10

observe correctly we are not creating a

335:14

new object we are basically passing the

335:16

reference of the old object

335:18

so here it will be not 1 0 3 it will be

335:22

1 0 1. okay so this obj1 here will get

335:27

the address 1 0 1 and we can simply say

335:29

obj1.h and now we are not referring to

335:33

this address we are referring to this

335:35

address so we can change the age value

335:37

30 and let's see if this works I hope

335:39

this will work compile sorry compile

335:44

run oh it is working it's working so

335:47

basically the important thing is how do

335:49

I differentiate between a instance

335:51

variable and a local variable is by

335:53

passing the object itself but there is

335:56

one little problem here the problem is

335:58

if you if you read this line it says obj

336:01

dot set age and then you're passing the

336:04

object of the obj object as well why we

336:07

have to do this why you have to mention

336:08

obj two times

336:11

knows this Java says hey you don't worry

336:13

why you have to pass the object two

336:15

times I mean you're calling the method

336:17

by using the object you're also passing

336:19

the object don't do that don't pass the

336:22

object by default I will give you this

336:24

obj you don't even have to accept this

336:26

object what if Java says hey don't worry

336:29

don't create the object don't pass the

336:31

object I will give you the object and

336:33

that object my friend in fact you know

336:35

when we had a human here we don't even

336:38

need this human line here we could have

336:39

removed that and even obj would have

336:42

worked so your Jawa says hey why do you

336:44

even use this obj we can when we can

336:46

simply use this now this is a keyword

336:49

which represents the current object now

336:52

when I say current object what it means

336:54

the object which is calling the method

336:58

set H is called by obj which is this

337:00

object

337:01

here in this method we can actually use

337:04

that object with the help of this

337:05

keyword so this refers to the current

337:08

object who is calling the set set age I

337:11

hope that makes sense so from this point

337:13

we are always going to use this which

337:15

represents the current object

337:17

okay so here as well instead of using

337:20

name or N I should have used name the

337:24

only thing is we have to refer the first

337:26

name here is this dot name which is the

337:29

object itself the current object I hope

337:32

uh this all this thing have made sense

337:34

this is what this keyword is in this

337:37

video we'll talk about Constructors now

337:39

what are Constructors now look at this

337:41

example here which is of human now when

337:42

you talk about human we have some

337:44

variables right of course when you talk

337:46

about these two variables these are

337:47

instance variable and then we have seen

337:49

in the image as well uh basically

337:52

whenever you create an object example in

337:54

this we are just creating one object and

337:56

that object is getting created inside

337:58

the Heap memory you can see this is the

338:00

object we got

338:01

and of course with that object we have

338:03

assigned the value for the age we have

338:05

assigned the value for the name as well

338:06

which which is ready but I have not

338:07

written it here but we know that the

338:09

object is created

338:11

now the thing is

338:13

when you create the object if You

338:14

observe the line number 29 on this line

338:17

when you create the object what happens

338:19

when you create the object in the hip

338:21

memory it will just create a blank

338:23

object example if I try to remove this

338:26

if I keep it empty it will create a

338:29

object with a value so the value would

338:31

be I mean it will have a variable which

338:33

is age and the value will be 0 and it

338:36

will have a variable called name and the

338:38

value will be null right I will just I

338:41

will just try to print those things uh

338:43

what if I do is I will just print the

338:45

same statement before assigning the

338:47

value

338:48

right so I'm printing it here and I will

338:50

comment this section so that's how you

338:51

comment right I don't want this line to

338:53

be executed and now if I go back here

338:56

and compile

338:58

and run you can see we got null null

339:00

basically is

339:02

when you don't have the data assigned to

339:04

object type that's right string is an

339:06

object type I will discuss about string

339:08

later in detail as of now just remember

339:10

by default the the value font string is

339:13

null and the default value for integer

339:15

is zero okay that means if you don't

339:18

assign these values there will be

339:21

default values what I want here is of

339:23

course there is a way you can assign the

339:25

values later but what if I want to

339:26

assign the value when the object is

339:29

created

339:30

that's right on this line itself I want

339:33

to assign the value that means the

339:34

moment I get the object I don't want to

339:36

have a null value I want to have some

339:37

default values example let's say by

339:39

default every object created the name

339:42

should be something else the name should

339:43

be let's say Babu or something else I'm

339:46

not sure but can we have a default name

339:48

can we have a default age so let's say

339:50

for my application uh this application

339:52

can be used only by those people who are

339:55

12 and above so I can simply say hey the

339:58

minimum age for this is 12 so by default

340:00

the value for the age should be 12. can

340:02

we do that

340:04

so how do we achieve that of course we

340:06

can assign the values here or we can use

340:09

something called a Constructor now how a

340:11

Constructor looks like so a Constructor

340:14

looks like a method itself let me repeat

340:17

a Constructor looks like a method itself

340:19

that means a Constructor will have a

340:22

name a Constructor will have the round

340:23

packets example if we talk about methods

340:25

here we have a name and we have a round

340:29

bracket in the same way for the

340:30

Constructor as well we have to use that

340:32

so what I will do is I will say public

340:34

now the only changes in the Constructor

340:37

you don't specify the written type okay

340:40

so we can directly mention the customer

340:42

name here but what should be my

340:44

Constructor name now there's a

340:46

compulsion that your Constructor name

340:48

should be same as your class name so

340:50

Michael what is my class name it's human

340:52

and then since it looks like a method we

340:54

can give a round bracket and we can open

340:56

and close the curly brackets now one

340:58

thing I wanted to mention you can see

341:00

with the cuddly packets are mentioned

341:02

just I mean for the opening brackets are

341:04

just mentioned on the same line and here

341:06

the curry packets are mentioned on the

341:08

new line both Works Some people prefer

341:10

to use on the same line Some people

341:12

prefer to use on next line so anything

341:14

works now in this Constructor I can

341:17

assign the values but at this point

341:18

let's not assign the value I will just

341:20

simply print the statement I will say s

341:23

out and here I will print in Constructor

341:27

so you can see we are printing in

341:29

Constructor here so that's our

341:31

Constructor okay so let me repeat what

341:33

is Constructor instructor is a special

341:36

method that's why because it looks like

341:38

a method it has the same name as a class

341:40

name so those are the rules you have to

341:41

remember the Constructor will have the

341:43

same name as a class name a Constructor

341:45

never returns anything so don't don't

341:47

have to mention ain't float or doing

341:49

anything there so just specify the

341:51

access which is public and then mention

341:53

the name which is human

341:55

and in this you can do whatever you want

341:57

okay so I'm just printing in Constructor

341:59

so let me just try and compile so

342:02

comparison done there is no problem

342:04

code one thing to observe nowhere see

342:08

when you talk about this methods like

342:09

get age or set age all these methods we

342:12

have called them right so this is where

342:14

you are calling them

342:15

Constructor you can see we are not

342:17

calling it but still Constructor will be

342:21

called

342:21

okay so even if you don't call it it

342:23

will be called now when it will be

342:25

called every time you create an object

342:27

it will call the Constructor so every

342:29

time you create the object it will call

342:32

the Constructor example let's say if I

342:34

create two objects I will just copy this

342:36

line and paste it here of course I

342:37

should have a different name for the

342:39

reference obj1 now you can see we have

342:41

two objects now when you say you have

342:43

two objects and if I try to compile and

342:46

run this

342:46

you can see we got in Constructor two

342:49

times so every time you create a new

342:52

object it will create a Constructor or

342:54

it will call the Constructor okay if

342:56

that makes sense uh what we can do is if

342:58

we can remove this line and I can assign

343:00

the values I can say age is equal to 12

343:03

and name is equal to let's say by

343:06

default John of course you can have any

343:08

name but let's say John

343:10

now in this case I can see if this if

343:13

these values are assigned what I can

343:15

also do is I can just comment this

343:17

section let's say I'm not assigning the

343:18

values let's see what are the default

343:20

values compiled one you can see we got

343:22

John and 12. so by default

343:24

you assign this value okay so this is

343:28

where Constructors are helpful now you

343:30

might be thinking hey we can assign the

343:32

values here itself right uh see there's

343:34

a idea there's a standards which we

343:37

follow whenever you want to perform any

343:39

operation always do that in a method

343:42

don't openly do it in the statement like

343:46

here if you want to perform some

343:47

operations like assignment or

343:49

calculation always doing the methods and

343:51

Constructor is like method right so you

343:54

can assign the values here there is no

343:56

harm in terms of compilation you can

343:58

assign the values here as well but we

344:00

have to follow standards right and also

344:02

just try to think about something

344:04

Advanced let's say you are trying to

344:06

connect with the database now in that

344:08

scenario your application is trying to

344:11

connect with a database or the network

344:12

you need that connection right now where

344:15

you would like the connection

344:17

you can write the connections in the

344:19

Constructor

344:20

and that's why this becomes very

344:21

important now if you remember we have

344:23

also talked about method overloading so

344:25

when you have two methods with the same

344:27

name but different parameters those are

344:29

called method overloading now can we do

344:32

that in Constructor that's tricky right

344:35

okay so now we know what is constructed

344:37

right but what about this I mean can I

344:40

pass the values here what if I don't

344:42

want to assign values like 12 and John

344:44

what if I want to customize it so what I

344:47

want is every time I create the object I

344:50

should have an option of passing those

344:51

values not 12 and John every time can we

344:54

do that of course you can assign the

344:56

values later with the help of set

344:57

methods but what if I have an idea where

345:00

hey you know I okay let's let's go with

345:03

this object as well the second object

345:04

and in the second object I want to pass

345:07

the values the first object I want it to

345:08

be default the second object I want the

345:11

age to be let's say 18 and the name

345:13

should be let's say Naveen can we do

345:16

that if you can see this line is giving

345:18

you error it says hey you know you don't

345:20

have a Constructor which accepts two

345:22

values so those two values of type

345:24

integer and string you don't have it and

345:27

if you go up actually you don't have it

345:28

you don't have a Constructor which takes

345:30

two parameters can we do that let's try

345:32

so I will say public

345:34

human and I want this human to take two

345:37

parameters so I will say int

345:40

a comma string name and then here I can

345:44

just come back and I can say age is

345:46

equal to a so not the default value

345:48

which I want to assign here I want to

345:51

assign the value which is sent by the

345:52

main method so if you can see we are

345:55

passing 18 and Naveen here right so I

345:58

can just go back here and H is equal to

346:00

a name is equal to n now if you look at

346:04

this particular Constructor here it is

346:06

taking some parameter so this

346:07

Constructor is called a parameterized

346:10

Constructor oh I should say this is n

346:12

now what is the difference between

346:14

normal Constructor and a parameterized

346:16

Constructor is in fact it's normal

346:17

constraint is also called a default

346:20

Constructor

346:21

now we'll discuss why this caused

346:22

default Constructor so this is called a

346:24

default Constructor this is called a

346:26

parameterized Constructor because we

346:28

have parameters there and whatever value

346:30

you pass here will be assigned now you

346:32

can use both syntax you can either

346:34

create an object by not specifying any

346:37

parameter or arguments and you can

346:39

create a object with the help of values

346:43

which we are passing it here so this is

346:45

parameter wise and this is default now

346:48

what I will do just to explain this is I

346:50

will just copy the same line and I will

346:53

print the values for obj1 this time not

346:55

for obj obj one so basically we are

346:57

printing both and let's see what's the

346:59

output is compile run you can see we got

347:02

the first object drawn 12. the second

347:04

object is not John and 12 which is

347:06

Naveen and 18 because you are passing

347:08

those values now what I will do is

347:09

timeline I will remove this part just to

347:11

explain you something

347:12

see when I mentioned that every time let

347:15

me just comment this section as well see

347:16

every time you create the object it will

347:20

call the Constructor right but then if

347:22

you see I have removed the Constructor

347:24

will this work and it was working before

347:26

remember when we started this example of

347:28

human we were not having any Constructor

347:30

it was still working

347:32

at this point your Java will say hey

347:34

don't worry if you don't mention

347:36

Constructor there I will create a

347:39

Constructor for you and that's why we

347:40

say that's a default Constructor so

347:42

behind the scene you will get this

347:45

Constructor here which is public human

347:46

this is what you will get even if you

347:49

don't mention okay the only thing is it

347:51

will be default it will be blank if you

347:53

don't mention but if you want to do

347:55

something of course you have to mention

347:56

that explicitly and we can use the same

347:59

code or I can actually ask my IDE to

348:03

give me the code so you can simply go to

348:04

source and action and you can say hey I

348:07

want to create a Constructor but which

348:10

Constructor so I want a Constructor

348:11

which takes both these arguments and I

348:14

also want a default Constructor

348:16

Source action I want a Constructor which

348:20

is default you can say we got two

348:22

Constructors

348:23

there's only one change the syntax which

348:25

we have returned was a and name and here

348:28

if you can see we have age and name

348:30

and to make it work we have to use this

348:32

keyword I hope to remember this keyword

348:34

video previously and that's how it works

348:37

it has the same way which works the only

348:39

thing is we have to assign the values

348:40

here I would say age is equal to 12 and

348:43

the name is John so that's how we can

348:46

basically use a default Constructor and

348:48

a parameter-wise Constructor of course

348:50

we can create some more consider here

348:52

what if you want to just specify the

348:53

name not the roll number so you can say

348:56

I want a Constructor

348:59

Source action

349:01

and I want a Constructor which takes

349:03

only name so you can you can say we got

349:05

a Constructor which only takes a name

349:07

now what happens to this type of objects

349:09

example if I create an object here let's

349:11

say the second object and this time I'm

349:13

not passing two values I'm just passing

349:15

one value which is name in this case the

349:18

age will be zero so what you can do is

349:21

even if you're accepting name you can

349:23

assign the age by your certificates at

349:25

12. you can say age because there is no

349:26

confusion or you can also say this dot h

349:29

just to maintain the uniformity okay uh

349:32

so that's how we use Constructors

349:34

default Constructors parameterized

349:36

Constructors in this video let's talk

349:38

about this and super now this is a very

349:40

super concept so let's focus so what I

349:42

will do is I'll just remove all this

349:44

thing just to make it simple and clear

349:46

this part as well and for this I don't

349:49

even need uh this particular classes so

349:52

let's let me just close these files and

349:54

keep it simple okay so we have

349:55

everything very simple code here now

349:57

what I will do is just to understand

349:58

this and super I will create a class

350:01

called a I know this is a very simple

350:03

name but just for the example we can

350:05

take them and then we got a class B

350:09

okay we got two classes and then I'm

350:11

also saying that this B Class extends a

350:15

of course we are doing an hat and CR and

350:17

it makes sense because we just talked

350:19

about inheritance and then what I will

350:21

do is I will create a Constructor for

350:23

both of course we can do that right now

350:25

before creating Constructor let me say

350:27

if I create object of B of course we can

350:30

do that right so this B Class we call an

350:33

object of B Class here and we can do

350:35

that let me create a Constructor of B

350:38

here now how do we get a Constructor

350:39

it's very simple you specify public and

350:41

then you say B back it open and then

350:44

here I will type in B I'm just trying to

350:47

keep it as simple as possible that's why

350:49

we have taken Class A and B and then a

350:51

very simple statement now what I will do

350:52

is I will try to compile this code and

350:54

you tell me what will be the output

350:56

comparison done right if I run this code

350:58

there will be some output right now

350:59

think about this we are creating the

351:02

object of B okay we are instantiating it

351:04

and we have learned about this that

351:06

whenever you create the object it will

351:07

call the Constructor of a class so the

351:10

object we are getting is a b and we have

351:12

a Constructor which is B here which is

351:14

which prints a statement so of course

351:16

I'm expecting that it will execute the

351:18

Constructor and it will and you that's

351:21

why you got in B that perfectly makes

351:22

sense and we have talked about this for

351:23

a long time

351:25

The Twist here is what if I have a

351:28

Constructor in a as well and here I am

351:32

printing in a now what do you think will

351:35

be the output

351:36

because of course we are creating the

351:38

object of B not a so it should call the

351:41

Constructor of B and that makes sense

351:43

but if you compile this code you will

351:45

see something strange if I compile this

351:47

code and run oh it is also executing the

351:51

Constructor of a now that's weird for us

351:54

right because we are creating object of

351:56

B it should only call the constitution

351:57

of B

351:58

but it is also calling the Constructor

352:00

of a and that sounds weird

352:02

now we'll understand that but at this

352:04

point just remember whenever you get

352:06

object of a class it will call the

352:07

Constructor of subclass and superclass

352:10

both a is a superclass here B is a plus

352:13

it will call the Constructor of both why

352:16

we'll talk about it in some time but it

352:18

calls the constitute of both now let me

352:19

just add a Twist here in B let me create

352:23

a parameterized Constructor and we have

352:25

seen that right so what is parameters

352:26

Constructor a Constructor which takes a

352:28

value let's say this is n okay and here

352:31

I will print

352:33

in B int because we have a Constructor

352:37

which takes a integer parameter I'm just

352:39

printing e just in just to differentiate

352:41

between these two statements now with

352:43

this if I compile this code and run what

352:45

do you think what will be the output

352:46

compile and run still it is in a and in

352:50

B why not in B int is because when you

352:53

are creating the object you are not

352:55

passing any value here basically you are

352:58

calling a default Constructor but if you

353:00

pass a value let's say 5 in this case

353:02

you are specifically mentioning hey you

353:04

know I'm creating an object of B and I'm

353:07

passing a integer here so it will call

353:10

the Constructor of B which takes integer

353:12

Now by doing this if I run this code or

353:16

you can see it is printing in a and in B

353:19

int okay that actually makes sense it

353:22

will call the concept of a but what if I

353:25

also have a parameterized Constructor in

353:28

a as well which takes a n and it also

353:31

planes in a end now since we were not

353:35

having a parameters Constructor it was

353:36

calling a default Constructor that's

353:38

what I'm assuming at this point now when

353:40

you have a parameterized Constructor for

353:41

both for A and B both

353:44

now there is an expectation that it will

353:47

print in a int and in B int and that's

353:50

what I'm thinking I'm not sure what

353:51

you're thinking uh let's drive let's see

353:54

if this works oh no

353:57

what is happening here it is calling the

353:59

parameters constitute of B that actually

354:01

makes sense but why the default concept

354:03

of a it should have been called this

354:05

this one right let's understand why it

354:08

is first of all Calling The Constructor

354:09

of parent class why in the first place

354:12

the thing is and remember this point

354:15

every Constructor in Java

354:18

has a method

354:20

which is there even if you don't mention

354:23

okay and that method is super

354:27

now this method is there even if you

354:29

don't mention you can imagine that's

354:31

like a John Cena I don't know if you

354:32

know the John Cena memes but that's just

354:35

that you can't say it okay so super is

354:37

there you can't say it even if you don't

354:38

mention super it is there so by default

354:42

every Constructor the first statement is

354:45

super

354:46

and every Constructor not just this one

354:48

every Constructor here as well super

354:51

okay so every Constructor will have

354:54

super same goes for a as well it will

354:57

also have super

354:59

and super irrespective which class it is

355:01

it will have this statement super even

355:04

if you don't mention it is there okay

355:05

that's a point to remember now let's try

355:08

to understand how it is working let's do

355:10

a dry run the moment you execute this

355:12

statement what is happening it is

355:15

calling the Constructor of B which is

355:17

parameterized so it is calling this

355:19

particular method so when you execute

355:21

this method the first thing it sees is

355:23

super not the system.out.pental N what

355:27

it is saying is super now what is super

355:29

now super method simply means call The

355:31

Constructor of a superclass so let me

355:34

repeat super means call The Constructor

355:38

of a superclass now watch this

355:40

superclass this is a superclass it is

355:43

calling the Constructor of a superclass

355:45

but but which one the parameterized one

355:48

so the default one if you can see we are

355:51

saying default we are not passing any

355:53

parameter here so just calling this

355:57

particular Constructor and that's what

356:00

is printing in a and that's why if you

356:02

can see the output it is print first

356:04

printing in a and then once it executes

356:08

the a Constructor it will execute the

356:10

remaining part of B Constructor which is

356:12

this part in B int and that's why we got

356:15

in Bay end okay that means if I don't

356:19

want to call this Constructor if I want

356:21

to call the parameter wise one we have

356:23

to do only one change which is what if

356:27

in this super you pass a number so N is

356:30

a value right it will have a value the

356:32

same value you are passing to this super

356:33

now what we are saying is hey Java I

356:36

know you are going to execute this super

356:38

glass Constructor but I am passing a

356:40

parameter there so now this time you

356:42

have to call the parameterized

356:44

Constructor by doing those changes let's

356:46

compile and run here we go it is

356:48

printing in a int and in Bay end so

356:51

super is a very handy tool it's there

356:54

already you don't have to mention that

356:55

but if you want to make some changes of

356:57

course possible may you have to mention

356:58

so if you want to pass a parameterized

357:00

Constructor then explicitly you have to

357:03

mention super I hope that made some

357:06

sense okay now what I want is I want to

357:09

call the default Constructor of B okay

357:13

I'm not passing any parameter here I'm

357:14

calling a default of B

357:17

so of course if I execute this

357:19

if I compile and run you can see it's

357:22

printing in a and in B why is because

357:25

when you are calling the default

357:27

Constructor here in default concept of B

357:29

it will execute first it is calling this

357:32

super that means it is calling this

357:34

middle Constructor which is printing in

357:36

a and then it is printing in B it will

357:39

continue with this execution of consider

357:41

B right but what I want now is I know

357:44

you got the point what I want is I want

357:46

to execute in a int and then I want to

357:50

print in B so the output which I want is

357:53

in a INT in B so how can we play with

357:57

that so what if here if I pass a value

358:00

let's say 5 or any number doesn't matter

358:01

if I pass a number it will try to create

358:04

object of B with a default Constructor

358:06

it will execute the default Constructor

358:08

but now we are calling a superclass

358:11

Constructor by passing a value

358:14

so which one which computer will be

358:16

called of course the second one and it

358:18

will print in a INT in B let's try

358:21

compile run and that's what we got so

358:25

that's how we can play with super and

358:27

that is fun right now I have a question

358:29

for you maybe you have observed it or

358:31

not see in B Class when we are saying

358:33

super it actually makes sense because we

358:35

have a super class for B okay this is a

358:38

here is a superclass but when you say

358:40

super for a what is a super class for a

358:43

we have not mentioned anything here

358:44

right okay now that's why you have to

358:46

introduce A New Concept here every class

358:49

in Java that repeat every class in Java

358:53

extends the object class

358:56

so object is a class in Java which has a

359:00

lot of methods we'll talk about those

359:02

methods later but every class in Java

359:05

extends this class even if you don't

359:07

mention so even if you don't mention

359:09

this it is extending it again you can

359:12

imagine this as a John Cena it's there

359:14

you can't see it okay so even if you

359:16

don't mention or you mention it's the

359:18

same thing

359:19

So when you say super here you're

359:21

basically calling a Constructor of the

359:24

object class

359:25

okay an object class also we have a

359:27

Constructor this is what you are calling

359:29

cool right uh so even if you don't

359:31

mention it is there now you might be

359:33

thinking the b class must be extending a

359:36

and object both no B Class extends a a

359:39

class extends object so this is

359:41

multi-level inheritance okay there is no

359:43

multiple still now okay so yeah that's

359:45

how it works we have object class so

359:47

with this super represents the

359:50

Constructor of the object class so yeah

359:52

so we have talked about super but what

359:54

is this okay now let me introduce this

359:56

here the output which I want now is I

359:59

want to execute both the Constructor of

360:02

B okay so let me just keep it default

360:03

now let me say super I want to execute

360:06

both the Constructor of B so I want only

360:09

one object okay with one object can I

360:11

execute both the Constructor of course I

360:13

want to execute the Constructor of a

360:16

which is default but I want to execute

360:18

the Constructor of both P so what I'm

360:21

saying is

360:22

I want in a I want in B and in B int is

360:27

it possible uh for that what we can do

360:29

is see we are creating the Constructor

360:31

let me pass a value here let me pass 5.

360:34

so of course by doing that what we are

360:36

doing is we are calling the Constructor

360:37

which is the parameterized one

360:39

and we are calling super and that's what

360:42

he's calling the superclass Constructor

360:43

but then we have one more method called

360:45

this we have talked about this keyword

360:47

right now what this this will do is this

360:50

will execute the construct of a same

360:52

class let me repeat this will execute

360:56

the Constructor of a same class which is

360:58

this one we are not passing in a

361:00

parameter so it will call the construct

361:02

of a same class and then this

361:04

Constructor is calling the construct of

361:06

a superclass

361:07

let's try if this works compile run and

361:11

you can see that we got in a in B in B

361:14

int I mean of course this will execute

361:16

first this method but this method this

361:18

particular method B Constructor is

361:20

executing this which is the default

361:23

Constructor the default Constructor

361:24

executes the super which goes here it

361:27

will execute in a it will execute in B

361:30

and then in Bay end

361:32

so that's what is which is this keyword

361:34

and also this method and super method so

361:37

this method execute the constitute of

361:39

the same class super method executes the

361:41

construct of a super class in this video

361:43

let's talk about the naming conventions

361:45

now if you see the code which we have

361:47

written till now somewhere I was using

361:49

capital letters I was using small

361:51

letters when to use what

361:53

and then what if you want to join two

361:55

words how will you make it more readable

361:57

now that's where Java says it follows a

362:00

naming Convention of camel casing so

362:02

basically it uses

362:04

camel casing now what camera kissing

362:06

means or let's talk about in general how

362:08

do we name our classes now whenever you

362:10

create a Class A Class name should start

362:12

with a capital letter so when you talk

362:14

about class it should start with a

362:16

capital letter example if you want to

362:17

get a class called calculator the C

362:20

should be Capital now on the other hand

362:21

if you talk about variables or methods

362:24

so for variable and methods what you

362:28

will follow is you will start with a

362:31

small letter example if your variable

362:32

name can be marks a variable method name

362:34

can be show so it will always start with

362:36

small class capital

362:39

variable method small in fact we also

362:42

have a concept of interfaces which we'll

362:44

discuss later so if we talk about

362:46

classes and interface we'll have to make

362:48

it capital let's say interface is

362:50

runnable so basically this is the

362:52

convention which which we follow class

362:54

interface we have capital letters

362:55

variable methods we have small letters

362:58

we also have a concept of constants

363:00

which we have talked about uh so when

363:03

you talk about constants it will have a

363:05

capital but all capital example if we

363:08

talk about the value of pi if we talk

363:10

about a constant of brand so it doesn't

363:14

matter what your constant is so you can

363:15

have all capital here now why do we do

363:18

this we do this just to see and

363:20

understand what could be the thing which

363:22

you are doing example if you say if you

363:24

see this type of things you can guess

363:26

hey it can be a class or interface if

363:28

you look at this you know this is a very

363:30

this is a variable if you look at this

363:32

you know this is a method and we look at

363:34

this this you know these are these are

363:35

constants it makes your code much more

363:38

readable and that's important modern

363:39

right it's very easy to write a code

363:41

which works it's difficult to write a

363:43

code which anyone can read and that's

363:45

why you should follow this convention so

363:47

this is how you use the main convention

363:49

but what if you have a word which which

363:52

has two letters example if you talk

363:54

about show my marks if I want to do this

363:58

so you have to say show my mark because

364:01

if you say show my marks it is actually

364:04

a bit difficult to read right so what

364:07

you can do is every time you combine

364:09

words make sure that the first step of

364:10

the second word or the first letter make

364:12

it capital it makes it much more

364:14

readable compared to small letters now

364:17

this is what camel kissing means you

364:18

know we have camel so it goes up and

364:21

down so we have small letters then we

364:23

have in between we have Capital small

364:25

again we have Capital so this is camel

364:27

casing now apart from this there are

364:30

different languages which follow snake

364:31

casing rule where we use underscores

364:33

example in some languages they follow

364:36

snake casing they use underscore and

364:39

people who jump between different

364:40

languages they get this tendency of

364:42

using underscore so again Java will not

364:45

give you any error but if you follow the

364:47

Java pattern Java developers will not

364:49

like this so if you're writing a code

364:50

where you have a Java team or everyone

364:52

that works on Java try to follow camel

364:54

casing and but again it will not give

364:57

you either it perfectly Works underscore

364:59

works in the names but that's what we

365:01

follow we follow camera guessing here

365:03

not snake casing yeah uh if you have the

365:06

class name which is

365:07

again uh I want to say my data is a

365:11

class so you can say m is capital

365:13

because that's a class name now I will

365:16

give you I will show you some names and

365:17

you have to guess what those are example

365:19

if I say h

365:21

that's variable right okay what if I say

365:24

data what is this this is a constant

365:27

because everything is capital

365:29

okay uh yeah in in terms of constant if

365:32

you want to say my data which is a

365:34

constant you have to use my data but

365:36

again everything is capital right so at

365:37

this point you can use underscore just

365:39

to differentiate this and again these

365:41

are the good practices which I'm showing

365:43

you how about this if I use something

365:44

like human but I use a round bracket any

365:48

guess yeah that's a Constructor why is a

365:50

Constructor is because the first is

365:53

capital that means it represents a class

365:54

name and we also have a round packet

365:56

method like something so that's a

365:58

Constructor so this is a naming

366:00

convention which we follow and try to

366:02

use good names for your variables your

366:04

classes your methods so that it will be

366:06

easy for others to read okay so now we

366:09

know how to create the object right so

366:10

we have a class let's say we have a

366:12

class A and then in this class A we have

366:14

a show method now how do we call show

366:16

method that's very simple first you have

366:18

to create the object of a and then you

366:20

can call the show method now what

366:21

happens behind the scene so of course uh

366:23

you have your big jvm here and then in

366:27

this you will be having your stack

366:28

memory and then you'll be be having your

366:30

hip area as well now in this Heap memory

366:32

so if you look at line number 14 it will

366:34

create an object let's say the address

366:36

is 1 0 1 and then in this you will be

366:39

having a variable or methods we don't

366:41

have a variable as of now so let's say

366:42

we have a method we just show so of

366:44

course in order to call this show we

366:46

need object so we have done that and

366:47

then we have obj here the address will

366:50

be 1 0 1 and it will refer here

366:53

and then every time you want to call

366:55

show basically you have to say obj dot

366:59

show that's how it works right okay now

367:01

just to clear the things this obj here

367:05

is not called the object it is called a

367:07

reference variable right so this is your

367:10

reference variable what is called a

367:12

reference variable because it is

367:13

referring to an object the object is

367:15

here so this is your object

367:17

okay so the question is if I want to

367:19

create this object is this line

367:21

compulsory or do we have a shortcut now

367:23

to understand this mode Let's also use a

367:25

concept of Constructor so let's say we

367:26

have a Constructor here which is a and

367:29

then I'm just trying to print something

367:31

here it says object created so of course

367:34

we know every time you create the object

367:35

it will call the Constructor as well so

367:38

it should print so if I compile and run

367:40

this code you can see it says object

367:42

created and it is printing in a show now

367:45

actually if you look at this line here

367:47

there are two steps not one example so

367:50

let's say if you create a local variable

367:52

called marks okay and then you set the

367:56

marks for it you set the value for it

367:57

let's say 99

367:59

we had everyone hates this type of

368:01

people right anyway so we have this

368:03

variable so what we are doing is we are

368:05

first of all declaring the variable and

368:07

then we are as a value the same thing

368:09

can be done

368:11

you can do it in two two steps we can

368:13

declare the object and then we can say

368:15

obj equal to new a

368:17

now this is called reference creation

368:20

right this is where you're creating a

368:21

reference and then this is where you are

368:23

creating an object and assigning the

368:26

value to obj

368:28

that means this is I mean when you say

368:31

new a this is where you create the

368:34

object let me show that let me remove

368:36

everything from here and say new a this

368:40

is object creation not the entire line

368:43

when you say new a Constructor that is

368:47

your object creation the moment you do

368:49

that in your hip memory of course we

368:51

don't have all this data now so ignore

368:53

all this data which is which is there

368:55

let me just remove this this is not

368:57

there imagine there is no object so this

368:58

line will create an object doesn't

369:00

matter what's the address let's say 501

369:02

and it will have this show method okay

369:06

the only thing is we are not doing

369:08

anything we are not creating any

369:10

variable inside stack example when you

369:12

say ain't marks the moment you do that

369:15

it will create a int variable called

369:17

Marks here the value will be the value

369:19

will be blank and whatever value you

369:21

want to assign you can assign that here

369:22

but if you can see we are not doing

369:24

anything here we are just creating a

369:26

object that is created inside the Heap

369:29

memory nothing in static and if you

369:31

don't trust me let me just show you the

369:33

proof If I compile this code and run it

369:38

says object created this is the object

369:41

and this type of objects are called

369:43

Anonymous object you know why it's

369:45

because this type of object don't have a

369:47

name right what do you call when you

369:49

have a reference it is called a

369:51

referenced object okay because there is

369:53

a reference in this tag so when you say

369:55

obj there's a reference here for the

369:57

anonymous object there is no reference

369:59

the only problem with this type of

370:00

object is you can't reuse them once you

370:02

have written this line you got the new

370:04

object in the Heap how will you use it

370:06

that's the problem right so you should

370:08

use this object only when it is required

370:10

so let's say if you want to call show

370:12

you can actually call this okay so you

370:14

can say new a

370:16

dot show this is object right this is

370:19

object so using the object you can call

370:21

show the only thing is you can't use the

370:24

same object again

370:26

so if I compile and run it worked the

370:30

only thing is you can't use this object

370:32

once more so if you write this line once

370:34

more what will happen now I mentioned

370:36

that you can't reuse it what happens

370:38

when you do that you can see it says

370:40

object created so it has created two

370:42

objects every time you say new a it

370:45

creates a new object so whenever you

370:48

want to use a normal object you can use

370:50

this of objects now let's talk more

370:52

about oops concept now oops basically

370:55

stands for object oriented programming

370:57

now in object-oriented programming we

370:59

have talked about object class

371:00

encapsulation right now let's talk more

371:03

the concepts now one of the most

371:05

important thing in the concept of Ops is

371:08

called inheritance now what is

371:11

inheritance now there are two words

371:14

actually here which which will confuse

371:16

people is is and has now what these two

371:20

words are now if we talk about a

371:22

computer now of course computer itself

371:24

is abstract concept right now A a

371:26

computer can be a laptop a computer can

371:28

be a desktop a computer can be a mobile

371:29

phone that's right so even if you talk

371:31

about this phone this is a computer in

371:34

the form of mobile phone of course and

371:36

the computer itself is an abstract

371:37

concept no one owns a computer you

371:40

basically buy a hardware which can be a

371:42

desktop laptop or a phone now can I say

371:44

that if you talk about a desktop so I'm

371:46

talking about the desktop which is this

371:47

big and it will have certain parts right

371:49

a desktop will have a motherboard a

371:52

desktop will have a hard drive CPU Ram

371:53

so when you talk about this thing it is

371:56

desktop has a motherboard desktop has a

372:00

hard drive it has a CPU right so the

372:03

important keyword there is has if we

372:06

talk about her house a house will have a

372:08

TV a house will have a sofa AC bed so

372:12

all those things what a house has right

372:14

so house is an important word but at

372:16

this point let's not focus on has in

372:18

this video let's talk about is now what

372:20

is is okay so example if you talk about

372:22

a laptop so can we say a laptop is a a

372:26

computer so what it means is laptop got

372:29

it features from a computer so can we

372:32

take another example here if we talk

372:34

about a car let's say let's say if you

372:36

talk about a Fortuner which is very

372:38

famous in India so this car is actually

372:40

a car right so Fortuner is a car so

372:43

that's how you represent something you

372:44

represent a device or a physical entity

372:47

by referring to its upper model right

372:50

you try to take the features of it now

372:52

coming back to the enhance Concept in

372:54

real world example let's say if we talk

372:56

about the human world you know when I

372:58

was in my 10th standard so just for the

372:59

example okay nothing not that important

373:01

but when I was in my 10 standard I did

373:04

my 10th in 2005. at that point Nokia

373:06

released a lot of smartphones right of

373:09

course Nokia is not there anymore but

373:10

Nokia was one of the most important

373:13

factor in the smartphone industry now

373:15

when they came up with a smartphone one

373:17

of the most famous smartphone which they

373:19

built is double six double zero I'm not

373:21

sure if you have seen that I'm not even

373:22

sure if that you're that old maybe you

373:24

were not even born in that year but but

373:26

yeah so it was double six double zero uh

373:28

amazing phone if you can just search on

373:30

Google uh Nokia depositor double zero

373:32

you will see that phone now a lot of my

373:34

friends were having that phone it was

373:36

costly phone at that point it was around

373:37

10 000 rupees and I was not having any

373:40

phones so they used to ask me hey which

373:41

phone you have I used to say hey I don't

373:42

have a phone but then I realized hey

373:45

till when I will say that uh the thing

373:47

is next time when people used to ask me

373:49

which phone I have I used to say I have

373:52

double three one zero now I was not

373:54

having a new phone now this is my

373:56

father's phone and we know the concept

373:57

right father's phone is ears phone in

374:00

fact in in general when we say our

374:01

parents property it's your property

374:03

whatever they own it's yours I don't

374:05

believe in that okay I'm not a big fan

374:07

of inheritance on nepotism but yeah you

374:10

get everything uh so I used to say I

374:12

have that phone okay so this is it

374:14

heightens now how will you use this

374:16

concept in programming now in Java what

374:18

you have is you have different classes

374:19

right example if you create a Simba

374:21

class here and let me get a very simple

374:22

class let's say we have a class here

374:24

called a calculator now what features a

374:27

calculator will have a calculator will

374:29

have some variables here and then it

374:31

will have some methods let's say method

374:32

is ADD method is sub okay and then

374:36

method is multi so we have different

374:38

methods here or let's also have division

374:40

why division should be the left there so

374:42

we have all these methods right and it

374:45

is working so you can basically create

374:46

object of calculator and you can access

374:48

all these methods no harm in that but

374:50

what if you want to create a advanced

374:52

calculator now of course let's say uh if

374:55

someone comes to you and say hey you

374:57

know you made an awesome calculator and

374:59

now I want you to create a scientific

375:01

calculator or advanced calculator now

375:03

you have a choice you can edit the same

375:05

class but there's only one thing what if

375:08

you enhance the same class you add some

375:10

more methods here now you want to give

375:13

calculator to someone someone asking you

375:15

for the calculator object they basically

375:16

access all the basic features and

375:18

advanced features as well

375:19

so it is good to create a submit class

375:22

okay so you can get a separate class and

375:24

you can say advanced calculator right

375:26

and then this will have all the advanced

375:29

features of course it will have all the

375:30

basic features and it will have the

375:32

advanced features as well so we have

375:33

basic features Advanced features both

375:35

but don't you think you need to Define

375:37

these basic features again and again why

375:41

we already have those features here

375:43

right we already have those features

375:44

here can I just use those features in my

375:49

new advanced calculator

375:51

so what I'm saying is basically this

375:54

advanced calculator is going to inherit

375:57

the properties and features from

376:00

calculator so I can also say Advanced

376:03

character is a calculator and it will

376:05

get all the features of calculator okay

376:07

uh it will make much more sense once

376:09

once we start coding but what we are

376:10

doing here is we are doing inheritance

376:13

so basically inheritance is a very

376:15

important feature using which you can

376:17

just copy the feature from the upper

376:20

class so this is your can you can I say

376:22

this is a parent class so this is a

376:24

pattern class and this is a child class

376:28

because child class is accessing the

376:30

feature of parent class okay so whatever

376:32

I earn it's my money plus my parents

376:35

money is my money that's how the system

376:36

works right we can also say this is a

376:39

super class we have different words for

376:41

it that's it and we can say this is a

376:43

subclass we can also say this is a base

376:46

class not your base and this is a

376:49

derived class so it depends upon how you

376:52

represent that if you say this is a

376:54

parent class then you have to say it's a

376:56

child class if you say that's a super

376:57

class then it becomes a subclass if you

376:59

say this is a base class you have to say

377:01

this is a derived class that's how it

377:02

works okay now this is what inheritance

377:06

is so in theory we have talked about

377:08

what is an Essence right basically you

377:10

can just use the features of a

377:12

superclass let's try to Implement that

377:14

in this code of course will not be

377:16

working with humans anymore let me just

377:17

remove the entire code from here

377:19

and all gone and let me also make sure

377:23

that this is empty gone so we have a

377:26

very simple method which is main here

377:27

now what I want to do is I want to take

377:29

the same example of calculator here so

377:31

let me get a class called calc and of

377:34

course I can just create this thing uh

377:36

in some other files as well that

377:38

actually makes sense but just to keep it

377:40

simple I'm using the same file here

377:42

so we got a class called calculator and

377:45

in this I can add some methods and some

377:46

variables so let's say I will Define

377:48

some methods here and I will say public

377:50

and it can add two numbers it will

377:52

accept two integers as well I will say

377:55

N1 comma and two so we got two variables

377:58

right and then I can just return N1 plus

378:02

N2 okay now let's say in this calculator

378:05

we have two methods one is ADD and one

378:08

is sub which is sub here and there's

378:12

there will be a subtraction operation so

378:13

we got two methods right now one thing

378:15

to remember if you look at file explorer

378:17

every time you get a file it also

378:19

creates a class file so for every class

378:22

in your code it will have a class files

378:24

we have used a calculator a computer

378:27

human student before and that's why we

378:30

have all these classes what I will do is

378:32

I will just delete them okay so you can

378:33

see all these classes are gone in fact

378:35

let me also delete the demo one to see

378:37

what happens and that's fine you can

378:39

also delete you can delete all the class

378:40

files so every time you compile the code

378:43

it will also create a class files for

378:44

you let me let me show you that so I

378:46

will say Java C and if I mention the the

378:48

file name which is

378:50

demo.java the moment I say enter it will

378:52

create two class files because we have

378:54

two classes if I go to explore you can

378:56

see we got demo.class and we also have

378:59

calc.class now when you run this code if

379:01

you say Java and if you say demo uh it

379:05

will just run the class files and as you

379:07

can see we are we don't have any code

379:08

here for calculator so it will only run

379:11

the demo class

379:13

and that's how it works so if I just try

379:16

to write this code here which which we

379:18

already have and if I try to create

379:21

object of it let's try creating object

379:22

of it in fact it let me know one more

379:24

thing before we start

379:25

let me for this example let me cut this

379:28

part and create a new file and let me

379:31

say this is calc dot Java uh and yes we

379:37

have this code here so you can see now

379:39

we have two files and before I continue

379:41

I will just delete this class files

379:43

again

379:43

okay class file deleted and now let me

379:46

just compile this code once again

379:48

so if you can see when I'm compiling it

379:51

it is only compiling the demo file it is

379:54

not combine the class dot Java right but

379:57

when I create an object of it if I

379:59

create object of calc obj equal to new

380:01

calc of course if you want to use it we

380:03

have to create object first and now if I

380:06

recompile this and let's look at the

380:08

class files we got the class file for

380:11

class as well so if you are not using

380:14

calc it will not compile it it will just

380:16

say hey my responsibility is to compile

380:18

the Java file I'm doing it but if you

380:20

want to work with the class the

380:22

calc.class as well or crack.java as well

380:25

you have to create object and that's how

380:27

it compiles now once the compilation

380:29

done you can Simply Save the value in R1

380:32

or we can say obj dot add it will add

380:34

two numbers let's say four and five and

380:36

then we can also find R2 which will be

380:39

result of subtraction so I can say sub I

380:42

will say 7 and 3. okay so basically you

380:45

can see we got these two methods and

380:47

when you try to print the values I will

380:49

print R1 and I will give a space and

380:52

also print R2 so basically I'm just

380:53

printing two values back to back and if

380:55

I say come file again because we have

380:57

made some changes and run you can say we

381:00

got 994 so four plus five is nine seven

381:03

minus three is four and that's what we

381:04

are getting here

381:05

everything is working fine right now

381:07

there are two conditions here why we are

381:09

going for inheritance the first one is

381:11

what if you want to add some extra

381:13

features you want so let's say if

381:16

someone is asking you for hey you know

381:17

uh Naveen I want a calculator and I want

381:20

two features addition and subtraction

381:22

what will be the cost I would say okay I

381:24

will charge you ten dollars now let's

381:26

say if the same person come back and say

381:27

Hey you know I want a calculator which

381:30

has four features addition subtraction

381:31

division multiplication and maybe at

381:33

this at this point I want to charge

381:34

fifteen dollars now of course I can add

381:37

those two methods here or I can create a

381:40

new plus you know why is because if I

381:43

edit the same file if I add more

381:45

features here I can't sell calculator

381:47

for ten dollars now I have to make a new

381:49

product worth fifteen dollars I don't

381:51

have calculator I have advanced

381:53

calculator which does four operations I

381:55

want both the models I want to sell the

381:57

10 calculator and a 15 calculator that's

382:00

one reason okay so let me just do that

382:02

so let me create a new calculator and I

382:05

will say this is advanced calculator

382:07

which has all the features dot Java and

382:10

in this I will basically have two more

382:13

methods which is public and multi which

382:17

will take two values and it should

382:19

return N1 into N2 and likewise we'll

382:23

have one more method here for division

382:25

so this is div

382:27

and division we have to say slash that's

382:29

it so we got advanced calculator which

382:31

has two features but now if I create an

382:35

object of advanced calculator now

382:37

instead of normal calculator I can I can

382:39

access multiplication and division I

382:41

can't access add and sub and that's why

382:43

you can see we got another here it says

382:45

the method add is undefined for type

382:48

advanced calculator that means in this

382:50

we don't have add-in Supply so what do

382:52

you think what could be solution here

382:53

think about this so one solution which I

382:56

can see is just write those two methods

382:59

here of course we can do that okay we

383:01

can just copy copy and we can paste it

383:02

here but don't you think we are writing

383:05

the same code again and again now when

383:07

you repeat the same code it is called

383:09

redundancy right we have same code

383:11

multiple times and redundancy is a crime

383:15

so make sure that you don't repeat the

383:17

values you don't repeat the methods

383:19

definitions and classes the point is if

383:22

I don't Define it here how would I

383:23

access them that's where the inheritance

383:26

concept comes into picture so United

383:28

says hey don't worry if you already have

383:30

a class called calc you can simply say

383:33

extends calc your job is done okay so

383:36

you simply say advanced calculator

383:38

extends calculator and now all the

383:41

features of calculator comes in here and

383:44

there's one more Beauty even if you

383:45

don't have this Java file that's fine

383:47

for inheritance you need a class file

383:51

not a Java file so even if you delete

383:53

this it will still work okay so that's

383:55

the first first reason even if you don't

383:56

have a code ready with you if you do

383:57

even if you don't have a Java file you

384:00

can still use a class file okay so

384:02

that's the beauty of inheritance we are

384:03

simply saying extends calculator which

384:05

is this class now what do you think will

384:07

it work let's try

384:09

so you can see there's no problem add-in

384:10

subtraction working we can add some more

384:12

variables here I can just copy this code

384:14

and paste paste another same line

384:18

this is my R3 this is my R4 and I can

384:23

just say this is multiplication okay so

384:26

multiplying number like uh let's say is

384:28

5 and 3 okay and then dividing the

384:33

number let's say 15 by

384:36

four let's do this and now when I

384:39

perform this operation of course I want

384:40

to print R3 and R4 as well on the same

384:43

line I'm lazy to write multiple lines of

384:46

code here and R4 so we are printing

384:48

everything in in the same line let's try

384:50

what happens compile no problem

384:54

run how it works you can see that

384:56

addition is nine uh suppression is four

384:58

multiplication is 15 if I'm not wrong

385:00

yeah and division is is three so 15

385:02

divided by 4 will give you a quotient of

385:04

three

385:05

okay cool this is working right so this

385:08

is how we use inheritance so important

385:11

thing here is this advanced calculator

385:13

here is called a subclass this

385:16

calculator here is called a superclass

385:17

and to inherit we use a keyword called

385:21

extends okay and this is basically a

385:24

relationship of is a uh advanced

385:27

calculator is a calculator and that's

385:30

why we say is important about that cool

385:32

can I can I have multiple classes in

385:34

hitting each other we can that will say

385:36

in the upcoming videos at this point we

385:37

just went for a simple inheritance where

385:40

we just use the feature of our

385:42

superclass you can say parent has all

385:44

these two features child is just

385:46

extending it or using it I can say this

385:49

is nepotism so so basically we were able

385:52

to do two advanced calculator which uses

385:54

a feature of calculator right so this is

385:57

extends now what if in future let's say

386:00

we got one more requirement we want to

386:02

build a let's say scientific calculator

386:03

and I don't know the operations I will

386:06

just say advanced calculator I'm very

386:08

bad with names I will just use this and

386:11

this advanced calculator will have extra

386:13

feature let's say it also has a feature

386:15

of finding the power of a number okay so

386:19

let's say public int I can simply say

386:21

power

386:24

I want to do that so it will take two

386:26

numbers the first value N1 and I also

386:30

want a power let's say N2 and let's say

386:32

this will return okay how do we do the

386:34

power so basically we have a math class

386:35

I can just simply use it matte dot yeah

386:39

we have a function of power just take

386:41

double value but that's fine I will just

386:43

pass integer double also takes integer

386:45

value N1 into

386:47

and the problem is it also Returns the

386:49

double value if you can see Power

386:50

returns a double value so what I will do

386:52

is let me return double value okay so

386:55

you have a choice you can return double

386:56

value or you can just Typecast it with

386:58

int and you can do that otherwise double

387:01

is okay so you can see we have a power

387:02

function which takes this method and it

387:04

it calculates now the thing is if I want

387:07

to create object of power when I will do

387:09

that I will just create that in demo I

387:11

will say hey if someone wants to try

387:13

very advanced calculator they can do

387:16

that okay it's just that very advanced

387:18

calculator if you can see we don't have

387:20

any other methods okay now that's

387:22

that's an issue right now I know in your

387:25

mind you have a solution right you have

387:27

a choice you can just add you can just

387:29

type all these methods here or we can

387:32

come back here and say extends okay now

387:34

it will extends what if we can also say

387:37

calculator but then this will only have

387:39

addition suppression division

387:40

multiplication but not the feature of

387:43

advanced calculator which is oh it will

387:44

not have this two as well because this

387:46

two is there in advanced calculator it

387:48

will only have ADD and sub you can see

387:50

multi and division not found

387:52

in fact you know I should also have R3

387:54

in fact this should be of type double

387:56

double R5 equal to obj Dot Power by

388:01

passing two values one is let's say four

388:04

raised to two at least we know the

388:05

output it should be 16 uh and I also

388:09

want to print R5 oh we are printing

388:11

everything in one line great and you can

388:13

see multi individual does not not

388:14

working so if you want to make it work

388:16

you can't just extend calculator you

388:18

have to extends advanced calculator so

388:21

you can see we have multiple hierarchy

388:22

here so very advanced calculator is

388:24

extending advanced calculator advanced

388:26

calculator is extending calculator so if

388:29

you try to compile this code let's see

388:30

if this works compile and run

388:33

you can see it's working you you got 16

388:35

at the end you got points over because

388:37

we are returning a double value this

388:39

works so if you're trying to imagine

388:41

this what we are doing is so we have a

388:44

calculator and then advanced calculator

388:47

is extending calculator and then we got

388:49

very advanced calculator is extending

388:52

from advanced calculator now this is

388:55

when you are saying multiple where we

388:57

are doing inheritance right so this is

388:59

called

389:01

multi-level enhance so basically we have

389:04

multi-level inheritance here because we

389:06

have multiple level but in the earlier

389:08

example we were having a just two right

389:10

so we were having this two now when you

389:12

say only two classes this is called

389:15

single

389:17

level

389:18

s and I know because I became a doctor

389:20

there but you got the point right so if

389:22

you have multiple layers that is

389:24

multi-level patterns and then you say

389:26

single that is single when you have only

389:28

two classes

389:29

now the very advanced calculator here is

389:31

taking the feature from advanced

389:33

calculator advanced calculator is taking

389:35

the feature from calculator so by

389:36

default all the character stuff will

389:38

come to varied once and that is

389:40

multi-level so in the last video we have

389:42

talked about what is single level

389:43

inheritance and then we have talked

389:45

about multi-level right now it is

389:47

multi-level so when you have the class

389:49

which is getting headed by someone else

389:51

and then someone else something else is

389:53

getting inhated by something else it is

389:55

multi-level now can we have a different

389:57

structure here let's take one more

389:59

example so if you say we have a we have

390:02

B and we have C now this is multi-level

390:05

right and of course when you have a and

390:09

b that's it this is single level but

390:11

what if you have a and b and then you

390:14

have C where your a is your parent in

390:19

fact in this situation as well if you

390:21

try to expand this one here as well we

390:23

have

390:24

this a as a parent

390:28

and then this B becomes a child

390:31

now b b becomes a child for a but then B

390:34

is also a parent

390:36

for C right so C becomes child here now

390:39

in the same way if we talk about this a

390:41

b c here we are saying both are parents

390:45

so we have a as parent and B as parent

390:48

and then C becomes a child

390:52

so C is anything from A and B both now

390:55

is it possible in Java let's try in fact

390:58

it is possible in other languages like C

391:00

plus plus let's try in Java what I will

391:03

do is I will just go back to the same

391:04

class okay same file demo and here let

391:07

me get a class called a and of course a

391:09

will have some methods this should be

391:11

capital A and then Class B and Class C

391:16

okay we have this uh three uh classes

391:19

here okay and let's say C is okay let's

391:23

try this first if B extends

391:27

from A and C extends from B okay this

391:32

works we can say there is no problem

391:33

here but what if I want the seed to

391:37

extend from A and B both as you can see

391:40

this syntax is not working it gives you

391:43

an error as well you can't put a comma

391:45

when you say extends a comma B okay does

391:48

it means we can use extends

391:52

extends B now even this will not work so

391:54

it doesn't matter what combination you

391:56

do multiple inheritance in Java will not

392:00

work

392:01

yeah so this thing will not work

392:03

multiple enhancements will not work but

392:05

the question is why so let's try to

392:07

understand that here now let's say if we

392:09

go back to this particular example here

392:11

let's say

392:12

you create object of B okay in this

392:14

scenario if you create object of B let's

392:16

say obj

392:17

now obj can call all the methods of b

392:21

and a both so let's say in B we have a

392:23

method called show okay let's let's say

392:25

there is a show method and then

392:27

from obj you are trying to call show

392:30

will this work yes this will work let's

392:33

say in a we have a method called ABC now

392:36

if you try to call obj Dot ABC yes this

392:39

will work but what happens you know when

392:41

you say obj dot ABC it will try to

392:44

search for it first in the b class in b

392:47

class we don't have it so it will say

392:49

okay I don't have that method maybe my

392:51

parent class will have so it will go to

392:53

a by checking hey do you have ABC parent

392:55

will say yes I have now in this case it

392:58

will work

392:59

the same thing is applicable for c as

393:01

well let's say if you create object of C

393:03

which is obj again and then if you try

393:05

to call a method let's say let's say a

393:08

has a method called X B has a method

393:10

called Y and C has a method called Z so

393:13

let's say we have Z method and then when

393:14

you're trying to call Z this will work

393:16

because in C itself we have the method

393:18

if you say obj dot y will this work yes

393:23

because it is coming from B now when you

393:25

say obj dot X it will also work because

393:29

it will call from a so first it will

393:31

search in C in C we don't have X it will

393:34

go to B in B also we don't have X it

393:37

will go to a yes in a we have X okay it

393:40

works but now if you come back to this

393:43

example let's say if you get object of C

393:45

as object so let's say this is C object

393:47

okay and then if you let's say in this a

393:51

we have a method called show okay and

393:54

that's how that's how we're working so

393:55

let's say if you if you call show now

393:57

now of course c will say okay I don't

394:00

have a method so will this work let me

394:03

ask my parent so it will go to a a will

394:05

say okay I have show problem solved this

394:08

will work right engine I'm not talking

394:09

about Java let's say in general this is

394:11

how it works now let's say we have in B

394:13

we have a method called X okay now it

394:16

will say obj dot X will this work now it

394:19

will search and see and see we don't

394:21

have it in a also we don't have it in B

394:23

we have it so this will also work

394:25

okay but what if there's a method called

394:27

Y which is there in a and b both okay

394:32

and now if you are trying to say obj dot

394:36

y

394:36

the problem is from say we don't have it

394:39

and a and b both has this method which

394:43

one you will choose that's a confusion

394:45

right

394:46

uh it is almost same like let's say if

394:48

you want to call someone your phone is

394:49

dead and then now you want to call so

394:52

you can ask your patents and both the

394:54

parents has a phone which one which

394:55

phone you will choose now if you chose

394:57

your dad's phone

394:59

your mom will feel bad if you if you

395:01

choose your mom's phone your dad will

395:03

feel bad again I'm just giving an

395:05

example here I'm not saying it literally

395:07

happens but in fact they will be happy

395:09

that you're not using their phone but uh

395:11

yeah that's a confusion we have here

395:12

right now this problem is called Ambi

395:16

Beauty problem okay so ambiguity simply

395:18

means we have we have a confusion here

395:20

which one to choose

395:21

now this problem is there in multiple

395:23

languages which supports multiple

395:25

enlightens Java says oh that's a problem

395:28

let's remove the feature itself

395:30

okay I know that's a weird way of

395:33

solving the problem but yeah they have

395:34

removed the problem itself so it's not

395:36

like they found a solution they said

395:38

instead of finding a solution let's

395:40

remove the feature that's right in Java

395:42

multiple inheritance does not work okay

395:46

so in the it's a very famous interview

395:48

question uh do we support multiple

395:50

entities in Java and the answer is no

395:52

there is a indirect way not actually a

395:55

way it's just that it is possible by

395:58

going all through steps but yeah that's

396:01

how Java is implemented in new feed in

396:03

new versions they have done something

396:04

which changes it uh with the help of

396:06

interfaces but we don't know interfaces

396:08

yet so let's talk let's say Java does

396:11

not support multiple enhancements

396:14

oh okay this makes sense so once we

396:17

talked about inheritance and Method

396:18

overloading as well right we have talked

396:20

about method overloading in this video

396:22

let's talk about method overriding

396:24

that's right so we have two different

396:26

method overloading and Method overriding

396:29

so let's talk about overriding here so

396:31

for that what I will do is I will just

396:32

try to keep it simple again and let's

396:34

use the class A and inside this class A

396:37

let's say we have a method so I will

396:39

just remove this part and here I'll say

396:42

public

396:43

void show just to keep it simple I'm

396:45

using a very simple example here which

396:47

is uh show which prints something let's

396:50

say it prints in show okay now if you

396:53

have a method inside a class of course

396:56

you can create the object and you can

396:57

call the method right and we have seen

396:59

that so let's say if I go back to my

397:01

main method if I say a obj equal to new

397:04

a and I can simply say obj dot show and

397:08

it will work right and it should work

397:11

that's what we have done from a long

397:12

time so let's compile this code and run

397:16

and you can see it prints in show just

397:18

to keep it simple we have we have went

397:19

with a very simple example now what I

397:22

want to do is I want to get a class

397:24

which is Class B and I want this class b

397:27

to extend Class A and of course when you

397:30

talk about inheritance this method show

397:32

it also will be available inside Class B

397:34

right so if I create object of Class B

397:37

yes of a it will also work because

397:39

that's how many items works right so if

397:42

I go back compile and run it works you

397:45

can see we got in show what I will do

397:47

now is I will also print in a show just

397:51

to differentiate that this is getting

397:53

printed in the a class okay now the

397:55

thing is

397:56

what if you want to have a different

397:59

definition of it okay why do we need

398:01

different definition let's say we got

398:03

two methods here public wide config so

398:05

let's say we have this method which is

398:07

called config I don't know what it will

398:08

do just to keep it simple I will just

398:10

say s out and I will say this is in a

398:13

config okay and let's say they have

398:15

different implementation they have

398:16

different statements at this point we

398:18

are not focusing on statements we are

398:20

here to understand the concept right so

398:22

let's say it does something show method

398:24

and config does something and I can

398:26

actually call both the methods not just

398:28

one I can call config as well okay and

398:31

if I

398:32

compile and run this code it works you

398:35

can see it says in a show in a config

398:37

now let's say I want a different

398:39

definition for sure okay not for config

398:42

so basically what I want is when when it

398:44

comes to B and when I call show it

398:47

should print in B show

398:49

how do I do that how do I change the

398:51

execution so what I want is when I call

398:53

show it should not call the show of a

398:56

because show of a will print in a show I

398:59

want to use config as it is so I can't

399:01

even remove this inheritance if I remove

399:03

this and if I Define the methods of

399:05

course I can Define show but then I want

399:08

this config to be coming from a so of

399:11

course you have to keep it you have to

399:12

say Class B extends a because we want

399:15

config what we don't want of a is show

399:18

because it is printing in a show I want

399:20

to do something else

399:22

and that's an audio you can say public

399:23

void show you can write the same method

399:25

name or maybe a different name you can

399:27

say in show one I mean so one and you

399:31

can print here in B show I mean of

399:35

course you can do this the only thing is

399:37

if you come if you execute this code now

399:40

it will still print in a show you know

399:42

why is because we are calling show and

399:44

both these methods are different one

399:46

with name show and one with name show

399:49

one

399:50

okay that means if I call show one then

399:52

it will work then it will print in B

399:54

show but what I want is I want them to

399:57

have a same name I want my this method

400:00

which is so

400:01

should override the method here

400:05

is it possible and the answer is yes in

400:07

Java we get this amazing feature where

400:09

you can basically override the methods

400:12

of the passing class so of course you

400:15

want everything except this part

400:18

okay I mean except this show method I

400:20

want config but I don't want show now

400:22

let me give you one more example here

400:23

remember when we talked about

400:24

inheritance I have used an example where

400:26

I was not having any phone in my 10

400:28

standard and my father had a phone which

400:31

is double three one zero now if you ask

400:33

me at that point which phone I have I

400:35

used to say double three one zero

400:36

because that's my father's phone

400:38

but after two years when I was in my

400:40

12th standard I got my own phone I got

400:42

Motorola c168 again uh not a smartphone

400:45

but it was good it was a very slim phone

400:48

so when now if you ask me which one I

400:51

have I was I will say I have Motorola

400:53

c168 my father still had double three

400:55

one zero so here if you can see

400:58

if someone asked my father father will

401:00

say I have double three one zero if you

401:02

ask me I have a choice of saying double

401:04

three one zero but I will go for c168

401:07

which is my phone right so you always

401:09

give preference to your own features

401:11

first example if you call show here it

401:14

will execute the method of b instead of

401:16

executing the method of a so basically

401:19

this method show

401:20

overrides the method of a here and we

401:24

can simply execute compile and Quran you

401:26

can see it prints in Bishop now this

401:29

will make much more sense when we say we

401:31

are creating not a but we are getting a

401:33

calculator

401:35

which has a method called let's say let

401:37

me remove config for some time let's say

401:39

that we have a method called uh add and

401:42

this will return a value okay and here

401:45

instead of printing this I can simply

401:47

accept two values I can say int and one

401:49

comma int and two

401:52

and here we have done this before and

401:53

that's why I'm writing this code very

401:55

fast against a return N1 plus N2 okay

401:59

so if your class which is instead of

402:02

saying B I can say Advance calc which is

402:05

extending calc it will also have some

402:07

features but let's say I want to add two

402:12

numbers which will also take int and it

402:14

will have the same parameter okay

402:15

because in method overloading we have

402:17

seen it can have the same method name

402:19

but different parameters here in method

402:21

overriding it will be same name same

402:24

parameters as well I mean same type of

402:26

parameters not the name doesn't matter

402:27

name uh let's say N1 N2 and it will have

402:31

the same name same number of parameters

402:33

same type of parameters but it will have

402:35

a different behavior let's say N1 plus n

402:36

two plus one I'm just adding extra 1

402:38

there so what I want is if I come in

402:42

this section okay at this point I'm not

402:44

overriding it I just have ADD method

402:46

inside calc and if I create the object

402:49

of advanced calculator by saying

402:51

advanced calculator obj equal to Nu

402:52

advanced calculator

402:54

and if I try to call the add method by

402:58

passing three and four of course 3 and 4

403:00

is 7 so if I get this value somewhere if

403:03

I say int R1 is equal to and if I print

403:06

the value for R1 and again since we have

403:09

done this code before I'm just typing it

403:11

fast

403:12

now what do you think what will be the

403:13

output I am passing 3 and 4. so of

403:15

course it will first search for this

403:17

method add inside the same class of

403:20

course we don't have that method so it

403:22

will go to the upper class by saying uh

403:24

hey you know uh calc you are my

403:26

parenting class I'm searching for ad

403:27

with you and yes it is available

403:30

so basically we can just compile this

403:32

code and run you can see we got seven

403:34

and that actually works but now I want a

403:38

different definition for add I just want

403:40

some changes instead of adding those two

403:42

numbers it should also add one at the

403:44

end okay so if I compile this code now

403:47

and run it will print eight so this ad

403:50

is overriding the add method of

403:53

calculator okay just remove this

403:55

statement that's extra so this concept

403:58

is called method over writing so this

404:01

method is overriding the method here I

404:04

hope that makes sense right in this

404:06

video let's talk about packages the

404:08

thing is whenever you have a file

404:10

doesn't matter is it a music files or

404:12

documents or code we normally put them

404:15

in a folder right and then we create a

404:17

folder structure as well in fact I

404:20

remember in my college days when we used

404:21

to store different songs you know we

404:23

used to create different folders like

404:25

you know we have a songs folder then

404:27

inside that we have rock songs DJ songs

404:30

songs romantic songs and in fact in that

404:33

as well when you go to sad songs we can

404:36

have normal sad songs ultimate sad songs

404:38

okay now that's exaggeration but we do

404:40

that right we create a structure there

404:42

in the same way when you talk about Java

404:44

project you will not be creating one or

404:46

two files right it would be having

404:47

multiple files sometimes your project

404:50

can have let's say uh 200 files 300

404:53

files okay maybe most of the time when

404:55

you have a complex project now how will

404:57

you put all these files in one folder

404:59

you can actually you can put everything

405:01

in one folder now if you see our project

405:03

structure here what we did is we have

405:06

this folder called codes and in this

405:07

codes we have all the files in fact we

405:10

only have three files now but just

405:12

imagine we have more files imagine we

405:14

have 100 files it's okay to put them in

405:16

one folder but it's always better if you

405:19

can create a structure where you can

405:21

have few files in one folder and few

405:24

files another folder and then you can

405:26

have a folder inside a folder now

405:28

normally this type of pattern in in Java

405:31

is called packages normally you you

405:33

store files in a package so yeah that's

405:35

right the folder is package here and of

405:38

course one package can be a part of

405:39

another package the way you have folders

405:41

inside a folder so how do we do that

405:44

here in Java so make you understand

405:46

let's say we have these two files you

405:48

know advanced calculator and calculator

405:49

now these two files are related right

405:52

they both work for the calculator so it

405:55

is a good idea to put them in one folder

405:57

now how will you do that you know you

405:59

have an option here of creating a new

406:00

folder that's one way all you can what

406:02

you can do is you can just come back on

406:04

top of your class and say you can create

406:06

a package so the keyword which you use

406:08

to create a package is called package

406:10

and you can put this class in that

406:12

particular package let's say the package

406:13

name is tools right so of course you can

406:16

create any pack any package name but it

406:18

should have one it should make some

406:19

sense so you can see I'm saying that

406:21

they are tools

406:23

okay and the thing is you can see we are

406:26

getting an error it says that declared

406:28

package tools does not match the

406:30

expected package the thing is you don't

406:33

have calc inside the package in fact

406:36

most of the IDS will give you an option

406:37

of directly if I click on Quick Fix yeah

406:41

so you can see most of the IDS doesn't

406:43

matter which ID you are using in vs code

406:45

we have this option in most of the

406:47

famous ID you will get this option of

406:49

doing something some red red symbol or

406:51

something like that here we have an

406:53

option of quick fix if I click on that

406:55

it says move calc.java to package tools

406:59

okay that is a good idea we can click on

407:03

this and you can see it created a folder

407:05

called tools

407:06

and now we can actually move this to

407:09

tools as well the only thing is when you

407:11

do that and when you go to this

407:14

particular class

407:16

okay it has already done the changes you

407:17

can see I have not written this it is

407:19

already there thanks to vs code and the

407:21

IDE uh so basically you get this here so

407:25

it's always a good idea to put your

407:27

classes in a package you can create a

407:29

complex structure if you want you can

407:32

create a one more folder here let's say

407:35

we're going to folder called database or

407:38

maybe DB connections or DB classes or

407:40

maybe you can say simply said DB now

407:42

this DB will have all the database

407:44

connections so let's say if you want to

407:45

create some files which will connect

407:47

with mySQL Oracle or it does something

407:50

with database so you can put all those

407:52

classes in database folder okay now

407:55

that's how you create a project

407:56

structure here

407:57

okay the only thing is if you want if

408:00

you have this structure here let's say

408:01

if you have calc and demo Advanced calc

408:03

and if you can see both are in a package

408:06

now the only problem you will face is if

408:09

you go to demo and you can see demo is

408:11

not a part of any package normally you

408:14

put the main class so the class which

408:16

has a main method separately outside the

408:19

package so that you can easily access

408:20

other classes

408:22

okay when I say easily access another

408:23

classes can I do that can I create

408:25

object of calc here so if I say calc obj

408:28

equal to new calc which was working

408:30

earlier if you remember our previous

408:32

examples

408:33

it's not working now you can say it says

408:35

calc cannot be resolved what's wrong the

408:39

problem is demo and calc now they are in

408:42

two different folders you can't directly

408:44

access it okay so when I say you cannot

408:46

directly access it do we have an

408:48

indirect way uh yes we have an entire

408:50

way so what you can do is if you if you

408:52

see this bubble

408:53

it says import calc that's right if you

408:56

want to use calc in your particular

408:58

class you have to say import calc but

409:01

then calc belong well calc belongs to

409:04

tools.calc so basically you have to

409:06

mention the package name and then you

409:08

have to mention the class name whichever

409:10

you want to import and now you can see

409:11

there's no issue how about advanced

409:14

calculator if I want to use advanced

409:16

calculator obj1 is equal to new advanced

409:19

calculator okay now you cannot access

409:21

access this one you know why is because

409:23

we have only imported calc not advanced

409:27

calculator how will you import this you

409:29

are right just come back here and say

409:31

import tools dot advanced calculator so

409:34

whichever class you want to use you can

409:36

just import them in fact Java internally

409:39

has a lot of classes example let's say

409:42

one of the classes which we are going to

409:43

see in future which is let's say list

409:45

now when we use least which design build

409:48

class or with input interface what is

409:50

interface we'll talk about that later or

409:51

maybe at this point we can talk about

409:53

let's say add a list which is a class

409:55

when we use analyst and when you create

409:57

object of it I will say list is equal to

410:00

new

410:01

add a list now the moment you do that it

410:04

will also come from a package you can

410:06

see it is now given given by the IDE it

410:09

is auto completing it but if I remove

410:11

this line you can say it will give you

410:13

error it says allergies cannot be found

410:15

so we have to import this one as well

410:17

and this belongs to a package now that

410:20

package is import

410:22

java.util dot as a list this is up to

410:25

you have to import now you might be

410:27

thinking why we have two dots here Java

410:29

dot util how it looks like so basically

410:31

we have a Java folder inside Java folder

410:34

we have a util folder so it's the folder

410:37

inside the folder and Java has multiple

410:40

folders efficiency Java dot we have

410:43

applet folder we have awt folder beans

410:46

folder Lang folder IO folder and all

410:50

this thing so in general whatever class

410:52

you use in Java belongs to a package let

410:56

me repeat whichever class in build class

410:59

which you use in Java belongs to our

411:01

package now you will be saying okay

411:03

everything makes sense but don't you

411:06

think string is also a class and one of

411:09

our favorite

411:10

class which is system system is also a

411:13

class right so when we are using system

411:15

we are we have never imported it so when

411:17

I say system.printer then there was no

411:20

need to import the package but the

411:22

system belongs to our package and the

411:24

answer is yes if I go to system and if I

411:27

go on top here can you see that it says

411:30

package so this class system class

411:32

belongs to our package java.lang but we

411:35

never imported it the thing is by

411:37

default every Java file will have

411:40

java.lang by default imported so even if

411:43

you don't mention that it's there this

411:46

line is there even if you don't mention

411:47

the thing is it's actually this not this

411:50

line what is by default is this

411:53

java.lang.star now what is this star now

411:56

in one particular folder if you have

411:58

multiple files and if you if you want

412:00

all the files you can simply say start

412:02

just like we have tools here in tools we

412:05

only have two files and we are importing

412:07

both so can we just do that yes we can

412:10

so we can simply put a start here our

412:12

job is done and by doing that I've also

412:14

removed the list we don't even need that

412:17

you know that so basically if you want

412:18

to import all the files you can say

412:20

start okay so that's how you can create

412:23

a package in Java as I mentioned you can

412:25

create a folder inside a folder example

412:27

let's say if I have a folder called

412:29

other so let's say we have other folder

412:31

and these tools actually belongs to

412:33

others so now if I move this

412:36

you can see we have tools inside the

412:39

other now how it will look like when you

412:41

talk about your the calc so you can see

412:43

it says other dot cap so you have to if

412:46

you want to go inside the package

412:47

normally in folder structure we use

412:49

slash here we use dot so just others dot

412:52

tools dot uh calc okay that's how you

412:56

have to import it and of course you can

412:58

say start it will import everything

413:00

now my question to you is if I say

413:02

import other dot star now is this two

413:06

line same

413:08

or can I remove this of course right

413:10

when you say start it should input

413:11

everything but now if you can see it is

413:13

giving you error the reason is

413:16

when you say others.start it so star

413:18

simply means all the files not all the

413:21

folders okay so in other if you have

413:24

some files let's say if I create a new

413:26

file here I will link this as a DOT Java

413:28

the moment you do that you can see this

413:30

a class belongs to other package now you

413:32

can easily use a but not calc so if you

413:35

say a obj 2 is equal to new a this will

413:39

work is because you are saying others

413:42

dot start it will import all the classes

413:44

which are all the files in in the other

413:46

folder not inside a folder which is

413:48

tools so if you want others I mean tools

413:51

as well so you have to mention

413:53

others.tools dot star so that's how

413:56

basically you use a package structure

413:58

now there is one thing you know not

414:00

important at this point but since we're

414:02

talking about package let's talk about

414:03

it because you will be seeing those type

414:05

of structure multiple times when you

414:07

create a project a big project normally

414:09

you deploy your project on the server

414:11

somewhere right

414:12

and maybe you're creating a library

414:14

that's right what you do is when you

414:16

create a project it's not always for

414:19

your use sometimes you create libraries

414:21

so that others can use your files now in

414:24

this case as well you will create a

414:25

package and then you will give your

414:27

package to others

414:28

the only thing is when you are sharing

414:30

your package to the entire world it

414:32

should be unique it should have a unique

414:34

name let me show you something I will go

414:36

to a website now this is one of the

414:39

website where you can get the libraries

414:42

the library simply means if you want to

414:44

do something and if you don't want to

414:46

write your the code entirely by yourself

414:48

you can use some libraries and most of

414:51

most of the time we do that example

414:52

let's say if I want to connect my

414:53

application with mongodb which is a

414:55

database so I can simply search for

414:57

mongodb drivers sync this is a driver

414:59

which we use to connect Java with

415:00

mongodb so you can use this package in

415:03

your project okay let me select a

415:05

version

415:06

so this is the package

415:09

okay now the thing is when you talk

415:11

about this package what should we name

415:13

of it so this name should be unique on

415:16

the internet now how will you make

415:18

something unique that's tricky point

415:20

right so when you are creating a project

415:22

how will you make it unique so one way

415:24

to do that is by reversing your domain

415:26

name that's right of course right when

415:28

you're building a library you will also

415:29

have your website your company website

415:31

maybe so let's say if you talk about a

415:33

company like Google so if I'm working

415:34

for Google and if I'm creating a library

415:36

so what I will do is I will name I will

415:39

say this belongs to a package so if I'm

415:42

making a project for let's say Google

415:43

and I'm making a calculator or advanced

415:46

calculator so I will say com dot Google

415:48

dot uh calculation now this is my

415:52

package name okay so by making

415:54

com.google I'm making sure it is unique

415:56

right only Google can own this domain

415:58

right it's their domain so that's how

416:00

you do that so depending upon your

416:02

project you will see this type of

416:03

packages in fact in future videos we

416:06

will be using some packages which have

416:08

com dot something it means they have

416:10

just reversed their domain name just to

416:11

make it unique but again for learning

416:13

purpose you can name your package

416:14

anything you want example the way we are

416:16

doing here pack each other but it should

416:18

have it should have a meaning that's

416:19

important now once we talked about

416:21

packages it's a good time to talk about

416:23

access modifiers now one of the access

416:25

modifier we have seen in fact we have

416:27

seen two private and public

416:30

apart from this we have more we have

416:32

protected and default as well so in this

416:34

video let's try to explore them or one

416:36

or two videos so what I will do is

416:38

initially I will start with public now

416:39

what is public basically let's go to

416:41

demo and we have we were trying to

416:43

import the packages and of course we

416:45

should do that right anyway we don't

416:47

have to do that java.lang it is it comes

416:49

already so we don't have to worry about

416:51

it and now what I will do is I will just

416:53

go back to my class A let's work with

416:57

class A now and in this class A you can

416:59

see that class A is public okay and it

417:02

belongs to a different package called

417:03

other and in this class I will just

417:06

create a variable called int let's say

417:09

marks so we have this variable called

417:11

marks and the let's say initial value

417:12

for Marks is six because we just want to

417:14

access this is it working or not now how

417:16

will you access this variable so you can

417:18

just go back to demo so we have ADD we

417:21

have a right which has this code which

417:23

is simple class and then we have our

417:25

marks let me go back to demo and in this

417:27

I want to access it so I will just

417:29

remove all this lines just want to work

417:32

in fact tools is also not I need now

417:34

that only work with other and in this

417:36

let me create a object of a now

417:38

connected object of a yes we can because

417:40

we can access it and then I'm just

417:43

trying to say obj dot marks now I'm not

417:47

trying to do that you can see it gives

417:49

you an error it says the field a DOT

417:52

marks is not visible

417:54

I mean that's weird it's there right

417:55

Marx is there the thing is it is not

417:58

public now when it is not public you

418:00

cannot access it outside the package you

418:02

can see that that's the issue we got now

418:04

what if you have a class in the same

418:06

file or maybe in the same package let's

418:09

say in the same package which of which

418:11

is of demo I will create a file called B

418:14

dot Java and in B dot Java let me just

418:17

say because they're in the same package

418:19

now so no package is same package

418:20

basically I will try to create a

418:23

variable called int let's say marks

418:26

let's see if I can access this marks and

418:28

demo I will just go back here and I will

418:31

repeat the same sentence same line in

418:33

fact two same same statement with the

418:36

help of B object now this is B the only

418:38

thing is we have to use a different

418:39

object name because we cannot have same

418:41

and using this obj can access marks so

418:44

you say there is no issue even if marks

418:47

is not public it's no issue because they

418:50

are in the same package the moment you

418:52

have a different package and if you

418:53

don't mention public it will not work

418:55

okay that's the issue you can see it is

418:57

not working now if you want something to

419:00

be accessed outside the package always

419:03

make it public and that's why methods

419:05

most of the time they are public

419:07

variables should not be public but

419:09

method should be public example if I say

419:10

public void show here now show can show

419:14

is acceptable I can use show from

419:16

anywhere example if I can come back here

419:18

obj dot show this will work because

419:21

that's public but if I don't write

419:23

public that it will not work but that's

419:25

one so public means it can be accessed

419:28

from anywhere okay now apart from public

419:31

we also have a private so if I talk

419:35

about private can I use private anywhere

419:37

private variable can be used in the same

419:40

class irrespective of the package okay

419:42

package the secondary part it can be

419:44

used in the same pack same same class I

419:48

can't even use outside the class example

419:51

let's say if you look at this B and if

419:53

you look at this demo they're in the

419:54

same package right

419:55

but now if I create a private variable

419:59

there here if I make this as a private

420:01

variable which is which you should

420:02

always Mark your variables as private

420:05

the moment you make it private even if

420:06

the same package you can't access it so

420:09

private variables can be used in the

420:11

same class irrespective of the package

420:13

it's on the same class now let me do

420:16

iterate when you talk about public it

420:17

can be used from anywhere okay you can

420:20

see this table a public can be used from

420:22

anywhere

420:23

private can be used from the same class

420:26

okay now apart from public and private

420:29

we have two more okay let's talk about

420:31

those now now if we talk about this a if

420:34

you don't mention private something

420:36

should be applicable right if it is not

420:38

private is not public what is it so by

420:41

default there is something called a

420:44

default access modifier okay which is

420:47

also called private protected but we'll

420:48

talk about that later but it is called

420:50

default Now default simply means it can

420:53

be accessed in the same package it's as

420:56

simple as that it can be accessed in the

420:58

same package example if we talk about

421:00

this B when private was not there it was

421:02

able to be accessed in the same package

421:04

so we got private which can be accessed

421:07

in the same class we got public which

421:09

can be accessed from anywhere and then

421:11

we got private which can you only be

421:13

accessed in the same class and default

421:16

in the same package okay apart from this

421:18

we have one more which is called

421:20

protected now what is protected now

421:23

let's say we have this class A which has

421:26

let's say if I make this variable as

421:28

protected okay and then I'm going back

421:31

to demo and inside this demo somewhere

421:33

I'm creating a class in the same package

421:35

basically it doesn't have to be in the

421:36

same class or same file in a different

421:39

package basically let me create class

421:41

called C and then I'm saying that this

421:45

class extends

421:48

a now in this class A so you can see

421:50

this is protected uh can I use protected

421:53

in some other package

421:55

you can see marks is predicted in the

421:58

other package like demo again Marx is

422:00

not working so basically

422:03

if you make it protected you can't use

422:05

it outside the package before coming

422:07

there you try to know what we should do

422:09

let's try to create a class here itself

422:11

let's say a class called launch and in

422:15

this class let me have a method which is

422:18

public void ABC let's say and let me try

422:21

to create object of a equal to new a

422:25

and see from here can I try if I try to

422:28

use marks let me just print marks obj

422:31

dot marks okay it is working so it works

422:34

in the same package

422:35

but it doesn't work in other packages

422:37

okay so you can see demo is another

422:40

package but what about a subclass we

422:43

have class C here and then

422:45

if I try to create

422:47

a method public void ABC and you can see

422:50

this class C looks same in both the

422:52

packages the difference is this is a

422:54

different package and it is extending a

422:55

now and now if I try to access

422:59

marks it works there's no issue you

423:02

cannot access marks outside the package

423:04

but you can access marks outside the

423:07

package but it should be a subclass

423:09

if you can see C is a plus of a now if

423:13

you see this image here this image looks

423:15

like right

423:17

let's talk about private private can be

423:19

used in the same class nowhere else

423:22

protected can be used in the same class

423:26

in the same package

423:28

in the other package but subclass

423:31

public can be used everywhere

423:34

default can be used in the same package

423:37

okay it's it's that simple so this is

423:40

how you specify the modifiers now how do

423:43

we use and which one to use basically

423:45

the idea is very simple

423:47

try to make your classes public which

423:49

you are doing and of course we don't

423:51

have a public here the thing is you

423:53

can't even make this class public

423:54

because you cannot have two public

423:55

classes in the same same file

423:58

but and the general idea is in one file

424:02

you should have only one class

424:04

so try to make your class public next

424:07

try to keep your variables the instance

424:09

variables private the methods most of

424:12

the time will be public but if you think

424:14

there is a method or a variable which

424:15

should be accessed only in the subclass

424:17

of other packages use predicted try to

424:21

avoid default default is not something

424:23

we should be using I know from from if

424:25

you check all this thing we are

424:27

promoting default a lot but don't do

424:30

that don't use default stuff example

424:32

this is default either make it private

424:34

public protected or public don't keep it

424:36

default default simply means you don't

424:38

know what to give the access so give it

424:40

private predicted or the public in this

424:43

video let's talk about polymorphism

424:44

remember we have talked about method

424:46

overriding and then we say there is

424:48

something more this is where we are

424:49

talking about so we have a concept of

424:53

polymorphism now this is a very

424:55

important Concept in terms of op and

424:57

most of the languages which implements

424:59

Ops has this concept now what is

425:01

polymorphism now basically as a concept

425:03

it has two words one is Paulie's second

425:06

is morphism now Paulie simply means many

425:10

and morphism means Behavior now what it

425:13

means is there is something which

425:15

behaves differently in different

425:16

situation based on uh yeah based on

425:19

situation let's say as a human you

425:20

behave differently at different

425:22

locations right example for as at home I

425:25

behave differently at my office I behave

425:28

differently when I'm with my friends I

425:29

behave differently right so that

425:31

situation changes me in the same way

425:33

because the name also changes right at

425:35

home I am uh I'm a son husband of a

425:38

father at office I'm a colleague I'm a

425:41

boss I'm an employee at my with my

425:44

friends okay they use different words

425:46

but then let's not mention that so the

425:48

important thing is your behavior your

425:50

name changes at every place right your

425:52

behavior changes based on that in the

425:54

same way in the world of object oriented

425:56

we have a concept of many Behavior

425:57

polymorphism so the same object or the

426:00

same reference will have different

426:02

Behavior based on how you create these

426:04

questions of it now to implement this

426:07

there are multiple ways in different

426:09

languages but before we go into the

426:11

implementation let's talk about its type

426:13

so there are two types of polymorphism

426:15

one is called a compile time

426:19

polymorphism and second is called a

426:21

runtime polymorphism of course they have

426:23

different names uh some compile time is

426:25

also called early binding runtime

426:27

problem is also called late binding but

426:29

we have these two words compile them at

426:31

one time now what do you mean by compile

426:33

time see if you can Define your behavior

426:35

so the behavior is defined at compile

426:38

time that is compile to polymorphism if

426:41

the behavior will be defined at runtime

426:42

that is called a runtime polymorphism so

426:46

when you compile the file and when you

426:47

know what thing will be executed at the

426:50

compile time itself it is compile time

426:52

now which method will be executed will

426:54

be decided at runtime that's run time

426:56

now we have done two examples here which

426:59

is overloading and over writing so if we

427:03

talk about overloading it's a part of

427:05

compile time because at the compile time

427:07

itself example let's say if you have two

427:08

methods you have let's say two methods

427:10

which is ADD which takes two parameters

427:14

ain't

427:15

an INT

427:17

and then we have one more method here

427:19

which is ADD and when you say ain't

427:21

ain't in which takes three parameters

427:23

now both this have the same name but

427:25

different different parameters that is

427:27

Method overloading now which method to

427:30

execute will it will you execute the

427:32

first or second method will be decided

427:33

at compile time now let's say you have

427:35

two classes Class A and Class B two out

427:37

of two different classes and Class B

427:40

extends Class A we have talked about

427:42

this in method overriding and let's say

427:44

you have a method which is ADD which

427:46

takes two parameters and also here we

427:49

also have the add method which takes two

427:51

parameters

427:52

now out of this too which one will will

427:54

get get executed it will be cited at

427:57

runtime so this that is called a runtime

427:58

polymorphism in fact there is one more

428:00

concept which we are going to discuss in

428:02

the implementation when we talk about

428:04

Dynamic method dispatch at that point

428:06

this thing will get more clear but just

428:08

to generalize what is polymorphism

428:09

polymorphism means many Behavior so your

428:12

behavior of your reference or your

428:14

execution will change based on some

428:17

situation if it is compile time which is

428:20

your method overloading where you decide

428:21

based on the parameters at compile time

428:24

itself in runtime you're not sure which

428:26

method will be called because both the

428:29

methods have the same name same

428:30

parameters now in this video let's talk

428:32

about Dynamic method dispatch now once

428:35

we have went through what is

428:36

polymorphism we have also mentioned that

428:38

it is same thing multiple behavior and

428:40

we have two types of uh polymorphism

428:43

which is compile time polymorphism

428:44

runtime polymorphism now to implement

428:47

runtime polymorphism let's try one more

428:49

example here and we do that with the

428:51

help of dynamic method dispatch again to

428:54

keep it simple we'll use a simple class

428:56

like Class A and then we got class B and

429:00

let's say Class B extends Class A okay

429:03

and let's say we have class C

429:05

and again we have discussed most of the

429:07

topic so let's make it quick so we got

429:10

class C so if you can see we have class

429:13

A which is getting extended by B and C

429:15

both so you can say class A is parent

429:18

and we have two Childers one is B and

429:20

one is C now what I will do is in this a

429:23

I will create public void show okay and

429:28

this show will print let's say in a show

429:31

okay and we'll be using class a bit

429:35

later as of now let's only focus on

429:37

Class A and Class B now my question to

429:40

everyone is I mean okay let's remove

429:42

this classy just to remove the confusion

429:44

let's go step by step so let's say we

429:46

have these two classes Class B X is

429:47

Class A now my question to you is how do

429:50

I create an object of Class B and of

429:52

course we have talked about this from a

429:54

long time I can say b obj equal to new B

429:57

and that's how you get the object right

429:58

and of course using this obj I can call

430:01

show there is no harm compile this code

430:04

and run you can see we got in a show now

430:07

this is how you create the object right

430:08

now there is one more thing

430:11

can I create a reference of a an object

430:14

of B is it possible since B in B extends

430:18

a so you can say B is a child of a so

430:21

can I refer this obj as a and the object

430:24

of object is B now when you look at this

430:27

obj what is this obj it's a variable

430:29

right and what is a type of a variable

430:31

the type is a here right so the obj type

430:35

is a but the implementation is B okay

430:39

that's the point remember and this

430:40

perfectly works now if you want to

430:42

imagine this let's let's go with this

430:44

one more example here which I will write

430:46

here let's say if I create a class

430:48

called computer and of course class

430:49

computer has some methods okay and then

430:52

class laptop and we all know that laptop

430:55

is a computer so I can also say laptop

430:58

extends computer yeah we can say that

431:00

right and now

431:02

if I say laptop is a computer if I want

431:05

to create object of a laptop of course I

431:07

will do it it's very simple you will say

431:09

laptop let's say lap is equal to new

431:12

laptop so let's instead of saying uh lab

431:15

we say obj1 and then I can just call all

431:18

the methods but my question is can I

431:20

refer this obj1 as laptop yes it is a

431:22

type of laptop I know I can also say

431:24

this is a computer and that's what you

431:26

do right when you look at the laptop you

431:27

can say hey that is a laptop but that's

431:29

also a computer we are referring that as

431:32

a computer so we are referring this obj

431:35

which is an object as a computer but

431:38

technically it's a laptop right in the

431:41

same way we can refer this obj of type A

431:44

so we can mention the type as parent but

431:48

the object can be of child okay so I've

431:50

just created this laptop computer just

431:52

to show you let me remove that

431:54

so yes it is possible to create a

431:56

reference of a superclass and the object

431:59

of a subclass okay and this will surely

432:02

work let's compile there is no issue

432:04

let's run and you can say it says

432:06

initial

432:07

but the twist is

432:09

if I have the same method and we have

432:12

talked about this in method over writing

432:14

if I have the same method and if I say

432:16

this is in B show

432:18

now what it will call Will it call in

432:21

Asia or in B show as per the method

432:23

overwriting it should call in B show but

432:26

look at reference reference is of type A

432:30

that's tricky right uh let's try in fact

432:33

what I will do now is just to first

432:35

let's run this code and I will show you

432:36

something you can see it says in B show

432:38

let me initially set the object of a

432:41

itself

432:42

and then let's go step by step so I will

432:44

say compile and run it says in a show in

432:47

fact I can call this obj show once more

432:49

but before calling object show what I

432:52

will do is I will make one change here

432:55

I can just come back here and you can

432:56

say obj is equal to new B that's right

432:58

we can assign a new object to the old

433:01

variable okay now if you want to

433:03

understand this with a memory of course

433:05

we have a stack here right in this tag

433:07

we have different variables and then we

433:09

have a heap memory here now initially

433:11

when you say obj that's your object

433:14

right and it will create object of a so

433:16

1 0 1 is object of a and talking about

433:18

line number 24. okay line number 24 we

433:22

have this line so that's an object here

433:24

and it will have some methods the method

433:26

name is show itself right and this will

433:29

have a value which is 1 0 1. so I'm

433:31

talking about line number 24 okay and 24

433:33

this is a link now when you say obj dot

433:36

show line by 25. so at that point it

433:39

will look okay so obj dot show that

433:41

means I have to call this show method of

433:43

object a so this my friend is an object

433:46

of a

433:47

and now on line number 28 we made some

433:50

changes that is now this particular

433:52

object is referring to the object of B

433:54

so we got one more object here which is

433:57

let's say 1 0 3 and this is an object of

434:00

B now okay this will also have a show

434:02

method because that is what we mentioned

434:04

there and now since we are changing the

434:08

value here this will be replaced by one

434:11

zero three there is no one zero one

434:12

anymore that means this link this link

434:14

is broken and we got a new link now on

434:17

line number 29 when you say obj dot show

434:19

it will call this show method of B so

434:23

it's not about what is a type of the

434:26

object or reference you can say type is

434:28

still a but the object is B and that's

434:31

what you call the B show let me compile

434:34

and run you can see at first for this

434:37

line it is printing in Asia for this 90s

434:40

printing in B show likewise if I create

434:43

another class here Class C which extends

434:46

a

434:47

and if I have the same method I will

434:48

just reuse my own code and if I said

434:51

this is inch C show what will happen if

434:55

I create if I just go back here and say

434:58

obj equal to new C and if I say obj dot

435:01

show what do you think what will happen

435:02

if I run the for the third time and

435:04

that's of course right different object

435:06

will have a different Behavior remember

435:08

runtime polymorphism and you can see we

435:10

got in C show so the same object which

435:13

is obj dot show is behaving differently

435:15

with different objects polymorphism

435:18

right so if you say we are not even sure

435:21

so when you compile this code at that

435:23

point we are not sure obj dot show will

435:25

call which method of of which class I

435:28

mean method of which class it will be

435:30

decided at runtime and that's what is

435:31

called runtime or polymorphism and all

435:34

this concept is called Dynamic method

435:37

dispatch because that's Dynamic which

435:40

method will it will call we are not sure

435:41

okay and that's how it works so Point

435:44

remember here is irrespective of what

435:47

type of object you have

435:48

or what type of variable you create is

435:51

it type it's a type A but it all depends

435:54

upon what object you have it is only

435:56

possible when you have inheritance okay

435:58

you can't simply say we are getting a

435:59

new class class D and you will say Okay

436:04

class D and you can't simply go back

436:07

here and say obj equal to New D this

436:10

will not work because D is not extending

436:13

a if you can see where if we can verify

436:15

D is not extending a so this will not

436:18

work this will only work when you have

436:20

inheritance so only you can have a child

436:23

objects for the parent variable which is

436:27

a obj parent type variable let's talk

436:30

about final keyword okay so the keyword

436:32

we are talking about now is final now

436:36

basically final keyword can be used with

436:38

a variable a final keyword can be used

436:40

with a method and a final keyword can be

436:43

used with a class and we'll go step by

436:45

step so let's start with the final with

436:48

variable

436:49

now when you make a variable let's say

436:51

if I create a variable called num and if

436:54

I say now the value for Num is 8. and of

436:56

course when you make a variable of

436:58

course you can change the valuator right

436:59

so initially the value was 8 and now you

437:01

are making it nine there's no problem

437:03

right and of course you can print the

437:05

value as well so if I compile this code

437:07

and execute the decent value is 9. now

437:11

there's a concept of making constants

437:13

right so what is constant so if you make

437:15

your variable constant you can't change

437:17

the value of it now the way you can do

437:20

that is by using a final keyword now in

437:23

different languages we use different

437:24

keywords I guess in C plus plus we use

437:27

const in Java we say final so the moment

437:30

you make your variable as final variable

437:32

the value the variable becomes constant

437:35

so once once you assign the value you

437:38

can't change it okay that's important

437:40

and that's why you can see we got an

437:41

error here in fact you can also get this

437:43

error by compiling the code you can see

437:45

it says cannot assign a value to a final

437:48

variable so that's final Yeah final

437:51

variable num okay so because num is

437:54

final you can't change the value and

437:56

this is actually very important so

437:58

whenever you have a value which will

437:59

which you think will not change try to

438:01

make it final it's a good practice to

438:03

make final variables is because you will

438:05

not get any unexpected output because if

438:08

you think the value should be constant

438:10

then y to even change it example if you

438:12

want to make a variable for pi we know

438:13

the value of pi right which which will

438:15

not change

438:16

so it's good to make that particular

438:18

variable as final so it's always a good

438:20

idea to make a final variable because

438:22

that's a stand that's what a good

438:24

practice is

438:25

so we have talked about what is final

438:27

variable so final variables simply means

438:28

making constants now let's talk about

438:30

final method in fact even before final

438:33

method let's talk about final class then

438:35

final method will make much more sense

438:36

so at this point I will just remove this

438:38

part so now we know what this final

438:39

variable is right so what I will do here

438:42

is I have a class here called let's say

438:45

calculator okay now I'm making this

438:48

class and in this or maybe let's keep it

438:50

simple let's say a so I've got this

438:52

class a right and now in this class I

438:55

have some methods let's say the method

438:56

name is public void show and you know

439:00

what let's make it calculated just to

439:02

make just to make it much more sense and

439:04

I will also say public void add this

439:08

time I will not return the value I will

439:09

just print the output here itself I will

439:10

say a

439:12

into B

439:14

and then I will just simply print a plus

439:17

b I'm just adding it here itself

439:19

right and then we got the method which

439:21

is show and here we are saying public

439:23

here we are saying in

439:26

calc show Okay so let's say we have this

439:30

class here which is calc and then we

439:32

have these two methods and if you want

439:33

to make it work of course you have to

439:35

get object of calc you will say calc

439:37

obj equal to new calc and with this

439:40

object you can basically call both the

439:42

methods you can say show you can say add

439:45

and you can pass two values 4 comma

439:47

let's say 5 right now with this let me

439:50

just compile this code and run compile

439:52

no problem and run you can say there's

439:55

no issue it is printing in calc show and

439:57

9.

439:58

now what if someone else is trying to

440:01

create a class called advanced

440:03

calculator

440:04

and maybe you maybe that they're trying

440:07

to extend they're trying to inherit the

440:10

features from your class which is calc

440:12

okay and by the way is possible anyone

440:14

can inherit your method or your classes

440:17

right because it's available

440:19

uh what I will do is I want to stop this

440:22

I want to say Hey you know no one should

440:24

be able to extend my class you want to

440:27

stop the inheritance how will you do

440:28

that so if I make this class as final

440:31

you can see it will give you an error it

440:34

says the type advanced calculator cannot

440:36

be a subclass of the final class that's

440:39

right so once you make your class Final

440:41

you are basically stopping the

440:43

inheritance okay so basically when we

440:46

talk about final variable it means you

440:48

cannot change the value of it when you

440:50

make a class Final it means no one can

440:53

extend it you're stopping the

440:55

inheritance okay that makes sense now

440:57

what if your your class is not final so

440:59

of course anyone can use it and maybe

441:01

you don't have an issue of people using

441:03

your class for your inheritance

441:06

the problem which we have is

441:08

someone will create object okay of not

441:11

your class but advanced calculator and

441:13

they will use your features and you are

441:16

okay with it because ultimately you're

441:18

printing your own class name which is in

441:20

character or maybe I can say bye Naveen

441:25

okay so maybe this this Calcutta is by

441:27

Naveen so if I compile this code and run

441:29

it will print my name at least I'm

441:30

getting the credit

441:31

but what if someone says hey you know I

441:34

will use your feature of Edition because

441:36

there is no great mention

441:38

but I will have my own create here so I

441:41

will create my own show method method

441:43

overwriting remember and I can simply

441:46

print by John I'm not sure who John is

441:49

but let's say if that person says bye

441:52

John now what will happen is when you

441:54

execute the show method and add

441:56

you can see add is my implementation but

442:00

they are just overriding show method and

442:03

it says bye John I don't want this I

442:06

don't want anyone to override my method

442:10

how do I do that

442:11

how do I stop over method overriding in

442:14

that case you can make your method as

442:16

final now when you make your method

442:18

final which we talked about here when

442:20

you make your method final no one can

442:23

override it you can see we got an error

442:24

cannot override the final method okay so

442:29

that's how we can use final keyword we

442:31

can use final keyword with variable to

442:33

make it constant you can use final

442:35

keyword with the class to stop in

442:37

heightens and you can you can use final

442:39

keyword with method to stop method

442:41

overriding so I hope I don't make sense

442:42

now in this video let's talk about

442:44

object class remember when we talked

442:47

about inheritance I also mentioned that

442:48

every class in Java extends the object

442:51

class so in this video let's try to

442:53

understand what is that so what I will

442:56

do here is first of all I will create a

442:58

simple class let's say class A so every

443:00

class in Java extends the object class

443:02

right so even if you don't mention it is

443:04

there but what is there inside this

443:06

class so what I will do is first of all

443:07

I will say let me create object of a

443:10

here I will say a

443:12

and uh okay so we got the object but

443:15

then using this object you can you can

443:16

see we can call so many methods we have

443:18

not defined any method in this class but

443:21

still we have the axis of equals method

443:22

then we got notify hash code uh notify

443:25

all two string weight

443:28

so many methods right now what they do

443:31

so let's understand that so what I will

443:33

do is in fact

443:34

this I will say laptop class just for

443:37

the example and here also let me get

443:39

object of laptop here so I will say

443:41

laptop obj equal to new laptop okay now

443:47

I will try to create some variables here

443:49

so I will say private int let's say

443:52

price and let me also get a string

443:55

variable here which is private string

443:57

brand okay so we got this two laptops

444:01

I can say model as well so we got model

444:03

and price right so if you talk about

444:05

MacBook or Dell XPS or Lenovo laptop so

444:08

it doesn't matter which laptop it is we

444:10

have a model and we have a price here

444:11

now if you talk about this particular

444:13

laptop here let me set the values to it

444:15

and timely you know what I will do I

444:17

will just remove this private so that I

444:19

can access it from outside here and I

444:22

can say oh uh okay so I will say obj dot

444:25

model I will set the model as let's say

444:27

Lenovo I don't know is it a laptop yoga

444:30

maybe and then I will say obj dot price

444:33

is equal to let's say thousand dollars

444:36

okay so I got a laptop I am I got the

444:39

values right what if I try to print the

444:42

object what happens behind this scene

444:43

when I try to print the object of course

444:45

we tried this before and it was giving

444:47

some weird output let's understand what

444:50

that output is now if you try to

444:52

concentrate on this output of course

444:54

this is not the values which we set but

444:56

it it prints laptop and it actually

444:58

makes sense because obj is a laptop

445:00

object and then we got the at the rate

445:03

symbol and then we got some weird

445:05

numbers I'm assuming they are heximal

445:08

values because you can see we got a d f

445:12

they are part of X decimal numbers okay

445:14

so these are the accessible format but

445:16

what is it let's try to understand that

445:18

now what if I say every time you try to

445:21

print end the object it by defaults

445:23

behind the scene it will call the two

445:26

string method let me repeat every time

445:29

you try to print the object it will call

445:31

to string method even if you don't

445:33

mention yeah and okay by doing those

445:36

changes let me just run this code once

445:38

again compile and run you can see this

445:41

it prints the same thing that means yes

445:42

it is getting called but what two string

445:45

is doing so if I go to two string now

445:47

first of all two string is not my method

445:50

if you can see in laptop plus we don't

445:51

have any method called tostring but in

445:54

the object class that is you can see we

445:57

got a two string method which returns a

445:59

string right that's what we are printing

446:01

right it returns something

446:02

what it returns it Returns the class

446:05

name if you can see it says get class

446:07

get class method will give you the class

446:09

name

446:10

then it calls get name so class name

446:13

okay that is laptop okay it's working

446:15

and then we got address symbol in

446:18

between okay that also makes sense why

446:20

we got it there and then it prints

446:23

something in hexa string that's why we

446:25

got hex decimal values here but what

446:27

exactly it represents it converts the

446:29

number into hex decimal string and that

446:32

number is actually hash code now what is

446:35

Hash code if you go to Hash code again

446:37

it belongs to the same class and behind

446:39

the scene implementation of a hash code

446:41

works with the it basically generates

446:43

the hash based on the values you have

446:45

okay now hash is a very simple Concept

446:48

in terms of cryptography and anywhere it

446:51

tries to create a single string of all

446:55

the data which you have so let's say if

446:56

you have a object which has five

446:59

variables it will apply a hash algorithm

447:01

and it will generate a string a number

447:04

string which will be of fixed size okay

447:08

to know more about how passcode you can

447:11

just go to YouTube and search for what

447:13

is Hash or go to Google and search for

447:14

what is Hash but at this point we are

447:17

returning a hash code okay that's what

447:19

we got here okay now the thing is we are

447:23

trying to print this right so body fight

447:24

is calling two string and that's why we

447:25

got this output but what if

447:28

when I try to print the object it will

447:29

print something which I want

447:31

and one thing we know that if you don't

447:34

have a method in your class it will call

447:35

the method of a superclass but what if I

447:38

create that method here itself so the

447:41

public method which returns a string the

447:43

method name is to string if I implement

447:46

this method and if I return something if

447:48

I return say hey that's it and if I

447:53

clear this and compile and run oh it

447:57

says hey you know it's not printing the

448:00

laptop class name and accessible values

448:02

it's just trying to call tostring which

448:06

is this it is written hey it's not going

448:08

for the superclass here

448:10

that is awesome now let me also try by

448:14

deleting this thing okay let's not call

448:16

to string by ourselves let's see if it

448:18

is getting called automatically it is

448:19

happening right now whatever you want to

448:21

return you can do that here right what I

448:23

want to return is I want to return the

448:24

model information with a

448:27

colon between and the price so that it

448:30

will print that particular data compile

448:32

and run you can say it says Lenovo Yoga

448:34

thousand dollars okay so that's how we

448:37

can basically work with the object class

448:39

methods if you don't mention it will be

448:40

coming automatically by the object class

448:43

now there is one more thing let's say if

448:45

I create one more object of laptop and

448:47

this time let's say the values are same

448:49

let's say this is object one object two

448:51

this is the object one one okay don't

448:56

give me suggestions yeah this is two and

448:58

two now if you compare these two objects

449:01

are the same yes the values are same but

449:04

do we have the objects which are same

449:06

let's try how will you compare this so

449:08

what I can do is I can just create a

449:11

Boolean variable here

449:12

and I can say result

449:15

is equal to how do I compile object 1

449:18

dot obija 2 or obision 1 is equal to

449:20

equal to Omega 2.

449:22

let's try I mean we can use double equal

449:24

to between obj1 and object 2 as well

449:26

let's try to print the result and let's

449:28

see what happens if I compile this code

449:30

and run

449:32

oh it says false okay maybe using double

449:34

equal to is not a good idea let's use

449:36

some inbuilt methods if you can see obj1

449:39

actually has a method called Equals it

449:42

checks for the equals okay that's great

449:45

it checks for the equals of obj1 and

449:47

obj2 okay let me try this I hope this

449:51

will work compile and run oh it is still

449:54

giving you false

449:56

why the objects are same right the thing

449:59

is if you go with the equals method here

450:02

you can see in our class we don't have

450:04

equals and the equals is actually coming

450:06

from the object class it basically

450:07

compares the two object by their hexa

450:10

decimal number we don't do that so we

450:12

want to compare them based on their

450:14

values so one thing we can do is we can

450:16

implement the equals method by ourselves

450:19

so we want the equals method which will

450:21

return a Boolean value the method name

450:24

is equals and it will accept only one

450:26

object I will say this as an object of

450:29

the same type laptop because we are

450:31

comparing laptop with laptop I will say

450:32

this is other and then the object which

450:35

is calling this equals is this object

450:37

and we have passed one more object here

450:39

which is other or maybe I can say this

450:40

is this and that so this is that object

450:42

this object so we are comparing two

450:44

objects

450:45

now how do I compare I can check if this

450:49

dot model dot equals so when you talk

450:52

about this model it's a string right now

450:54

string has equals implemented properly

450:56

so we can use string equals to compare

450:58

the string and this dot we are comparing

451:01

that dot model right we are comparing

451:03

two strings here and we have to also

451:05

compare the price this dot price is

451:08

equal to equal to that dot price now why

451:11

I'm using equal to for integer because

451:12

that's how it works right

451:14

and we have to also say open and close

451:17

if this matches then I will return true

451:23

as I will return

451:26

false

451:28

simple right and since we have only one

451:30

statement I can actually do this

451:33

right

451:34

in one line you can see I'm just

451:36

comparing the values here I'm combining

451:38

these two strings which is model model

451:40

and price and price

451:43

it will not use the equals of object

451:45

class it will use the equals of my own

451:46

class let's see if that works compile

451:49

and run oh it works can you see that we

451:51

got true now if I change the value let's

451:53

say it becomes yoga one

451:56

and if I try to compile and

451:59

then you can see it says false because

452:00

we have a different value

452:02

cool

452:03

okay in fact you know I can actually

452:04

simplify this more why to even mention

452:06

if else anyway this is returning true

452:08

and false right

452:10

what I can actually do is the entire

452:12

stuff from here

452:13

you can cut this part and say return the

452:17

entire string

452:18

because the string itself will compare

452:20

right it will return true or false

452:23

I can do that in one line

452:26

yeah this one way but there are some

452:28

rules in Java if you are trying to do

452:30

equals

452:31

when you when you say two objects are

452:33

equal of course they should have a same

452:35

value and they should also have a same

452:37

hash code so what I mean by that is

452:39

don't Define the equals and equals

452:42

method by yourself you can use your ID

452:44

and you can say

452:46

Source action and you can see it gives

452:49

you an option of generate hash code n

452:50

equals click on that

452:52

select all the variables which you want

452:54

to compare and click on OK it will

452:56

generate the equals method for you and

452:59

you can see it does multiple checks we

453:01

have only checks for the value but what

453:03

if you want to check if the object is

453:06

not null maybe you want to check for the

453:08

class name as well maybe you want to

453:10

check uh if the model is not null before

453:12

comparing so it does multiple checks for

453:15

you it's a it's a good practice

453:17

and that's why I say sometimes you have

453:19

to prefer your own IDE output and it

453:22

also implements hash code you know this

453:23

is how the hashmer is generated

453:24

basically uh it takes your data and

453:27

works and applies some algorithm on your

453:29

data to generate the result if this

453:32

algorithm can change you can Implement

453:34

your own algorithm but of course it

453:36

should have all the variables which

453:37

should come comes into play example

453:39

let's say I have one more string here

453:41

which is serial number

453:43

and I'm okay if two objects have the

453:46

same model number and same model same

453:48

price by different cell number that's

453:49

okay with me so maybe you can just skip

453:51

this serial in your equals and hash code

453:53

okay that's the important thing the

453:56

thing you have to remember here is the

453:58

object class has multiple methods and we

454:00

have worked with two string which is

454:02

here in fact you know we don't even have

454:04

to mention two string here we can right

454:06

click and we say

454:07

Source action generate two string with

454:10

both the variables okay it will generate

454:13

a two string for you which is much which

454:15

gives you a much better output than what

454:18

you can type

454:19

now I know with by asking the code from

454:22

your IDE you get these two extra

454:23

annotations here in fact three

454:25

this is actually we'll discuss that once

454:28

we talk about annotations at this point

454:29

is not needed it's not compulsion but it

454:32

has some importance which we'll discuss

454:34

later yeah that's how we can use object

454:36

class we can work with

454:37

hash code equals tostring and yeah in

454:41

this video we'll talk about what is

454:43

downcasting and upcasting now before

454:46

this we have to understand what is

454:47

typecasting so let's say we have a

454:50

double value so let's say double D is

454:52

equal to let's say 4.5 and then of

454:56

course I can print this value I can say

454:57

d and we all know what will be the

454:59

output it will print 4.5 but what if I

455:03

want to save this value somewhere okay

455:05

so example if I say int is I equal to D

455:08

can we do that can we have the double

455:10

value inside integer of course it will

455:12

not support right because 4.5 cannot be

455:15

stored in integer so yes we can at least

455:17

store 4 here right so maybe we can

455:20

remove 5.5 we can at least store four

455:22

but how will we do that by default Java

455:25

will not allow you that because Java

455:26

says I type mismatch cannot convert from

455:29

double to int it's because it says you

455:31

know uh you might lose data and we can

455:34

say hey that's okay for me I am okay to

455:37

lose data so what you can do is you can

455:39

Typecast it so you can say hey I don't

455:41

get what value you send just send that

455:43

in the integer format I'm okay with it

455:45

okay so this is typecasting you are

455:48

converting the type of a value right now

455:50

by doing this if I try to print I let's

455:52

see what happens of course we are

455:55

showing that integer we will lose data

455:57

so we are losing 0.5 but that's okay we

455:59

at least we got four now this is called

456:01

typecasting

456:03

now coming back to the world of oops can

456:06

we do this in oops so let's try and we

456:09

have talked about inheritance we have

456:10

talked about uh how can we have methods

456:13

into two different classes how can we

456:14

override those stuff now just to keep it

456:17

simple what I will do is I will create a

456:20

class here and we'll name this class as

456:22

let's say a again I'm just trying to

456:23

keep things as simple as possible we

456:25

could have gone with other other names

456:27

as well like calculate or something just

456:29

to keep it simple let's say we have

456:31

these two classes Class A and Class B

456:33

and Class B extends a so we are

456:36

basically trying to achieve in heightens

456:38

here

456:39

and now with this what we can do is we

456:41

can create object of a of course we can

456:43

do that right we can create object of a

456:45

and that perfectly works now in this two

456:48

classes I want to have some methods

456:51

let's say in a I have a method which is

456:53

show or maybe I will say show one and in

456:56

B I can have a method which is public

456:59

void Show 2 right and then I will just

457:02

print something here

457:03

so I can print in a show

457:07

and now you know what I would say here

457:09

see we can write any complex logic to it

457:11

right but the idea here is to understand

457:12

the concepts that's what you're focusing

457:14

so in B show now what I want to do is

457:17

using this a object so if you can see we

457:19

are creating this obj of type A and also

457:22

the object is also of a right and now

457:26

with the help of this obj and you can

457:28

see I'm not trying to achieve method

457:31

over writing here there are two

457:32

different methods so one show two two

457:33

different methods and I can call show

457:35

one and you can see I can only call so

457:37

when I can't call show two what's the

457:39

reason I can't call show two it's it's

457:41

very simple we don't have Shoto inside a

457:43

and a don't even know B exists okay

457:47

that's a fact B knows what a is but a

457:50

has no idea what B is I hope that is not

457:53

the scene in real world your parents

457:55

knows you so let's let's try this

457:59

compile and run you can see it says in a

458:02

show but now my question to you is

458:05

can I have the reference of a an object

458:08

of B and then we have seen that it is

458:10

possible I can actually create this I

458:12

can have a reference of a and object of

458:14

B we have done that in Dynamic Methodist

458:17

patch we can basically do this right now

458:20

what we are trying to do here is we have

458:22

an object of class subclass and then we

458:25

are trying to assign that to a pattern

458:26

class now we can actually say hey you

458:29

know what you can do please Typecast it

458:31

so whatever object you are sending is of

458:32

child class but Typecast it with a which

458:35

is a parent class

458:37

the same thing we have done for the

458:38

integer interval right now when you are

458:40

doing this we are basically going up

458:42

right which is if it if you try to

458:45

compare this A and B A is a super Class

458:48

B is a plus so we are trying to say

458:50

refer I mean objectives of B but refer

458:53

to a so we are going up which is up

458:55

casting so this concept here is called

458:58

up casting and of course this will work

459:01

there will there is no issue you can

459:03

still call in a show because in b as

459:07

well we got the same method even if you

459:08

have the object of B it will still work

459:10

okay so this is called upcasting so even

459:13

this is not compulsory it is something

459:16

which is which happens implicitly behind

459:19

the scene so you don't have you don't

459:20

have to mention the casting part it

459:22

normally works so this is upcasting now

459:25

what is downcasting

459:27

so let's say I'm creating a reference of

459:30

B now why we have to go for the

459:32

downcasting first of all see using this

459:34

even if the object is of B using this

459:36

obj I can't call show 2. if you can see

459:39

the object is of B and the method show

459:42

to belongs to B but still you can't call

459:44

it reason is because the reference is of

459:47

a and a has no idea what is this show

459:50

too okay so to solve that problem what

459:52

we can do is we can create a new

459:54

reference let's say obj1 but then I want

459:56

to use the same object I don't want to

459:58

get a new object of course you can do

459:59

this but you're getting a new object can

460:01

we do this can we say obj1 is equal to

460:05

obj now the problem is even obj is

460:08

referring to a new B object but the

460:11

problem is it is referred to a it's the

460:13

type of obj is a so it's the same

460:15

problem of double and int right similar

460:18

so what we can do is we can do a

460:20

typecasting here we can say hey I know

460:22

this is reference of a but the object is

460:26

of B right so whenever you use assign

460:27

fine I want the type to be of B and

460:31

that's how it works this is called down

460:32

casting so this obj is the parent object

460:36

or parent reference you are downcasting

460:38

it to the child reference Now using this

460:41

new obj1 I can call show two okay so

460:44

this is

460:45

downcasting this is downcasting this is

460:48

up casting in general they are called

460:51

typecasting okay

460:53

so you don't have to actually remember

460:55

this terms every time up casting down

460:57

casting at after some time you actually

460:59

you get used to this concept you will

461:01

implicitly use those things anywhere you

461:04

want

461:04

but yes while teaching we have to use

461:06

some words so upcasting downcasting so

461:09

yeah that's about this concept but

461:10

remember this we can create a parent

461:12

reference and child object and we can

461:15

also come back to the child reference

461:17

here in this video let's talk about

461:19

abstract keyword now what is abstract so

461:22

before that let's understand something

461:23

so we know what is class what is object

461:26

what is in items and how do we do

461:28

overriding of the methods right now with

461:30

those knowledge let's talk about

461:31

abstract keyword so for that what I will

461:34

do is I will create a class here and let

461:36

me name this class as let's say a car

461:38

okay now in this card you will Define

461:40

some methods let's say if you talk about

461:42

car a car can drive

461:45

of course right a car can play music as

461:49

well public void I mean that's what we

461:50

do right we play also we also play music

461:52

in the card you know some people they

461:54

don't start their car before playing the

461:56

music

461:57

or they don't start they don't uh set it

462:00

on a drive mode before uh before they

462:02

play the music okay so we have these two

462:03

methods let's say maybe we can add some

462:05

more methods in the car maybe you can

462:07

sleep in your car and other methods but

462:09

let's say we have this too we have drive

462:11

and we have play music and of course in

462:14

this method I can Define some statements

462:15

let's say when I say it is playing music

462:17

I can say play music or we can write

462:20

multiple statement logic is not

462:21

important the important thing is a

462:23

concept let's focus on that so let's say

462:25

in the play music we have this statement

462:26

and in Drive let's say at this point I'm

462:29

not sure I want to build my own car but

462:31

I'm not sure how can I drive a car I

462:33

know how to implement play music but I

462:35

don't know how to set up the engine I'm

462:37

manufacturing my own car so I don't know

462:39

how to set up the engine how to set up

462:40

the wheels but a car should drive that's

462:43

what I know and that's why it's we call

462:45

it right if you don't have a drive

462:46

feature then why you will call it a card

462:49

you can State it's a home theater anyway

462:51

now if I want to use this car I can

462:54

simply create an object I can say obj

462:58

and then I can call the methods I can

463:00

call both the methods opj dot drive and

463:04

obj Dot play music of course we have two

463:07

methods I should be able to call them

463:09

now if You observe in the drive we are

463:11

not doing anything why we are not doing

463:13

anything because I don't know how to

463:14

implement drive but I know how to

463:16

implement play music so I'm able to

463:18

create this object and I'm able to call

463:20

these two methods and I I think it

463:22

should work and if I go back and run it

463:25

works it's not like if you don't Define

463:26

something it will not work it's working

463:28

the only thing is when you're defining

463:30

this method you are actually not

463:33

implementing anything here you're not

463:35

writing any statements so it's valid in

463:37

Java syntax term but as a feature I mean

463:40

just imagine you're buying a new phone

463:41

and your phone says it can do this

463:43

feature this Feature Feature one feature

463:45

two feature three but then none of the

463:46

feature works of course you will not buy

463:48

any phone like that right in the same

463:50

way if you talk about this card you are

463:52

writing a method over here which is

463:54

drive when you're not implementing it

463:55

why someone should be able able to

463:57

create object of this why in first place

463:59

someone should create a class itself

464:01

right when you don't know how to

464:03

implement it see the thing is when you

464:05

build an application you have a

464:07

structure which you follow you design

464:08

your application not I'm not talking

464:10

about the thing you which you see on

464:12

this screen I'm talking about when you

464:13

build an application you design on paper

464:15

what are the classes you need what are

464:16

the methods you need now at this point

464:19

you know how to play music but you don't

464:21

know how to drive now in that case

464:23

should I remove this method completely

464:25

if I do that then how will you call it

464:28

as a car so what you can do is you can

464:30

say Hey you know I am creating this

464:32

method drive but I'm not sure what

464:35

should I implement it here so instead of

464:37

defining it here so when you open the

464:39

curly packets and close you are

464:41

basically defining a method so instead

464:43

of defining a method you can basically

464:45

declare a method which means you can put

464:47

a semicolon at the end when you do that

464:49

you are declaring a method that also

464:52

means that the drive feature you had

464:54

declaring but it is not working and no

464:56

one should be able to create object of

464:58

it okay again we'll see that in some

465:00

time but when I declare it it is giving

465:01

you an error now first of all why you

465:04

will declare when you are not defining

465:05

anything maybe I have this mindset

465:07

someone else can just come back and they

465:10

can say they can Implement a car maybe

465:12

they will build a a wagon let's say and

465:16

a wagon will extend my car and it is the

465:20

wagon's responsibility in fact you know

465:22

instead of saying wagon let's say wagonr

465:23

it's a very famous car if you search

465:25

online uh so we have this wagonal car

465:28

which some of them don't like not not a

465:31

big fan of it but

465:33

okay so let's say we have this card

465:35

wagonal extends card in fact I have a

465:37

card which is smaller than wagonr which

465:40

I love so we have this class wagonr

465:42

extends car here and what I want here is

465:45

I want this wagonard to implement the

465:48

drive feature see if you think card is

465:51

just an abstract concept right no one

465:52

actually buys a card what you buy is a

465:54

real car like wagonard or BMW GLS or the

465:59

Toyota Fortuner or Ford ecosport right

466:02

so you buy a real car you don't buy a

466:04

card this is just a concept so when I

466:06

say drive here a car don't know how to

466:08

drive but then when you when you create

466:10

a real car that car will know how to

466:12

drive so I want this wagonr to be

466:16

responsible to Define this method okay

466:17

so I can just copy this and paste it

466:19

here and here instead of declaring it I

466:22

will Define so what I want is I want

466:24

this wagonr to implement it of course

466:27

the other feature like play music can

466:28

simply be extended but not drive drive

466:31

should be implemented here and wagonr I

466:34

mean the company who built wagonal cars

466:37

it's their responsibility to define the

466:40

method here I can say driving of course

466:42

they will write a complex logic but just

466:44

to make it simple I'm writing one

466:45

statement so they are responsible to

466:48

Define this method and we are not doing

466:49

it here now if you want to declare a

466:52

method you can see we are getting an

466:53

error here you can use an abstract

466:56

keyword So when you say abstract it

466:58

means I'm just giving you the abstract

467:00

idea of a car should have a drive

467:02

feature but I don't know how to

467:04

implement it now when you are using my

467:07

feature and when you call your vehicle

467:09

as a car it should have a dry feature or

467:13

whatever object you're creating is a

467:14

card you need to have a drive feature

467:16

and that's how you make it so you make a

467:18

public abstract void drive so you have

467:21

to use an abstract keyboard if you want

467:23

to declare a method okay here we are

467:25

defining it but if you can see even if I

467:28

do abstract it is giving you an error it

467:30

says the abstract method Drive in type

467:33

car can only be defined by an abstract

467:35

class okay what it simply means is if

467:38

you have an abstract method abstract

467:39

method can belong to only abstract class

467:42

so that means you have to make this

467:43

class as abstract as well so you can

467:46

have abstract methods only in the

467:48

abstract class and whichever class is

467:51

extending the abstract class will have

467:53

this drive method if we don't do that it

467:55

will give you either example if I change

467:57

the method name you can see it will give

467:59

you error it says the typewagonr must

468:02

implement the United abstract method car

468:04

dot drive so if you are extending an

468:07

abstract class you need to Define it

468:09

compulsory otherwise it will not work so

468:11

you have to Define you have to basically

468:13

override the method now when someone

468:15

creates an object of a car in fact first

468:17

of all you can't create object of

468:19

abstract class Point very important

468:21

point and that's why you can see we got

468:22

an error here it says cannot instantiate

468:25

the type car it simply means you cannot

468:28

create object of car so the point

468:30

remember you cannot create object of an

468:32

abstract class okay but then connect it

468:35

to reference of abstract class yes that

468:36

is possible you can say car obj that is

468:40

absolutely correct but if you want to

468:42

create an object you have to get object

468:43

off not card but wagonr okay and then

468:47

with this object you can call drive you

468:49

can call uh play music and we have

468:51

talked about Dynamic method dispatch and

468:53

we know when you call drive it will be

468:55

calling Drive of webinar not of car

468:57

anybody is it is declared right uh so

469:01

basically that's how you can create an

469:02

abstract class that's how you can create

469:03

an abstract method and basically that's

469:05

how you can restrict the creation of the

469:08

object here now a few more points to

469:09

remember is it compulsory to have an

469:12

abstract method in the abstract class I

469:13

mean we know that it is compulsory to

469:15

have the abstract method you need

469:17

abstract class right but is it

469:19

compulsory to have a abstract method in

469:21

the abstract class let's do that if you

469:23

come in this section okay you can say

469:25

there's no problem it's not like a

469:27

abstract class should have abstract

469:29

method

469:30

abstract class can have abstract methods

469:32

it can have normal methods so you can

469:34

have an abstract class with which only

469:36

has abstract methods you can have

469:37

abstract class which has only

469:40

implemented methods which are this which

469:42

are non-abstract methods and you can

469:44

have a abstract class which can have

469:46

both normal methods as well as the

469:49

abstract method and you can create

469:50

multiple abstract method okay example if

469:52

I can come back here and say Hey you

469:55

know I want a car to fly I don't know

469:57

when this is possible but in the movies

470:00

you see that right so a car jumps from

470:02

one building to the building uh okay and

470:04

I hope you know the movie name but

470:06

anyway so if you talk about this

470:07

abstract method which is fly and you can

470:09

see I'm declaring it here now when you

470:11

declare a method and when you say you

470:13

have an abstract class which is getting

470:15

implemented or which is getting uh

470:17

extended by another class it becomes

470:19

compulsory for you to Define one more

470:21

method here so you can say public not

470:24

abstract you can say public void you

470:26

have to Define fly as well otherwise it

470:28

will not work so you can say a flying

470:32

okay and now with this you can call the

470:34

methods you can say obj dot fly and this

470:38

should work compile run and you can see

470:41

it says driving play music and flying

470:43

now there's one important thing here

470:45

which is what if when you are creating

470:48

this class wagonr and then you are not

470:50

able to implement fly what will happen

470:52

first of all it's compulsory to Define

470:54

all the methods okay all the abstract

470:57

methods so you can see in this class we

470:59

have three methods but only two methods

471:01

are abstract and that's why it says it

471:03

should Define fly as well as drive but

471:06

we are not implementing fly so you can

471:07

see it says the type again I must

471:09

implement the United abstract method

471:11

car.fly but what if you don't do that

471:15

so in that case if you have a class

471:17

which is not able to implement all the

471:19

methods in this case even this class

471:22

becomes abstract class so you have to

471:24

make it abstract class

471:25

and you can see there's no error but the

471:27

problem now is

471:29

you can't create object of abstract

471:31

class right so maybe you have to create

471:33

a new class here by saying uh updated

471:36

webinar which can also fly and this will

471:39

extend now which class you want me to

471:41

extend the car or the wagonr of course

471:44

wagonal right because you will get this

471:46

drive feature from a wagonr so you can

471:48

simply extend it with wagonr and then

471:51

here in this class you can basically

471:53

create object of fly in fact if you are

471:55

using IDE and if you don't if you are

471:57

lazy to type you can come to the seller

471:58

and you can say fix Quick Fix add

472:01

unimplemented methods and it will do it

472:03

for you okay and here we can say flying

472:07

so even you can do that the only thing

472:09

is now the object will not be of wagonal

472:11

the object will be of updated wagonr

472:15

is it the method class name okay it's up

472:18

it should be updated right so that's how

472:19

you can create a object so again we are

472:22

not getting object of abstract class we

472:23

are creating object of the class which

472:26

is not not abstract okay now since we

472:28

are calling this class as abstract class

472:30

even this class should have a name right

472:32

so normally we call them as a concrete

472:34

class so we can say this is a concrete

472:37

class

472:38

okay so we have abstract class and then

472:40

we have concrete class so you can create

472:41

object of a concrete class not an

472:44

abstract class so I hope this makes

472:46

sense uh that's about abstract keyword

472:49

so let me just reiterate abstract

472:50

keyword can be used with methods if you

472:53

want to declare them and if you have any

472:55

abstract methods you have to make a

472:57

class as abstract but again on the other

472:58

hand if you have abstract class there is

473:00

no compulsion that abstract class should

473:02

have abstract methods abstract class can

473:05

have normal methods also I mean all

473:07

these normal methods no abstract methods

473:09

even that is possible next Point

473:11

remember we cannot create object of an

473:13

abstract class if you want to create

473:15

object you have to get a class which

473:17

which implements all the methods a

473:19

subclass which implements all the

473:20

methods and then that class will be

473:22

called as a concrete class and you can

473:24

create object of it so that's about

473:25

abstract keyword in this video we'll

473:27

talk about inner class that's right a

473:30

class inside a class I know that sounds

473:32

weird at first but let's let me go with

473:34

that see most of the time when you build

473:36

an application you might have a scenario

473:39

where a class will be used only for that

473:42

particular purpose okay so let's say you

473:45

have a class A and then that class needs

473:47

variables so we can do that example we

473:49

have a class A here and this class A can

473:52

have variables this class can have

473:53

methods so when you have this class A

473:55

this class can have variables this class

473:57

can have methods right now this

473:59

variables and methods only work for this

474:01

class A so if you want to access them

474:03

you have to get a class first

474:06

in the same way let's say if you have a

474:08

class where that class is totally

474:10

dependent on Class A there is no other

474:12

need for it in that case you can create

474:15

a class inside the class

474:17

okay the way you do that is by defining

474:19

a class like a and then you can define a

474:21

class called B inside it now just the

474:24

way you have class A here I can create

474:26

variable that works we can say variable

474:29

age and then we can have method as well

474:32

which is public

474:35

white show now we have these two things

474:38

we have a variable which is age and then

474:39

we have a method which show and then we

474:41

have a class as well so imagine a class

474:43

having all three things a variable a

474:46

method and a class

474:48

and inside this class let's say I have

474:50

created I create a method which is

474:51

public void config and in this config I

474:54

will print something

474:55

let's say in config just to print

474:58

something okay and I'm trying to make

475:01

this example as simple as possible

475:03

that's why I'm using this class name in

475:04

real life don't do that make sure that

475:06

you use proper class names

475:08

and so let's say we have this scenario

475:10

now question is how will you call this

475:12

config so it's very easy to call show in

475:14

fact I should print something here just

475:15

to see what it prints

475:17

in show now question to you is how will

475:21

you call this show and then we have seen

475:23

this right you simply have to create

475:25

this object of a and then you can call

475:28

obj

475:30

dot show I mean show will perfectly work

475:33

right if I compile this code and run you

475:35

can see it says in Show Now I want to

475:38

call config how will you do that do that

475:40

of course you will say if it is a Class

475:42

B then you have to get object of B to

475:44

call this config let's try if I try to

475:47

create object of B by saying B obj one

475:50

new B you can see it will give you an

475:54

error it says

475:55

uh B cannot be resolved oh it's not able

475:59

to find B the thing is B is not openly

476:02

available right B is a part of a so if

476:04

you want to have a b here you need to

476:06

say a dot b so B belongs to a that's why

476:09

you have to say this

476:11

okay now why I'm doing this so when you

476:13

say if you compile this code and if I go

476:15

back here can you see that we got two

476:17

classes and we have seen this right

476:19

every time you compile a code you will

476:21

get those number of class files so

476:23

you've got a class file for demo that

476:24

makes sense you can see we have

476:26

demo.class uh you will get a DOT class

476:28

as well because uh a is a class but we

476:32

also got B is also classified it should

476:34

have its own class file and that's right

476:36

we got it we got B dot class the only

476:39

thing is B belongs to a so the dollar

476:42

symbol separates these two so it says b

476:45

class belongs to a class and that's why

476:46

it's a dollar B

476:48

okay that makes sense so if you want to

476:50

access B you have to say a dot b that's

476:53

how you access it and you can say obj1

476:55

you can say there's no problem in

476:57

declaring the variable the problem is

476:59

when you say new now how will you do it

477:02

if you say B it will not work again it

477:04

will say cannot able to resolve B in

477:07

that case if you say a dot b no that's

477:10

not the proper syntax the thing is if

477:12

you want to create object of B you need

477:15

object of a first

477:17

now you will say why it's very simple if

477:20

you want to call B if you want to call

477:22

this show now show is a method right

477:24

it's a non-static method which means if

477:28

you want to call show you need object in

477:31

the same way if you want to access this

477:32

variable age you need object in the same

477:35

way if you want to create object of B

477:37

you need object of a because that is a

477:41

non-static method okay so I'm focusing

477:43

but on static here right can I create a

477:46

static class uh yes we'll see that in

477:48

some time but we got this object Here

477:50

and Now using this obj1 I can call the

477:55

config method if you want to create

477:56

object of a inner class which is B in

477:59

this case you need to First specify

478:01

where it belongs to so B belongs to a

478:03

and then you need object of a outer

478:05

class which is obj here to create object

478:08

of inner class and once you have that

478:10

you can simply call this methods and

478:11

that will properly work you can see what

478:13

in show and in config okay now we have

478:17

all also focused on a study class right

478:19

you can also make this class as static

478:21

and we all know when you refer it to our

478:23

static class you don't need object so

478:25

basically you don't call this with the

478:27

help of object you can directly refer

478:29

with a dot b now this will work only

478:31

when the class is static the inner class

478:34

is static it will not work for

478:36

non-static class now can we really make

478:38

study class yes we can provide it there

478:40

it's an inner class if it is out of

478:43

class you can't make out of class static

478:45

you can see it will give you add it says

478:47

illegal modifier for the class A only

478:50

public abstract final are permitted so

478:53

basically you can't use static here

478:55

static can be used only for the inner

478:58

class okay I hope that makes sense so

479:01

that's the concept of uh in a class now

479:03

once we know about inner class let's

479:06

talk about Anonymous in a class I know

479:07

that sounds weird right anonymous

479:09

anonymous simply means something which

479:11

doesn't have a name okay so what I will

479:13

do is I will just try to remove

479:15

everything here and let's work with this

479:17

particular class okay and in fact I

479:20

needed a a class let's say class A is

479:22

there okay and inside this class A I'm

479:26

creating a method which is public void

479:28

show again just to keep it simple I'm

479:29

using simple names here and here I will

479:32

print in a show okay and if I want to

479:36

call a of course I will simply say A obj

479:38

you need to create object of a and then

479:41

I will say opj dot show let's see if

479:45

this works compile and run it works you

479:48

can see God in a show that perfectly

479:50

works now the question I have for you is

479:52

very simple question what if I want to

479:54

change the behave behavior of show

479:56

method okay that means I wanted to do

479:58

something else now I know in your mind

480:00

you have the idea right we can simply

480:01

create a class which is B and this class

480:05

B will extend a so basically basically

480:08

we are trying to do method over dividing

480:09

here in this B as well you will be

480:11

having the same method which is show

480:13

which will print in B show we just want

480:15

to do something else and by doing

480:16

something else what I mean by in this

480:18

code is I'm just trying to print

480:19

something else but of course you can do

480:21

whatever you want I just want to change

480:23

the behavior of this show method so we

480:26

created a new class called B and here

480:29

what we can do is we can create the

480:31

object of B but the references of a so

480:33

what we are doing is we are overriding

480:36

the behavior of show and then you will

480:39

say hey we have already talked about it

480:40

right we have talked about method over

480:41

writing hold on hold your horses let's

480:44

see if I compile this code and run it

480:46

says in Bay show but now if you think

480:49

about this class now first of all uh as

480:51

we mentioned every time you compile a

480:53

code it will create all the class files

480:55

see we got a B because of the previous

480:58

code let's delete that you can see we

481:00

got B dot class as well now you will say

481:02

what's a harm in that see the Harvest

481:05

the only purpose of this class B was to

481:09

extend or to extend a class to override

481:12

the method and maybe it will be used

481:14

only once now of course if it is using

481:16

multiple times in different places of

481:19

course it should create a new you should

481:20

create a different class but what if we

481:23

know that this class will be used only

481:25

once only for this particular use

481:27

in that scenario why to create a new

481:30

class oh then you will say if you don't

481:33

get new class how will you get a new

481:34

implementation okay let me show you

481:36

something what if I can simply come back

481:39

here just before the semicolon and I can

481:43

Define what should be the new

481:45

implementation

481:46

I know this looks tricky but just be

481:48

with me see till this point we have seen

481:50

that first you have to create a class

481:52

and then you create the object right and

481:55

that's what we are doing from a long

481:56

time but now this is the first time that

481:58

I'm saying I want to create object of a

482:00

but this is the implementation

482:03

this is the design for it don't go for

482:06

the original design I will give you new

482:08

design the new design is I will use the

482:10

same method name or same method here the

482:12

only change I want to do here is it will

482:14

print in new show

482:17

I will just provide some indentation so

482:19

that it will look better so what I'm

482:21

doing here is I'm providing a different

482:22

implementation for show method

482:25

okay and let's see if this works I just

482:28

want to see

482:29

compile and you can see that it says in

482:31

new show it is not going for the

482:33

original method

482:34

so we are providing a new implementation

482:37

here now this is called uh in a class

482:40

okay you will say hey how it's a class

482:42

we are not creating we are not using a

482:44

class keyword right that's why we are

482:46

not using that last keyword and we are

482:47

not even giving a name to it and that's

482:49

why it is a inner class because this

482:51

class is created inside the demo class

482:53

and also there's no name for the class

482:56

and that's why if you go back to your

482:58

files can you can you see this

483:01

we got demo inside that we have a class

483:04

but there's no name for it and since we

483:07

don't have a name for it it says one of

483:09

course if we create multiple classes you

483:10

will have a different number it will say

483:12

2 3 4 and list goes on

483:14

and since we don't have a name it is

483:16

called Anonymous class

483:18

so this is Anonymous in a class which we

483:21

are going to use a lot in the upcoming

483:22

videos uh once we talk about interfaces

483:24

must we talk about functional interface

483:26

Lambda Expressions this is very famous

483:28

concept so just get used to it now if I

483:32

if you want me to type this code one

483:33

second I will do that for you I will

483:35

what I will do is I will just go back

483:36

here

483:37

see it is not difficult it's just that

483:39

you are unfamiliar with this concept and

483:41

it will take some time

483:43

okay so what you do is just before the

483:45

semicolon and after the Constructor open

483:47

the curly packets and close this is your

483:49

class and in this class you provide the

483:52

method which is show and whatever you

483:54

want to do in show you can do that here

483:56

you can say I'm printing in new show and

483:58

then you can call this show so when you

483:59

call this show it will call the show of

484:01

the new implementation not this one okay

484:05

and yeah that's how it works so that is

484:08

your Anonymous inner class okay so we

484:12

have talked about abstract class we have

484:13

talked about inner class in fact we have

484:15

talked about Anonymous in a class can we

484:17

just use both let's try what I will do

484:20

is I will just make this class as an

484:23

abstract class because I want to create

484:25

this method show as an abstract method

484:28

okay let's say this is abstract

484:30

okay and then uh let's say I'm not doing

484:34

all those things

484:35

now I have a question for you if I want

484:37

to create a object or if I want to

484:40

instantiate a can I do that of course in

484:42

the abstract class we have seen we can't

484:44

do that because you can't instantiate

484:46

the abstract class and we have found a

484:50

solution as well you can create a

484:51

separate class by saying extends a and

484:54

then you can provide the implementation

484:56

here you can just go here quick fix

484:58

implement the method and one more thing

485:01

you might be saying that I'm reviewing

485:02

this line multiple times it's just that

485:04

we have not talked about what is at the

485:06

rate override will we have a separate

485:08

topic on that so at that point we'll

485:09

discuss that so here I just want to say

485:13

in the show okay and then of course I

485:16

can create the object of B and that's

485:19

done with the help of this object obj I

485:23

can call show again we have seen this in

485:25

abstract class concept

485:27

but I want to talk about something else

485:29

so you can say it says in B show

485:31

now if you look at the entire purpose of

485:34

this class B is to implement the show

485:37

method that's it and if you are using

485:40

this method only once then why to even

485:42

create this class can I use something

485:44

else can I use anonymous inner class

485:48

here okay so what I'm what I'm saying is

485:50

I can simply say new a

485:53

and okay it's not working but can I

485:55

provide the implementation here and I

485:58

can create a method which is public void

485:59

in fact I will just simply paste the

486:01

code because we already have that code

486:03

with me

486:04

and here I will say in new show so what

486:06

I'm doing is I'm providing the

486:08

implementation for the abstract class

486:10

here itself okay and you might be

486:14

thinking we are still getting new a

486:15

right is it the object of an abstract

486:18

class we can't do that right how we are

486:21

doing this the thing is this is a class

486:23

here right the anonymous in a class and

486:25

we are creating the object of not a we

486:28

are getting the object of this Anonymous

486:30

inner class

486:33

that's a point to remember

486:35

so most of the time when we have

486:36

interfaces or abstract class we use this

486:38

concept so if you want to implement uh

486:41

the interface or the abstract class only

486:42

once

486:44

then you can use anonymous inner class

486:46

and this perfectly works so let me just

486:48

try it out

486:49

compile this code and run you can see it

486:52

works so we can use anonymous in a class

486:54

for the abstract class as well

486:57

in fact if you have multiple methods you

486:59

can still do it if you let's say we have

487:01

one more abstract metal here which is

487:02

public void let's say config we can

487:05

create a method for config as well and

487:08

since we are using an IDE it give you

487:10

the method so you can simply use this

487:13

and you can print

487:15

in config so it's not just for one

487:17

method if you have multiple methods you

487:19

can provide the implementation in the

487:21

anonymous inner class

487:22

in this video we'll talk about

487:24

interfaces now before we start with the

487:27

interface let's talk about the abstract

487:29

class now let's say I create abstract

487:31

class here so if it's abstract class A

487:33

and in this class let me create some

487:36

methods now we all know in the abstract

487:39

class we can create our abstract methods

487:41

and normal methods as well but let's say

487:43

if I have abstract class which only has

487:45

let's say abstract methods if I say

487:48

public abstract void show and we have

487:52

public abstract void config now when you

487:56

have abstract class with two abstract

487:57

methods of course this works there is no

487:59

compulsion to have non-abstract methods

488:01

here or we call them as concrete methods

488:03

there is no need for them now whenever

488:05

you have a situation where you have

488:07

abstract class which only has abstract

488:09

methods we have another way of doing it

488:12

now see all these are decided at design

488:14

time so when you make an application at

488:16

designing you specify things okay what

488:19

are the classes I need and what are the

488:21

abstract class we need

488:23

so you create this hierarchy of system

488:25

of course we'll talk about hierarchy in

488:27

some time why do we need it but at this

488:29

point we create a hierarchy and when you

488:31

have a class which only has abstract

488:32

methods the other alternative for that

488:34

is to create an interface so instead of

488:36

creating an abstract class we can create

488:38

an interface

488:40

most short time we use this interface in

488:42

project development so let's understand

488:44

that so we have the interface a now you

488:46

will say what is changing there now

488:48

first of all interface is not a class

488:50

okay so that's why that's the main

488:52

difference between abstract class and

488:53

interface and by default every method in

488:57

the interface is public abstract so

488:59

these two keywords actually not required

489:01

because anywhere they are public

489:02

abstract so even if you don't mention

489:04

they are by default public abstract and

489:07

that's why if you can see when I put a

489:09

semicolon just after the method there is

489:11

no error because behind the scene public

489:13

abstract is there now this is your

489:16

interface so interface basically means

489:18

it's like a class just that all the

489:21

methods are by default public abstract

489:24

okay but then how will I use this how

489:27

will I instantiate this the same problem

489:29

was there with abstract class right we

489:31

cannot instantiate it now if you look at

489:33

this how do I instantiate let's try so I

489:35

will say a obj see there is no problem

489:38

in creating a reference of it so that

489:40

also also means we are creating a type

489:42

the way you create classes so let's say

489:44

if a is a class you can say a obj so a

489:47

is a type right in the same way here the

489:50

interface a is a type

489:52

okay this works but now I'm going to try

489:54

to create object for this

489:57

it will give you an error it says if you

489:59

see the error it says cannot instantiate

490:02

the type A because this interface you

490:04

can't instantiate it okay the what's the

490:07

solution

490:08

the solution is see first of all why do

490:10

we get interface we create interface so

490:12

that we can design something we can say

490:13

example let's say if you ask me about

490:16

how do we create a table I can say or

490:19

maybe if you ask me how to reach

490:21

somewhere so I can say go straight take

490:23

a right go straight take a right take an

490:24

auto uh take a bus get down at the next

490:27

stop so what I'm doing is I'm giving you

490:29

the path right I'm giving a design you

490:32

have to literally take a walk I will not

490:35

work for you I will just show the

490:36

directions in the same way interface

490:38

says I will tell you what are the

490:41

methods you need but I will not

490:43

Implement them it is your job to

490:45

implement so when I say yard now I'm

490:46

talking about classes see ultimately you

490:49

create object of a class interface just

490:52

show you the design

490:54

so here what I can do is I can create a

490:56

class let's say Class B so we have a

490:58

class B here and then in this class I

491:00

want to implement these two methods now

491:03

in terms of abstract class we say

491:05

extends right because class extends

491:08

another class here a is the interface we

491:11

have to use a different keyword here the

491:13

keyword which we use is implements so

491:15

whenever you want to implement the

491:17

interface you say implements keyword and

491:20

when you say implements it becomes

491:21

compulsory for you to Define both the

491:24

methods okay if you fail to Define both

491:27

the methods by default your class also

491:30

becomes abstract okay but we don't want

491:32

to get abstract class right we want to

491:33

create a concrete class we want to

491:35

create object of B in that case it's

491:37

your job to Define both the methods okay

491:41

there is one way you can use a shortcut

491:43

here you can just go back here and say

491:44

quick fix it will ask you to implement

491:46

the methods or you can do the hard work

491:49

you can say public void show and then

491:52

you can define something here I can say

491:53

I'm defining in show and then we can

491:57

Define another method as well which is

491:58

config or public

492:00

void config and here we can Define in

492:04

config so basically what you're doing is

492:06

you are implementing both the methods as

492:10

I mentioned before if you fail to

492:11

implement these methods or this class

492:13

becomes an abstract class but we are

492:15

implementing it right now when you

492:16

implement basically you can't create

492:19

object of a but you can create object of

492:23

B and you can say it works and now using

492:27

this obj I can call show I can call uh

492:31

what are the method we have we have

492:32

config so we can call both the methods

492:34

and that perfectly works if I compile

492:36

and run this code you can say it says in

492:38

show in config okay what else can I have

492:43

uh so the way you create variables here

492:45

also the way you create methods here can

492:47

I create variables now first of all we

492:50

don't uh Define variables right we

492:52

declare them so we can actually so if I

492:54

say int let's say eight that's a

492:56

variable I know I don't know why I'm

492:58

taking this age variable every time but

492:59

let's say we have eight age here and

493:02

let's say we have string area so that's

493:04

the area name

493:06

but you can see we got an error that

493:08

means can I I cannot create variables

493:10

that's weird I just said we can right so

493:13

the only thing is every variable in the

493:16

interface we can create actually those

493:18

variables the only thing is they are by

493:20

default final and static so all the

493:24

variables inside the interface are by

493:25

default final and static

493:29

okay uh so that means we have to first

493:32

initialize the variable So when you say

493:33

it's it's final we have to initialize

493:35

the variable right okay that's not my

493:36

age but let's say 44 and let's say area

493:39

is Mumbai

493:41

okay so basically what we're defining is

493:43

we are defining the age which is 44 and

493:45

the area is Mumbai now this variables

493:48

are final and static now first of all

493:51

since they are static which means we can

493:54

directly use the interface name remember

493:56

when we use static variable we don't

493:58

have to

493:59

use the object example if I want to

494:02

print the value of name or area here I

494:05

can simply say a DOT is the interface a

494:08

DOT area I can directly use it not with

494:11

the object

494:12

and if I compile and run it works you

494:15

can see we got Mumbai

494:16

if I try to change the value of this if

494:19

I say a DOT area is equal to let's say

494:23

if I want to change the area to

494:25

Hyderabad

494:27

it's not working you can say it says the

494:30

final field that's right the area is

494:33

final it is static we are using directly

494:35

the interface name and it's also final

494:38

okay so that's one thing you have to

494:39

remember now you might be thinking why

494:42

it has to be static final see first of

494:44

all you don't instantiate the interface

494:46

right whatever you instantiate is a

494:48

class and you are not doing extent you

494:51

are doing implements what you get in

494:53

implements is only the methods right you

494:56

don't get the variables there in

494:57

implements so that's why it is static

495:00

because we want to use the interface

495:01

name and final because interface objects

495:05

or interface don't have their own memory

495:07

in the Heap right what you create object

495:10

office of B not not of a

495:12

so if you don't have your own memory how

495:15

can you have a non-final variable so

495:17

this is interface in fact before talking

495:19

about the need for interface we have one

495:21

more thing to add here

495:23

so let's say if I have a interface and

495:26

I'm creating a class which implements a

495:28

one interface here can a class Implement

495:30

multiple interface example let's say if

495:33

I have interface which is called X so if

495:35

I say interface X so B Class implements

495:38

a and X both and the answer is yes if

495:42

you can see there is no error here the

495:43

only thing is whatever method you define

495:45

here so let's say the method name here

495:46

is run you can see we got an error here

495:50

the only thing you have to do is you

495:51

have to Define run as well so you will

495:53

say public void run so basically we can

495:57

have one class implementing multiple

495:59

interfaces now that's not the case with

496:01

abstract class right because in abstract

496:02

class we can we can extern only one

496:04

class in interface you can have a class

496:07

implementing multiple interfaces

496:10

so here I can simply print running okay

496:14

so what we are doing is we are using

496:16

multiple methods here we have three

496:17

methods coming from two different

496:18

interfaces this interface says two

496:21

methods show in config and this

496:23

interface is has only one method which

496:25

is one and you can see we got three

496:27

methods here uh there's one more thing

496:29

let's say we have one more interface

496:31

which is called uh interface y now the

496:34

thing is when you talk about class and

496:35

Class A Class can extend another class

496:38

right what about interface but can we do

496:40

interface and interface what keyword we

496:43

have to use here see whenever you have

496:45

interface to interface we say extends so

496:47

interface y extends X so inheritance is

496:51

is valid here as well now in this y

496:54

interface you will be having a method

496:56

called run so that's the inheritance

496:58

okay so you will get this declaration in

497:01

the interface y as well let me show you

497:03

if I remove this X and if I keep y even

497:07

now we have to use one otherwise if you

497:09

remove run from here if you you comment

497:11

this part you can see it will give you

497:12

error it says the type B must implement

497:16

the United abstract method one

497:19

okay but we are implementing y right

497:22

it's just that y extends X so we have to

497:25

use one

497:26

now Point remember here is whenever you

497:28

have a class to class we say the extends

497:33

whenever you have class to interface

497:36

basically a class is in implementing

497:39

interface so we have to say implements

497:40

and whenever you have interface to

497:43

interface we say that extends again okay

497:47

this is the point you have to remember

497:48

so again I will comment uncomment this

497:51

part

497:51

okay now there's only one thing you have

497:53

to remember whenever you create a

497:55

reference of a in phase and object of B

497:58

which is a class I can only call two

498:00

methods which is of part A if I try to

498:03

call run with a it will not work because

498:06

a has no idea what one method is if you

498:09

can see in a we don't have one in fact

498:12

what we will do is I will just remove

498:13

this code for time in yeah and then if

498:16

you want to implement one if you want to

498:18

call one in that case we need to create

498:21

the reference of X which is obj1 is

498:24

equal to Nu B now with this obj one I

498:28

can call one but again I can't call

498:30

other two two other methods okay so

498:33

that's one thing you have to remember

498:34

okay now once we are good with this

498:36

let's try to understand what's the need

498:39

of the interface let's try to understand

498:40

what is a need for the interface now for

498:43

that let's take an example here so let's

498:45

say we have a class here and so the

498:48

example which I'm trying to take here is

498:49

for the developers let's say when you

498:51

join a company of course as a developer

498:53

when you join a company you want to

498:54

write code right you want to build

498:55

application and if you have seen the

498:58

trend whenever you join a company most

499:00

of the company will give you a laptop to

499:02

work on and luckily if you get a Macbook

499:04

that's great so they give you a laptop

499:06

right so that's that's the thing right

499:08

if you want to work in a company let's

499:10

say if you go to our office and if you

499:11

want to work there you will say hey I

499:13

want a laptop so in this case let's say

499:15

you are a developer okay so I will say

499:18

you are a deaf right of course you're a

499:21

human but let's say you're a developer

499:22

and then as a developer what you do you

499:25

make applications right or maybe I can

499:27

say you write code I'll set the app you

499:30

you develop applications right now when

499:33

you say you are developing an

499:34

application of course you will say

499:37

coding okay you're building an

499:39

application here of course as a company

499:41

if I want to build a product I have to

499:44

hire developers now in this case let's

499:46

say if I hire a developer let's say

499:48

developer name is Naveen and then if you

499:50

say new developer and with this object

499:52

Naveen I can say Naveen dot Dev app

499:54

right so I want the window develop

499:56

application and if I compile this code

499:59

and if I run it perfectly works right

500:01

now let's say

500:03

if I say hey you know I will make an

500:06

application but I need a laptop first I

500:09

cannot work without laptop but we don't

500:11

have laptop right so what I will do is I

500:13

will create a laptop class I will say

500:15

class laptop and in this class laptop of

500:18

course in a laptop what you do is you

500:20

write code and I can say calling

500:25

right in fact what laptop when you say

500:27

code you it basically you code the new

500:29

compile then you run so you do all those

500:31

stuff right so I'm saying code compile

500:33

and run and now as a developer what I

500:37

want is if I if you ask me to write a

500:38

code what I will do is I will just say

500:40

hey give me a laptop so basically you

500:43

have to accept a laptop object here

500:45

that's right you can also pass the

500:46

object so this developer will say give

500:49

me a laptop and then I will write code

500:51

so here instead of saying this you can

500:53

simply say lab dot code right now since

500:56

you have a laptop object you can

500:57

basically call this method called code

500:59

and it will work so what I'm doing is I

501:02

got two classes here which one is

501:03

developer and second is laptop and

501:06

inside laptop class I have a method

501:07

which is code and then once I pass this

501:10

object I can simply code right now the

501:13

only thing is when you ask a developer

501:15

to write a code developer will say hey

501:17

give me a laptop so that means first you

501:19

have to create an object of laptop so I

501:23

was a laptop lap is equal to the new

501:26

laptop so we got a laptop object of

501:28

course every developer needs a different

501:29

different laptop object so let's say if

501:31

you hire five developers you need five

501:33

laptops to give them

501:35

okay I was a lab here now this should

501:37

work there's no problem if you can see

501:38

the code everything should look good uh

501:42

if I run you can see it still works so

501:44

basically we are saying code compiled

501:46

one so what you're doing is from the

501:48

main method you are calling Dev app

501:50

which is here and then you are passing

501:52

the laptop object okay so you're passing

501:54

this object here and when you accept

501:56

this object you're calling the code

501:58

method so it will call this code method

502:00

and you have to execute this part

502:02

everything works well right now the only

502:04

thing is do you really need a laptop see

502:07

in some companies they give you a

502:08

desktop right so if you go to the office

502:10

you they will have a desktop where you

502:12

have a monitor you have a desktop a good

502:14

desktop which works so why as a

502:17

developer you are asking for a laptop

502:19

what if company wants to give you a

502:20

desktop you can't say hey I cannot work

502:22

without laptop right you should be able

502:24

to work on any computer

502:26

so let's say you also get an option of

502:29

desktop there in fact in desktop as well

502:31

I can do the same thing but I will say

502:34

faster like desktops are always faster

502:37

than a laptop right so you can see when

502:40

you're coding uh it says code compiled

502:42

One faster because it works fast now

502:45

what if a company says hey you know I we

502:48

don't have laptops with us but what we

502:49

have is we have desktop our desk is

502:52

equal to new desktop and they don't want

502:56

to pass the laptop object so that when

502:58

the company says Hey Naveen you have to

503:00

work you have to make an application and

503:01

what we are giving you is not laptop we

503:04

are giving you a desktop now nobody will

503:06

say I can't work you can see Naveen says

503:09

I can't develop application because my

503:12

application only needs laptop you can't

503:15

do that so writing laptop here is an

503:18

issue

503:18

we can't do that right so how do we

503:21

solve this problem and that's what you

503:23

know when I was talking about abstract

503:25

class and interface I told you about the

503:27

design thing right so when you back an

503:29

application you have to make it more

503:30

flexible here we are what you are doing

503:32

is you're doing a tight coupling here

503:34

which is you are saying that developer

503:36

is dependent on a laptop no developer is

503:39

not dependent on laptop developer is

503:41

dependent on a computer a computer can

503:44

be anything your computer can be a

503:45

laptop a computer can be a desktop so if

503:47

you want to improve this example what

503:49

you can do is you can create a class

503:51

called computer okay and in this

503:55

computer class you can say public void

503:57

code and basically you know I want to

504:00

keep this empty because I don't want to

504:02

add a code because we don't computer

504:04

does not exist right it's just a concept

504:06

and I can say a laptop is actually a

504:10

computer so when you do that the

504:12

advantages in fact desktop is also a

504:13

computer so when you do that what

504:15

happens is now both this laptop and

504:18

desktop they are computer so when I'm

504:20

creating a reference of it so can I say

504:23

computer lab is equal to new laptop and

504:28

computer

504:30

desk is going to do that new desktop we

504:32

can do that right we can create a

504:33

reference of a parent class an object of

504:36

a subclass and that's what we are doing

504:37

here same thing can be applicable here

504:40

right as a developer you are focused

504:42

more on the computer not on laptop or

504:45

desktop the moment you do that you can

504:47

say everything is working there's no

504:48

problem here is because what you are

504:51

passing here is Dex desk right which

504:53

desktop basically but that is referred

504:55

as a computer and what you are referring

504:58

here is a computer there's no mismatch

505:00

here you are passing a computer

505:02

reference you are accepting a computer

505:04

reference but when you call code it will

505:07

be called based on what object you are

505:09

passing so basically we are passing

505:11

desktop object So when you say code it

505:13

will call the code of the desktop not of

505:16

the laptop let me show you so if I

505:18

compile this code and run you can say it

505:20

says faster that's desktop but now if I

505:23

pass a laptop let's say computer company

505:25

decided to send you a laptop and now

505:28

when you compile and run you can see it

505:31

says code compile and when there is no

505:32

faster here that means that's laptop

505:34

code

505:35

so that's how you design the application

505:38

right to make it Loosely coupled where

505:40

your developer object is not dependent

505:43

on the laptop or desktop it is dependent

505:45

on the computer any computer will do a

505:47

mobile phone also do because that's a

505:49

computer now there's one change

505:51

see if it if you look at this code we're

505:53

doing nothing right so instead of

505:55

defining it here can I simply declare

505:57

this and we know when you're declaring

505:59

it we can make it abstract and since

506:01

your method is abstract you can also

506:03

make your class abstract and problem

506:05

solved and that's the importance of

506:07

abstract class right you can create that

506:09

abstraction layer where you say we are

506:12

not working with laptop and desktop in

506:13

real world as well we do that right uh

506:16

sometimes we don't mention rows or

506:18

lilies or sunflower we simply say

506:20

flowers right we don't say apartment or

506:24

a villa or anything we say house right

506:29

so we try to generalize things in real

506:31

life in real in virtual world as well

506:33

it's good to generalize things instead

506:36

of specializing things there's one more

506:38

don't you think we can also use

506:39

interface instead of using abstract

506:40

class so we are using interface we don't

506:43

need public abstract the only change you

506:45

have to make is instead of using App

506:48

extends we have to say implements

506:50

and that's the beauty of interfaces

506:52

right

506:53

I know we have written a lot of code

506:55

maybe you have to watch this video once

506:57

again to understand and try it by

506:58

yourself that's when you will understand

507:00

the power of the interfaces and what you

507:03

are creating here is not the classes

507:06

reference we are getting interface

507:07

reference yeah and that's the importance

507:10

of the interface in this video let's

507:12

talk about enums now enums are named

507:15

constants which we create so basically

507:17

if you try to have some consonant

507:19

example you know when you talk about

507:20

other codes or if you have worked on a

507:23

networking concept so let's say when you

507:24

send a request to the server the server

507:27

will give you some status right if it is

507:29

accepted the request if it is rejected

507:31

the request or the resource not found

507:33

the four not four error so basically you

507:36

know we have these numbers for every

507:37

type of data you get from the server and

507:41

also we can have a name for it now when

507:42

you know that we have a specific error

507:44

codes or when you have a specific

507:46

constants which you want to use instead

507:48

of creating a different object by

507:50

yourself you can create are enum now how

507:54

it looks like let me just give you a

507:56

simple example so let's say we have enum

507:58

here and I'm saying I want to go with

508:00

enum of status so I will say status now

508:04

in this status I can have different

508:06

constants which I want to use now of

508:08

course you can create a class and you

508:10

can create multiple objects of it so

508:12

let's say this status can be of running

508:13

State a fail State success pending so

508:16

you can have all those things and of

508:18

course you can create a simple class

508:20

called status and you can create

508:21

multiple objects right but just to keep

508:24

it simple we can use enum where we can

508:27

Define all the names constant and they

508:29

said this is pre-built okay example

508:32

let's say if you talk about status I can

508:34

have status like running status I can

508:36

have status and of course you can if you

508:38

can separate them with the help of comma

508:40

you can say running then we can have

508:42

failed uh then we can have pending and

508:46

let's say success of course you can

508:48

change the sequence doesn't matter but

508:50

you can have this constant now the

508:52

advantage for this is if you want to use

508:54

an application where you want written a

508:56

status to the client uh that status can

508:59

be running the status can be failed

509:00

pending or success instead of getting

509:03

returning a string format or a specific

509:06

object you can simply return this status

509:08

now how easy it can be it's very easy

509:11

you can just go back to your main method

509:13

and you can say I want to have a status

509:16

so you can create a status reference

509:18

which is s let's say and this is same as

509:21

what you use normal variables example if

509:24

you want to say int I and then you'll

509:26

send the value as well right let's say

509:27

five so we have this int as a type then

509:30

we have this I as a variable and then we

509:32

have a value which is five in the same

509:34

way you can have a type as status that's

509:36

right this is a type of it so all these

509:40

are objects now this is something

509:42

different in Java now if you're coming

509:44

from C plus plus in C plus plus we

509:46

directly create these status and we have

509:49

all these constants

509:50

and it treats in them in a different way

509:53

but Java says oh everything is classier

509:55

so basically the status is a class here

509:58

I know that sounds weird but yeah that's

510:00

how it is implemented this is a class

510:01

and all this are actually objects of

510:04

status so by default you got you got

510:06

four objects and you can use any of the

510:08

object here example I want to use let's

510:11

say a status

510:13

dot running now that is my object which

510:18

has been assigned to S now of course you

510:20

can print this SEO and it will print the

510:22

running status so I can say compile and

510:25

run you can say it says running so

510:27

basically you can use any status here

510:28

now what are this just to reiterate

510:31

these are actually named constants so

510:34

instead of using numbers or string we

510:36

can use named constants it will make

510:38

your work bit easy again I'm not saying

510:40

you have to use this everywhere but if

510:42

you have a scenario where you want to

510:44

use constants uh we can we can use this

510:47

concept and you can use any constant

510:48

here we are using running I can also use

510:51

failed even that works compile and run

510:55

you can see we got a failed status but

510:58

yes if you try to use a status let's say

511:00

no idea status now if you can see this

511:03

status don't have any no idea and that's

511:06

why you can see we got an error here it

511:07

says no idea cannot be resolved or is

511:10

invalid field that's right so you can

511:12

only use those status which you

511:14

mentioned here now in other languages we

511:16

have one more thing you know all this

511:18

actually has a number uh so basically

511:20

you get numbering for it maybe one two

511:23

three four so that you can pick this

511:25

state as based on numbers in Java it

511:27

starts with zero so this is zero one two

511:30

three and if you want to get that you

511:32

can actually do it so you know you once

511:34

you get the status you can use a method

511:36

called ordinal so so different constants

511:40

here have different numbers this is

511:41

based on the numbers right or the order

511:43

of it so this is first second third

511:45

fourth right but when you have zero one

511:47

two three so when I try to create the

511:49

ordinal of running of course it will

511:51

point zero and that's what you want to

511:53

see so you can see we got zero but yes

511:55

if you change it to success which is

511:59

three in this case you can say we got

512:00

three in fact you know you can also

512:02

print all what if you don't want to

512:04

print one you want to print all here uh

512:07

so what you can do is instead of getting

512:08

one status you can get all the status

512:10

first of all because if you say status

512:12

doc success it will only give you one

512:14

status but what if you want to get all

512:17

so in that case instead of saying this

512:19

you can say status dot so there's a

512:21

method here if you can see we got values

512:23

and that's right why we got methods is

512:25

because this status is a class and you

512:28

get some inbuilt methods now from where

512:30

you are getting these methods we'll talk

512:31

about it in some time or maybe maybe

512:32

next video but uh you got methods so you

512:36

can see status dot values will give you

512:38

what okay so let me just jump to values

512:40

just to see what it will give you so

512:44

status dot value will give you an array

512:47

you can see it gives you an array so on

512:50

this side we have to say this is not a

512:53

normal variable this this is a array and

512:56

for that maybe I can say SS just to have

512:58

a different name and once you got this

513:00

array you can print all how you can just

513:03

come back here you can print all let's

513:05

try I'm not sure if you can get all here

513:08

no compile time issue run or it is

513:11

spending the address or something I know

513:13

that

513:14

okay so what you can do is you can

513:15

specify the index value I can say 0 it

513:18

will print running yeah it works but

513:20

then what if you want to print all and

513:22

we have seen it right we can use a for

513:23

Loop here now which one I will use

513:26

enhanced for Loop so I will just cut

513:27

this part I will use enhanced for Loop

513:29

here and every time you iterate of

513:32

course it will give you status right so

513:34

what is this added this arrays of type

513:35

status so it will give you one status at

513:38

a time so you can say status s colon SS

513:41

and here you can print the status so

513:45

basically it will print

513:47

all the status for you so you can say it

513:49

is printing all the status now with this

513:51

s you can also print the order of it

513:54

just to see how it works so you can say

513:56

s dot ordinal it will print yeah so it

513:59

will also paint the order of it 0 1 2 3.

514:03

yeah so let's expand our example of enum

514:06

with if else and switch now as I

514:10

mentioned before these are constants

514:11

right so basically when you have this

514:13

constant can we just compare them we can

514:15

actually the way you compare normal

514:17

variables or strings so let's take an

514:19

example here let me just remove int

514:21

variable there and let me also remove

514:23

the array let's keep it simple now so

514:26

let's say I have a status here and S is

514:28

equal to and the status of which I want

514:30

to set is let's say uh pending okay

514:33

initially the state value pending and

514:35

this value can be coming from anywhere

514:36

maybe you called a method which returns

514:39

this success or this status and you want

514:42

to check what is a status and based on

514:44

that you want to print some value

514:45

example if it says running I can print

514:48

all good if it is failed I can say try

514:50

again if it is pending I can say please

514:52

wait if it is success I can say done so

514:56

how do I know what is this value for

514:58

that you have to compare right so you

514:59

can say if the S is equal to equal to

515:02

status start running if this is a status

515:05

I will print I will come back here and I

515:08

will print all good and I can say if

515:11

else I want to check for the second one

515:13

I will say if status dot uh if it is

515:16

matching with failed I can print try

515:19

again else if if the status is equal to

515:23

I mean s is equal to status dot let's

515:26

say pending now in this case I can say

515:29

please wait else of course you can also

515:32

do if else else here but we only are

515:35

left with only one status I can say else

515:37

I can print done okay so basically you

515:41

can actually compare the status with the

515:43

help of if else and we can just come

515:45

back here say compile and run you can

515:48

see it works it says please wait because

515:50

this is pending but what if you got a

515:52

status which is a running state in that

515:55

case it will print all good okay so this

515:58

is how basically you can work with if

516:00

else but what if you want to work with

516:01

switch because say in switch there is a

516:04

limitation which now a switch can

516:07

support right it supports integer string

516:09

and many more but here the question is

516:12

does it support enum and the answer is

516:14

yes so what I will do here is instead of

516:16

using IF else I will just reuse the code

516:17

but I will use a switch here and in this

516:20

switch you can pass this status as it is

516:22

there's a change compared to if else

516:24

which is when you say case you know in

516:27

if else you basically have to use the

516:29

status as well but in case you don't

516:31

have to do that because anywhere the S

516:33

is of type status right so it knows

516:35

whatever your passing is basically a

516:37

part of a status so you can directly use

516:40

the constant itself so I can say running

516:43

if it is running I can just print this

516:46

statement and of course in switch we

516:48

have to also put a break and then we can

516:51

check for the second case

516:53

so that's case uh failed and here we can

516:57

have try again so of course we have to

516:59

say break here as well what else we have

517:01

two more

517:03

anyway let me just delete the entire

517:05

stuff from here if part you know I'm so

517:07

lazy I can just reuse this so the next

517:09

status is what pending in pending I can

517:13

say please wait and then the last one is

517:18

success in check for Success yourself

517:19

even writing the case we can say default

517:22

and here we have to give a colon and we

517:25

have to say done so basically whatever

517:27

we have done with if else we can also do

517:29

it with switch and switch actually makes

517:31

much more sense here because we are

517:34

working with constants right so switch

517:36

looks good in the case of enums and now

517:39

the question is will this work let's try

517:41

compile and run it works you can see it

517:44

says all good but yeah if I change the

517:46

status to let's say success I just want

517:49

to see if it prints done

517:52

yeah we got done so this also works with

517:54

uh switch so that's how we can use

517:58

with enum if you

518:00

if it enum and I've also mentioned that

518:02

enum is a class right so does that mean

518:05

we can extend any other class uh the

518:07

thing is no that's that's a thing which

518:09

is not suitable here you can't extend

518:12

enum within any other class apart from

518:14

this everything is same that means you

518:16

can Define methods you can Define

518:17

Constructors

518:18

that's right you can create Constructors

518:20

you can also create your own variables

518:22

inside the enum and that's what we are

518:24

going to do here

518:25

okay so what I will do now is and

518:27

there's one more thing is remember with

518:29

enum we have use certain methods for

518:31

example if I come back here and if I say

518:33

uh status was the object s okay here

518:35

let's do it here so if I say s dot you

518:38

can see we'll get some methods here and

518:40

we all know that by default every class

518:42

in Java extends object and that's why

518:44

you will get the hash code and then two

518:48

string Methods which is here but what

518:50

about this methods ordinal uh name and

518:54

then uh compared to now from where you

518:57

are getting all these methods so by

518:59

default enum in Java extends our

519:01

enumclass okay let me just prove that to

519:04

you I will just print it here s dot get

519:07

class

519:09

dot get

519:11

so what I'm doing is I'm just print I'm

519:13

getting the class name and the super

519:15

class of it so I'm just I'm just trying

519:16

to print this super class of our class

519:19

enum and I will just compile and run you

519:23

can say it is enum so inum in Java

519:26

extends E M class and all those extra

519:28

methods you get it from enumclass okay

519:31

now once we are good with this let's

519:33

treat our own class as enum and I will

519:35

do a different example here instead of

519:37

using status I can let's try to use

519:39

laptop because we have to use some

519:41

Constructor stuff which makes much more

519:43

sense with the laptop and let's say we

519:45

have some laptop models which I which I

519:47

love uh example MacBook then we have

519:50

Dell XPS

519:52

then we got surface not a big kind of

519:54

surface by little surface the Microsoft

519:57

laptops and we have ThinkPad I used to

519:59

love ThinkPad when it was with IBM but

520:01

anyway

520:04

let's see right and then what we can do

520:05

is let's say for every laptop I want to

520:08

define the price as well now how can we

520:10

do that so what we can do here is just

520:13

after this MacBook because that's an

520:15

object right so just after this I can

520:17

say hey Macbook will cost you let's say

520:19

two thousand dollars uh XPS will cost

520:22

you around let's say uh two thousand two

520:24

hundred dollars Express a bit costly and

520:27

surface is let's say 1500 I'm not sure

520:30

the cost and let's say ThinkPad is

520:32

eighteen hundred dollars so we got this

520:34

cost for this laptops here right and

520:37

then okay it gives you an error but

520:38

we'll solve that error in some time now

520:41

first of all what is that error so we

520:43

are trying to say that this is a price

520:45

but how will your enum will know that

520:47

you're talking about a prize here now in

520:49

this case what we can do is let's create

520:51

a variable int price so that we can have

520:54

an instance variable and then if You

520:57

observe here don't you think this is a

520:59

Constructor you can say it says that a

521:01

Constructor of laptop that's why we can

521:03

have a Constructor here so when you

521:05

create the object by passing a value

521:06

that's what we are doing here we are

521:08

saying these are the objects we have

521:10

and this is a Constructor where we are

521:12

passing a value

521:13

okay that means I can simply ask my

521:16

editor hey you know I need a Constructor

521:19

here which takes the price

521:22

and yeah we got a Constructor and now

521:25

you can see there is no error that means

521:27

whatever value you're passing here will

521:29

be assigned to this price and the beauty

521:31

is this laptop is a class and all these

521:35

are objects and every object will have a

521:37

different value for the price it's not

521:38

like once you define price for one

521:40

object it will be applicable for all no

521:42

that's not the case and once you have

521:44

this let's get back to our main and here

521:47

let me create a laptop lap is equal to

521:50

and let's say by default I maybe I want

521:52

a Macbook here and once you got the hold

521:55

of MacBook I can let's say I want to

521:58

print I want to print lab which of

522:00

course it will print MacBook let's try

522:02

once

522:03

yeah it plays MacBook I also want to

522:06

print the price of it oh now that's

522:08

tricky because the price is private and

522:11

we have seen that make make sure that

522:12

your variables the instance variables

522:14

are private how to access this now one

522:17

way to do that is with the help of

522:18

Getters and Setters the only problem is

522:20

can we use them here yes we can create

522:24

our own methods so what we can do here

522:26

is I can right click and I will say hey

522:29

I want a Getters and Setters basically

522:32

you can change the value as well okay so

522:35

that means if I want to print the price

522:37

I can just come back here and I can

522:39

print lab dot get price now this is

522:42

specifically for Macbook so if I use a

522:44

different object here I will get a

522:45

different price and with that if I try

522:48

to compile and run you can see we got

522:51

two thousand now what if you want to get

522:53

all the laptops okay let's try so we'll

522:56

just comment these two sections first of

522:58

all you need to get all the laptop that

523:00

means you need arrays so it's the

523:02

laptops or in fact you know what you can

523:03

do you can directly use a loop here and

523:06

you can say laptop lab colon from the

523:10

laptop so this is the enum you want all

523:13

the machines or all the constants so how

523:16

do we get all the values so dot we have

523:18

seen this right we can use values here

523:20

which will give you the array and one by

523:23

one you can fetch the value

523:24

so what you can do is you can print the

523:26

object and also you can print the price

523:30

let me print the price how do I get the

523:32

price it's a lab dot get price

523:36

okay I hope this will work let me just

523:39

verify once yeah we can do that so I'll

523:41

say compile run you can see we got all

523:44

the values and at any point you can

523:46

change the value as well okay so you try

523:47

that so try to call set price and pass a

523:50

particular value here and your price

523:52

will change okay there is one more thing

523:54

uh what if I don't specify the value for

523:57

surface let's say we have a surface here

523:59

and then I don't mention this value now

524:02

what will happen you can see we are

524:04

getting an error

524:06

why before it was working the thing is

524:09

see by default we have a default

524:10

Constructor right the moment you mention

524:12

a parameterized Constructor it says hey

524:15

if you want to get a constant you need

524:17

to set the price now because you have a

524:18

parameterized Constructor there is no

524:19

default Constructor here

524:21

so you can say okay I know how to solve

524:23

that you can create a default

524:25

Constructor here so you can just right

524:27

click and say Source action yes I want a

524:29

Constructor without the price okay and

524:32

you can see we got a default Constructor

524:34

here and it's your choice if you want to

524:37

specify some price here so you can say

524:39

price is equal to let's say minimum five

524:41

hundred dollars you can specify those

524:43

things and now if you can see the price

524:46

for surface will be 500. even if you

524:49

don't mention you will get a default

524:51

Constructor and the beauty is you create

524:54

four object it will call the Constructor

524:56

four times this Constructor three times

524:58

and this one once let me just prove that

525:02

point I will just try to print something

525:04

here I will say in laptop and let me

525:07

also print the object name itself I can

525:10

just say Okay this dot so we got a name

525:13

here let's try to see if this works

525:15

compile and run okay so that we got the

525:20

name as well so it says in laptop

525:21

MacBook in laptop XPS in laptop ThinkPad

525:24

but not surface is because surface

525:26

because it was created with the help of

525:27

default construction we are not printing

525:28

anything there okay so that's how we use

525:30

it and and mind you we are using it as a

525:33

private Constructor not a default or the

525:36

public it's because we are creating the

525:39

object in the same class itself and

525:42

that's how the private Constructor works

525:43

you can create the object in the same

525:44

class

525:45

in this video let's talk about

525:47

annotations now annotation simply means

525:50

a supplement to the Impala or to the

525:52

runtime or we also call it as a metadata

525:56

so basically what happens you know when

525:58

you write a code sometimes you want to

526:00

interact with a compiler by saying

526:02

something of course it will not change

526:04

the way your code will work it's just

526:06

that we want to supply some extra

526:09

information to the compiler or to the

526:11

runtime

526:12

how do we do that so let's say uh we

526:15

will go for a normal class here so let's

526:17

say we have a class A and then we got a

526:21

class B so basically I'm trying to go

526:23

for method over writing here so of

526:25

course we have to extend a class and

526:27

then uh this will have a method which is

526:29

show and then in this show I'm printing

526:32

let's say in a show and the same thing I

526:37

can do in a b as well the only thing is

526:40

we have to change it here now what we

526:43

are doing here is we are overriding the

526:46

show method of a with the show method of

526:48

BM then we have seen this before right

526:50

okay let's create the object of B just

526:52

to understand this so I will say b o b j

526:54

equal to new B and then I can call obj

526:57

Dot show and we all know what will be

526:59

the output uh so if I compile this code

527:03

and run you can see it says in B show

527:06

perfect now let's say I change this

527:09

method name okay not sure but something

527:11

else I will say

527:12

show the data which belongs to this

527:19

class okay so normally in Java we give a

527:22

meaningful name to a method right and

527:24

sometimes your method name can be big

527:26

and yes okay I know this is exaggerated

527:28

but sometimes you do have a big method

527:30

names with two or three words or four

527:32

words and then of course if you want to

527:34

override this particular method we have

527:37

to type the same thing here so I will

527:38

say the data which belongs to this class

527:41

okay so again I'm just trying to

527:43

override the method of a class with the

527:47

method of b class right and of course we

527:49

have to call this now so I will just try

527:52

to get it from the suggestion okay so if

527:55

you can see I'm just trying to call this

527:57

method

527:59

method because we have overed it in the

528:01

method so it should call in B sure right

528:03

so if I compile this code and if I run

528:06

this code oh it is printing in a show

528:09

now we were expecting in B Sure in fact

528:11

I wanted in B show but we got in a show

528:14

now this type of problems in programming

528:16

is called Bugs okay uh bugs are simply

528:20

logical problems where you are expecting

528:22

something else and you got something

528:24

else example you want to say two plus

528:26

two and the output is 4 but you got five

528:29

that is logical problem it's not a

528:31

compile time issue it's not a runtime

528:33

issue it's The Logical it's the logic

528:35

which you have written is wrong

528:36

and one of the most difficult problem to

528:39

solve is logical problems you know we

528:41

always say in programming that you know

528:42

you spend less time in coding and more

528:44

timing debugging that what is happening

528:46

here so of course you have only two

528:49

classes here and it is easy to debug but

528:51

if you have a lot of classes it will be

528:53

very difficult now how will you debug of

528:55

course we are trying to call the method

528:57

of b class but we got the method of a

529:00

class right how would you solve this

529:01

problem so let's try to understand the

529:03

method name on purpose I change the

529:06

method names okay I know it might be

529:08

visible for a few people you can see it

529:10

belongs here we have S in this method I

529:13

don't have it and sometimes you can miss

529:15

it right and then when I'm calling it

529:17

I'm passing s on purpose I was calling

529:19

the method of a class this type of

529:21

problems can arise when you write a code

529:23

so what you do is you can show your

529:25

intention to the compiler and compiler

529:28

will help you here of course right

529:29

whenever you make a mistake compiler

529:30

gives you error and looking at the error

529:32

we can easily understand hey I did

529:34

something wrong and let's go back and

529:35

solve this so what if you can ask your

529:38

compiler hey compiler you know what I'm

529:40

doing here I'm trying to overwrite the

529:42

method here of Class A and if it is not

529:46

happening let me know so you can show

529:48

your intention and you can show the

529:49

intention with the help of annotation

529:51

there are a lot of inbuilt annotations

529:53

available in Java so one of them is

529:56

overdrived so you can simply use add

529:58

rate here and you can say override now

530:02

you are saying by doing this to your

530:04

compiler hey you know I'm trying to

530:05

override the method and your compiler

530:07

says you know what there is no method

530:10

called show the data which belongs to

530:13

this class in the a class and then you

530:16

will know oh I made a mistake and then

530:18

you can solve this problem just by

530:20

putting s of course you can compare

530:21

these two methods and you can say I got

530:23

the problem so of course you have to

530:25

solve the problem so compile is helping

530:27

you by showing the problem right at

530:29

least you got some help and solving the

530:31

problem at compile time is much better

530:33

than solving the problem at runtime or

530:34

when you deploy the application to the

530:36

users and then users will give you a

530:39

prompt hey you know you made a mistake

530:40

so let's avoid that embarrassment and

530:43

let's solve the problem at compile time

530:45

so at overnight is one of the way you

530:46

can do that and now if you try to

530:49

compile and run you will get in Bishop

530:51

now apart from this override we have

530:53

different annotations as well uh which

530:56

we normally use in terms of core Java we

530:58

don't use most of the annotations of

531:00

course you can create your own

531:01

annotations but in general we don't use

531:04

much now once you start working with the

531:06

Frameworks example uh when you talk

531:08

about hibernate framework or spring

531:10

framework at that point it is mostly

531:13

driven by the annotations now there are

531:16

some annotations which works on the

531:17

methods there are some annotations which

531:19

works on variables there are some

531:21

annotations which works on class level

531:22

so example uh there might be some

531:24

annotation which you will be using on

531:26

class level example if I say deprecated

531:28

now this is one of the annotations we

531:30

have now what we are saying that we have

531:32

this class A deprecated simply means you

531:35

can use it but don't use it it is

531:37

deprecated soon it will be removed from

531:39

the Java language or there is a better

531:41

alternative for this so you can mark

531:43

your class as duplicated so that if

531:46

someone tried to use your class they

531:47

will get to know that this class is

531:49

deprecated

531:51

now apart from this you can also use

531:53

okay these are all class level

531:55

annotations in fact we are going to see

531:57

one mode when we start with interfaces

531:59

okay so if I try to use something on the

532:02

method level let's see what we have so

532:03

we have safe varrogs for the variable

532:06

arguments we have suppressed warning if

532:09

you want to hide the warnings once we

532:11

start with hibernate framework we'll

532:12

also say transient a functional

532:15

interface we are going to see in the

532:16

interface concept yeah uh in fact you

532:18

know when you create your own

532:19

annotations we can also set the

532:21

retention so example let's say whenever

532:23

you use an annotation till what level

532:25

you want to set it do you want to set

532:27

for compiler level that simply means if

532:29

you set a detention for compiler it

532:31

means once the code is compiled The

532:34

annotation will not make any sense after

532:35

that but if you say annotation is

532:38

applicable for runtime as well so after

532:40

compiling as well when the code is

532:42

running at that point also The

532:43

annotation will be applicable

532:45

so we have different options there and

532:47

of course this will make much more sense

532:48

once we start working with Frameworks

532:51

so yeah that's about annotations and uh

532:55

so we have different annotations and

532:56

remember when we were doing method over

532:58

writing and if you ask example let's say

533:00

if I remove this part and if I ask my

533:02

idea to generate a overnight method so I

533:05

can just go back here and say override I

533:07

want to override this particular method

533:09

by default it will give you at override

533:12

okay so that's you have to remember now

533:14

let's talk about functional interface

533:16

and we know the functional interface is

533:18

an interface which has only one method

533:20

so let's let's create one so what I will

533:22

do is I will create the interface here

533:24

and I will call this interface as a okay

533:27

and in this interface let's create one

533:29

method okay only one method so I can say

533:31

public abstract void show and public

533:33

abstract is optional so we can simply

533:35

say void show right and then I can

533:38

implement this I can use the I mean how

533:41

do I create the object of this of course

533:42

we know we can create a class we can

533:45

implement this interface and then we can

533:46

create the object of it right now this

533:49

interface is called a Sam which is

533:51

single abstract meta interface or we can

533:54

call this interface as functional

533:56

interface and to specify that this is a

533:59

functional interface we can use an

534:01

annotation which is a functional

534:03

interface by doing that we are making

534:05

sure that we only have one method here

534:07

okay see if I don't mention functional

534:10

interface and my intention was to have a

534:12

functional interface but if I if I write

534:14

one more method here which is one okay

534:16

there is no error here and we want

534:18

errors right

534:20

so if you have an intention of creating

534:21

only one thing I mean single abstract

534:24

method interface which is only one

534:25

method and then by mistake you return

534:27

two methods here you want compile time

534:29

errors right so if you write it

534:31

functional interface you can see it will

534:32

give you error it says invalid

534:34

functional interface annotation a is not

534:37

a functional interface so it's good to

534:40

specify The annotation here by

534:42

specifying a function interface but then

534:44

why we are giving so much importance to

534:46

functional interface we'll understand

534:48

that in next video but let's say let me

534:50

create the object for it okay now of

534:52

course I can't simply create a obj equal

534:55

to new a that is not possible because we

534:57

cannot instantiate the interface we can

534:59

do one thing we can create a class here

535:00

called B we can say B implements a and

535:04

we can basically Define this method

535:05

which is public void show and then here

535:08

we can print in show of course you can

535:11

do anything here but just to keep it

535:13

simple I'm saying in show and now

535:15

instead of creating object of a I can

535:17

get object of B which is possible right

535:19

and then I can call all obj dot show

535:21

this is one way the another way of

535:23

implementing this is we have already

535:24

seen which is Anonymous in a class so

535:26

what I can do here is instead of using

535:29

this class here I can just remove this

535:31

entire stuff or maybe I can just comment

535:33

this part and I can come back it by

535:34

saying Hey I want to instantiate a by

535:37

defining its own implementation here

535:39

okay in fact if I just say enter it will

535:42

back it's open close okay so you can do

535:44

this and then here itself we can Define

535:46

the method which is public void show and

535:49

you can Define whatever you want to do

535:50

which is in show so what we are doing is

535:53

instead of creating another class we are

535:54

using Anonymous inner class and we know

535:57

how to use this right and then I can

535:59

print uh show here let's see if this

536:01

works compile and run you can say we got

536:05

in Show

536:06

now since it is a function interface uh

536:09

now we are talking about Java 8 features

536:10

so till this point everything was good

536:13

till Java 7. now in Java 8 they have

536:16

done something awesome you know they

536:18

made sure that Java is no more verbose

536:21

now what is verbose means is specifying

536:23

everything making a detailed code it's

536:26

good most of the time to read the code

536:28

but sometimes you know you need to

536:30

shorten the code to reduce the number of

536:32

lines to reduce the bugs and all those

536:34

stuff so in Java 8 we got this amazing

536:37

feature called Lambda expressions and we

536:40

can use Lambda expression only with

536:42

functional interface now let's try to

536:44

use Lambda expression here so what we

536:47

are doing is we have a functional

536:48

interface and then we have discussed

536:50

about it function interfaces interface

536:51

which only has one method and then we

536:54

were able to instantiate a with the help

536:56

of a Anonymous in a class

536:58

and yeah we were able to call the method

537:01

now how do we use Lambda expression here

537:03

now Lambda expression says we just

537:05

simplify the anonymous inner class okay

537:07

only for Lambda expression now see if I

537:10

tell you okay just try to imagine this

537:11

if I tell you that there's an interface

537:12

called a and there's a method called

537:15

show so in your mind you know there is a

537:17

interface a which has a method show and

537:20

if I ask you to do this if I say try to

537:22

complete the sentence if I say a obj

537:25

equal to now in your mind you know a obj

537:27

equal to means there should be new a

537:30

round bracket 30 packets open and then

537:33

public void show now how you know that

537:36

this is new a because it's a object

537:38

right we're trying to instantiate the

537:39

interface here of course you can't

537:40

instantiate the interface but we're

537:42

trying to instantiate the class of

537:44

interface here a class which implements

537:46

the interface so basically when you say

537:48

aobj this is something you can imagine

537:51

right there is something you can think

537:52

about in the same way now since you're

537:55

talking about the interface a which only

537:56

has one method you also know that the

537:58

method name is show which returns

538:00

returns nothing it's avoid type and it's

538:02

also public by default so from here till

538:05

here you know it right so if you know it

538:09

even Kampala should know this right why

538:12

you as a programmer need to type this

538:13

code what if Java says hey

538:16

don't worry I'm there you just don't

538:19

have to type all those things you just

538:21

say back it open I will understand what

538:23

you're trying to do the only thing is

538:25

you have to put that uh Arrow here

538:27

that's it this is called a Lambda

538:29

expression this Arrow here is a Lambda

538:32

okay and this is the entire expression

538:34

we have okay so just to redo the same

538:37

stuff what we are doing is we are

538:39

removing from here

538:40

till here and since we are removing this

538:43

one curly bracket here we have to remove

538:46

this one as well so first let's remove

538:47

that Curly bracket and then we'll remove

538:50

the entire part from here and then we

538:52

have to put a arrow so by doing this we

538:55

are asking our compiler hey I will not

538:58

type the code I will simply say aobj

539:00

equal to it's your job to complete the

539:02

sentence see behind the scenes a

539:03

compiler will do it for you it's there

539:05

behind the scene new a public void show

539:09

is there it's just that syntactically

539:11

the code is getting reduced this is

539:13

called a syntactical sugar right where

539:15

you are reducing the code okay so this

539:17

is how it looks like and also if in this

539:20

method you only have one statement so if

539:22

you have multiple statements you need

539:23

curdly packets but if you have only one

539:25

statement you can skip that as well if

539:28

you can skip this semicolon as well

539:29

because it's not doing anything

539:31

and you can write everything in one line

539:34

and look at the beauty you can reduce

539:36

the code just like that and I know you

539:39

still have that doubt in your mind will

539:41

it this work let's try

539:43

and it's working you see that this is

539:45

Lambda expression okay can we do

539:47

something more with this of course I

539:49

want you to re-watch this video The

539:51

Cutting part and try it out now let me

539:53

can we do something else here what if

539:54

the show method actually accepts a value

539:57

which is let's say int I okay and then

540:00

from here also you have to pass the

540:01

value the only problem is how will you

540:04

accept the value here now instead of

540:06

making the changes here what I will do

540:08

is I will just go back to our previous

540:09

syntax so that I can show you step by

540:11

step what we are doing

540:13

okay so again I'm passing a integer I

540:16

here and then we have to pass the value

540:17

here now this is not a method which is

540:19

not taking any parameter this we are

540:20

taking a parameter so I have to accept

540:21

it here as well so you have to say int I

540:24

on any variable it doesn't matter and

540:26

then whatever you want to do with that

540:28

variable that's your choice let's say I

540:29

want to use it it's your choice if you

540:31

want to use it or not but I'm printing

540:33

in show I the value whatever the value

540:35

of I is which is 5 in this case let's

540:37

try to run this code to see is it

540:39

working yeah it is working you can see

540:40

we got five now can I use a Lambda

540:43

expression here and the answer is yes

540:44

what you can do is again from here till

540:47

here we don't need that and we can

540:49

remove the study packets as well the

540:50

only thing I have to do is you have to

540:51

put a arrow here we don't need this

540:53

semicolon we can just put everything in

540:55

one line and that's what we have done

540:58

and yes if you even if you have

541:00

variables you can do that not just one

541:01

variable if you have multiple variables

541:03

uh you can put multiple variables here

541:05

let's say a into J as well and then here

541:08

also you can accept into J just it's

541:10

just that you have to pass two values

541:12

let's say five and eight we're not doing

541:14

anything with J but it's your choice if

541:15

you want to use it or not as of now we

541:17

are working with only one variable so

541:19

let's

541:20

stick to that let's pass only one value

541:22

and let's write only one let's see if

541:26

this works compile and run it works you

541:28

can see that we got in show five so

541:31

there's one more thing

541:32

you don't even need to mention the type

541:34

of the variable you know why is because

541:36

we have mentioned it here right when you

541:38

know that the variable type is end why

541:40

to mention it okay

541:42

and one more thing if you have only one

541:45

variable you don't even need this round

541:47

brackets you can simply mention the

541:49

variable name here

541:51

oh we have reduced the code so much

541:54

okay so let's try to compile this code

541:57

and run it works right now let's do

542:01

something more with this now let's say

542:04

what if you have a method which returns

542:06

something what if you have a method

542:08

which accept multiple values so let's

542:09

try to do the example for that in the

542:11

next video but if you see the files here

542:13

okay this is the older one let me just

542:15

delete all the class files

542:18

just a keep it simple delete and let's

542:21

recompile the code here compile the

542:23

moment you compile you can see it is

542:24

creating only two files a DOT class and

542:27

demo.class of course behind the scene

542:29

there is anonymous class but it is not

542:31

creating a class file for you so it's so

542:33

it saves the amount of files which you

542:36

create of course the demo.java file will

542:38

be quite heavy now if you compare the

542:40

file size before using Lambda expression

542:42

after using Lambda expression the demo

542:44

file size will increase but it will not

542:46

create a new file okay so let's files to

542:49

manage okay so that's how we use Lambda

542:51

expression here so let's try to create

542:53

some more methods which returns

542:54

something else so let's say instead of

542:56

creating show let's work with ADD which

542:58

returns a integer value it will take two

543:00

parameters and I and in J right let's

543:03

say we have this and even before we go

543:07

with the Lambda expression let's try to

543:08

create normal objects I will say a obj

543:11

equal to new a and then you can also go

543:14

with the creating a different class or

543:15

we can go for anonymous class now we are

543:17

used to it right and the method name

543:19

which is public int add which accepts

543:22

two value into I comma into J and now

543:26

you want to return I plus J right of

543:30

course we can create a different

543:32

variable we can add we can return but

543:34

again just to reduce the number of lines

543:36

we are just saying return I plus J okay

543:38

and then once you have this we can

543:40

simply

543:41

uh take the value in result is equal to

543:45

obj dot add and we can pass to values 5

543:49

comma 4 okay

543:51

and then we can print result

543:55

simple stuff right now if I go back here

543:58

and if I see if this works compile and

544:00

run you can see we got nine so this is

544:03

working

544:04

now can I use a Lambda expression here

544:06

let's try so first of all let's remove

544:08

the entire part here this is not needed

544:10

and since we are removing this curly

544:12

brackets we can remove this one as well

544:14

so remove

544:16

and remove and then we just have to put

544:19

a arrow here okay just missed one

544:22

semicolon okay so one more thing as we

544:25

know that this is optional I mean we

544:28

only have one statement right so we

544:30

don't need to put curly brackets so we

544:31

can remove that one as well things are

544:33

working

544:34

okay let me just run this before

544:35

removing it to see if everything is

544:37

working compile and run oh it's still

544:40

working and now I can do one more thing

544:42

we can remove this curly packets

544:44

now we can write everything in one line

544:46

as you can see there's one thing

544:48

whenever you have one statement and that

544:51

to the statement is return you cannot

544:54

write return just return so you have to

544:55

remove that as well

544:57

so whatever you mention here actually

544:58

becomes height and type okay so when you

545:01

have one statement which is returned

545:02

which is an expression you can see I

545:04

plus Json expression it becomes a

545:06

written type

545:07

a written value

545:09

one more thing you can remove this ain't

545:10

and int as well

545:12

and you can see the code is so small now

545:15

and that's the beauty of Lambda

545:18

expression and one more thing so let's

545:20

say if you are calling a function or the

545:22

method by passing the object you can

545:24

also pass the Lambda expression in fact

545:27

once we start with the topic of

545:28

collections we'll see that how do we

545:30

pass the Lambda expression as the

545:33

argument okay that will be quite fun so

545:36

yeah so even if you have a method which

545:37

returns a value you can still use a

545:39

Lambda expression there so some points

545:41

remember Lambda expression only works

545:44

with a functional interface if you don't

545:46

have functional interface it will not

545:47

work because if you have multiple

545:49

methods let's say add and subtract both

545:51

and when you write this type of line

545:53

which method you're implementing is it

545:55

add or is it sub that's the confusion

545:57

right and that's why it is not allowed

545:59

so Lambda expression Works only with the

546:01

functional interface

546:03

so yeah that's about a function

546:05

interface which has a method which

546:07

returns something in this video let's

546:09

talk about different types of interfaces

546:12

so when you talk about different types

546:15

what are the options we have so

546:16

basically you know we can divide the

546:18

interfaces so we'll say type of

546:20

interface we can divide interface into

546:22

three parts okay the first one can be we

546:26

can call them as normal interface the

546:28

second one is your

546:31

functional interface and the third one

546:33

is your marker interface now this

546:36

functional interface is also called as

546:38

Sam so why it is called sample discuss

546:40

in some time but let's talk about

546:42

interface here now when you talk about

546:44

interface at this point we know that in

546:46

interface we can only declare methods

546:48

right

546:48

other thing is after Java 8 we also got

546:51

an option of defining the method in the

546:52

interface but we'll do that later at

546:54

this point we only know that in the

546:55

interface we can only declare methods so

546:58

all the methods and interface are public

547:00

abstract right now whenever you create

547:02

the interface which has normal interface

547:05

where where you have multiple methods it

547:07

is called a normal interface so example

547:09

if you create a interface here and let's

547:11

say the interface name is ABC and in

547:14

this interface if you have two methods

547:15

or more that is normal interface so

547:18

whenever you have an interface with two

547:20

or more methods that's normal interface

547:22

and by doing this you might have guessed

547:25

it right what could be the function

547:26

interface now basically before talking

547:28

about function interface let's talk

547:29

about Sam now Sam stands for single

547:31

abstract method okay so that simply

547:34

means if you have the interface and

547:37

let's say the interface name is ABC

547:38

again and in this interface if you have

547:40

only one method that is called a single

547:43

abstract method interface which is Sam

547:45

and we can also call it as a functional

547:48

interface now why this called functional

547:50

that we'll understand in the next video

547:51

when we talk about function interface we

547:53

have a separate video on that and next

547:55

twin we have is a marker interface now

547:57

what is Mark interface a marker

547:59

interface is an interface which has

548:02

no methods that's right a blank

548:04

interface now why someone will create a

548:07

blank interface is to update something

548:09

to the compiler so let's say if you want

548:11

to talk to a compiler by saying please

548:13

allow this example you know uh in Java

548:16

we have this Concept of Civilization

548:18

which means you can take the object and

548:21

you can store the values of the object

548:23

in your hard drive let me repeat you can

548:25

take the object let's say object has

548:27

multiple values so we can create let's

548:28

say in your HEAP memory okay I will do

548:30

that here so let's say in your HEAP

548:32

memory you got an object and this object

548:34

has different variables a b c and this

548:37

has some values let's say a value is 10

548:39

B value is 20 C value study and then if

548:43

you want to save this data in the hard

548:45

drive let's say we have a hard drive

548:46

here and if you want to store this data

548:49

we can use a concept of serialization

548:51

and then you can destroy this object the

548:53

thing is why we are saving these object

548:55

is because what if you want to recreate

548:57

this object later so let's say when

548:59

you're playing a game and when you're

549:00

playing this game you want to save the

549:01

game now now when you save the game

549:03

let's say when you're playing this game

549:04

the game takes around 4GB or 5gb in your

549:07

RAM but when you save the game it will

549:09

not consume that much of memory right it

549:11

will consume around in KBS it's because

549:13

when you restart the game after closing

549:16

it of course when you start the game you

549:18

will get a default values now if you

549:20

want the old values you want to resume

549:22

the game you will simply load the

549:25

current stats so let's say after some

549:28

time you close the game now game is no

549:30

mode but you have a file in your hard

549:32

drive and now you just started the game

549:34

the value for ABC will be zero zero zero

549:37

what you can do is you can load these

549:39

values from here now this is called

549:41

deserialization so this is

549:45

serialization and this is

549:47

deserialization okay now by default

549:50

every object is not allowed to do that

549:53

so it is not allowed okay you have to

549:55

give the permission how will you give

549:56

the permission so you can simply create

549:58

a class and you can give the permission

550:00

with the help of Market interface so we

550:02

have a lot of Market interface in Java

550:03

uh serialization or serializable then

550:06

when you talk about the advanced part

550:08

also we have some interfaces but it's

550:10

it's important okay so whenever you see

550:13

an interface which is blank that's a

550:15

marker interface and maybe in the

550:16

upcoming videos when you talk about some

550:18

Frameworks we'll talk about different

550:20

marker interfaces but that's what it is

550:22

uh so what is normal interface which has

550:25

more than two methods or two or more

550:26

than two methods the functional

550:29

interface or Sam only has one method now

550:32

this is actually important for the

550:34

upcoming videos and try to understand

550:36

this every time you have an interface

550:37

which has only one method uh we can use

550:40

some extra features with it in this

550:42

video we'll talk about

550:44

exceptions so before you understand

550:46

exception we have to first understand

550:48

different types of Errors so basically

550:49

when you talk about errors we have

550:52

different types for it the first one is

550:54

your compile time error now what this

550:57

errors are the second one you have is

551:00

you have runtime errors and the third

551:03

one is logical error now how do we

551:06

describe this let's say if I write a

551:08

statement here and if I try to print

551:11

something and if I make a spelling

551:13

mistake let's say if I make this as a

551:14

capital P of course your IDE gives you

551:17

an error here but even if you compile

551:19

this code you will get a compile time

551:21

error so it says either cannot find

551:23

symbol print now this type of errors are

551:26

actually easy to solve because your

551:27

compiler will show you hey you know you

551:29

make a you made a mistake here so this

551:31

is capital P this will not work so you

551:34

will know okay uh I have to make it

551:36

small and then it will work right of

551:39

course when you compile this code you

551:40

will not find any error now so when you

551:42

get error like this this is syntactical

551:44

errors and also a compile time error

551:48

which are very easy to solve the second

551:51

time of error we have is run time what

551:53

is run time without now in fact let's

551:54

talk about logical first and then we'll

551:56

come back to runtime now logical error

551:58

means everything is working there is no

552:00

compile time issue everything is working

552:02

and then you what you did is you added

552:04

two numbers let's say you added two plus

552:06

two and of course for this the output

552:08

you were expecting is for and the output

552:11

will be full and that's very simple

552:13

right but what if you did some

552:14

calculation not a simple code like this

552:16

you did a multi-line of code where the

552:19

output is not four output is five now it

552:22

is working there is no compile time

552:23

issue there is no runtime issues not

552:25

like at runtime it is getting stopped

552:27

but the output you are getting is wrong

552:29

right so you got five where you wanted

552:32

four now this type of others are called

552:34

logical errors also sometimes we call

552:35

them as bugs so even this type of

552:37

problems are actually difficult to solve

552:39

because you need to do a lot of testing

552:41

you know if you heard about this

552:42

whenever you make an application you

552:44

have to test your application as well so

552:46

in that case you will do testing you

552:48

will check for the inputs and outputs

552:50

and everything

552:51

so maybe you will able to solve this

552:53

problem but the thing is

552:54

from the developer side you do check

552:56

compile time issues which compiler will

552:58

help you there you do proper testing

553:00

where you can solve logical issues now

553:01

once everything is tested you can ship

553:03

your product to the client and client is

553:05

happy there is no compile time issue

553:07

there is no logical issues now runtime

553:09

issues are a bit different the problem

553:11

is in runtime issues what is what is

553:13

happening is everything is working I

553:15

mean compile time issue no problem your

553:17

code is working as well but suddenly

553:19

because of some reason the code just

553:22

stopped working you got errors okay uh

553:26

basically you know whenever you get

553:27

errors one time others your execution

553:29

normally stops and that's an issue let

553:31

me get an example here so let's say uh

553:34

you made an application where you your

553:37

application opens a file a txt file from

553:40

a C drive okay or maybe a home folder in

553:42

Unix system till this point it was

553:44

working because the file was there and

553:46

now let's say uh you deleted that file

553:49

so yesterday night you are doing

553:50

something some experiment with your

553:51

system and then the file is no more and

553:54

now today when you are trying to use the

553:56

application application is trying to

553:58

find the file the file is not there it

554:00

will give you a runtime error the

554:04

execution will stop in between now you

554:06

will say what's the big issue right you

554:07

can restart the application

554:09

yes you can and everything will work

554:11

provided the application is not that

554:14

critical if you are using a calculator

554:16

and calculator stops in between that's

554:19

okay you can reopen it when you're

554:20

playing a game and suddenly The Game

554:23

Stops

554:24

you can do it but let's say you are into

554:26

a game battle where where you will get

554:28

let's say one million dollars if you win

554:30

and suddenly your game crashes now we

554:33

don't want that right uh we can also go

554:35

for some applications which are the

554:37

critical example let's say if you talk

554:39

about medical softwares you have all

554:42

those scanning machines and then which

554:43

where you go inside the machine and then

554:45

it scans right it throws some place or

554:48

some things I'm not sure about medical

554:50

terms but what if the software just

554:52

stops in between risky what if you're in

554:55

a plane and then the plane software

554:57

stops working very critical right so you

554:59

have to make sure that even if you get

555:01

the errors runtime errors it should not

555:03

stop your application I'm not saying

555:05

they will not be a runtime error I'm

555:07

just saying we need to handle the

555:09

runtime errors so this runtime errors we

555:12

can call them as exceptions and

555:15

basically we need to handle the

555:17

exceptions okay so handling the

555:18

exception is very important and that's

555:20

what we are going to do in this set of

555:22

videos so in this video we have talked

555:24

about what is exceptions exceptions are

555:26

basically one time matters it's not like

555:27

we are going to stop the entire metals

555:29

of course we'll try to check for the

555:31

conditions you know if when you try to

555:33

open the file first you have to check is

555:35

the file exists if the file exists then

555:37

you open try to open the file those are

555:39

the things you can avoid runtime errors

555:41

let's say you're dividing a number by

555:43

zero so when you divide a number and if

555:45

you divide by zero it will give you

555:46

exceptions

555:47

so even before divide if you can check

555:49

if the denominator is non-zero then

555:52

divide so those are the checks we can do

555:54

but in case if there's exception you had

555:56

to you have to handle it now let's try

555:58

to handle the exceptions now we know

556:00

what is exception let's try to handle

556:02

them so what I will do here is first of

556:04

all let's create exceptions right that's

556:05

why then we'll then we'll know how to

556:06

handle the exception now even before we

556:09

talk about handling the exception what

556:12

type of statements we need to handle so

556:14

for that what I will do is let's talk

556:16

about statements since statements can be

556:18

of two types test statement can be of a

556:21

normal statement and a statement can be

556:24

a critical statement okay see in real

556:26

life as well we try to think about

556:28

situations which are normal and we try

556:30

to think about situations which are

556:32

critical example let's say if you're

556:33

walking with a kid on the street and or

556:36

let's say in the mall or in the

556:37

restaurant and then of course you will

556:39

grab the hand of kid right but what if a

556:42

kid just runs in that case if it's a

556:44

restaurant you will not be that Panic

556:46

right you will try try to say okay he's

556:48

roaming around and then you will try to

556:50

catch the kid but what if the same

556:53

situation happens on the street a busy

556:55

highway where you are holding the hand

556:57

of a kid but kids try to run in that

556:59

case you will jump to grab the kid

557:01

because that situation is critical in

557:03

the same way when you talk about

557:05

statements it can be normal statements

557:06

and critical statement example if I

557:09

write a simple statement here which is

557:11

int I is equal to 9. now this statement

557:14

is normal statement because we know what

557:16

what may go wrong here everything looks

557:18

good right so this is a normal statement

557:20

but what if you say ain't J is equal to

557:23

I divided by or maybe I can say 8

557:27

divided by I now this statement can be

557:29

critical if you look only this statement

557:31

now why this is critical is because if

557:33

the value of I is 9 that's completely

557:36

fine you will get the output example if

557:37

I just come back here and if I try to

557:39

print the value of J in fact you know

557:40

what I will do I will just make this 18

557:42

so that it will get some output instead

557:43

of 0. compile and one this works right

557:46

but what if the value of I is not 9 it's

557:50

0. now in this case first of all let me

557:53

just create the screen so that we can

557:54

see the output properly and you can see

557:56

we got an error it says exception

558:00

exception in thread main uh which is the

558:04

exception is this arithmetic exception

558:06

so if you can see it's arithmetic

558:09

exception and the message is divided by

558:11

zero now the moment it happens it

558:13

actually stops the execution let me show

558:16

you that if I try to print by here you

558:19

will see if I try to run this code

558:21

and run you can see it is not printing

558:24

by the result is not printing by is

558:27

because at this point the execution has

558:30

just stopped okay so that's why it is

558:33

always better to handle the exception

558:35

even if you know nothing may go wrong if

558:39

you think that's critical handle it how

558:41

that's a question to handle the

558:43

exception what you can do is you can put

558:45

this type of statements in some other

558:47

block now first of all let me just

558:49

declare the variable here by zero and

558:51

then

558:52

this is a statement which is critical

558:53

right so what I will do is I will just

558:55

put that in a separate block so I will

558:58

put that in a separate block some

558:59

something like this and now what I'm

559:03

saying is hey Java just try to execute

559:06

this code okay if it works that's fine

559:08

otherwise execute the other part don't

559:11

stop the execution the way you can do

559:13

that you can say hey Java try to execute

559:15

this statement so I can say try try to

559:18

execute okay Java will say okay uh now I

559:21

know that you are just trying to execute

559:23

if not it's not working then I will

559:24

continue with the normal execution here

559:26

but then what if Java tries to execute

559:29

this and there's a error in that case it

559:31

will throw the error and you as a

559:33

developer you have to catch the error so

559:36

in this case you will catch the

559:38

exception the moment it throws the

559:40

exception it is showing you an object

559:42

and you have to accept it as an object

559:44

so you will create an object or you will

559:47

refer that with an exception class so

559:49

this exception here is a class and you

559:52

are referring that with any object will

559:54

do it's not just a you can say obj as

559:56

well but most of the time you will say e

559:58

because it makes sense right exception e

560:00

you can say obj as well that perfectly

560:02

works but let me say e here and if you

560:05

want to print something you can do that

560:07

here so whatever handling stuff you want

560:08

to do you can do that in the catch block

560:10

so I will simply say something went

560:13

wrong okay so what I'm doing is I'm

560:16

trying to handle the exception if it

560:19

works that's great if it is not working

560:21

it will execute the catch block now

560:24

let's say the value of I is not zero the

560:27

value of I is let's say 4. in this case

560:29

you can see there's no exception so

560:31

let's try to execute the s with the

560:33

value 4

560:34

and if I run this code you can say it

560:36

says 4 and buy there's no problem with

560:38

the value we got 4 because of this

560:40

statement and we got by of course

560:43

but then what if something will go wrong

560:45

what if the value of I is 0. now in this

560:48

case this will throw the exception so it

560:52

will execute the catch block and I'm

560:54

expecting it should print something went

560:56

wrong let's try compile and run oh you

560:59

can see that it says something went

561:01

wrong now even if something goes wrong

561:03

the execution is still happening the

561:06

value of J is 0 because before the

561:08

execution of this try catch the value of

561:11

J was indeed 0. right that's how you

561:14

handle the exception and again you don't

561:16

have to put all the statements here you

561:18

can you can put normal statements in try

561:20

as well that completely works but it's

561:23

better to put only critical statements

561:25

inside my blog and you can write

561:27

multiple try blocks uh so one we have it

561:29

here what if we have some few more

561:31

statements here you can try catch that

561:33

as well that that works so I hope you

561:35

got the idea what is tried try will

561:37

basically try to execute these

561:38

statements if everything goes good it

561:40

will execute the remaining statement if

561:42

something was wrong if there's exception

561:44

it will execute the cache block so Point

561:46

remember the cash block will be executed

561:48

only in case of exception otherwise cash

561:52

block will be skipped so that's it from

561:54

this where we talked about try catch and

561:56

we have seen how to handle the exception

561:58

okay so we know that to handle the

562:01

critical statement we have to use try

562:03

block and inside that if something goes

562:05

wrong it will execute the cash block

562:06

otherwise it will simply execute the

562:08

remaining statements okay that that

562:10

makes sense right now what I will do is

562:11

I will try to print the exception as

562:15

well so that's why you can actually

562:16

print the exception so we are saying

562:19

something went wrong let me just remove

562:20

one dot from here so that we can save

562:21

some space and after this I want to

562:24

print the exception as well I want to

562:27

see the message what it says

562:28

so to do that what I will do is I will

562:30

compile this code and run you can see it

562:32

says something went wrong okay that

562:34

because we are printing that apart from

562:37

it the message which you are printing

562:38

here which is e it says arithmetic

562:41

exception oh okay that means apart from

562:44

exception we also have arithmetic

562:46

exception cool that also makes sense

562:50

so just try to remember this point that

562:52

apart from exception there is one more

562:54

class here which is called arithmetic

562:56

exception we'll see that in some time

562:57

now what I will do is I will try to

562:59

create some more exceptions here one of

563:01

fit is this if I create an array let's

563:04

say the array is of type integer and

563:06

let's send nums and I will say the size

563:09

of this array is let's say five now in

563:12

this case when you have a size five of

563:13

course by default all the values are 0

563:16

of this nums and you can fetch the

563:19

values let's say at this point I'm

563:21

trying to print the value of nums of 0

563:25

or 1 let's say one of course it will be

563:27

zero because the values are zero and I

563:29

will also try to print the nums of five

563:33

now see when you say nums are 5 what

563:35

value you will get now first of all the

563:37

size of this array is 5

563:39

and then if you are saying you're trying

563:42

to fetch one that means you're fetching

563:44

basically second element because the

563:46

index number starts with zero so zero

563:48

one two three four oh so for the five

563:52

elements the index number ends at four

563:54

and we are trying to get the value from

563:57

five okay so basically we are going out

564:00

of bound okay so let's let's see what

564:03

happens if we do that uh first of all I

564:05

just want to make sure that I don't have

564:07

this exception 0 here or maybe let's

564:09

let's keep it zero I will say compile

564:11

and run of course we will get that error

564:13

because uh the dividing number which is

564:15

I is still zero let me just make it two

564:17

see the problem is the moment you get

564:19

exception here it will directly jump out

564:22

of this try block it will not even

564:24

execute the remaining price statements

564:25

okay and that's why we have not seen

564:27

those outputs now what I will do is I

564:29

will just try to compile this once again

564:31

and run

564:32

this time if you can see we got a

564:34

different exception yeah it says

564:36

something went wrong that actually makes

564:37

sense because it is executing the cash

564:39

block but look at the exception the

564:42

exception is add a index out of bounds

564:45

exception which means the size of your

564:47

array is limited and then you are trying

564:49

to access the element outside it and it

564:52

says that it says index 5 is out of

564:54

Bound for the length five okay uh makes

564:58

sense so basically apart from automatic

565:00

exception we have other index outer

565:02

bound exception as well but I have only

565:04

one issue the issue is why we are

565:06

printing something went wrong see you

565:08

know you try to show the message to the

565:10

user as well and if you can see the

565:12

actual the problem is with the user what

565:14

if the value of I is coming from the

565:16

user what if the value of this 5 here

565:19

numbers of 5 is also coming from the

565:21

user now user is not sure what is the

565:24

problem here it says something went

565:25

wrong you have to be very specific here

565:27

you have to say Hey you cannot divide

565:29

the number by zero so user will know

565:31

okay the values which I'm entering is

565:32

actually wrong I should not be entering

565:35

a denominator which is zero okay or

565:37

maybe you can say you know you're trying

565:39

to access the element outside the array

565:41

the user will know that you know you

565:43

have to be in your limit I want to print

565:44

a customized message here so if I print

565:47

here something like this if I say cannot

565:49

divide by zero that looks like a good

565:52

message okay let's not print this a e

565:55

anymore let's only print cannot divide

565:57

by zero and let's see what happens okay

565:59

clear and I will keep the same error the

566:01

other is still with the array not with

566:03

the denominator but if you run this code

566:05

you can see it says cannot divide by

566:07

zero no that's weird because I'm trying

566:09

to fetch the value which is outside the

566:11

limit and it says cannot divide by zero

566:13

that's wrong this is the error makes

566:16

sense when the value of I is 0 right so

566:19

if I compile this code and run now this

566:22

works you can say it says cannot divide

566:23

by zero but then if you if the value of

566:25

I is not 0 in that case as well it is

566:28

printing cannot divided by zero it's

566:30

because the moment you get the the

566:32

exception is in dry it can be from this

566:34

statement or this statement it will

566:37

execute the same cash block and in the

566:40

same cash block we have the same message

566:42

see what you can do is before printing

566:44

you can actually check if you can check

566:46

the type of this e you know is e

566:49

arithmetic or is e either index Auto

566:51

bound and then based on that you can

566:53

print a different message that's one way

566:55

or what you can do is you can ask your

566:58

Java itself hey Java I want you to

567:00

handle different exception so you can be

567:02

very specific you instead of saying

567:03

exception I can say it is arithmetic

567:07

exception now I'm being very specific

567:08

here I'm saying I'm not talking about

567:10

exception I'm talking about arithmetic

567:12

exception when arithmetic exception

567:14

arises print this message but what if

567:18

there is an array index out of bound

567:19

exception let's see if we are able to

567:21

handle that so if I compile this code

567:22

now okay first of all let me just make

567:24

this I as 0 and let's see what happens

567:26

again I will compile and run you can say

567:30

it says connector by zero that makes

567:31

sense because we are able to handle the

567:33

exception and you can see we are

567:34

printing buy as well that means we are

567:36

handling the exception

567:38

but if you make this as two and if you

567:40

try to compile this and run oh you can

567:43

see that we are not printing by this

567:45

exception you can see it's a there's a

567:46

cross symbol as well it says other index

567:48

autobound exception that means we are

567:50

not handling it even if you mention that

567:52

in the try block if you don't have a

567:54

catch block for it that means you're not

567:56

handling it how do we handle that so in

567:59

that case what you can do is you can

568:00

write multiple catch block so you can

568:02

write catch here and in this sketch you

568:04

can mention add a index out of bounds

568:07

exception that's right we have to create

568:08

one more catch Block in which you'll be

568:10

having a different type of exception

568:13

and here we can print stay

568:16

in your limit again I'm just trying to

568:19

be witty here but you can print a

568:21

appropriate message whatever you want to

568:23

uh now if you try to see what happens if

568:26

I compile this code once again

568:28

and one you can see it says stay in your

568:31

limit there's no abnormal exit of the

568:33

code it is executing everything and this

568:36

time it is executing the cache block the

568:38

second cache block not the first one but

568:40

what if you make this zero then

568:42

and again it goes in sequence okay so in

568:44

try block it goes in sequence it will

568:45

first execute this statement which may

568:47

throw arithmic exception and if that

568:51

works well it will go for this one this

568:53

might do an exception which is at the

568:55

index.bound exception so let's see if

568:57

that works compile and run you can see

568:58

it says cannot divider zero so you can

569:00

basically have two different exceptions

569:02

here now what if there is another type

569:04

of exception here which we are not sure

569:06

about you know there are a lot of

569:07

exception classes it's not like we only

569:09

have these two in fact in the next video

569:10

we'll try to understand some of the

569:12

famous classes or famous exception

569:14

classes we'll try to work on that but at

569:16

this point there are multiple exceptions

569:18

and we are handling only two you know

569:21

it's always a good idea to handle all

569:22

the exception but we are not sure what

569:24

exception it can arise so in that case

569:27

you know to be on the safer side at the

569:29

end you can handle something with

569:31

exception now exception handles

569:34

everything if you remember the concept

569:36

of in heightens where you have a parent

569:37

class child class so exception is a

569:40

parent class it can handle all the

569:43

exceptions okay now you can say

569:45

something went wrong because even you

569:46

are not sure what went wrong so you can

569:48

say something went wrong now if there's

569:50

some some exception which you are not

569:52

handling in that case if you execute

569:54

this part

569:54

to give an example let's say I create a

569:57

string here string Str and let's say the

569:59

value of Str is null as of now and body

570:01

fight will be null and then here what

570:04

you're doing is you're trying to get the

570:07

length of this string so what you can do

570:09

is you can print string SDR dot length

570:12

now if you try to find a length on a

570:16

null string it will give you a different

570:19

type of exception first of all I just

570:21

want to remove this exception I don't

570:22

want exception on line number 13 I just

570:24

want it to work so that it will execute

570:27

this line which will give you exception

570:28

and let's see what happens if you say

570:30

clear and compile and run it says

570:33

something went wrong now what went wrong

570:35

is because there's a new type of

570:37

exception in fact here it's a good time

570:39

to actually print e because it should

570:42

print something some exception so that

570:43

you'll understand I'll try to print this

570:45

and you can see it says something went

570:47

wrong because that's what we are doing

570:48

here

570:49

and then it prints the exception which

570:52

is null pointer exception now this is

570:53

one more exception like this we have a

570:55

lot of exceptions

570:56

and we'll understand that in the next

570:58

video but then before that we have one

571:00

more thing what should the sequence here

571:01

the sequence is whenever you have a

571:03

child and parent make sure that your

571:05

parent is at the bottom so you can say

571:08

exception is a top class right parent

571:10

let me show you how it looks like so if

571:12

I go to Ultimate exception now

571:14

arithmetic exceptions are class which

571:15

extends runtime exception so runtime

571:18

exceptions are present class for

571:19

arithmetic but if I click on runtime

571:21

exception you can see it extends

571:24

exception here so exception is a patent

571:27

of parent and in fact this exception

571:30

actually extends through able so there's

571:31

one more class called through able which

571:34

is like the topmost class and yeah so

571:36

that's how you basically the hierarchy

571:38

looks like in fact I will show that in

571:39

the next video as well but make sure

571:41

that you have at the end but if you try

571:43

to keep this on top here it will create

571:46

an issue because the exception can

571:48

handle everything right at this point if

571:50

you get the exception it will try to

571:52

search for a catch which can handle this

571:53

exception and it looks at Exception by

571:56

saying hey exception can handle

571:57

everything right then why we even need

572:00

this too and that's why it says you know

572:02

it says this exception cannot it's

572:04

unreachable cancel that message there it

572:06

is unreachable because everything is

572:08

handled by this exception so it's a

572:10

practice or it's compulsion to have the

572:12

parent class at the end now as we have

572:14

seen exception is a super class right

572:17

and in that we have a lot of subclasses

572:19

now let's try to understand the entire

572:21

hierarchy in fact we'll not talk about

572:22

all the classes let's try to give you

572:24

the general idea and with this idea

572:26

we'll understand okay what class belongs

572:28

where and which exceptions are

572:30

compensory to handle and which are not

572:32

okay that's important point to remember

572:34

so on top we have the object class now

572:37

why it's on top because every class in

572:39

Java extends object even if you don't

572:41

mention it's there right so give let's

572:42

give the respect to object class now

572:44

below that we have one class called

572:46

throwable now if you remember while

572:48

searching for the class uh we have seen

572:50

exception class extends variable so

572:53

that's on top one more trivia here most

572:55

of the names which ends with able are

572:57

actually interfaces okay runnables

573:00

realizable cloneable but coming to

573:02

exception tribal is also able at the end

573:05

but it's a class okay that's the only

573:06

exception which I have seen till now now

573:08

below this we have two and okay that

573:11

sounds weird because we have only some

573:12

exception right so we have exception

573:14

here and apart from this we have one

573:17

more which is called error okay now why

573:20

we have not talked about error yet we

573:21

are only talking about exception the

573:23

thing is when we talked about runtime

573:24

exception we have mentioned that it is

573:26

something you can handle okay so

573:28

exceptions are something which you can

573:30

and you should handle others you can't

573:33

okay when the others hit it will simply

573:35

stop the execution normally it doesn't

573:36

happen but if it happens you can't do

573:39

anything with that example of this it

573:41

can be the thread that what if your

573:44

threat dies example of this can be i o

573:47

error something goes wrong with the

573:48

input output we have few more which is

573:51

your virtual machine error it can be one

573:55

of the example I can give you here is

573:56

what if you have out of memory so what

573:58

if your memory is not there so what how

574:00

can you handle that okay so this type of

574:02

Errors you can't actually handle so this

574:04

is something it's there it will not

574:06

happen but we cannot handle that even if

574:08

it happens what you can handle is

574:09

exception now in exception as well we

574:12

have multiple options here so we have

574:14

the first one which is runtime exception

574:16

so we have the subclass of exception is

574:19

one-time exception and on the other hand

574:22

we have a lot of different exceptions

574:23

here example of this could be a skill

574:26

exception example of this could be IO

574:28

exception and many more but we are just

574:30

trying to understand the simple one so

574:32

we have these two uh we have many more

574:33

as I mentioned now in runtime exception

574:36

also we have lot of different exceptions

574:37

here we have seen most of it in fact we

574:40

have seen a arithmetic exception uh then

574:44

we have add a

574:46

index out of bounds exception and then

574:49

we have null Point exception now all

574:51

this comes on the runtime exception this

574:53

is an null Point exception now the thing

574:54

is why you have to remember this thing

574:56

not everything you have to remember but

574:58

few of them see all these runtime

575:00

exceptions are actually called unchecked

575:03

exceptions and all the other here on on

575:06

this side they are called checked

575:08

exception now why they are unchecked and

575:10

checked most of the time when you write

575:11

the line your compiler will not force

575:15

you to handle the exception okay it will

575:16

say hey it's your choice to handle the

575:18

exception so all the runtime exception

575:20

all this all the classes here it's your

575:22

choice if you want to handle them or not

575:23

but whenever your compiler sees that

575:25

there is a line which can raise a

575:27

exception which is not runtime exception

575:29

example it can raise uh SQL exception IO

575:32

exception in that case it will ask you

575:34

compulsory to handle the exception

575:36

if you remember when we were trying to

575:39

load the class with the help of class

575:40

dot for name on the right side of the

575:43

method we have mentioned it throws the

575:45

io exception right so it was forcing you

575:48

to handle it okay okay we have not

575:50

talked about those yet but we'll talk

575:51

about it so basically uh these are all

575:54

checked and this are all unchecked here

575:56

and there's something you should not be

575:58

worrying about it will not happen most

576:00

of the time and this is something you

576:02

have to handle by yourself so I hope

576:04

this diagram makes some sense this is

576:07

the top class which is there in every

576:09

Java code and this is the authorable

576:12

which is a parent class of exception and

576:14

error in exception we have lot of

576:15

different exceptions if you get SQL

576:17

exception in your code you have to

576:19

handle it compulsory I have exception

576:21

compulsion apart from this if you have a

576:23

runtime exception it's your choice to

576:24

enter it or not but as a good programmer

576:26

if you want to keep your client happy

576:28

please handle the exceptions now let's

576:30

try to to understand a keyword called

576:33

Throw okay so basically we have two

576:35

keywords here one is throw second is

576:37

throws we'll see them one by one so what

576:40

I will do here is first of all I will

576:41

just reduce the number of lines we have

576:43

so that it will be easy for us to

576:44

understand I will remove all the

576:46

exception okay I will stick to one which

576:49

is divided by zero and let's put only

576:51

one statement here let me also remove

576:53

this one because not needle we only want

576:56

to exception arithmetic and if something

576:58

goes wrong which we have not handling uh

577:01

that will be handled by exception

577:03

okay so let's say when you divide this

577:05

number of course the value of I is 2 so

577:07

everything is good and you know most of

577:09

the time people will put curly brackets

577:11

on the same line and it normally works

577:14

and this basically preferred okay so

577:16

let's say when you do this execution of

577:18

course there's no exception here because

577:19

we are dividing by two right so if I try

577:22

to compile this code and run everything

577:25

is smooth you you can see we are

577:27

dividing the number 18 by 2 which is

577:30

nine and that's what you got here

577:31

because we are printing the value of J

577:33

and then we are printing by everything

577:34

is normal now the thing is what if I'm

577:37

dividing this number by let's say 20.

577:40

now in this case when you have a greater

577:42

number of course you can divide 18 by

577:44

20. now the question here would be zero

577:48

if you can see the value is 0 and

577:50

there's no exception but what if you

577:53

want to create or you want to generate

577:55

an exception maybe you are doing

577:57

something here let's do this thing let's

577:59

say the value of I is zero so of course

578:00

you will print cannot divide by zero now

578:02

instead of printing cannot divide by

578:03

zero what you can do is let's skip this

578:06

part let's say let's not print any error

578:07

let's handle the error if someone is

578:10

trying to divide a number by zero I want

578:12

to divide the number by 1 18 divided by

578:16

one okay so what I'm doing is I'm not

578:19

printing anything I'm just trying to

578:20

handle it it will print some output so

578:22

if I compile and run you can say it says

578:24

18 okay I don't want to print

578:28

added or something if someone enters a

578:30

denominator 0 I will say maybe I can

578:32

also print here that's the default

578:35

output so that if it prints 18 it will

578:38

also print that's the default output if

578:40

you can see if I do that so it will say

578:42

that's a default output it will print 18

578:44

okay now what I want is every time this

578:48

exception it should do this thing so

578:50

it's not just about printing the message

578:51

you can actually handle something

578:52

example let's say if you're trying to

578:54

connect to an internet and then the

578:55

network is not there of course it will

578:57

throw the exception so instead of

578:59

sending a message to the user we were

579:02

not able to connect to the server you

579:03

can simply retry right so what you're

579:05

doing is you're trying to handle the

579:06

exception here we just tried of course

579:09

this is not a good logic I'm just trying

579:10

to make you understand how we can do it

579:12

one more example let's say you have two

579:15

databases one is a primary database

579:17

second cell backup database you are

579:19

trying to connect to a primary database

579:21

and something goes wrong of course you

579:22

will not simply tell user hey you know

579:24

we are not able to connect to the

579:25

primary database you will simply try to

579:28

connect with the backup database and

579:30

that's what you can do in the catch

579:31

block that's what we are doing here with

579:33

this logic now let's say you are

579:35

dividing a number by 20. now in this

579:37

case it will print zero okay but what if

579:40

you don't want to print 0 you want to

579:42

print the default output is 18. maybe

579:45

you won't do that that means what I'm

579:46

saying is even if the catch block is not

579:48

getting called I want to call the catch

579:50

block because in the catch block you are

579:52

basically trying to handle the exception

579:53

and if you don't have the exception it

579:55

will not call the cash block that is not

579:57

something we want so to solve that

579:59

problem what you can do is you can

580:01

basically check if the value of J is 0

580:06

okay because we don't want zero in this

580:08

case if the value of J is 0 you can

580:10

basically call The Catch block but how

580:12

do you call the catch block catch is not

580:13

a simple function right where you can

580:14

write cats by passing a message node

580:16

it's not a normal function or normal

580:18

method what you have to do is you have

580:19

to basically use a keyword called Throw

580:22

and you have to create an object of

580:25

arithmetic exception so you can do that

580:27

you can simply say new

580:28

arithmetic exception it's a class right

580:30

you can create the object it's that

580:32

simple now what you're doing is you're

580:34

basically trying to create an error here

580:36

you're trying to throw the error so this

580:38

is the exception you got you got the

580:40

object and you are throwing now since

580:42

you are throwing catch will catch it

580:45

okay let's see if this works compile no

580:47

issue Run Okay so that it says that's

580:50

the default output it is printing 18. so

580:53

you got zero here but then we are

580:55

checking if it is 0 then throw the

580:57

exception

580:58

okay uh maybe you want to connect to

581:01

another database which you have written

581:03

the code in the catch block so you can

581:04

do that here okay you can literally call

581:06

The Catch block with the help of throw

581:08

keyword there's one more thing what if

581:10

you also want to print the message

581:12

somewhere here you want to print e Now

581:15

by default if you try to compile this

581:17

code and run yes it will say a rhythmic

581:19

exception but you can see it is not

581:21

printing the message I want the message

581:23

as well remember it was printing slash

581:25

by zero or divided by zero I want to

581:27

print such messages so you can actually

581:30

pass a message in the Constructor you

581:32

can say I don't want to print zero okay

581:37

something like that whatever your

581:38

message you want to send you can do that

581:40

in the Constructor so you can say it

581:42

says this is your message I don't want

581:44

to print zero so if you want to send a

581:46

message put that in a Constructor it

581:48

will do it for you don't worry you don't

581:50

have to manually go and type the message

581:52

so whatever message you have every time

581:54

you create this object you can pass the

581:55

message and that's how we can use a

581:57

through keyword so so just reiterate row

582:00

keyword is used to throw the exception

582:02

okay and then catch block will be

582:04

catching it now basically we were able

582:06

to throw the

582:07

throw that which is arithmetic now

582:10

to the exception and what if you want to

582:12

throw your own exception let's say I

582:13

don't want to throw ultimately exception

582:15

I want to throw my own exception can we

582:17

do that can I say my exception you can

582:20

see uh straightforward it says my

582:22

exception cannot be resolved that's why

582:23

we don't have any class called my

582:25

exception but I want my own exception

582:28

here or maybe I can say issue of my

582:30

exception I can say Naveen exception you

582:32

know what Naveen actually means new okay

582:34

so it's a it's a term which means new

582:36

okay anyway so I want to have a new

582:39

exception here the way you can do that

582:40

is by creating a class which is called

582:43

Nomine exception and done oh okay we

582:47

have to also create a Constructor

582:49

because we are sending a message so what

582:51

I will do is I will create public now in

582:53

exception which accepts a string Str

582:57

okay string string that works for me

583:00

and still is not working it says no

583:04

exception of type novel exception oh I

583:06

have created a class right the class is

583:08

there what's the problem I have created

583:09

my own exception and it's not accepting

583:11

it see the thing is you can't simply

583:13

create a class and call it as exception

583:15

see exception needs special features

583:17

right and we all know if you want to get

583:19

something you have two choice you can

583:20

actually build something or you can just

583:22

get it from your parents so I can simply

583:24

say extends exception so what we are

583:26

doing is we are getting all the features

583:28

from the exception okay so you can say

583:32

now in exception extern exception and

583:34

now it is not crying okay now the only

583:37

thing is if you try to compile this code

583:40

okay not this one compile first and run

583:43

it says something went wrong oh that's

583:46

weird why something went wrong I want

583:48

actually to print oh okay the thing is

583:50

you are doing an exception but you are

583:53

not catching of an exception I want to

583:54

print this one that's a default output

583:57

so what you can do is in shop knowing

583:58

automatic exception you can say nothing

583:59

exception here and you can accept the

584:01

object and then you can print this

584:02

message that's the default output and

584:05

also you want to print e to get the

584:06

message the message is this I want to

584:08

print something now if I try to compile

584:10

this code again and run oh it works can

584:12

you see that it says that's the default

584:15

output and the error is Naveen exception

584:18

okay that's good where is this message I

584:20

don't want to plane zero the thing is

584:22

the message which you have sent actually

584:24

is received here but this output when

584:26

you're trying to print a here is

584:28

actually handled by the exception class

584:30

okay this should know that you are

584:33

accepting a string so what you can do is

584:35

if you go to exception even exception

584:37

has a Constructor which takes a string

584:40

and we all know how to call a superclass

584:43

Constructor we have seen that right so I

584:44

can simply say super and pass this

584:46

message so whatever message you are

584:48

accepting here just pass the message to

584:50

the exception and they will take care of

584:52

it you don't trust me let me show you

584:55

you can see that it says Naveen

584:56

exception and it prints the message as

584:59

well the message is I don't want to

585:01

print zero so that's how it works you

585:03

can create your own exception so just

585:05

reiterate now in exception is not a part

585:08

of java I'm creating my own exception

585:10

and yes you can do that the only thing

585:12

is you have to create a class with the

585:14

exception and this class need to extend

585:17

exception or runtime exception your

585:19

choice let's try with one-time exception

585:21

and if I clear this

585:23

compile run it works so you can extend

585:26

exception or runtime exception and the

585:28

only thing you have to do is if you're

585:29

accepting a message you have to pass the

585:31

message to the runtime exception

585:33

Constructor and that's how it works now

585:35

in this video let's talk about throws

585:37

keyword so we have talked about throw

585:39

keyword now it's time for throws now

585:42

this is not a plural form of throw it's

585:43

not like if you want to throw multiple

585:45

exceptions you will be using close a lot

585:47

of people have this confusion those has

585:50

a different working so before you

585:51

understand those what I will do is let's

585:53

talk about a scenario let's say you are

585:56

writing a method so let's say the method

585:59

name is D now of course in this method

586:01

you will write certain statements right

586:03

and it is possible that all the

586:05

statements there is one statement here

586:07

which is critical so let's say this

586:09

statement is critical and we know

586:11

whenever you have a critical statement

586:13

it is always better to write try catch

586:15

so let's say we have a try here for this

586:19

and we have a cache block as well so

586:21

just try to imagine this in a coding

586:23

perspective say we have a statement

586:24

which is critical and then you try to

586:27

handle that with help of try catch and

586:28

we're doing that in a method D okay and

586:30

let's say we have one more method here

586:32

which is called E and in this method as

586:35

well you are writing some statement out

586:37

of which there is one statement here

586:39

let's say the first statement itself

586:41

which you need to put that in try catch

586:43

okay because this statement is critical

586:46

okay so if You observe we have two

586:48

methods here which is d and e both have

586:50

a statement which is critical now of

586:52

course it makes sense to write try catch

586:54

in each of the method or we have one

586:57

more Choice here what you can do is

586:58

let's say we have a method which is C

587:01

and C method is calling d and e both

587:04

okay so C method is calling the method

587:07

and E method of course in C let's say we

587:09

have some more statements let's say we

587:12

have method C and then this you have

587:14

multiple statement out of which the two

587:16

statements are calling D and E of course

587:18

you can call methods right and then we

587:20

have some more statements here now if

587:23

you try to observe the situation what we

587:25

are doing is we are trying to handle try

587:28

cats by ourselves right in the D and E

587:31

but what if you could say hey you know

587:33

what d and e both have the same

587:37

exception so both have a critical

587:38

statement they will rise the same

587:40

exception so instead of handling those

587:43

exception inside try catch in each of

587:45

the method can we just handle the try

587:48

catch and see itself so what you can do

587:50

is you can put try here and catch so

587:53

when you are basically calling those

587:55

methods D and E you can put them in try

587:58

catch because both of the methods has a

587:59

critical statement in fact the entire

588:01

method becomes a critical methods right

588:04

because when you're calling it

588:05

so instead of handling those things here

588:08

let's not handle uh using try catch here

588:10

yes we have a critical statement but

588:12

will not handle the exception in D and E

588:16

you can handle the exception in C okay

588:18

but then even if you work with this it

588:21

will give you an issue it will say okay

588:22

there's a problem with d and e but still

588:25

it will stop the execution so one thing

588:27

you can do is you can write here throws

588:30

so you can say throws exception and same

588:34

goes here you can say throws exception

588:37

okay so by doing this what you're doing

588:39

is you're saying hey whatever problem

588:40

arises in D and E they will not handle

588:44

it they will just send the exception to

588:46

the method who is calling it sometimes

588:47

just call ducking the exception because

588:49

we are ducking it we are not handling it

588:51

the method which is we are into let's

588:54

say d and e says it's not my

588:55

responsibility so the method which is

588:57

calling d and e which is C in this case

588:59

will be handling those exceptions okay

589:01

and in C of course you have to have try

589:03

catch if you don't have a try catch in

589:05

see it will again create a problem for

589:08

you because it will stop the execution

589:09

now we have one more thing what if you

589:11

are not doing try catch here so of

589:12

course some method will be calling C

589:14

right so let's say B is calling C method

589:17

okay now in this case what you can do is

589:19

you can put try catch here and let's say

589:21

uh if you don't want to do try catch

589:23

here we have a main method now let's say

589:25

main is calling B method here even main

589:28

can have a try catch so basically you

589:30

can go up the ladder and you can write

589:32

try catch in whatever method you want to

589:34

write so if you write try catch in main

589:36

then B says okay even I will throw the

589:39

exception c will say even I will show

589:41

the exception I will not handle example

589:43

by myself c will say I want B to handle

589:46

it B will say I want main to handle it

589:47

and of course your D and E both says c

589:51

will handle it okay now that is called

589:53

ducking the exception for a particular

589:54

method but it is done with the help of

589:56

those keyword now this makes sense for

589:59

checked exceptions okay see for uncheck

590:02

exceptions it's not compulsion for you

590:04

to handle the exception but when you you

590:06

have a checked exception example if you

590:07

talk about IO exception okay now in

590:10

terms of IO exception which happens when

590:13

you use input output let's say you are

590:15

reading something from the file or

590:17

you're writing something something to

590:18

the file or when you're doing SQL which

590:20

is database connectivity at that point

590:22

as well when you're finding a query it

590:24

might raise an exception in that case

590:26

you will be getting a checked exception

590:27

and we know we have talked about this in

590:29

checked exception if you write that

590:31

statement it reads compulsory example

590:32

remember we have worked with one of the

590:35

thing which is called class dot for name

590:36

and in which you can mention the class

590:38

name let's say my class name here is

590:40

demo itself

590:42

so basically what you're doing is you're

590:43

writing this statement and this

590:45

statement says it might throw or it will

590:47

throw a class not found exception now

590:50

class not found exception is also comes

590:52

into hierarchy where this is a checked

590:54

exception so it is asking you to

590:57

compulsarily handle this exception so

591:00

you can put that in a try catch so you

591:02

can put that here and you can say catch

591:04

now which exception we have to handle uh

591:06

the name of the exception is class not

591:09

found exception and you can print not

591:13

able to find the class in fact let's say

591:17

let's write a class called calcare and

591:19

you know we don't have a calc class

591:21

nowhere okay now if you try to run this

591:24

code compile and run you can see it will

591:27

say not able to find the class now which

591:29

class we're talking about class is not

591:31

there in fact I can also print the error

591:34

message here with this so compile and

591:36

run you can see it says class not one

591:38

exception which class calc it's not

591:40

there but if you do it for demo all

591:42

there is no issue because we do have a

591:44

demo class here right so if I compile

591:47

this and run you can see it works it

591:49

says the default output blah blah blah

591:51

but there's no exception there's no

591:53

problem with this okay so in fact we can

591:56

also put this we can just cut this part

591:58

and let's say we are doing that in a

592:01

separate method so let's say we have a

592:03

class A and in this class A we have a

592:05

method which is public void show and in

592:07

this show we are doing this we are

592:09

trying to load the class demo here and

592:11

of course we have to call this now what

592:12

I will do is I will just try to remove

592:14

everything else so that we can make our

592:15

code simple and remove this custom

592:17

exception as well and you can see we

592:19

have a very simple code now and if you

592:21

want to work with so of course you have

592:22

to get object of a you will say a obj

592:24

and then you will say obj dot show okay

592:28

and now if you try to work with this

592:31

when you say compile and run you can see

592:34

there is no problem because anyway we

592:37

are not printing anything we are just

592:38

trying to load the class of course if

592:39

you write a static block here that will

592:41

surely work let's try to do that as well

592:43

because we have seen this let's create a

592:45

static block and I will print last

592:46

loaded in fact another the weird thing

592:48

is anyway we are writing main method

592:50

right so the class will be loaded anyway

592:51

but still we're just trying to see if

592:53

we're getting some output again the

592:55

class voltage is not happening because

592:57

of the load it's only because the main

593:00

method is there in the demo so anyway it

593:01

will be called but the idea is we are

593:03

trying to print something but what if I

593:05

try to work with a class called calc now

593:07

in this case if you try to work with

593:09

calc it will give you an exception you

593:11

can see it says not able to find the

593:13

class which is scalp okay cool and now

593:17

what if I don't write try catch so what

593:21

I'm doing is I'm saying hey I will not

593:23

handle the exception so in this case you

593:25

can simply go back to shows and say

593:27

throws class not found exception and you

593:30

can say there's no problem now because

593:31

this state treatment knows that show is

593:34

basically throwing the exception so show

593:36

says I'm not responsible to handle the

593:38

exception you know when you work for the

593:39

company there are some employees they

593:41

don't take responsibility of what went

593:42

wrong they will simply say hey talk to

593:44

my manager right so the same thing is

593:45

happening here it says hey I'm not

593:47

responsible to handle the exception talk

593:49

to my manager which is mean because many

593:51

is calling show so it is made

593:52

responsibility to handle the exception

593:54

and that's why you can see we got an

593:56

error here it says unhandled exception

593:58

okay so now you have two Choice what you

594:00

can do is either you can write try catch

594:02

here or you can say those last not found

594:06

exception okay so you have this choice

594:07

this is not recommended okay never do

594:09

that with main because if you see when

594:11

you're throwing the exception for main

594:13

the problem is who is calling main it's

594:15

jvm now you are asking jvm hey jvm you

594:19

have to handle this not a good idea

594:20

because JV will say okay if you throw it

594:22

to me I will simply stop the execution

594:24

so it's not a good idea to throw

594:26

exception for main but yeah for other

594:27

classes let's say for sure because we

594:29

know who is calling show it's your main

594:31

that domain will be handling it so don't

594:33

write those here okay you can just use

594:36

try catch in fact you can ask your IDE

594:38

you can say Quick Fix uh surround with

594:40

try catch and done you don't have to do

594:43

anything here and this might be new

594:45

print a stack Trace basically what it

594:47

does is it prints the entire stack which

594:49

method is calling which method and you

594:51

can actually track the entire method

594:53

calls so that you know because of which

594:56

method the problem has been arised okay

594:57

so if I compile this code and run you

595:00

can see this is the entire stack so

595:02

basically your main is calling show and

595:05

behind the scene we are calling class

595:07

dot fortnite which is behind the scene

595:08

is calling this and that so there's a

595:11

hierarchy of method which is getting

595:12

called that's what the print stack Trace

595:14

does so it's helpful to debug the

595:16

application but it's also good if you

595:18

want to print a message okay so yeah

595:20

that's about ducking the exceptions and

595:22

using this Rose keyword in fact we are

595:24

going to use this a lot in the upcoming

595:26

sessions so you will get used to it now

595:29

it's a good time to talk about how to

595:31

take input from the user I know you have

595:34

been waiting for this one a long time

595:35

because we have talked about different

595:37

concepts and maybe you had this question

595:40

in your mind okay everything looks good

595:42

we talked about objects classes and

595:43

heightens polymorphism but how do I take

595:45

the input from the user because in

595:47

between as well I've mentioned that what

595:49

if this value comes from the user and at

595:51

the same point you might be thinking how

595:53

Okay so let's try to answer that in this

595:55

video so basically if you want to print

595:57

something on the console so this is a

595:59

console right on the right hand side

596:00

whatever you have here it's a console

596:02

and if you want to print something on

596:04

the console what you do is you say

596:06

system dot out dot println right now you

596:09

have to understand this first see print

596:11

Ln is a method now if I ask you just to

596:14

think about it print Ln method belongs

596:16

to which class or it's a method of which

596:18

class now this is the interview question

596:20

a lot of people say that it is a class

596:23

it's it's a method of system class but

596:25

that's not the case see if I press my

596:28

control button on windows and command on

596:30

Mac and if I click on this if I hover on

596:33

this print Ln if I click on it you can

596:36

see it gives you definition it says

596:37

print Ln and print Ln method belongs to

596:41

print Stream So plainstream is a class

596:44

and println is a method of print stream

596:47

okay that means if you want to call

596:49

Print Ln you first have to create object

596:51

of a print stream but okay don't worry

596:54

you don't have to create the object the

596:56

beauty is the object is actually already

596:59

created can you see that it we already

597:01

have a out object and that's why we are

597:02

using this object for a long time so out

597:05

is the object of print stream but this

597:08

object is created as a static variable

597:11

inside the system class okay it's a it's

597:15

inside system class now since it is

597:17

static so we can use out with the help

597:19

of system dot out and once you have the

597:21

access to the out object you can call

597:22

println and that's what we have here

597:25

system.out.println so if I want to print

597:27

something if I want to print let's say

597:28

hello of course I can do that and I can

597:30

just compile this code and run it

597:32

perfectly works

597:34

now let's say I want to take the entry

597:36

from the user I have to say hey enter a

597:39

number now in this case when a user

597:41

enters the number how will I accept it

597:43

that that's tricky right so what do you

597:46

think what could be a solution here what

597:48

is the method we have to use now by any

597:50

chance if you're coming from CC plus

597:51

plus we use C out C in in C plus plus we

597:55

use printf scanf in C right and it's

597:58

straightforward right there are

597:59

straightforward methods but in terms of

598:01

java when you say system.out.printl and

598:04

it prints right it is out and that's why

598:06

it says out it is outputting so to input

598:09

there should be in right let's see do we

598:11

have in so if I go to system and if I

598:14

scroll down to out yes there is out here

598:17

and need to find in this is also in if

598:20

you can see in is a static method and

598:23

this object in is of type input stream

598:27

oh we do have it okay so if I go back

598:30

here and if I try to use system dot in

598:34

dot oh there should be a method right

598:36

because to print we have a print method

598:38

for the input there should be read oh

598:41

there's read okay great it works you can

598:43

see we have a read method and read

598:45

method returns uh into value right it

598:48

Returns the end value that's great the

598:49

only thing is it might throw an

598:51

exception and if you can see which

598:52

exception is IO exception and we know

598:54

what is IO exception now so you have a

598:57

choice you can handle the exception and

598:58

this is checked exception which means

599:00

you have to handle it it's your job to

599:02

handle it compiler will force you to do

599:04

that

599:05

so you can either use try catch here or

599:08

you can be evil and you can say throws

599:11

exceptions not a good idea but it works

599:13

for this code since we are learning so

599:15

we can do that but when you are making

599:16

an application don't do that try to use

599:18

try catch here okay so system.in.read

599:21

will give you the integer value that's

599:23

great what I will do is I will just put

599:25

that in a variable called num because it

599:27

returns a value right read will give you

599:30

the value so you have to put that in a

599:31

variable and once you have the variable

599:33

you can perform any operation if you

599:35

want or you can print it that's your

599:37

choice let's go back here and compile no

599:41

problem

599:42

if you try to read oh it says entire

599:45

number I'm excited let's enter 5 and

599:48

enter oh we got five but we also got

599:52

three now that's weird I don't want

599:54

three there okay let's try another

599:56

number let's see with three oh we got

599:58

fifty one what is happening here the

600:00

thing is this system dot in dot read

600:03

actually gives you the ASCII value for

600:05

the number you enter example let's say

600:07

if I run this code once again and if I

600:09

enter capital A it prints 65. so every

600:12

character on your keyboard has a ASCII

600:14

value the same way for a Capital Area 65

600:17

for small age is 97 I guess for 0 which

600:21

is 48 example if I enter the value of 0

600:24

it is 48 for Value 1 it is 49 so this

600:28

goes on right so basically it is not

600:30

giving you the exact value it is giving

600:32

you the asciate representation of that

600:33

value

600:34

okay that is tricky actually so what one

600:37

solution you have is what if you

600:38

subtract the number by 48 or maybe it's

600:42

not not 48 your subtract the number by

600:45

48 not by 48 minus num it is none minus

600:48

48 and now if you try to earn this call

600:50

you have to first compile this code

600:51

compile and run let's enter number five

600:54

oh it works now can you see that because

600:56

now we don't want to ASCII value we want

600:57

whatever ask about you are getting by

600:59

subtracting it right so if I enter six

601:01

okay if I enter 6 it works okay now it

601:07

works so we were able to manage it right

601:09

this is not an ideal way of taking the

601:10

input but that's what Java provides you

601:12

Java says we will give you this method

601:14

read which will give you ASCII value

601:15

what if you want to get a bigger number

601:17

what if you want to get example if I

601:20

give a bigger number here let's say 55

601:22

you can see it only gives you 5 because

601:24

it reads only one character at a time oh

601:28

that means if you want to read multiple

601:30

characters we have to use a loop but

601:32

let's not do that let's not take

601:33

headache off by ourselves so Java says

601:36

don't worry I will give you a

601:38

specialized class to work with this so

601:40

instead of using system.in dot read we

601:43

have a special option which is there's a

601:45

concept called buffered reader now

601:47

buffer reader is basically a class which

601:50

works with IO okay and it belongs to a

601:53

package java.io so if you want to use

601:55

buffer data you need to import this

601:57

package so IO exception buffer data

601:59

board belongs to IO package now whatever

602:01

class we have used to allow most of them

602:03

belong to java.lang which is Auto Import

602:06

but if you want to use any class which

602:08

is not there you have to explicitly

602:10

mention the package name okay so let's

602:12

create object or buffer data so we'll

602:13

say buffer data BF is equal to new

602:15

buffered reader

602:17

okay but what next

602:20

the thing is if you want to read we have

602:23

to actually pass the in object can you

602:24

see that if I go to Constructor buffer

602:26

leader buffer data Constructor says okay

602:29

I will work but first you have to pass

602:31

an object of a reader okay now which

602:34

date is talking about so basically there

602:36

is one more class which you have to work

602:37

with I know this code will be bit

602:39

lengthy because that's how Java is

602:41

implemented it but let's use it in fact

602:43

there is a better way as well which

602:45

we'll see in the next topic so we have

602:46

to pass the input stream reader so we

602:50

have to get object of input stream

602:51

reader let's say in in this case and I

602:54

will say new input stream reader okay

602:57

and whatever object you are creating

602:58

here just pass it here your job is done

603:01

but there's a problem you can see it

603:03

input stream reader also needs an object

603:05

of input stream okay in this case what

603:07

you can do is it needs object of input

603:09

stream right and if you remember when we

603:12

talked about in object it is input

603:14

stream object so what you can do here is

603:17

you can pass system dot in I know it is

603:20

bit complex and that's why in later

603:22

version of java they have introduced

603:24

better solution but I just want to show

603:26

you this option as well which was there

603:28

and people were using it for a long time

603:29

okay so you have to say input stream

603:32

reader in and then we have to get the

603:35

object of input stream reader then you

603:36

can create object of buffer data by

603:38

passing the object of input stream now

603:40

once you have it you can create a num

603:42

variable and you can use buffered reader

603:44

to read a line so you can see we have a

603:48

read line the only thing is read line

603:50

will give you the string okay we don't

603:54

want string we want integer so to do

603:57

that we have to use a special class

603:59

called integer dot pass int which we

604:01

have seen already and integer.pass int

604:04

not in okay so whatever string you're

604:07

getting basically you will get a number

604:08

in the string format you just have to

604:10

convert that into integer and now if I

604:12

try to print number it should work

604:14

otherwise we have wasted so much of time

604:17

compile and run let's enter 55 it works

604:21

doesn't matter how big your number is it

604:23

will accept it you can say we got the

604:25

number so basically this is how you take

604:27

the input from the user there's another

604:29

way which I will show you in some time

604:31

but the important thing is whenever you

604:32

use buffer leader it is trying to access

604:34

input stream reader and the beauty is

604:37

buffer data can take the input from

604:38

anywhere not just from system keyboard

604:41

it can also take input from the file

604:43

from the network and you can mention

604:45

wherever you want to take the input from

604:46

you can mention it here you can mention

604:48

the file you can mention all those other

604:49

stuff now the thing is buffer reader

604:52

whenever you're using it it's actually a

604:53

resource now why it's a resource is

604:55

because let's say when you are trying to

604:56

read from a file of course file is a

604:58

resource right and when you open the

605:00

file it is your responsibility to close

605:02

it when you open the network it is your

605:04

responsibility to close it when you open

605:05

the database connection it is your

605:07

responsibility to close it how will you

605:10

close it so once your job is done at the

605:12

end you can just come back here and say

605:13

BF Dot close consider we have a closed

605:16

method so it is all always a good idea

605:18

to close the resources again it's not

605:22

compulsory it will not give you error

605:23

but a good practice is close the

605:25

resources okay otherwise you are making

605:27

resources you are licking the file

605:29

you're leaking data you are keeping the

605:31

resource busy and the thing is if you

605:33

are using a resource no one else can use

605:35

it okay so don't do that don't be a evil

605:37

person here so you close the resource

605:39

okay so this is how you take the input

605:41

and I know you are waiting for the

605:43

better way of doing this let me show you

605:45

a simple way so instead of using buffer

605:47

data we can use one more technique here

605:49

which is a scanner now scanner was

605:51

introduced later in Java so if you can

605:53

see if I go to scanner class scanner

605:55

class was introduced in 1.5 so people

605:58

before 1.5 were using buffer data I know

606:01

bad days but yeah we got scanner here we

606:03

can create object of scanner let's say

606:05

SC we can use any object name doesn't

606:07

matter and we can say scanner now

606:09

scanner takes the system dot in that's

606:12

constant because we have to mention from

606:14

where we are getting the input are you

606:16

getting the input from the Soul are you

606:18

getting the input from the file you have

606:19

to mention that here and once you get

606:21

this scanner object the beauty is in

606:23

scanner you can say SC Dot and there are

606:26

so many methods you can see that we got

606:27

next line which will give you string you

606:30

can say next int oh that means it

606:32

automatically gives you integer value

606:33

you can also say next chart uh it will

606:37

it has so many methods but we are going

606:38

to use next int not next line because if

606:41

we use next line you have to pass that

606:43

into integer so let's use next tint

606:45

which will not create a lot of headache

606:47

for us and run enter a number 567 and we

606:51

got it so I know scanner looks much

606:54

better than buffer leader but yeah it

606:56

was introduced later so people before

606:57

that were using buffer data so we have

606:59

seen it in fact in the next video we'll

607:00

try to understand more about how do you

607:03

close the sources okay and that's why we

607:06

are teaching this concept now because

607:07

now we know what is IO exception and all

607:09

those stuff so yeah you have two choice

607:11

of taking the input from the user you

607:13

can use buffer Twitter or you can use a

607:16

scanner class okay now in this video

607:18

let's try to implement try with finally

607:21

our private resources basically so what

607:24

I will do is I will just remove

607:25

everything else of course we'll do this

607:26

once again we'll try to use buffer data

607:28

because we love it right okay we are not

607:31

doing an exception as of now uh we are

607:33

not running any code for that let me

607:34

remove that as well and here what I'm

607:36

going to do is let's try to understand

607:37

try now if we talk about try of course

607:40

in this try you can do whatever you want

607:42

normally we put the critical statements

607:44

right and then if there's a critical

607:46

statement which is throwing an exception

607:50

what you execute is a catch block right

607:52

and we know how to write a cash block

607:54

the thing is is it compulsory to have a

607:57

try with catch there's no compulsion you

607:59

can write you can write try but you can

608:01

see if I don't mention catch it gives

608:03

you an error the thing is if you have a

608:06

try and if you don't want to mention the

608:08

catch you can basically write finally

608:10

here so yeah so try can be used with

608:13

finally if you don't want to write catch

608:15

if you don't have a scenario where you

608:16

want to handle the exception you can

608:18

write finally but then what's the use of

608:20

try when you're not catching it of

608:22

course right try is responsible to

608:24

handle the exception and when it it will

608:27

simply close the exception which will

608:29

catch will catch it and then it will

608:31

solve it or it will print something

608:33

finally what finally is doing see the

608:35

thing is even if you have a catch block

608:36

here let's let's work with catch block

608:38

and in this cache block I will say

608:40

exception e and then let me create a

608:43

scenario which is int I is equal to zero

608:46

I know you'll be thinking I'm just using

608:48

the same example just for Simplicity I

608:50

just want to keep this thing simple and

608:52

here I want to say J is equal to 18

608:55

divided by I I don't know why I'm using

608:57

18 every time it's just because we have

608:59

used it before now we know this will

609:01

throw an exception so it is our job to

609:03

print something here so I will say get

609:05

lost oh so it will say something went

609:08

wrong ignore that get lost so something

609:11

went wrong right okay so if you get the

609:15

exception it will call the catch block

609:17

right now sometimes you what happens you

609:19

know you want to print something you

609:20

want to do something which should be

609:22

executed irrespective if you get the

609:24

exception or not example let's say if I

609:27

want to print buy here in the try block

609:30

the thing is if you compile this code

609:32

and if you run this code you can see it

609:34

says something went wrong it is not

609:35

printing by you know why it's because in

609:37

the try block the moment you get the

609:39

exception here it will call the catch

609:41

block and that's what it's not printing

609:42

a buy it will actually jump out of the

609:44

try block right so one of the solution

609:46

you can have is you can just write this

609:48

in Cache block as well okay so that

609:50

irrespective if you get the exception or

609:52

not it will print by so you can say it

609:53

is printing Buy in the catch block but

609:55

if you don't have the exception it will

609:58

print by which is coming from the try

610:00

block okay so at least we are printing

610:03

buy now of course in the real project

610:05

you will not be printing by the in real

610:06

project you will do something there are

610:08

some set of statements which you want to

610:11

do is respect you if you get the

610:13

exception or not now instead of doing

610:16

those things here one of the amazing

610:18

thing we got is finally so what you can

610:20

do is you can write your statement

610:22

inside finally now finally has this

610:24

amazing feature which is irrespect you

610:26

if you got the exception or not it will

610:29

execute the final block so in case of

610:31

exception as well it will execute the

610:33

finally block let me show you that to

610:34

you compile and run it says buy no

610:37

problem even if I get the exception

610:39

compile and run it says buy so finally

610:42

block now this block will be executed

610:45

irrespective of the exception okay and

610:49

the beauty is you can actually skip the

610:51

cash block if you just want to use try

610:52

and finally but why you will do that why

610:55

in this world you will write try with

610:58

finally you're not handling the

610:59

exception right see finally makes sense

611:01

whenever you want to close the resource

611:04

that's right whenever you want to close

611:05

the resource that's where you will be

611:07

using finally okay what I'm talking

611:10

about is let's say we are not doing this

611:11

we are using a variable called num and

611:15

the value for Num initially will be zero

611:17

and what I want is is I want to take the

611:19

input from the user and we know how to

611:20

do that right so basically we have to

611:22

get object of buffer data of course we

611:24

can use scanner here but let's use

611:25

buffer data so buffer reader

611:28

b r equal to new buffered reader and we

611:32

know if you want to work with buffer

611:33

data first you have to get object of

611:34

input stream reader we'll say

611:37

in is equal to new input stream reader

611:41

which will take the object of system Dot

611:44

in right and here you can basically pass

611:48

this in which you have created here okay

611:50

and using this buffer data basically we

611:53

can say okay we can num is equal to b r

611:57

dot read line and then we have to pass

612:00

it right we have to say integer dot pass

612:03

int okay now it says it might show the

612:06

error right but then you will say hey we

612:08

are handling right no actually we are

612:10

not handling we don't have a catch block

612:11

here right so basically we are not

612:13

handling the exception and that's what

612:14

it says so what we have to do is we have

612:16

to either handle the exception or we can

612:19

Quick Fix and throws okay this time it

612:22

is throwing two exceptions number format

612:24

exception and IO exception because we

612:26

are passing it okay that's fine with us

612:28

okay and let's say I'm printing the num

612:29

value as well now after doing all those

612:31

things we know one thing right whenever

612:33

you open the resource example is a

612:35

resource right it's always better to

612:38

close the resource now my question to

612:40

you is where should I close the resource

612:42

should I close the resource in try block

612:43

if I am handling the exceptional do that

612:45

in the catch block of course we have

612:47

tried two statements right instead of

612:49

that the finally block is normally used

612:52

to close resource so whenever you want

612:54

to close the connection whenever you

612:55

want to close the resource always use

612:58

finary block it is meant for closing the

613:00

resources okay the only thing is can you

613:02

see that we are creating the reference

613:04

inside the try block the scope is

613:06

getting over itself here so what I can

613:07

do is I can declare it outside and I can

613:09

use it inside so when I declare outside

613:12

even the final block can use it and I

613:14

can make it null initially and now you

613:16

can see there's no problem so basically

613:18

we are closing the resource in the final

613:21

block so Point remember here is the

613:23

finally block is used to close the

613:26

resource it can be for file for the

613:29

input for for the connections network

613:31

connection database connection

613:32

everything finally will be used for

613:34

closing it makes sense right okay before

613:36

I go ahead I just want to do one more

613:38

thing don't you think we are passing in

613:40

here so can I instead of passing in can

613:43

I pass the entire state see ultimately

613:45

we need object right an object is

613:47

created by this line here so actually we

613:49

can do this so we don't need this line

613:51

so most of the time when you read

613:52

somewhere it will show this type of

613:54

statements and that's completely fine it

613:55

works okay now one more thing so try

613:58

with finally works you can close it here

613:59

but you know we can actually optimize

614:01

this mode in the data version of java

614:03

they have introduced A New Concept which

614:05

is try with resources where you don't

614:08

even have to mention finally block

614:10

that's good right but how will you do

614:12

that so what this says in the trial

614:14

itself create a round bracket and then

614:16

you create your declaration whatever

614:19

object you are creating create them

614:21

inside your try block I mean inside the

614:23

brackets and you have to declare that as

614:25

well but for reader so you will not

614:28

mention that here now so what I'm doing

614:30

is I'm creating the object in the the

614:32

dry itself now watch The Adventure of

614:34

this the advantage is once the try gets

614:37

over this resource will be closed

614:40

automatically it's because if you go to

614:42

buffer data buffered reader extends

614:44

reader and if I go to reader you can see

614:47

it says closable

614:48

and if I go to closable it is auto

614:50

closable so whenever you have a class or

614:52

the interface which is auto closable it

614:55

means that will be closed automatically

614:57

with the help of try you don't have to

615:00

do the closing manually so this is try

615:02

with resources so you have this syntax

615:05

option you can use Drive finally that

615:07

depends upon how you want to implement

615:09

this I prefer to use try with finally

615:11

because I know which resources I can I'm

615:13

closing and sometimes you have multiple

615:15

object created so it's good to use

615:17

finally but if you think you can make a

615:19

shortcut this also works so this here is

615:22

called try with resources in this video

615:25

we'll talk about threads now what are

615:28

threads now think about this when you

615:30

run the application of course the

615:32

software which you have written it will

615:34

be running on an OS right and below the

615:36

OS you have a layer which is called

615:37

Hardware so if you want to represent

615:39

this what we normally have is we have a

615:43

software which you built of course so

615:45

let's say this is your software and this

615:47

software basically runs on and Os of

615:50

course on this OS you'll be having

615:51

multiple softwares right and underlying

615:54

that is the hardware right so this is

615:55

where you have the physical set example

615:58

in this uh you will be having your CPU

616:00

you'll be having your RAM this is what

616:02

we use to run applications right

616:04

now if we talk about ram ram acts like a

616:07

temporary memory for your processing and

616:09

your CPU is basically something which

616:12

executes something okay so let's say in

616:15

in your software if even if you want to

616:17

add two numbers let's keep it very

616:18

simple let's say if you want to add two

616:20

numbers by saying two plus two now that

616:23

operation will go to OS and Os will say

616:25

okay you want to add two numbers let me

616:27

just send the request to the CPU of

616:29

course CPU will do the processing right

616:31

now CPU says okay that's a task I'll

616:33

just have to add two numbers it will do

616:35

some processing and then you got the

616:36

output

616:37

the thing is

616:39

for a simple operation it's so simple

616:41

right but then what if you are running a

616:44

big software let's say uh this is your

616:46

software and apart from this you have

616:48

multiple softwares which you want to run

616:49

on the same OS now of course in your in

616:52

your system you will be having multiple

616:54

uh softwares right and then sometimes

616:57

you want to run that at the same time so

616:59

that means OS should support multiple

617:02

software working at the same time so can

617:04

we say that that's multitasking

617:06

right so we have multiple tasks running

617:09

at the same time okay now most of the

617:12

modern os does support that the thing is

617:14

it's not like they are executing this at

617:16

the same time so this on the CPU there's

617:18

a concept of time sharing example let's

617:19

say this software one they software to

617:21

this software three uh let me just

617:23

remove this part for time media so let's

617:25

say if you have three softwares here

617:27

which is which you want to run on the OS

617:29

uh they will be going in time sharing so

617:31

your Osa OS can say hey uh the first

617:35

software you execute it will say Okay

617:37

second software now you execute and then

617:39

they will be executing badly by showing

617:42

the time okay

617:43

that's one thing so that's why we can

617:45

achieve multiple tasking and that's why

617:47

you know at the same time you can browse

617:48

a web page you can listen to a music you

617:51

can type a code so you can do all those

617:53

things at the same time because you have

617:54

a concept of multitasking but if you

617:57

talk about one single task okay what if

617:59

you want to break this break down this

618:02

task into multiple things

618:04

it is possible that in the same software

618:07

you have multiple

618:08

process or multiple things to run at the

618:11

same time let me give an example here

618:13

let's say you are writing something on

618:16

the editor so we are using vs code right

618:18

now the moment you write something on vs

618:20

code let's say if I say A B C uh you can

618:24

see the moment I type it it is showing

618:26

me that it is showing the text on the

618:28

software it is also showing us the error

618:31

right so that means these two things are

618:34

happening at the same time maybe uh we

618:36

can also enable some sound effects the

618:38

moment I type A to create a sound effect

618:39

so we are doing multiple thing at the

618:42

same time in the same software

618:44

so that means it's not just about

618:45

multitasking it's also about

618:48

dividing your task into small things the

618:52

smallest unit

618:54

how can we do that and that's where we

618:56

have a concept of threads so in the same

618:58

task in the same program you can have

619:00

multiple threads running at the same

619:01

time

619:02

okay so example when you play game you

619:05

know any game which you play Maybe Mario

619:07

or Cricket or football

619:09

I'm talking about video games okay now

619:11

at that point what happens you know if

619:13

you talk about Football let's say if you

619:14

have multiple players running at the

619:16

same time the ball is moving at the same

619:17

time uh the audience is cheating for at

619:20

the same time so you have multiple

619:22

things happening at the same time so

619:24

that is possible with the help of

619:25

multiple threads in fact if you see your

619:28

activity manager on Mac or if you go to

619:30

task match on Windows you can see

619:32

whatever task you have example I have a

619:34

Discord helper here uh we got login

619:37

window we got code helper so all this

619:41

software is another thing right stream

619:42

direct Zoom OS zoom zoom software

619:44

everything is running now all those are

619:47

tasks and individual tasks if you can

619:49

see we have threads there so the kernel

619:52

is consuming highest number of threads

619:54

524 then we got Java because we are

619:57

running Java behind the scene so Java is

620:00

using 20 or 20 42 threads

620:02

right so in a task as well you have the

620:06

smallest unit the beauty is they can run

620:09

parallely

620:10

and it they are lightweight basically

620:12

they don't they they also share the

620:13

resources it's not like if you have 42

620:15

threads they will consume 42 different

620:18

resources now they multiple threads can

620:20

share resources and that's why threads

620:22

are important to understand so in the

620:24

upcoming videos we'll focus more on

620:26

threads so I hope you got the just what

620:28

is threads and uh yeah just to redefine

620:31

that's a smallest unit which you can

620:34

work with and of course you can have one

620:36

multiple choice at the same time now

620:37

once we know why we need threads let's

620:39

try to create multiple threads here and

620:42

of course to create multiple threads

620:43

it's not like just because we have a

620:44

concept of threads we are going to use

620:46

in every application what if I say most

620:49

of the time you don't even create

620:50

threads by yourself see when you build

620:53

applications we use certain Frameworks

620:55

right now behind the scene this

620:57

Frameworks will create threads for you

620:59

so if you work on a big project there's

621:02

a high chance that you will not even

621:03

write threads by yourself but then it's

621:06

always better to get Hands-On on some

621:08

underlying Concepts right so let's do it

621:11

here so what I will do is let's say

621:12

let's go for a imaginary concept here

621:14

let's say I have a class hi whose job is

621:17

to print high okay or maybe I will just

621:19

say a Class A whose job is to print high

621:24

so what I will do is I will try to

621:26

create a function here public void show

621:28

and in this show I want to print hi

621:31

nothing fancy just hi here the only

621:34

thing is I want to print this High

621:35

multiple times so I will simply use a

621:37

loop here I can say int I equal to 1 and

621:41

I less than equal to 10

621:44

and I plus plus

621:46

okay and then we are putting this inside

621:50

a loop so of course when I call this

621:52

particular function which is show of

621:53

course it will execute height 10 times

621:55

right now let's say I I just copy this

621:58

code and I paste it here now what I'm

622:01

going to do is we have two classes with

622:03

this with the same concept almost

622:05

because they both have the same method

622:07

name which is show they are both are

622:09

running a loop for 10 times the only

622:11

thing is the second Loop actually prints

622:12

hello the first function here prints hi

622:15

the second one prints hello that's the

622:17

only difference and of course the class

622:18

name is different

622:19

and now I will go back to my main method

622:21

and here I want to create object of

622:23

these two classes so we got a class we

622:25

got B Class let's create object for them

622:27

so I will say a obj1 is equal to new a

622:31

so we got the object for a and then I

622:33

can say a object 2 is equal to new B and

622:36

we got the object for B okay simple

622:38

stuff and now I want to call those

622:41

methods of course I can simply say

622:43

obj.1.0 and

622:48

obituary what I'm doing is I got object

622:50

of both the classes and I'm calling the

622:52

show method and I'm thinking it should

622:53

work and why not it will white will not

622:55

work we have done so many Java codes to

622:58

allow it everything was working

623:00

let's see what happens so I will compile

623:02

this code and you can see there's no

623:04

problem there's no compile time issue

623:05

and if I run this code oh okay uh we got

623:08

the output right so we got all highs

623:10

here and we got all hellos right now

623:14

that's how your system basically works

623:15

you know your execution will start from

623:17

main main will say okay uh I mean when

623:20

your jvm is executing the Java code it

623:23

will start the execution from Main and

623:25

from when it says okay this is the first

623:27

line which I want to execute the first

623:29

line is simply create the object now

623:31

behind the scene in the Heap memory you

623:33

will get this object okay now the second

623:35

line says okay create the B object it

623:38

will say Okay B object done and since

623:40

everything goes in sequence it will

623:42

complete this task this two tasks then

623:45

it will go for the next statement which

623:47

is calling the show method of a class

623:50

right so it will say okay and now I have

623:53

to actually move so if you try to

623:55

imagine a pointer here not the pointer

623:57

in different languages I'm saying just

623:58

saying the which statement is getting

624:01

executed okay where is your executor

624:03

here so the executor says okay my job is

624:06

to execute this show method right so the

624:09

executor will directly jump to this

624:11

method which is show

624:13

okay now it will start executing this

624:15

method but when you are executing this

624:19

method

624:20

there is a hold on demo right I mean the

624:22

main method is not continuing now so

624:24

main will continue for the next

624:26

statement only when the previous

624:27

statement is done executing so once you

624:30

complete this show method execution then

624:32

only it will go for the next show right

624:34

and that's what it will do so it will

624:36

execute this show here it will run this

624:38

Loop for 10 times and it will print high

624:40

10 times now once this show method is

624:42

done with his work it will go back to

624:44

the main method and it will continue for

624:46

the next method calling so that

624:49

everything basically goes in a sequence

624:51

right now what I want to achieve here is

624:53

I want to say Hey you know when obj one

624:56

dot show is getting executed at the same

624:58

time parallely I want to execute this

625:01

show as well

625:03

okay so if you want to execute two

625:05

things at the same time or two behave at

625:07

the same time

625:08

since methods are behaviors we can use

625:10

something called threads okay so we can

625:13

say Okay I want to execute this two

625:15

objects but then they are not normal

625:16

objects you can't execute normal objects

625:19

in multiple threads or you can't execute

625:22

normal object execution simultaneously

625:25

so you have to use multiple threads here

625:26

so how do you make your normal objects

625:29

as threads now that's tricky right

625:33

it's actually very simple

625:35

if you want to make these classes or

625:38

these objects as threads just extend

625:41

with a class called thread it's that

625:45

simple the moment you extend with

625:46

threads or with a third class now this

625:49

class is not a ordinary class

625:52

this is a thread okay the same thing you

625:55

can do for B so you can say thread and

625:58

exit exchange thread okay and then you

626:01

can come back here and our job is done

626:04

right now we don't have normal classes

626:05

we have threads oh is it so simple okay

626:09

let's see compile and run oh it's not

626:13

that simple you can see they are still

626:15

running in sequence they are not running

626:17

in parallel what I want to do is I want

626:18

these two methods to run parallel so

626:21

while it is printing High it should also

626:23

print hello on the side by side okay

626:24

it's not it's not working so what I am

626:27

imagining with my code is what should

626:29

happen is see uh there is a main method

626:31

right so main will continue its work

626:33

here so main will keep executing

626:36

everything now there will be a point

626:38

where main wants to execute the show

626:40

method but with two different threads so

626:42

I want a show method here and I want a

626:45

show method here of course this show

626:47

method the first one is of a the second

626:50

one is of B but then your our main When

626:54

Calls show it should be with the new

626:56

thread so I want this to be a new thread

626:58

I want this to be a new thread okay now

627:01

just by mentioning that this class is

627:04

thread it will not get new thread for

627:05

you basically we want two threads now

627:07

the way you do that is by not calling

627:11

show what you have to do is you have to

627:12

call a method called start because when

627:15

you are thinking that at this point I

627:18

want to start a new thread you know we

627:20

want to start a new thread here we want

627:22

to start a new thread here we can do

627:24

that with the help of a start method so

627:26

we have to call this method and that's

627:27

what we are doing here so you have to

627:28

say start for both the threads okay see

627:31

we are saying that those are the obj102

627:33

but there are threads now

627:35

now the only thing is

627:37

when you call start it should also say

627:40

start right

627:41

is so now think is behind the scene when

627:44

you go to start so you can see if I go

627:46

to start start is a method belongs to a

627:49

thread class

627:51

so of course you can do your work here

627:52

but the thing is it will create new

627:54

thread for you

627:55

but it will call not the start method

627:58

even if you call it it calls a run

628:00

method so this thing you have to

628:02

remember in every thread you need to

628:04

have a run method that's how it works so

628:08

when you say start here it's it's almost

628:10

like you know when you are when you want

628:12

when you want to participate in a race

628:15

right so we have multiple we have

628:17

multiple Lanes there right so we have

628:19

multiple Lanes multiple people are

628:20

waiting to start the game here and of

628:24

course you took a position to run but

628:27

then you will not run you will wait for

628:29

it right what you will wait for you will

628:31

wait for some volunteer here to say

628:33

start right the moment they they shoot a

628:36

gun or they say start you go right in

628:39

the same way the moment you say start

628:40

here it will execute this one method

628:43

okay that's the point you have to

628:44

remember

628:45

okay let's see if this works now uh we

628:48

have done everything what we could have

628:49

done to make it work and let's say it

628:52

works oh okay

628:54

still not working now what is happening

628:56

here

628:57

see the thing is what if I say they are

628:59

actually running parallel the only thing

629:01

is when we are saying that see we are

629:03

starting with this one right obj1 and

629:06

then we are assuming that obj1 will

629:09

execute one high and parallely the

629:12

second thread which is the B thread will

629:14

execute hello at the same time

629:16

but the moment you give some time

629:19

because the machine is so fast now the

629:21

moment I give I have given some time to

629:23

the eighth where to execute it is

629:26

executing the entire thing at the same

629:28

time

629:29

okay let me show you what I'm saying

629:30

what if I do this for 100.

629:33

okay

629:35

and let's see what changes so if I

629:37

compile this code and run if you can see

629:40

if I scroll up things are happening in

629:43

parallel

629:44

okay so you can see we got few highs

629:46

here and then we got few hellos and then

629:49

we got few highs we got few hellos

629:52

okay things are running in parallel so

629:55

these two methods are actually running

629:57

at the same time

629:58

yeah so what happens is behind the scene

630:00

there's a concept of scheduler okay so

630:02

in your OS we have a concept of

630:04

scheduler now scheduler will say okay if

630:06

you want to execute something you have

630:08

to come to me first now it doesn't

630:10

matter how many threads have you have in

630:12

your system of course in my machine I

630:14

have shown you that in the last video If

630:16

you go to activity monitor you can see

630:17

we have so many threads here we have

630:20

five 24 threads running and only for one

630:24

task in total we I got 2382 threads now

630:28

of course you can't execute all at the

630:30

same time right now the thing is

630:32

nowadays we have multi-core CPUs so you

630:34

have multiple cores there right uh and

630:37

the beauty is you can execute multiple

630:39

thread at the same time so you can

630:40

execute let's say if I have four cores I

630:42

can execute fourth at the same time if I

630:45

have eight core CPU which is OCTA code I

630:47

can execute eight threads but we have 2

630:50

380 94 threads now how will execute all

630:55

so of course you can't execute all at

630:57

the same time right so they go for the

630:58

time sharing so scheduler in between so

631:01

let's say this is your OS here okay so

631:03

there's a concept of scheduler which is

631:04

responsible to basically allow

631:07

or thread to execute let's say we have

631:09

multiple threads waiting to execute okay

631:10

so let's say we have eight threads here

631:12

and then we only have two cores

631:14

available so on your CPU basically you

631:17

have only two cores right so security

631:19

knows you can actually run only two

631:21

threads at the same time so what it does

631:22

is it basically allows let's say uh This

631:25

Thread and this thread to execute and

631:27

then after they they execute they will

631:29

allow This Thread and this thread to

631:31

execute right so it is schedule a job to

631:33

allow which thread to go to execute on

631:36

the OS okay uh and that's why

631:40

so when when your scheduler allows some

631:43

time for your threads to execute your

631:45

thread might be executing this number of

631:47

iteration at the same time okay and

631:50

that's what's what is happening here so

631:51

basically we are running multiple pledge

631:53

at the same time

631:55

so yeah that's how you create multiple

631:56

threads with the help of the authority

631:58

class okay now let's try to execute this

632:00

thing simultaneously so what I want is I

632:02

want to of course they are running at

632:03

the same time but what I want here is I

632:05

want to execute High and Hello one after

632:07

the other so example if you can see the

632:10

output here we are printing so many

632:11

highs and then we are going for the

632:13

hellos right how do we do that see the

632:15

thing is you cannot control your

632:17

scheduler you can only suggest your

632:18

scheduler to give the priority so yeah

632:21

we have a concept of threads priority as

632:23

well example we have two threads right

632:25

we have obj one object two and we have a

632:28

concept of threads priority so let's say

632:30

let's I just want to understand what is

632:32

the current priority of the thread so I

632:33

can say obj one dot this is a method

632:35

called get priority which will give you

632:37

the current priority of a thread okay

632:38

let's try to print this let's see let's

632:40

see what happens compile there is an

632:42

issue okay we forgot to put one loan

632:45

bracket and compile this code and

632:48

run you can see it is printing all this

632:50

thing and we got thread variety where

632:54

it's here so you can see we got five

632:56

okay so since we are uh starting the

632:59

threads after printing the variety you

633:01

can see that's why the we got the value

633:02

first but we got the priority as five

633:05

now what is this by ID what's the range

633:06

basically so the range of the priority

633:09

goes from 1 to 10. so one is the least

633:12

priority 10 is the highest priority or

633:14

maximum priority and five is default

633:17

priority which is normal okay so by

633:19

default every thread will be having a

633:21

normal priority so what you can do is

633:23

you can change the priority of a thread

633:25

uh maybe you want the second thread to

633:27

have a higher priority so you can say

633:29

obj dot set priority and you can mention

633:32

the value so of course you can mention

633:34

one which is the lowest or you can

633:36

mention 10 which is the highest or you

633:39

can use some constants so thread class

633:41

has some constants which we can use

633:44

example we have Max priority we got Min

633:46

priority we got numpy ID right so if you

633:49

want to specify maximum priority you can

633:51

do that with the help of this constant

633:52

otherwise you can also say let's say if

633:55

you want to set the priority nine so you

633:56

can say Max by T minus 1 that will be 9

633:58

right but then by doing this it's not

634:02

like you will get all hello first and

634:04

then it will be high so even if you see

634:08

we are Printing High and then it is

634:10

printing hello and it's still hi hello

634:14

hi hello it's it's working in uh

634:16

simultaneously

634:18

the only thing is when you are

634:20

specifying that this the priority for

634:22

this thread is the maximum you are

634:25

basically suggesting to your scheduler

634:27

hey you know this thread should get a

634:29

highest priority it's not like scheduler

634:32

will give you that variety scheduler is

634:34

see different schedule have different

634:36

algorithms to work with and if it feels

634:39

okay now we have multiple threads and

634:41

they are reaching to a point at the same

634:42

time scheduler can say okay now I will

634:45

say the priority or maybe a scheduler

634:47

will also see who will take the lowest

634:49

time so if there's a threat which will

634:51

take less time so it might go for that

634:53

one so again as I mentioned different

634:55

scheduler has a different algorithm to

634:57

work with using priority you can only

634:59

suggest what should be done okay that's

635:01

one

635:02

so we know now how to set the priority

635:04

and get the priority apart from that if

635:07

you still want to make this work one

635:08

thing we can do here is

635:10

after printing hi see what is happening

635:13

is as a scheduler will give you some

635:14

time to execute right so let's say

635:16

schedule says Hey thread a you can

635:18

execute for let's say one millisecond

635:20

and we were assuming that in one

635:22

millisecond it will print one high but

635:24

no my machine is so fast in in fact most

635:27

of the machines are so fast nowadays

635:28

right uh in that one millisecond it is

635:31

executing this number of highs how do I

635:34

control this so one thing we can do is

635:36

after printing every High let's ask our

635:39

thread to wait

635:41

okay so the way you can do that is by

635:42

using thread dot there's a method called

635:45

sleep

635:46

now in this clip you can basically

635:48

mention for how much time you want this

635:50

thread to sleep so you have to specify

635:52

that in milliseconds So when you say

635:54

sleep you are asking your thread to wait

635:57

wait for 10 milliseconds and then

635:59

continue okay so what we are basically

636:02

trying to do is once you print high at

636:04

this particular threader the A3 will go

636:06

into weight stage right waiting stage

636:09

and then the B thread will execute hello

636:12

and again once B thread done is work you

636:15

can just put a sleep there as well now

636:17

the only thing is this Sleep Method will

636:19

throw as a remember we talked about

636:21

checked exception so this Sleep Method

636:24

basically throws a interrupted

636:27

exception so what you have to do is we

636:29

have to put a try catch all we can set

636:31

those but let's put try catch here so

636:34

with this suggestion I can say surround

636:36

with try catch and our job is done

636:39

right and just to simplify this what I

636:41

can also do is I can put this in one

636:43

line so you can see we got try catch and

636:46

then just to reduce the number of lines

636:47

we have we I'm just writing that in one

636:49

line the catch block the same thing can

636:52

be done for the hello as well

636:53

and you can see we got the sleeper as

636:56

well so we got two threads and now I

636:59

hope it will work let's compile and

637:03

run okay see that we are getting high

637:07

hello in sequence uh if you start from

637:10

here you can say we got hi hello hi

637:13

hello and then go on but if you can see

637:16

this is where you got two two hello at

637:18

the same time now it is possible that

637:21

after doing this calculation after doing

637:23

this iteration of hi hello

637:25

two threads are going back to schedule

637:27

at the same time and schedule has given

637:30

the chance to hello first so it all

637:32

depends upon how you work with threads

637:34

how I mean not you you can only make

637:36

multiple so it's all depend upon your

637:37

schedule how to run this and that's why

637:40

when you play games sometime you know

637:41

you can see some of the characters are

637:42

not moving a sequence or not in sync

637:45

because the threads are not working

637:47

properly there

637:48

so as a programmer you can just optimize

637:50

it more okay you cannot control it one

637:53

thing I can do is if I want the hello to

637:55

come after that maybe I can just put a

637:58

a sleep here as well

638:00

maybe of

638:01

two milliseconds so after so I I just

638:04

want both both of them to start with a

638:07

gap in between of two milliseconds

638:10

so compile and run and if you can see

638:14

I hope there will not be any High hello

638:16

which got two time print you can see now

638:18

it's all once it's because there's a gap

638:21

in between hi and hello who is reaches

638:24

to the schedule at the same time so yeah

638:26

that's how you can optimize again as I

638:28

mentioned we are just trying to optimize

638:30

it there's no guarantee that you will

638:31

get this output every time oh we got it

638:34

cancel that we got two hellos here again

638:36

my idea was just to optimize it and it

638:38

depends upon your schedule how to work

638:40

with this or maybe I can just if I make

638:42

it five then we can control that so try

638:45

this out and let me know how it is for

638:47

you and your machine

638:49

so we have understood two methods uh two

638:51

things from this video one is the

638:53

priority so you can set the different

638:55

priority for the threads main priority

638:57

Max priority and normal priority and you

638:59

can set any number between one to ten

639:01

and by doing this you're only suggesting

639:03

to your scheduler to give the current

639:06

threat my example IIT if you set it that

639:08

way and then we have talked about sleep

639:10

now when you say sleep it actually goes

639:12

into a waiting stage again we'll talk

639:13

about uh what is that waiting stage or

639:16

block State bit later but uh yeah that's

639:19

a stage which follows there are multiple

639:21

way is to create a thread one thing we

639:24

have seen which is of thread by showing

639:26

a class by thread but we have more

639:28

options but why do we need more options

639:30

now think about this let's say this

639:32

class A want to extend some other class

639:34

let's say we have one more class here

639:38

um and the class name is z and this P

639:40

the it I want this to be a parent class

639:42

and a to be a child class

639:45

so I want to basically make this a as

639:47

thread as well as I want to make this as

639:50

a subclass of Z and we all know right

639:54

the multiple inheritance in Java is not

639:56

possible so how do we solve this problem

639:58

so this idea of extending Authority is

640:01

not actually a good idea so the other

640:03

option we have is okay let's go back the

640:06

other option we have is if you click on

640:08

this threader If You observe thread is a

640:11

class which implements Venable okay and

640:14

if you go to the enable you can see we

640:16

have the method called run now this is

640:18

important right if you remember in the

640:20

code which we have written we have went

640:22

with a class name we have went with a

640:24

method name called run right that

640:26

actually means

640:28

that if I implement or enable here

640:31

instead of using a thread even this

640:34

should work

640:35

right because it has another method

640:37

so what I will do now is instead of

640:39

extending with a thread I will simply

640:41

implement it with the help of an

640:43

interface called runnable okay so both

640:46

the side now I have a choice of

640:47

extending a class as well if you have

640:49

some other class because we can have a

640:52

class which is extending another class

640:54

or implementing an interface at the same

640:55

time

640:56

but the point is you can create thread

640:58

like this as well you are making this

641:00

class A as runnable okay now by doing

641:04

this what how can you work with this

641:07

don't mind you go down first of all let

641:09

me just remove this we don't need this

641:10

code in fact you know before I continue

641:12

I just want to make some more changes to

641:14

the code uh the first the first thing

641:16

I'm going to change instead of having uh

641:19

let's say 100 question let's go for five

641:21

iterations so that we can see the output

641:22

fast

641:23

and yeah that's the thing okay so the

641:26

start will not work you know why the

641:28

start will not work is because in the

641:30

vulnerable interface we don't have a

641:31

start method the start method before it

641:34

was coming from third class okay that's

641:36

weird so we can't use this right

641:38

see the idea is if you want to if you

641:41

have a runnable class example this a is

641:45

actually a runnable class right a class

641:47

which is implementing an enabled so in

641:49

that case if you want to work with start

641:51

you can't do that so in that case what

641:53

you can do is you can create a two

641:56

different threads you can say T1 this is

641:58

your first thread

642:00

and then you got the two this is your

642:03

second thread right so we got two

642:06

threads here and instead of saying obj

642:09

one dot start we have to say T1 dot

642:11

start because we know that start method

642:13

belongs to a thread

642:15

and to also right you can see now we are

642:17

able to call start now let's see if this

642:20

works I will just go back here

642:22

and compile

642:25

run

642:26

oh that's weird we have not got any

642:29

output

642:30

see a thing is yes we got two threads

642:33

yes we are saying start it it's actually

642:35

creating two threads behind the scene

642:37

but it's not doing anything because

642:40

these two threads have no idea about

642:43

this obj1 ob2 they have no idea about

642:46

these two classes

642:48

so in that case what we do is if you go

642:50

to threaded Constructor you can see

642:52

thread has multiple Constructor one of

642:54

the Constructor takes as an enable

642:56

object

642:57

that means

642:58

obj1 here is actually a runnable object

643:01

you know let me prove that so instead of

643:03

using the a can I use runnable and we

643:05

have seen that right we can create a

643:07

reference of the interface and object of

643:09

a class

643:11

even here we have to say

643:14

the reference of the interface and

643:18

object of a class okay so what we can

643:20

now do is in this particular Constructor

643:22

of thread we can pass obj1 and here we

643:25

can pass object 2. so we are passing the

643:29

runnable object in the thread class or

643:32

the thread Constructor now there's a

643:34

link right so when you say start it will

643:36

know that which run run which run to

643:38

call so this start will call the Run of

643:41

these two classes let's see if this

643:43

works

643:45

I will say clear

643:47

compile run oh it worked and see that we

643:50

got hi hello hi hello and it is working

643:53

so basically whatever you want to

643:56

a thread we have two choice you can

643:59

extend our thread class or you can

644:01

Implement a enable interface the only

644:03

thing is this enable interface will not

644:05

have the thread methods in that case you

644:08

need to create a separate thread object

644:09

and then we can use these features

644:13

okay now if you remember we have also

644:15

learned about Lambda expression right so

644:17

like let's reduce this code by

644:19

implementing Lambda expression okay and

644:21

let's do that step by step

644:23

now if I ask you one thing the entire

644:26

class A why we are getting this class we

644:29

are creating this class is because we

644:31

want to implement runnable right

644:34

okay that makes sense how about if I

644:37

remove this class so of course this

644:39

variable will not work example let's say

644:42

if I comment the entire section here

644:44

okay when this part

644:48

come in the entire section you can say

644:50

this will give you another so I want to

644:52

create an enable obj1 but I can't create

644:55

a because we don't have a so can I say

644:57

news enable

644:59

of course we can right we have seen how

645:01

do we uh create how do we use anonymous

645:03

class to create the object so imagine

645:05

this is anonymous class right now in

645:07

this Anonymous class we have to

645:09

implement the method which is that in

645:10

learnable now which method is there the

645:13

same method which we have here so can I

645:15

just copy this code from here and I can

645:17

say copy

645:19

and paste let me remove the entire

645:23

comment section so basically what we're

645:25

doing is we are instant we are trying to

645:26

instantiate our interface with with the

645:28

help of a Anonymous class here

645:30

and in this class which is which has a

645:33

method which is run and basically we can

645:35

we are basically implementing this

645:36

Venable here itself right

645:38

okay that works let me just try if this

645:41

works before we continue

645:43

so I will say compile and run it worked

645:46

and see that we got the output okay

645:47

that's great

645:48

okay what else we can do uh can we do it

645:52

for the second one as well yeah we can

645:54

right but before we go for that one can

645:56

I convert this into a Lambda expression

645:59

and we have seen that right Lambda

646:00

expression works whenever you have a

646:02

functional interface and runnable is

646:04

actually a functional interface which

646:06

means I can use Lambda here and in

646:09

Lambda this is optional

646:11

so I can remove this I can put a arrow

646:14

here and I can remove this bracket as

646:16

well

646:17

okay this looks cool and I can just put

646:20

that back here okay so as you can see I

646:23

have written everything in one line for

646:25

the try so that you know it will be over

646:26

in one line and now uh can you can we do

646:30

it for the second object as well of

646:32

course I can I can just copy this code

646:34

and the only thing we know we need to

646:36

change is

646:38

the hello part right so from high we

646:42

have to make this hello that's the only

646:44

change I have to make and I don't even

646:46

need these two classes now I can remove

646:48

them and see the number of lines we have

646:50

reduced and also if you know that this

646:53

class would be used only one so this

646:55

makes much more sense to use right uh

646:57

the anonymous class and we can convert

647:00

that into Lambda as well so this is the

647:01

uh the Lambda version of it cool and now

647:05

we can simply run this and this should

647:07

work

647:09

compile and run and it works and that's

647:13

the beauty about Lambda Express right

647:14

and you can reduce the number of lines

647:15

you have

647:16

and there's no extra classes see it will

647:19

be tricky to type the entire thing from

647:21

start to end because till this point

647:24

what we are doing is we are trying to

647:25

convert the anonymous class into Lambda

647:27

expression which is easy by typing the

647:29

Lambda in the first scope will be

647:31

difficult so just try it out multiple

647:33

times you might make mistakes with the

647:35

syntax But ultimately you will get used

647:37

to it

647:38

so those are the two options we have

647:40

creating with the help of extending with

647:42

a thread and by implementing the

647:43

interface runnable

647:45

in this video we'll talk about race

647:47

conditions now we have talked about

647:49

threads right and till this point

647:51

everything looks good because we now we

647:53

know that threads are important if you

647:55

want to execute multiple things at the

647:56

same time our threads will be useful now

647:59

sometimes we create two threads

648:01

sometimes we go for multiple threads

648:02

maybe eight threads ten threads it

648:04

depends upon your application and as I

648:06

mentioned before there are certain times

648:08

when you actually write threads by

648:11

yourself otherwise most of the time uh

648:13

your Frameworks will do it for you okay

648:16

let's say you create your own threads

648:18

okay and then we know that threads are

648:20

good on the other hand we have one more

648:22

concept right so let's let me just write

648:24

it down here so if we talk about a

648:26

concept like threads threads are

648:28

important we know right apart from this

648:30

we have one more concept which is called

648:32

mutation so if I say mutation here now

648:36

what do you mean by mutation mutation

648:37

simply means that you can change

648:39

something example if you talk about

648:40

variables so primitive types variables

648:43

or object type variables we can change

648:45

is them right example if you have ain't

648:47

I so let's say if I say ain't I is equal

648:50

to 5 and then later on I'm changing this

648:53

value of I I'm saying I plus plus maybe

648:56

after some time I'm assigning the value

648:57

for I to 6 I can change this value right

649:00

and it is actually useful to mutate a

649:04

value of and of course that's what we do

649:06

with digital processing right so when I

649:07

say you got data you want to process it

649:10

most of the time you might want to uh

649:13

change the value of your variable and

649:15

that makes sense right so that is

649:17

mutation but then if you remember when

649:20

we talked about strings uh we have

649:22

mentioned that strings are immutable

649:24

right so there are some Concepts which

649:26

which makes them immutable so which is

649:29

good mutation or immutability uh at this

649:32

point if you if you can change something

649:34

notation looks good right so yeah so

649:37

till this point mutation looks good but

649:38

then let's try to discuss something more

649:40

what about threads our threads are good

649:42

of course if you want to make things

649:44

faster threads are good but then what if

649:47

you mix both of this what if you are

649:50

doing mutation with multiple threads

649:51

will this work so what I'm saying is

649:54

what if you have two threads let's say

649:56

T1 and T2 and both are trying to work

649:59

with the same variable I so T1 changes

650:02

the value of I T2 changes the value of I

650:04

now it will create instability in your

650:08

code I mean think about this but what if

650:10

you just have one bank account and you

650:12

got two cards two debit cards of course

650:15

as a single person you cannot use two

650:17

cards at the same time but what if you

650:19

have given the other card mind you I'm

650:21

talking about the same account so we

650:22

have one bank account and we have two

650:24

cards what if you give one one of the

650:26

card to your friend and then you went to

650:28

two different ATM machines okay and now

650:31

you are trying to uh withdraw the money

650:34

so let's say you in your account you

650:36

have 10 000 rupees and both of you at

650:39

the same time at the same second you try

650:42

to withdraw the money

650:44

you try to withdraw let's say seven

650:45

thousand seven thousand so in total

650:47

you're trying to withdraw 14 000 rupees

650:50

the balance is only ten thousand rupees

650:52

so that's an issue right if you try to

650:54

do things simultaneously and we'll also

650:56

do that in the code as well just to see

650:57

what happens so threads and mutation

651:00

together is not a good idea okay and

651:02

that's why if you are working with

651:04

threads always try to make sure that you

651:06

work with data which are immutable or if

651:09

you think that there's a method which is

651:11

which can do some mutation

651:14

make it thread safe now what is the

651:15

concept of thread safe so thread safe

651:18

simply means so if I say thread save

651:20

thread safe simply means that only one

651:23

thread

651:24

can work with that at one point example

651:27

if you have a method let's say show

651:29

and the show method so there are two

651:33

methods we're going to call so T1 and T2

651:35

so only one of them can be able to get

651:38

into show at one time so if T1 is

651:41

executing a show T2 cannot execute so

651:43

okay that's the Restriction we can add

651:45

but how let's see that in the code here

651:49

so what I will do is first of all I will

651:51

remove these two threads or maybe we can

651:53

just use these two threads as they are

651:55

so you can see we got two threads here

651:57

the only thing is I want to make sure uh

652:00

the iteration is not for

652:02

five the iteration is there for let's

652:05

say 1000 and maybe I don't want to print

652:08

something I don't want to apply a Sleep

652:10

Method just keep it clean and let me

652:12

just clip this clean as well so you can

652:14

see we got two threads and both are

652:17

doing something but as of now you can

652:19

see we are not writing any code here the

652:21

for Loop is basically empty

652:23

so what we can basically do here is

652:25

first of all what I want to achieve just

652:27

a very simple thing let's say I have a

652:30

class here called counter and in this

652:33

counter basically I have a method which

652:34

is public wide increment okay so that's

652:38

the increment method we have and then

652:40

let's also create a variable so let's

652:41

say the variable is Count okay and by

652:44

default the value for count will be 0

652:45

because that's an instance variable and

652:47

then inside this increment I'm just

652:49

saying count plus plus okay

652:52

so basically what we have is we have a

652:54

class called counter and in this we have

652:56

a variable called count

652:58

and then every time someone calls

653:00

increment it will be doing count plus

653:02

plus and of course if I call increment

653:04

once the value of account will be one if

653:07

I call increment 10 times the value of

653:09

count will be 10 right but then what I

653:12

will do now is I'm going to call this

653:16

counter or this this increment with a

653:18

multiple times

653:19

so before we even use that let me create

653:22

the object of counter I will say counter

653:24

C is equal to new counter and then from

653:29

this two threads what we have we can

653:31

simply say C dot increment so basically

653:35

I am going to call increment

653:38

multiple times now you tell me how many

653:40

times you are going to call it so of

653:42

course I'm going to I'm saying that

653:43

increment in both the threads and each

653:46

thread is going to do this

653:47

incrementation increment for 1000

653:50

iterations right now when I say 1000

653:53

10 000 in total this increment will be

653:56

called for 2000 right

654:00

okay let me just Retreat we have two

654:03

threads each three is calling increment

654:05

thousand times so in total the increment

654:07

will be called 2 000 times right so if I

654:10

go back here

654:12

and after it starts I just want to print

654:15

the value of count let's see what the

654:17

what's the current value of count is

654:20

now you tell me what will be the current

654:21

value of count so if I compile and run

654:24

now when I run this code I want this to

654:26

be 2000 right let's say what happens oh

654:30

it's only one one six the weird thing is

654:33

every time we run this code you will get

654:35

a different output for this what's

654:36

happening

654:38

see what we are trying to do here is let

654:40

me just do that on my pad here see we've

654:43

got a main thread right this is your

654:44

main thread and on line number

654:48

uh 34 and 35 what we are doing is we are

654:51

creating two two new threads we got T1

654:54

and T2 now T1 jobs is to call increment

654:58

thousand times right and T2 job is to

655:01

call increment thousand times and

655:02

ultimately we want this count variable

655:05

to be around 2000 right

655:08

but the thing is the main is doing

655:09

nothing right Main in main we are saying

655:12

hey just print the value of count

655:14

now we were expecting that T1 and T2

655:18

will complete the work and then main

655:20

will print count but that's not the case

655:22

anyway these two we have two threads

655:24

right T1 T2 and they are running any

655:26

anyway now when these two threads are

655:29

running in any case

655:31

they are running right main is doing

655:33

nothing so main says okay my job is done

655:35

I have started these two threads and now

655:37

I will simply print the value of count

655:39

so in between the iterations maybe T1

655:42

has done some iterations T2 has done

655:43

some iterations main is printing count

655:46

anyway we have to ask main hey you know

655:49

don't print count here let them come

655:51

back so as main continues let them come

655:54

back let T1 and T2 complete their work

655:57

by doing 100 iterations when T1 comes

656:00

back and T2 comes back

656:03

after this just try to print the value

656:05

of count and I'm sure at that point the

656:08

count will be two thousand so what I can

656:10

do is uh how do I ask main to wait for

656:13

T1 T2 so one thing you can do is you can

656:16

say T1 dot join now join in a special

656:18

method which allows your main thread to

656:21

wait for the other two threads to come

656:23

back and join

656:24

okay so if at this point if you want to

656:27

wait for these two threads you can use

656:30

join there okay but the only thing is

656:32

join me through an exception so I can

656:34

simply say add those declaration you can

656:38

see we got close exception anyway we

656:40

have done with the join and now I think

656:42

it should be good we should be able to

656:45

see two thousand so if I compile and run

656:48

oh

656:50

okay we were expecting 2 000 but we got

656:54

okay now we got two thousand now we got

656:56

1746 and that's why you know threads are

656:59

sometimes be unpredictable because they

657:02

have a different behavior and

657:03

unfortunately they cannot control

657:04

directly uh so what is happening here

657:07

the example which we have taken before

657:09

uh which was about the bank account it

657:12

is possible then when we have two

657:14

threads so we got T1 and T2 they both

657:18

are executing the same method and in

657:22

this method what is happening so when I

657:23

say increment it is actually getting the

657:25

value of count if you see we are saying

657:27

count plus plus right it's actually two

657:29

steps first it will get the value of

657:31

count the current value of count and

657:34

then it will say Okay count is equal to

657:36

count plus one because that's how count

657:39

plus plus works right so there are two

657:41

steps get the value for count and

657:42

whatever new value you get just add it

657:44

with one and then assign it to count

657:46

back so let's say when T1 reaches to

657:49

count the value of count is zero right

657:52

so if I just say this is the count value

657:54

is 0 then 0 plus 1 is 1. so the new

657:57

count value is one again if we trade

658:00

this part so this will be one the value

658:02

of count is one right so T2 goes there

658:04

T2 says okay uh give me the value of

658:06

count it says one plus one is equal to

658:09

two now again T 1 goes back T1 says okay

658:12

new value is two I will say 2 plus 1 is

658:14

equal to three and that's how the

658:15

iteration should go right but what if

658:17

both the threads reaches to this count

658:20

at the same time so let's say T1 and T2

658:23

both are reaching to do count at the

658:25

same time they both got the value which

658:27

is three they both said okay three plus

658:29

one is equal to 4. we got two iterations

658:32

I mean we called increment two times but

658:35

the incremented happens only once

658:38

okay that's what happens when you have

658:39

two threads working with the same

658:41

variable or shared variable

658:43

oh okay so mutation and threads are not

658:46

good and every time you run this you

658:47

will get different output now how do you

658:49

make this consistent okay you might be

658:51

thinking why we got two thousand years

658:52

because maybe your threads are not going

658:55

to the increment at the same time okay

658:57

so that's not the case in fact if you go

659:00

with a bigger number if I go with ten

659:02

thousand and ten thousand you can see it

659:05

will lose lot of different uh bits in

659:07

between

659:09

compile and run you can see 7000 we are

659:12

losing 7000 values we got nine eight

659:14

thousand values lost

659:16

right and the chances of getting 20 000

659:18

is way less now

659:20

so the point is this is not a good way

659:22

now how do you make sure that only one

659:24

thread works with that at one time

659:26

that's why you can do it right so

659:28

example event T1 is working with

659:29

increment T2 cannot work with increment

659:32

okay so T2 has to wait

659:34

in that case you can use a very simple

659:36

keyword you can use a keyword called

659:38

Synchro nice

659:41

you just use this one keyword your jaw

659:44

will make sure that this increment will

659:45

be called only by one method at a time

659:48

so if T1 is working with increment T2

659:50

has to wait

659:53

so if I compile this code now

659:56

and run you can see we got 20 000. now

659:59

it doesn't matter how many times we run

660:00

this you will get 20 000 because now

660:02

both the threads are not going to the

660:04

diet method at the same time

660:06

that's how we used two threads with the

660:08

shared variables and you make them as

660:10

synchronized so basically we saved

660:13

ourselves from this condition now in

660:15

this video we will talk about different

660:18

diff of a threader now if you remember

660:20

we have started with creating a thread

660:22

then we uh talked about the thread is

660:25

running and then we have talked about

660:26

Sleep Method we have talked about joint

660:29

method so basically we were basically

660:31

playing with the thread States right

660:33

where the thread was running and then we

660:35

asked this way to wage come back right

660:39

so how do we what are different states

660:40

available try to understand that see

660:43

every time you create a new thread that

660:46

goes into a new state okay so this is

660:49

your new state now apart from this new

660:52

state we also have something called a

660:54

runnable state okay so we have uh the

660:57

naval state

660:58

now after that we also have a running

661:01

State then we have a waiting state

661:05

or we can say also say block state

661:08

and then the last date is we have is

661:11

dead okay so we have

661:13

all the states available for the thread

661:15

now when you say new state what exactly

661:17

happens so when you create a new third

661:19

example in the code if you remember when

661:21

we said uh thread T1 equal to new thread

661:23

at that point it becomes into a new uh

661:26

the new state

661:27

now the moment you say start you know we

661:29

have this method we just start so when

661:31

you say start it goes to an enabled

661:32

state so maybe we can say start so when

661:35

you say start it says it goes into an

661:36

enable state

661:37

now there's a difference between

661:38

vulnerable state and running

661:40

the difference is when your thread is

661:43

actually running on the CPU that is

661:45

running state but when your thread is

661:49

executing and then uh it is waiting for

661:52

the scheduler that is into an enable

661:54

state so example when your thread is

661:55

running with the help of run method that

661:57

is your running State now what if while

662:01

you are running this and then maybe you

662:03

want to keep it on hold with the help of

662:05

Sleep Method

662:07

so that goes into waiting State now

662:09

apart from sleep we also have a method

662:11

like weight which can make it go into

662:14

wedding state right so when your thread

662:16

is running State you can also say wait

662:18

now wait will take it to the waiting

662:20

state right now how will you come back

662:21

now you will come back with the help of

662:23

notify the only thing is when you say

662:25

notify you will not come back to running

662:27

State you will go into the enable State

662:29

now whenever the scheduler is assigning

662:32

with the task or whenever scheduler says

662:34

it's your turn at that point it will go

662:36

into running State uh in fact if you if

662:38

your 30 is running and if you want to

662:39

keep it there you can also say stop

662:42

there's also stop is available but that

662:43

is deprecated actually don't don't stop

662:45

threads okay it will create instability

662:48

in your software but yeah once the work

662:50

is over it will automatically going to

662:52

stop State uh you can also send a thread

662:55

from the runnable to dead even with the

662:57

help of stop

662:58

even you can do that uh what else we

663:01

have here

663:02

okay so basically if we have this option

663:03

so whenever you create a thread that is

663:04

good that goes into a new state and then

663:07

when you say start it goes into enable

663:08

now depend upon your availability of

663:10

your CPU that goes into running State

663:13

now while running it will execute

663:14

certain things and then if at that point

663:16

if you want to say wait you can do that

663:18

with the help of sleep or you can do

663:19

that with the help of weight now the

663:21

only thing is when you say wait you have

663:23

to use a notify method to come back the

663:26

only thing is when you say notify it

663:27

will not come back to running status it

663:29

will go to the learnable state right so

663:32

it goes from the waiting to learnable

663:35

from running to waiting waiting to

663:36

enable that's the cycle it follows and

663:38

then again from runnable to a running

663:40

State yeah those are the states

663:41

available we have this number of states

663:43

in Threads especially in Java now let's

663:46

talk about collection the funny thing is

663:48

in this topic we have three different

663:50

types of collection it starts with small

663:52

say which is a collection API so let me

663:54

just write down somewhere uh so we have

663:57

a concept called The Connection API then

664:00

we have a collection

664:03

and we have collections now this is the

664:06

only thing I think it's confusing in

664:08

this topic now when I talk about

664:10

collection API I'm talking about a

664:11

concept when we talk about collection

664:13

I'm talking about an interface and when

664:15

you talk about these collections with s

664:17

it is your class okay uh so if I say hey

664:21

something belongs to collection API that

664:23

means it belongs to this concept if I

664:25

say collection I'm talking about the

664:27

interfaces and then there are some

664:28

classes which implement this and then

664:30

we'll talk about collections which is a

664:33

class and it has multiple methods to

664:34

work with

664:35

okay now why I'm talking about

664:37

collection here so the thing is whenever

664:39

you want to group The Elements right

664:40

let's say if you have one value you can

664:42

store that in one variable right I can

664:44

simply say int uh num is equal to five

664:47

and I'm happy right the only thing is

664:49

what if you want to have multiple values

664:51

now that's what problem creates how do

664:53

you store multiple values and I know you

664:55

know the answer right the answer is

664:57

array and we have worked with it right

664:59

so I can simply create a nums here

665:02

which is an array and I can say new int

665:06

and bracket I can mention the size which

665:08

is five now this is your added right and

665:10

it perfectly works okay then why we are

665:13

talking about this discoloration topic

665:14

see the thing is if we talk about

665:16

working with data if you talk about

665:18

working with algorithms

665:20

there is a concept called data

665:22

structures right and I hope you have

665:24

heard about that in multiple places why

665:26

data structures are very important the

665:28

way you fetch data the way you the way

665:30

you store data is very very important

665:32

now sometimes example let's say if you

665:36

store data in a box here now sometimes

665:38

you want to save data and you want to

665:41

fetch the last element okay so whatever

665:43

element has been inserted last you want

665:45

to maybe you want to fetch that or maybe

665:48

uh you you have another type of data

665:50

where you want to insert data in this

665:52

values and the value you want to fetch

665:54

is the first value right so this is

665:56

where I'm talking about the last in

665:57

first out and this is what I'm talking

666:00

about first in first out just take a

666:03

normal queue right example if you want

666:04

to take a ticket you go you stand in a

666:06

queue right in the same way this concept

666:08

which I'm talking about here is Q uh the

666:11

concept which I'm talking about here is

666:12

stack right now apart from this there

666:15

are multiple data structures which one

666:16

to work with right and we can actually

666:19

Implement all these data structures with

666:21

the help of array as well there's no

666:22

harm in that so as a programmer you need

666:24

to do it right and it's good I know it's

666:27

fun the only thing is when you work on

666:29

our project you don't want to actually

666:30

work with an array where you create this

666:32

structure by yourself and that's where

666:34

they thought hey can we just create an

666:36

API and the beauty is it actually

666:38

introduced in the second version of java

666:40

1.2 The Collection API where you can

666:43

actually work with all these data

666:45

structures apart from this we also have

666:47

linked list right so you can work with

666:49

all these things by using some inbuilt

666:51

classes so it makes your work much much

666:54

easier and also when you want to fetch

666:56

data when you want an expandable array

666:58

that's right the thing is the attic

667:00

which we have worked with it is fixed

667:01

size example if I create an idea of size

667:04

5 it will be five right you can't extend

667:08

it but if you have a scenario where you

667:11

want to expand an array maybe you want

667:13

an element you want an array of seven

667:15

now

667:16

uh so of course in the existing area you

667:18

can't do that okay now that's one

667:20

problem of course you can find a

667:22

solution for this as well what you can

667:23

do is you can create a new model let's

667:26

say nums two

667:27

and you can say new

667:30

int and you can mention seven here so

667:32

you got a new array of size 7 and then

667:34

you can copy all the elements from the

667:36

above array but the thing is you have to

667:38

manually create an array you have to

667:40

copy the array and it sometimes it

667:42

becomes complex to do all these things

667:44

what if you can just get a class or this

667:47

feature where you can create an array

667:49

which is dynamic right so it's quite

667:52

exciting to work with creation API where

667:53

you can store data and you can use

667:55

different algorithms to work with it

667:57

what if you want to sort the array what

667:58

if you want to create an array of unique

668:01

values what if you want to have an added

668:03

to add is one where you have all the

668:05

keys one you have all the values

668:07

okay so another of course as I mentioned

668:10

before in other you can do it but then

668:11

what if you can get some API using which

668:14

it will help you in writing a code

668:16

now that's where we can we are going to

668:18

use collection API of course array makes

668:20

sense in most of the cases when you know

668:22

that the length will be fixed so you

668:24

will be using array now whenever you

668:26

have a specific requirement of different

668:27

algorithms and structures that's where

668:29

you will be using collection API okay

668:30

now once we know the theory of

668:32

collection let's start the

668:33

implementation and of course this will

668:35

make much more sense once we do it so if

668:37

you talk about collection and I'm

668:39

talking about the collection interface

668:40

now right remember we talked about

668:41

collection API which is a concept right

668:44

so in this concept we have multiple

668:46

classes interface to work with so the

668:48

first interface we are exploring here is

668:50

called a collection interface and if you

668:52

can see the collection interface belongs

668:54

to java.utl package as we have seen

668:57

before most of the classes which we have

668:58

used belongs to java.lank package and

669:01

that's why we have not imported them but

669:03

if we talk about this collection

669:05

interface it belongs to a java.util

669:07

package in fact when you talk about all

669:10

the collection classes interface belongs

669:11

to this package which is java.util okay

669:14

so we have to import that as well so you

669:15

can do that with the help of import and

669:17

you can mention the package name and the

669:19

interface name which is collection

669:21

now once you say collection you can

669:23

create a collection of numbers let's say

669:24

and then I can say new okay

669:28

now the only problem here is I can't say

669:30

new collection right because collection

669:32

is the interface right now you might be

669:34

saying hey you know it's simple you can

669:36

just create Anonymous class provide the

669:38

implementation maybe I can try but then

669:40

if you can see the collection interface

669:43

has so many methods and we have to

669:45

Define all the methods see when you talk

669:47

about API API simply means a a library

669:50

or some feature which you can simply use

669:53

it you don't have to implement them then

669:55

what's the use of using an API right so

669:58

what I will do now is I will remove the

669:59

entire part I don't want to I don't want

670:01

to Define this method by myself

670:03

the second option we have is if you

670:05

don't want to create your own class can

670:07

we use some in the classes and that's

670:09

right we have so many classes available

670:11

which you can work with so if I want to

670:13

represent this so basically on top we

670:16

have something called a collection right

670:17

now this is an interface which we are

670:19

using now there are certain more

670:21

interfaces which actually implements The

670:25

Collection one of them is list

670:27

we have q

670:29

we have set so all these base all these

670:33

classes or all these interfaces

670:34

basically implements or basically

670:36

extends The Collection interface and

670:39

individually these interfaces have their

670:41

own class implementation example for

670:42

list one of the most famous one is at

670:46

the list so we can use this for Q also

670:49

we have different classes for set we

670:51

have a very famous class which is called

670:52

hash set so we have different classes

670:54

implementation example for Q we have

670:57

something called DQ which we can use uh

671:00

so for at least we have any list uh

671:02

apart from other list we also have

671:04

linked list which we can use if you have

671:06

worked with data structures before we

671:08

have a concept of linked list as well

671:10

and then in most of the languages we

671:11

Implement them by ourselves right but

671:13

then in collection API we get the

671:15

implementation in fact inside also we

671:17

have something called a linked

671:21

has set but then the most famous one or

671:24

simpler one is to use other list and has

671:26

set for this set now apart from this if

671:29

you talk about the concept of collection

671:30

the concept I'm talking about collection

671:31

API uh we have one more thing to work

671:34

with we have something called a map

671:36

right and map has multiple

671:37

implementation which we'll see when we

671:39

start with map okay so at this point

671:41

let's try to use either list or we can

671:44

use set but let's start with a list here

671:46

so what I will do is instead of saying

671:48

new collection I can say new add a

671:52

list and you can see this is a class

671:54

ignore this angular packet I know it is

671:56

coming from multiple times but ignore

671:57

that time bin so we got this collection

671:59

nums equal to new other list now in this

672:01

numps basically we can add values okay

672:03

uh now how do we add value which is very

672:05

simple you can just simply say nums dot

672:07

let's explore the methods you can see uh

672:10

we have method like add we have methods

672:13

add all contains uh equals for each okay

672:16

we'll say for each as well which is very

672:18

interesting uh then we have notify

672:20

notify all uh remove then we have we can

672:23

also convert a collection into array we

672:26

have to add it there and then we have a

672:28

weight method but at this point I just

672:30

want to add the values right so I can

672:32

just come back here and I can say add

672:33

and in this ad I can add values so let's

672:36

say I want to add value which is 6. so

672:38

since this is numbers I'm expecting that

672:40

this will only have numbers so I will

672:41

just copy this code and I will try to

672:45

add multiple values so let's say this is

672:47

5 this is 8

672:50

and this is 2. so basically we got this

672:52

value right now ignore this uh yellow

672:56

zigzag lines here we'll talk about that

672:59

later okay but yeah that's an important

673:01

concept which is coming up but at this

673:03

point I got this collection right and I

673:04

want to print this now if you have an

673:06

array of course you can also get array

673:08

with this elements but the problem there

673:10

is you need to you cannot simply print

673:12

an array right you have to use a loop to

673:14

print each element but that's not the

673:16

case with collection I can simply come

673:17

back here and I can say s out and I can

673:20

print nums and you can see if I try to

673:23

run this code

673:24

if I say

673:26

compile

673:28

okay so when you compile this code

673:30

there's no error but you can see there's

673:31

a warning here again we'll talk about

673:33

that watching in some time but let's run

673:35

this quota and see what happens and you

673:36

can see we got the output so we can

673:39

directly print a collection that's a

673:40

beauty okay uh can we print this value

673:44

one by one actually we can okay so what

673:47

you can do is you can use a loop here so

673:49

I can say for

673:51

we can use index values let's let's see

673:54

if do we have index option here so

673:56

against the nums dot uh if I say get or

673:59

unfortunately we don't have the indexing

674:02

for collection API or collection

674:04

interface here no issue I can use uh

674:07

enhanced for Loop remember we have used

674:08

enhanced model with the loop so I can

674:10

say int and colon nums

674:14

and I can print each n value so I don't

674:17

have to do this again how do we use

674:19

index that we'll see later but at this

674:21

point this should work

674:23

okay the only problem here is okay let's

674:25

let's talk about the problem which we

674:27

are getting this great uh this yellow

674:28

line here so the thing is when you talk

674:30

about a collection when you're getting

674:31

nums here right see in Array you specify

674:34

the type as well you say eight nums

674:36

right so you know that in this Nubs

674:38

you'll be having only integers

674:40

but when you talk about collection

674:41

nowhere we have talked about the type of

674:44

it

674:44

how it will work okay and that's where

674:47

what you have to do is you can mention

674:48

the type and that's the error you're

674:50

getting here because it says the values

674:52

which I have added here is not actually

674:54

numbers this are object type that's

674:57

right

674:58

uh as we talked about integer float

675:01

classes all these classes basically

675:03

extends the object class and by default

675:06

The Collection API works with objects

675:08

and this 6 5 8 2 they're not integers

675:12

they are objects and that's why you

675:13

cannot store that into integer how do we

675:16

solve this problem so if you want to

675:17

solve this problem we can use a concept

675:19

called generics

675:20

okay now how it works so basically where

675:23

should we mention the type

675:25

maybe what they could have done is you

675:28

know this is what they have done in 1.2

675:30

1.3 1.4 but the realized one problem

675:33

right which is by default all the values

675:36

here are objects

675:37

so how do we specify maybe they could

675:39

have created some extra classes by

675:40

saying integer list integer collection

675:42

string collection what if you have your

675:45

custom class like student so it will be

675:47

difficult for them to create all the

675:48

classes right so what they did is they

675:50

have given you one option where you can

675:52

use angular brackets here so just after

675:54

the collection use the angular bracket

675:57

and in that you mentioned the type of

675:58

element you want to work with so we are

676:00

working with integers so integer is

676:03

basically a class

676:04

ah which is the vapor class for the int

676:06

type because int is primitive integer is

676:08

a class and collection normally work

676:10

with objects not the Primitive values so

676:12

if you want to store integer numbers

676:14

which is you have to use a class integer

676:16

and you can see now there's no problem

676:18

there is no yellow zigzag lines and we

676:22

got the there's no error here as well

676:24

let's see if this works I will say

676:26

compile and

676:27

run and you can say we got the values

676:30

now one more thing why this type here is

676:32

so important see it also helps you to do

676:35

to remove the bugs of your code now what

676:38

bugs it can be example let's say if I

676:40

don't mention integer here okay let's

676:42

say remove if I remove this part and

676:44

everything is object right and then here

676:47

what I'm doing is I am

676:49

uh we can use an answer the only thing

676:51

is I can't use object here I have I

676:53

can't use in I have to use object and

676:55

then I am converting this object into

676:57

integer so we can do that by typecasting

677:00

it here so I can just type casting it to

677:02

integer in fact we can do it outside as

677:04

well let me just do that so I will say

677:06

int num is equal to I'm getting a num

677:09

variable again and I'm converting this n

677:11

to the integer type okay so you can see

677:14

we got num and what if this with this

677:17

num I'm performing some operation

677:18

because I want to perform the operation

677:20

that's why I can't use object

677:23

and now if you try to work with this you

677:26

can see again we got the warning because

677:27

we are not using type there and if I run

677:30

this code it works there is no problem

677:32

but what if by mistake you are doing the

677:35

code and you have added a number but

677:38

that too in double quotes

677:40

let's say five

677:42

now what will happen Okay so let's see

677:44

if I compile this code there is no

677:46

problem if I run this code we got an

677:48

exception and it's bad right and this is

677:50

also runtime exception this is runtime

677:52

error basically we don't want this and

677:54

that's why I think this type of problem

677:55

should come in compile time itself so if

677:58

you mention the type with the help of

678:00

generics here

678:01

and if I mention integer here as well

678:04

now if you can see

678:06

we will get the compile diameter not run

678:09

time error and that's why if you compile

678:11

this code

678:12

okay you can see we got a compile time

678:14

around and that's why it is better to

678:15

put this type every time so whenever you

678:18

work with collection always mention the

678:20

type there

678:22

cool so that's how we use analyst and it

678:24

has multiple methods now the only

678:26

problem is we are not using the we are

678:30

not able to get the index value right

678:32

see the thing is if we talk about

678:33

analyst as I mentioned in the diagram as

678:35

well analyst is a class if you can see

678:39

here so it's basically a class which

678:41

implements a list right and if you talk

678:43

about this list list does have a method

678:46

called get because list works with index

678:48

in the values

678:49

so which one to use should I use a

678:51

collection here or the list so if you

678:53

want to use if you just want to add

678:54

values and fetch collection makes sense

678:56

but if you really want to work with

678:58

index value instead of collection we

678:59

should use list because list supports

679:02

index value and you can see now I'm

679:04

working with

679:05

uh list here and now if I want to print

679:09

a particular index value let's say I

679:10

want to fetch the second index value

679:12

so I can say nums

679:15

dot get as you can see we have a get

679:17

method and in this get you can mention

679:19

two so which is it will in fact it will

679:21

give you three it will give you eight

679:23

which is at index number two right let's

679:25

try I will say in fact let me just not

679:28

print this Loop compile

679:32

and okay run and you can see we got

679:34

eight so if you want to work with index

679:36

you can use list and list has some extra

679:38

methods if you can see against a nums

679:40

dot add was that uh we got get we can

679:44

also set the value in between with index

679:46

value and anything else yeah we also

679:48

have index of

679:49

so if you have an element if you want to

679:51

know the index of it example let's say I

679:53

want to see I want to find the index of

679:55

this two value so what I can do is I can

679:57

say s out

679:58

I will say nums Dot

680:02

index of I want to print the index of 2.

680:05

right so 2 I'm talking about the value

680:07

now or maybe you know this is this looks

680:09

like quite confusing so I will say five

680:11

so index value is one if I compile and

680:14

run you can see we got one there so yeah

680:16

it has multiple methods and I would

680:18

recommend you to explore more methods of

680:20

list and that's how you use what you

680:22

work with list and other list now to

680:23

this point we have talked about least

680:25

right now list is one of the collection

680:27

API concept it it supports index values

680:30

right so we can create a list and we can

680:32

work with indexes there the only thing

680:34

is when you talk about list of course

680:35

it's great uh it does support multiple

680:38

values example let's say and that's a

680:40

feature now I'm not talking about the

680:41

drawbacks of it so I can say I can add a

680:44

element let's say six again if you can

680:46

see I have two I have two elements with

680:48

the same value which is six now if you

680:50

are wondering what is this e here again

680:52

this is given by my vs code it simply

680:54

means element and if you see if I try to

680:57

run this code if I compile this code and

680:59

run you can see okay we're not printing

681:01

all the values is it no we're not

681:03

printing all the values so I will just

681:05

remove this two lines and focus only on

681:07

the for Loop here and let's see if this

681:09

works now so I will say compile and run

681:12

you can see we got oh uh I guess we are

681:15

doubling as well right anyway you can

681:17

see we have a list which has same values

681:19

so yes your list does support repeated

681:22

values but sometimes you want to have a

681:24

collection where you can only have a

681:26

unique values remember when we talked

681:28

about the collection as an interface it

681:30

is also extended by a set interface Now

681:34

set supports unique values okay so what

681:36

I will do now is I will have the same

681:38

values okay but the instead of using

681:41

list I will say set and let me import

681:43

the package for this I will say control

681:44

space enter and we got a package for set

681:47

as well and then I want to specify not

681:50

an analyst okay because I see the thing

681:53

is if we talk about set yes set extends

681:56

collection but the class which

681:58

implements set is different and the

682:00

class name is Hash set okay I can import

682:03

the package for this as well and done

682:06

apart from this the syntax will remain

682:08

same we also specify the type here the

682:10

generic type which is integer and okay

682:12

let me remove the list input let's only

682:15

have set and has set and you can see we

682:17

can have the same method add also here

682:19

and we can print all the values the only

682:21

thing is here I will be very specific

682:23

that I am working for integers

682:25

um let me remove this and I also want to

682:27

print the value now that's it

682:31

okay now when you have these values let

682:33

me just compile and run you can see now

682:38

the value is 2 5 6 8. first of all we

682:42

can't have a repeated value you can say

682:43

six and six uh we only got 1 6 here the

682:47

second thing is it is not following

682:49

these sequence remember the if you can

682:51

see the sequence is six five eight two

682:54

but we are getting the values in sorted

682:56

format is it really sorted let's try

682:58

what I will do is I will just make this

683:00

okay first of all we know that we cannot

683:02

have repeated values I mean we can add

683:04

it it's just that it will not have the

683:06

same value again uh let me just make

683:08

this as 54. I will make this as 82 I

683:10

will make this as 21 and this is let's

683:12

say 62. again we have a bigger values

683:14

now let's see if that really is sorted I

683:16

will say compile and run

683:18

if you can see it's not actually sorted

683:20

it's 82 21 54 62. okay this is not

683:24

sorted and it is not go even going for a

683:26

sequence and that's how this set works

683:29

set will not give you value in this

683:31

sorted format okay it basically uses uh

683:35

and it also doesn't have a index value

683:39

example if I say nums dot for at least

683:41

remember we had a get here we don't have

683:43

get I mean we don't have a get which

683:44

takes element we do have a get class but

683:46

we don't want it we want a get method

683:48

with using which you can specify the

683:49

index value set does not support index

683:52

value

683:53

so certain things to remember for set so

683:55

set basically is a collection of a

683:57

unique value if you see documentation it

683:59

says a collection that contains no

684:01

duplicate elements and uh it does it

684:04

does not even have uh index value is it

684:07

not mentioned anywhere

684:10

anyway so you can't have index value I

684:12

know when I mentioned about collection

684:13

API it was introduced in 1.2 and also uh

684:16

it is also called collection framework

684:18

okay instead of saying uh API you can

684:20

also say its collection framework cool

684:23

um so this is your set and that's how it

684:26

works what if you want a sorted value so

684:29

instead of using hash set we can use

684:30

something called a tree set so let me

684:33

import the package for this

684:35

import

684:37

and now if you try to work with this

684:39

so you can see we got all the sorted

684:41

values so basically this tree set

684:44

actually extends if you see it extends a

684:49

abstract set and implements a

684:51

navigational set

684:52

and if I go to navigational set you can

684:54

see this interface actually extends a

684:56

sorted set and that's why all the

684:58

elements in this tree set will be sorted

685:01

okay it is a set but then it is sorted

685:04

set so it depends upon what type of

685:06

elements you want to work with if you

685:07

want to trace a particular element then

685:09

preset makes sense but if you want to uh

685:12

want to have a set which will have only

685:14

unique values there is no need for

685:15

auditing you can go for hash set there

685:18

one more thing instead of saying set

685:20

here we can also say collection even

685:22

that works in because anyway uh the tree

685:24

set implements set and set extends

685:26

collection right so we can use that as

685:28

well one more thing to add if you if you

685:30

look at collection and if you look at

685:31

our diagram on top of collection we have

685:34

one more interface which is called

685:35

iterable

685:36

uh since we can iterate between the

685:38

elements right with the help of enhanced

685:40

for Loop or different we have different

685:41

options so it actually extends a tribal

685:44

and if you go to a table we have an

685:47

option called iterator so that even that

685:49

is one option you can use iterator

685:51

method which will give you a iterator

685:53

itself let me show you how that works

685:55

instead of using a for loop I can say

685:59

nums Dot iterator

686:02

right so we got the iterator now this

686:04

iterator will return will give you

686:06

iterator back the Twitter object

686:09

I would say this iterator is integer I

686:11

will import the package and here I can

686:13

say

686:14

uh values equal to so from this iterator

686:17

I got all the values here and then from

686:20

this values I can print the values

686:22

I know that sounds weird right but let's

686:24

do that I'm not using a for Loop so how

686:26

do you print all the values so basically

686:27

values has a method called has next and

686:31

it also has a method called

686:32

uh next which will give you the next

686:34

element

686:35

okay it will give your next element but

686:37

it will only give you one element right

686:38

I can put that in a loop to get all the

686:40

elements

686:42

uh but which Loop we have to use in fact

686:44

what I will do is I will just try to

686:45

plant

686:46

this next element

686:49

but the problem is it will only give you

686:50

one element how will I use a loop here

686:52

because if I use a loop I need to know

686:55

how many elements we have right so one

686:57

thing we can do here is we can use a

686:59

while loop because it has a method

687:01

called values dot has next now has next

687:04

will tell you that do we have a next

687:06

element okay and let's try if that works

687:09

compile and run and you can see we got

687:11

the values so even this we can also use

687:14

iterator that's also an option and

687:16

that's why we got the top most interface

687:18

which is iterator

687:19

so that's how we use set a Twitter and

687:22

reset we can also try it with the linked

687:26

has set but then yeah internally they

687:29

work differently but you can get the

687:30

values okay so we have talked about

687:31

least we have talked about set of course

687:34

we have different classes as well for

687:36

different structures and this is a

687:37

famous one now there is one more which

687:39

is called map Now map is not an

687:41

implementation or it does not extend

687:44

collection interface but it's a part of

687:46

collection concept relation API right so

687:48

what I will do now is let's work with

687:50

map here but why do we need map see the

687:52

thing is when you work with list let's

687:53

say we have a list here and list of

687:56

values we have okay so we have list of

687:58

values let's say the values are 56 23 67

688:04

and 92. now we have this badges right

688:07

and let's say uh if it if you work with

688:10

analyst here if you work with list

688:11

basically for this values if you want to

688:13

work with these values you need to work

688:15

with the indexes why do you have to say

688:17

0 1 2 3 right but sometimes it's better

688:21

to have a name or a different kind of

688:25

index instead of having zero and one two

688:27

three uh example let's say if you talk

688:29

about phone book we have a name and a

688:31

number right so can we have something

688:33

like this I want to say these are my

688:35

students marks right so I want to say

688:36

this marks belongs to Naveen uh this

688:39

marks belongs to let's say hush uh this

688:42

marks is belong to social

688:44

and we got Kiran so instead of having

688:47

index value can we have a name to it

688:51

okay instead of having this values so

688:53

can we do it and that is possible with

688:55

the help of a concept of key and a value

688:58

pair so we have a key and a value so

689:00

example this value which is 56 23 67 92

689:04

there are values and the key for this is

689:06

Naveen the key for this is Hersh the key

689:08

for this is social and the key for this

689:11

is Kiran right and we can achieve this

689:14

two things in one data type or data

689:17

structure which is called a map

689:19

so this map map is basically a

689:21

collection of not map is a collection of

689:24

key and a value pair how do we achieve

689:27

that so what we can do is we can remove

689:28

the entire section from here just to

689:30

keep it clean and let's work with Okay

689:32

we don't even need these Imports now

689:34

so let's create a map so where you can

689:37

create a map is map and okay let's

689:39

import the package for this again

689:40

java.utl and we can say this is students

689:44

that's a map of students and new Now map

689:48

itself is an interface if you can see

689:50

and it supports key in a value pair now

689:52

if you remember when we talked about

689:53

analyst it was having either right

689:55

because it represents elements in map we

689:58

have KV it is just a character

690:00

representation of what you are going to

690:02

insert now this key this K and this V

690:05

can be replaced with data types like

690:07

string integer integer float or we can

690:10

have student

690:12

string whatever type of you want to work

690:14

with so now we have to look for a class

690:16

which implements this so the class name

690:18

is hashmap right and in this as well we

690:22

have to mention the type of it the type

690:23

of key we are working with is string and

690:25

the type of value you're working with

690:26

here is

690:28

integer okay and of course it can be

690:30

anything it can be string student it can

690:32

be storing string that's your choice

690:33

whatever data you're entering but we got

690:36

this map here now once you've got this

690:38

map I can simply add some values here

690:40

how do we add values it's very simple

690:42

you say students dot now we don't have a

690:44

add method we have a put method so we

690:46

have to use put and we can specify two

690:48

things the key in a value and as per our

690:50

diagram we have seen the key I want to

690:53

insert is Naveen and the value is what

690:55

56 and likewise we can have all the

690:58

values here copy paste paste paste and

691:03

this is hush

691:05

and the value here is 23.

691:08

this is social

691:10

the value is 67 Kiran it's 92. so we got

691:14

all this key and a value right and then

691:16

if you want to print it you can you can

691:18

actually do it before this we were using

691:20

list and then for every value here we

691:22

were using index but now we have a

691:24

logical name to it it is much easier to

691:26

work with names right

691:27

uh so I will say s out and here let me

691:31

print these students

691:33

basically I'm printing the map itself I

691:35

will compile this code and run and you

691:38

can see we got a map we got Naveen 56 we

691:40

got social 67 we got killer 92 and hers

691:43

23. now one thing to observe even this

691:46

is not following a sequence you can see

691:48

we got Naveen first then we got social

691:49

we got Kiran we got harsh okay it looks

691:52

like more of a set right let's Explore

691:54

More uh okay so we can do that otherwise

691:57

if you want to fetch a particular

691:59

student only one student you can say

692:00

student dot you can say get and in this

692:04

get you can specify the key let's say I

692:05

want to get the marks for not all the

692:08

students but let's say hush I can

692:10

specify the key here

692:12

and if I compile this code and run you

692:14

can see we got 23 that's the marks for

692:15

hush

692:16

okay we can do that okay one more thing

692:19

I want to print all the values

692:21

but then what I will do now is let me

692:23

say during my school days we used to

692:25

after the exams when the paper is

692:27

checked we used to get the paper back to

692:29

calculate the marks right so we used to

692:30

turn the page we used to calculate each

692:31

and every marks and at the end once we

692:34

add all the values of all the marks of

692:37

each question if that marks is more than

692:40

the marks which you got on the paper

692:42

which is checked by your professor we

692:44

used to go and collect it right of

692:45

course if you got extra marks then you

692:47

don't go there but let's say uh has got

692:50

23 and after calculation we have changed

692:53

it so let's say students.put

692:55

and now I'm saying hush and the new

692:59

value is let's say 45. there are a lot

693:02

of mistakes in the paper so let's say we

693:04

got 45 here now tell me will it add a

693:07

new student as hush or will it replace

693:09

the existing value let's see what

693:11

happens

693:12

if I compile this code and if I run you

693:16

can see for Hirsch we only got one Hersh

693:18

here which is 45 we don't have two hush

693:20

that means the key cannot be repeated

693:23

keys are unique the values can be

693:25

example I can have two students with the

693:27

same number and of course we do get same

693:30

marks right if you copied anyway so we

693:33

cannot have keys repeated

693:35

so can we say these keys are actually a

693:38

set

693:39

that's right and the values are least

693:41

here so we have a set and a list

693:43

combination to get to get a map here

693:46

okay so what I want to do now is I want

693:49

to print all the values and that's why

693:50

you can see there's no sequence here

693:51

because it's a set okay I want to print

693:53

all the values but one by one I want to

693:55

use a for Loop here can we do it the

693:57

problem is we don't have see if you work

694:00

with list and if you use a for Loop it

694:03

will give you one value at a time right

694:04

there's no one value here we have two

694:06

key and a value so what you can do is

694:09

first of all let me just get all the

694:10

keys and let's see how it works

694:12

I will say students I want to get all

694:15

the keys so do we have a method which

694:18

will give me all the keys if I go down

694:20

down down yeah you can see that we have

694:23

a key set oh we were right it's a set uh

694:27

so I'm printing all the key set now so

694:29

if I compile this code and run okay so

694:32

that we got all the keys

694:34

if key set gives me all the keys

694:37

if I run a loop on this if I say for

694:41

what type of case set it's a string

694:43

right so I can say string Name colon it

694:47

is coming from

694:50

students.key set

694:52

okay so what I'm doing is this key set

694:54

will give me a set right and from that

694:56

set you are fetching one name at a time

694:59

and with that name I'm printing first of

695:01

all name as it is

695:03

and I will give a space in between or

695:06

maybe a colon and then I can provide the

695:09

I want to get the value how do you get

695:11

the value so you can say students dot

695:12

remember we have worked with this get

695:14

and you can pass the key which is name

695:16

of course right your key is in is in in

695:18

this name or maybe I can just to make it

695:20

simple instead of same name I can say

695:22

key itself and here as well I can say

695:25

key

695:25

here I can say key

695:28

right simple and now compile

695:32

Run Okay so that we got key and a value

695:35

there so that's how you can print all

695:37

the values from a map now why we are

695:40

using our put method here is because

695:42

when we say add we are adding new

695:45

element when you say put it says try to

695:48

add the element if you already have this

695:50

key just replace it let's put

695:54

okay apart from this do we have some

695:55

more methods to explore so if I say

695:57

students dot we got a put we can also

696:00

say get if we can also get values all

696:02

the values the way you have key set

696:04

which gives you all the keys when you

696:05

say value it will give you all the

696:06

values uh apart from this we can use uh

696:10

remove to remove the particular element

696:13

or the particular entry or we can

696:17

replace as well yeah those are the

696:18

methods we have and please explore those

696:20

methods to understand this mode

696:22

so that's how basically you can use map

696:23

in fact apart from this we also have a

696:26

hash table

696:27

so if you can see we have hash table

696:29

here and both works almost same if you

696:32

compile this code and if you run this

696:35

you can see we got the same output so

696:37

what's the difference between hash table

696:38

and hash map if you go here and if you

696:41

go on top

696:44

okay so if you can see in the hash table

696:46

code it says the new collection

696:48

implementation which is Hash table is

696:50

synchronized now it depends upon which

696:52

you want to use so if you want to use a

696:54

synchronized version you can use hash

696:56

table if you want normal we can use

696:58

hashmap now what is synchronized if you

697:00

remember we have worked with threads so

697:02

if you have multiple threads working

697:03

then it's better to use hash table to

697:07

make it synchronized right so if you go

697:10

up

697:10

let's go to hashmap to see what it says

697:13

so if you go to Hash map

697:16

okay it says if you want to work with

697:17

multiple threads and if you still want

697:19

to use hashmap then use synchronized

697:21

externally so if you want to use

697:23

synchronized use hash table otherwise

697:25

Map works

697:28

so yeah that's how basically you can use

697:29

map and uh that's how you can fetch the

697:32

values in this video let's try to work

697:34

with sorting so let's say if you have a

697:36

collection and you want to sort it how

697:37

will you do it so first of all I will

697:39

create a list of values I will say list

697:41

of uh I will say integers this time and

697:44

then let's say nums equal to new add a

697:47

list right and then we can import this

697:50

okay now one thing to observe when you

697:52

talk about this type here on the left

697:54

hand side there is no compulsion to add

697:56

that on it inside okay at the start when

697:58

generic concept came it was compulsory

698:00

for us to assign the values or to set

698:03

the type on both the side but after 1.7

698:05

they have kept it optional if you

698:07

specify the left hand side on the right

698:09

hand side you can simply keep empty

698:10

bracket that completely works okay so in

698:13

this added let me add some values I will

698:14

say nums dot add also four and likewise

698:19

let me add some elements here

698:21

okay so let's say we have these values

698:23

here and what our do is of course if I

698:25

print this value so it will be printed

698:27

as it is and it follows the sequence as

698:29

we have seen so if I compile this code

698:31

and

698:32

run you can see we got the values now

698:34

what I want is I want to sort this

698:36

values how do I do it now we have a very

698:38

special class called collections okay as

698:40

I mentioned in the first video uh we

698:42

have a collection class as well and this

698:45

collection class has lot of methods one

698:46

of the method is called sort and in this

698:49

sort you can pass the array or address

698:51

which you want to sort okay now

698:53

collection class belongs to util package

698:55

okay and then once you say sort and

698:58

after that if you try to print these

699:00

values and let's say the output if I run

699:02

this code you can see we got three four

699:04

seven nine so sorting is actually very

699:07

easy in terms of collection here right

699:09

now thing is what if you want to sort

699:13

this based on your own logic how will

699:15

you do it example let's say the values

699:17

are 43 uh 31 and then 72 and 9 or let's

699:22

say 29. so we got all these values right

699:25

and then I want to sort them based on

699:28

their last digit not the entire number

699:30

because again if you sort this value it

699:32

will be sorted based on the values right

699:34

I want to sort based on the last digit

699:36

so I want 31 to be first and then I want

699:39

42 then I want 72 then 43 then 29 so

699:42

this one first then this 2 then this 3

699:46

and then this nine how do I change a

699:48

logic here say by default sort will take

699:50

its own logic right in case if you want

699:53

to have your own logic in that case what

699:56

you can do is we can use something

699:58

called a comparator as you can see we

700:00

have a comparator here okay how do I use

700:03

it so if you talk about comparator let's

700:06

try to use comparator here so if I use

700:08

compare it vector and if you go there

700:11

comparator is basically an interface

700:13

okay so how do I use comparator so I can

700:16

say comparator comp is equal to new

700:18

comparator now this is an interface

700:20

right and we know we have multiple ways

700:22

of implementing an interface we can

700:24

create a class which implements the

700:25

interface or we can use something called

700:28

an anonymous interface Anonymous class

700:30

here

700:31

okay this comparator works with integer

700:33

and I can specify that here as well

700:35

which is integer

700:37

in terms of anonymous class such as

700:39

compensate to specify on the ident side

700:40

as well okay we can use this comparator

700:43

here and in this I will Define the

700:45

method which method we have in

700:46

comparator so if you look at this

700:48

comparator we have a method called

700:50

compare okay that makes sense we can use

700:52

compare here

700:53

uh what's the Syntax for compare it is

700:56

okay not this one

700:58

the syntax is int compare and pass the

701:01

two values so T is a type here so

701:03

integer integer okay so I will say

701:06

public int compare

701:09

and we have to specify two values

701:11

basically one is your uh I will say int

701:15

I comma ain't J see when you basically

701:18

sort the values you know normally you

701:20

compare two values example let's say if

701:22

I give you uh four values here two three

701:25

let's say we got four we got three we

701:27

got one we got six if I want to sort

701:29

this at one time I will compare two

701:32

values I want it to be ascending order

701:33

so smaller value come first and then the

701:35

bigger value go second so I will compare

701:38

these two values if the first value is

701:40

greater than second value I will swap so

701:42

after swap it will be four three oh

701:44

sorry three four one six then I will

701:46

compare these two values uh then it will

701:48

be three one four six then we'll compare

701:52

these two values so this will be three

701:54

one six or sorry 346 because the first

701:57

value is smaller than second value no

701:59

swap in this case we have swapped it

702:01

because it was a bigger value now once

702:04

you got one of three one four six we'll

702:06

do this iteration multiple times again

702:08

if you compare this two this will be one

702:10

three four six then this two it will be

702:14

one three four six

702:17

now we don't have to do that we don't

702:19

have to compare the last two because in

702:21

the first iteration you know the biggest

702:23

value comes at the end so we got we got

702:25

that right so we have to do that only

702:27

three times and again for the next

702:29

iteration we can say one three I know

702:31

this is sorted but still we have to

702:32

continue the iteration first compare one

702:34

first two one three four six and you're

702:37

done

702:38

right so this is how basically you sort

702:40

the elements the the aim here is when

702:42

you sort elements uh this is one of the

702:44

algorithm for sorting basically we have

702:47

to compare two values and swap that's

702:49

basically basic rule right so here as

702:51

well when you say you are sorting you

702:53

have to just sort of specify when to

702:54

swap and not to swap so when you compare

702:57

I and J

702:59

if you say okay now we want to compare

703:01

and how do you specify that we have to

703:03

swap so if you return one it will swap

703:06

if you return minus one it will not swap

703:08

it's that simple

703:09

okay so what I will do is I will I will

703:12

check and I want to check the last

703:13

number right so I will check if how do

703:15

you get the last number oh mod 10. if I

703:19

mod 10 is greater than the J

703:23

mod 10 in that case swap else you can

703:27

say return

703:30

-1

703:31

okay so this is how basically you

703:33

specify the logic and I can still see an

703:36

error here what is the error the compare

703:39

method is correct okay there should not

703:42

be any error here okay let's see what

703:44

happens so what I will do is I will pass

703:46

this com object here itself I will say

703:48

com

703:49

and okay there's still an error

703:54

I don't see any reason to get the error

703:56

did I forgot any colon or something

703:59

things are looking good let's compile

704:01

this one let's see what happens compile

704:03

there is an issue is not abstract does

704:06

not override

704:08

okay it is giving an issue with the

704:10

compare method is it the right method

704:12

when I'm using compare okay that's right

704:15

okay do we have to use the class names

704:17

here

704:18

integer and integer oh it was because of

704:22

the class name my bad yeah so we have to

704:23

use a class email right because

704:25

collection works with the vapor classes

704:27

and now I will try to compile this code

704:30

and okay compile two times

704:34

and run you can see we've got distorted

704:36

values we got one two three nine so

704:41

basically what we are doing is we are

704:42

changing the way it sorts the value if

704:44

you want to specify your own logic we

704:46

have to pass an object of a comparator

704:48

now in this object relation of

704:50

comparator you have to specify the logic

704:52

on based on which you want to sort this

704:54

uh in fact let me give you a challenge

704:56

here what you can do is instead of

704:57

creating the list of integers create a

705:00

list of string and sort this string

705:03

array with the help of the length of the

705:05

string okay that will that will be

705:07

awesome right try that

705:09

this is how you sort the value based on

705:11

the comparator so what what is

705:13

comparator basically so comparator is a

705:15

concept or an interface using which you

705:17

can specify your own Logic for sorting

705:19

okay

705:20

that makes sense right now I will do

705:23

something else here what I will do now

705:25

is instead of having a list of integers

705:28

let me have a list of students okay so

705:30

what I will do is I will get a class

705:32

here called student and

705:35

let's say we have int age for a student

705:38

and string name and I also want to have

705:41

uh two streamers so that I can print the

705:43

object as it is so this is Source action

705:46

I want to get a two string method name

705:49

because I want to print the student

705:51

values as it is and let me also have a

705:53

Constructor using which I can assign the

705:55

values to them

705:56

I will say Source I want a Constructor

705:59

which can take both the values okay this

706:03

makes sense let me remove this at

706:05

override for time being okay so we got

706:07

the Constructor we got two strings what

706:09

I will do now is instead of creating a

706:10

list of integers let me create a list of

706:13

students I will say student here

706:16

and then because that type of student

706:18

right and we cannot add numbers now I

706:20

will say these are stoods

706:23

uh to replace this with stoods

706:27

or maybe let's have numbers what's an

706:29

issue and then here instead of adding a

706:32

number we have to add a student right so

706:33

I have to say no student and this will

706:35

be value one or let's say we have to

706:36

specifying age right let's say age 21

706:38

and name is let's say Naveen then let me

706:43

just create some more objects here so

706:45

let's say age 12 name is let's say John

706:50

and then H we got here is 18.

706:54

let's say puddle

706:56

and we got age 20. let's say this is

707:00

Kiran okay so we got this four student

707:03

here with different age and different

707:04

names now what I would do is I want to

707:07

sort them okay timely let me just

707:09

comment this section I want to print the

707:11

edit as it is and let's see what happens

707:13

if you compile and run you can see we

707:16

got all the values the only thing is

707:17

they are getting printed in a weird way

707:19

I want to print them one by one so we

707:22

can use a enhanced for Loop here so I

707:24

can say student as Colon's nums and we

707:28

should have used studs there

707:30

now what I can do is

707:32

I will replace numps with stoids okay

707:36

this should solve the issue and done

707:39

okay so we got some students and I want

707:41

to print one student at a time let's see

707:44

if that works

707:46

compile and run okay so we got all these

707:49

values now what I want to do is I want

707:52

to sort these values based on their age

707:55

how will I do that

707:57

in that case I have to say sort okay let

708:01

me just try natural sorting the moment

708:04

you do that it is not working can you do

708:05

that with the sword weather is not

708:06

working

708:07

okay let me pass the comparator object

708:10

the only thing is now this comparator is

708:13

not working with integer it is working

708:14

with students so it is a student now and

708:16

both this object so again we have to

708:18

compare two students right so we need to

708:20

show it at a time so I will say student

708:23

and student

708:24

let's say this is I and J now how do we

708:27

compare them we are comparing that age

708:29

right

708:31

so we have to say I dot h is greater

708:35

than

708:36

J dot h then swap

708:39

it's so simple right

708:41

since we have worked with the integers

708:43

it should work normally and if you want

708:45

this code okay so that we got all the

708:47

students with the ascending order of age

708:49

we got 12 18 20 and 21 this works right

708:52

so this is how basically we can use

708:54

compat and we can sort a student as well

708:56

but there's one more thing

708:58

why we have to pass a comp here why not

709:02

if I just say student white is not com

709:04

why it's not sorting it's because this

709:07

student we have not specified the logic

709:09

for the student right because if you go

709:11

to the integer class let me just type

709:12

integer here just to see if you go to

709:15

integer class you can say integer class

709:17

Implement something called a comparable

709:20

and by that's why by default the sort

709:23

works with integers our student is not

709:26

implementing comparable so if you want

709:28

to have a natural sorting you can also

709:30

Implement something called a comparable

709:32

the only thing is this compatible

709:34

interface has a method called compared

709:37

to okay so we have to Define that method

709:40

so I can just come back here and say

709:42

implement this comparative method

709:45

this is the signature

709:47

okay and we are comparing this with a

709:50

student itself

709:52

and let me say this is that okay the the

709:55

name of the variable is that will I will

709:57

tell you why I've said that but now what

710:00

I'm doing is I'm adding a method called

710:01

compared to

710:03

and I'm implementing something called a

710:04

compatible now and then this comparable

710:07

actually works with a student we have to

710:09

mention that as well now if you go back

710:10

you can see there's no error now so sort

710:13

method does work with student provided

710:15

you are implementing comparable if you

710:17

if your students are not compatible how

710:19

you can how can you sort them that's

710:21

what it says but then if you try to

710:24

compile this code and run it will not

710:25

work the way we want it you can see this

710:27

is not sorted

710:28

and if we are not using compared to now

710:30

I'm not using comparator what I'm using

710:31

is I'm using compared to off compatible

710:33

so whatever logic we have written here

710:35

the same logic I have to write here

710:39

okay now the only thing is we have to

710:42

compare two students right

710:44

one student is that so we can say this

710:47

is that what about the first student the

710:50

thing is we are defining this method

710:51

inside the class itself right so what we

710:53

can do is we can say this remember this

710:55

keyword current object so this dot age

710:57

the current object the current student

710:59

we are comparing the current student

711:00

with the other student it's something

711:01

like when we don't have a compatible and

711:04

if you have two students here we need a

711:07

third person to compare two students but

711:08

now I'm giving this power to the student

711:10

itself they can compare themselves so if

711:13

I'm comparing with someone else

711:14

I'm this and that is that

711:17

okay and let's see if that works now

711:20

compile and run oh it works inside we

711:23

got 12 18 20 21. so these are basically

711:25

we can use compatible and comparator so

711:29

you have two choice either you can make

711:30

your class implements compatible if you

711:33

don't want to do this you can also use

711:35

comparator

711:36

in fact even if you mention comparable

711:38

you can still override the logic by

711:40

comparator that's your choice okay one

711:43

more thing before we close this let's

711:44

say I want to use comparator here if a

711:46

pass comparator and I want to you I want

711:48

to reduce this code remove a Lambda

711:50

expression this is a functional

711:52

interface we can use Lambda here as well

711:55

so what I can do is I can remove this

711:56

entire part from here to here I know you

711:58

have to watch this video multiple times

712:00

to make it work uh I can put an arrow

712:03

and we can put this curly brackets as

712:05

well

712:06

one more thing instead of using IF else

712:09

we can also use a title operator here so

712:11

the same logic I can just comment this

712:13

part I can say return I dot h is greater

712:18

than J dot h return one otherwise return

712:21

minus 1. so instead of using IF else I'm

712:24

using turn it operator we can remove

712:25

this part now since we only have one

712:28

statement and that to return we don't

712:29

need return that is optional we can

712:31

remove curly brackets as well and we can

712:33

write everything in one line again watch

712:36

this video multiple times to understand

712:37

what I'm what I'm doing here and also in

712:40

the Lambda expression you don't have to

712:41

specify the type you can simply say I

712:42

and J

712:44

and your job is done you can see that

712:46

entire comparator went into one line and

712:48

that's why functional interface are so

712:50

important

712:51

let's see if this works and what I will

712:52

do is I will just remove this from

712:53

comparable

712:54

just to see if that works without

712:57

implementing compatible Implement run it

713:00

works what is comparator if you want to

713:02

specify on which logic you want to sort

713:04

the elements you can use comparator what

713:06

is compatible if you want to give a

713:08

power to the class itself to compile

713:10

itself or to compare its object to

713:11

itself we can use uh compatible there in

713:15

this video we'll talk about stream API

713:18

now before we understand stream API

713:20

first of all it's a New Concept it came

713:21

in 1.8 Java 1.8 basically and it

713:24

provides some amazing feature the only

713:26

thing is why do we need those features

713:28

we'll understand that first so what I

713:30

will do is first of all I want to create

713:32

a list of values okay so let's create a

713:34

list here and I will say this is of type

713:36

integer and I will say nums and of

713:38

course just to keep it simple I'm going

713:40

with the default names so that you you

713:42

don't have to focus much on the naming

713:44

convention I want you to focus on the

713:46

concept so we have the analyst here and

713:49

in this at least I want to have some

713:50

values we have nums dot let's add some

713:53

values I would say add four in fact you

713:56

know what I'm going to do instead of

713:57

adding values like this there's another

713:59

another way you is using which you can

714:00

add values in the list uh which is

714:02

there's a class called arrays and arrays

714:04

dot as list and you can simply pass the

714:07

values here instead of manually typing

714:10

the values even this works so as list is

714:13

a method which gives you a list and it

714:14

belongs to a class called arrays okay

714:17

this is much simpler right now once you

714:20

got this list what if you want to print

714:22

these values of course I can say s out

714:24

and I can say nums and if by doing this

714:27

we can simply print all the values and

714:30

you can see we got all the values now

714:32

what if I want to perform some operation

714:34

on this okay now what kind of operation

714:36

I'm talking about so let's say I want to

714:39

uh double all these values and in fact I

714:42

want to First apply the filter here so

714:44

let's say I don't want odd numbers I

714:47

only want to focus on even numbers

714:49

example we have 4 and then we have two

714:51

ignore this dot dot a basically it

714:53

represents an array so we got this 4 and

714:56

2 I want to filter them and whatever

714:58

filter of whatever value you get after

715:01

filtering I want to double those values

715:04

so example 4 and 2 becomes 8 and 4 and

715:07

at the end I want to reduce it so I want

715:09

to say okay 8 plus 4 is 12. so I want

715:12

the output to be 12. the way you can do

715:15

that first of all I want to apply the

715:16

filter right so we can use a for Loop

715:18

and we can go from the first row to last

715:20

value so I can say int n colon from nums

715:24

and once you get this value I want to

715:27

apply the filter because see I don't

715:28

want to add all the values I want to add

715:31

only the odd numbers and that to have to

715:32

doubling it I want to add even numbers

715:34

basically so we can check if my n mod 2

715:39

is equal to equal to 0 that means that's

715:42

a even number if that's a even number I

715:45

will first of all double the values I

715:47

will say n is equal to n into 2. so

715:50

doubling done and then once you have

715:52

doubled it I want to add it into a

715:57

to a sum let's say I will create a in

716:00

some variable the initial value is zero

716:02

and whatever doubling you get you simply

716:04

say sum is equal to sum

716:06

plus n okay uh so by doing this what I'm

716:10

doing is from all the values which we

716:12

have here let's say we have some more

716:13

values let's say 6 as well and let me

716:17

add one more value here which is three

716:19

so you can see we have so many values

716:21

but I want to first of all filter only

716:23

even numbers the example I want to fetch

716:25

this 4

716:26

uh then just 2 then this 6 I want to

716:29

Double H value so example 8 plus 4

716:31

becomes 8 2 becomes 4 and 6 becomes

716:34

twelve so this is 8 plus 4 is your 12 12

716:38

plus 12 is 24. I want the output to be

716:41

24 and let's see do we really get 24

716:43

here let me print the value for sum

716:45

compile and run and you can see we got

716:47

24. now this is one way of Performing

716:50

the operation on your list now sometimes

716:53

you perform such an easy operation and

716:55

sometime maybe you do something by using

716:57

which the values in fact the original

716:59

values gets changed so instead of doing

717:01

those things uh we got this amazing way

717:04

of doing this calculation which is

717:05

called a stream API now it was

717:08

introduced in uh in later version before

717:09

this people used to work with this way

717:11

but in in stream we got a different way

717:14

of doing this okay now basically we got

717:16

this new thing which is called Stream So

717:19

if I jump to stream which belongs to

717:22

java.utl.stream package and if I click

717:25

on stream you can see stream frame is an

717:27

interface and it provides you multiple

717:29

methods like we got filter that makes

717:32

sense right if you want to apply the

717:33

filter on the stream or the collection

717:35

we can use filter if you want to change

717:37

a value example we have doubled the

717:39

values right that can be done with the

717:41

help of map

717:42

and we'll individually see this methods

717:44

later

717:44

so if you go down you have sorted in

717:47

fact there is also a method called

717:49

reduce we are going to use that as well

717:51

as well okay we'll see that later so

717:54

basically stream provides a lot of

717:56

methods to work with this now how

717:57

exactly do you stream that we'll see in

717:59

the next video but before that I want to

718:01

show you one thing select so let's say

718:03

we got we were able to do this right at

718:05

this point I will just determine this

718:06

section because we'll talk about that

718:07

later I want to do one more thing before

718:09

we start with stream which is how do I

718:12

print all these values a simple

718:14

splinting so we can use a normal for

718:16

Loop right the normal for Loop Works in

718:18

this way we can use int I is equal to 0

718:20

and int are a less than uh we have to

718:24

get the nums dot length or size yeah the

718:28

method name is size and then we have to

718:30

say I plus plus so we have basically we

718:32

have to use normal for Loop and then we

718:34

can print each value I can say num start

718:37

get I can pass the index value

718:39

and if I clear this compile

718:42

and run and you can see we got all the

718:44

values here okay there's one way

718:46

I can comment this section the another

718:48

way of doing this is using a normal for

718:50

loop I can say for

718:51

int n is equal to num and here

718:55

I can simply print the value of num even

718:58

this works

719:00

right now you tell me which is better

719:02

using a normal for Loop or the enhanced

719:05

for Loop see normal photo what it does

719:07

is it basically goes to the loop oh it

719:11

basically goes to the list by saying Hey

719:13

I want to fetch the first element I want

719:15

to fetch this second element right on

719:17

the other hand if we talk about the

719:18

enhanced for Loop it says the array

719:21

itself will give you the value and that

719:23

value goes into n right now again this 2

719:27

looks good but then we have a better way

719:29

of doing it so I can just comment this

719:30

section the better way is very simple

719:32

what you can do is you can say nums dot

719:34

now we got a new method called for each

719:37

method Now using this for H it will give

719:40

you one value at a time you can save

719:43

that in n

719:45

and then you can simply come back here

719:47

and you can say I want to print this n

719:50

here so for each is a part of the list

719:53

interface you can simply say for each

719:55

and then it will give you one value and

719:58

you can do whatever you want to do with

719:59

that value so I'm just printing it here

720:01

in here

720:02

and if I compile this and run you can

720:04

see we got the same output so we have

720:06

three options now we can use a normal

720:08

for Loop we can use the enhanced for

720:10

Loop or we can use a for each method so

720:12

let's understand what this for H and

720:15

what is this statement inside this this

720:17

looks weird right yeah I know we have

720:19

worked with Lambda expression it looks

720:20

like Lambda expression but what is

720:22

happening behind the scene to understand

720:24

that what I will do is I will just

720:25

remove the entire formula we don't need

720:26

that and let me just push this down

720:29

this will be required later once we talk

720:31

about how do we stream API but at this

720:33

point let's only focus on for each year

720:35

so what I will do is first of all I will

720:37

remove this section or maybe I can just

720:39

comment it so that we can use we can see

720:41

this later

720:42

here at this point let's try to use nums

720:45

so when I say nums dot you can see there

720:47

is a method called for each now for each

720:50

method if you see and if you go to this

720:53

definition for each basically got

720:56

introduced in 1.8 okay and what it

721:00

basically does is it takes an object of

721:02

consumer you can see for each method

721:04

okay let me just go to definition itself

721:06

so you can see for each method takes an

721:09

object of a consumer okay that sounds

721:11

weird right let's see what this consumer

721:13

is so if I want to work with foliage I

721:16

need to create object of consumer okay

721:18

so the moment you say you have to get

721:20

object I can simply say consumer and you

721:23

can see consumer is a interface

721:26

which belongs to our package

721:28

java.util.function okay that's new and

721:32

this is a interface which is a

721:34

functional interface that means Lambda

721:36

expression will be applicable and the

721:38

only method we have to work with this is

721:40

accept okay that means I have to create

721:44

object of consumer which takes the type

721:46

of integer because if you see the

721:48

consumer interface you have to specify

721:50

type as well

721:51

so I would say consumer integer and then

721:54

I will come back here let me say

721:56

con is equal to how do I create an

721:59

object of it it's very simple you can

722:01

say new consumer

722:02

now since this is the interface we have

722:05

to define the class which is anonymous

722:07

class

722:08

okay that is also simple the only thing

722:10

is we have to define a method also the

722:12

method name is public void is it white

722:15

let me just check once consumer has a

722:18

method called accept which is of type

722:19

void okay which returns a void and the

722:22

method name is except I will say that

722:24

and then it takes the integer value I

722:26

mean whatever type you mentioned I will

722:28

say integer let's say n okay now the

722:30

thing is once you got this consumer

722:32

object you can simply pass an object in

722:35

this I can simply say con so for each

722:38

needs an object and we are passing the

722:41

object of consumer our job is done right

722:43

the thing is when you talk about for

722:45

each what it does is it gives you one

722:47

value at a time okay now whatever value

722:50

you are getting here or for it is giving

722:52

you that goes into this consumer object

722:55

and the consumer object has only one

722:57

method which is except

722:59

okay now this except will accept a

723:02

number that's right the forage will give

723:04

you a number right that goes into this n

723:06

now you can decide what you want to do

723:09

this with n okay I would say I want to

723:11

just I just want to print it I mean

723:12

that's your choice you can do whatever

723:13

you want to do with this n at this point

723:16

I'm just printing it okay and now let's

723:18

see if this works I will just go back

723:20

and clear the screen compile

723:22

run and you can say it's working you you

723:25

are able to print all the values

723:27

okay and you can see the entire code all

723:30

the other codes are commented so they

723:31

are not running this code is running and

723:33

it is printing all the values makes

723:35

sense

723:36

now we also know that the consumer

723:38

interface is a functional interface

723:41

which means we can use Lambda expression

723:42

here that also means from here to here

723:45

we can delete this part let's delete

723:47

that let's delete this curly brackets

723:49

and I hope till this point you have

723:51

you've got a Mastery in Lambda

723:52

expression and I can give a arrow here

723:55

that looks good and since we only have

723:58

one statement you don't need to put

723:59

curly brackets I will remove that as

724:01

well and I can write everything in one

724:03

line that looks cool right

724:06

and then we don't even have to mention

724:08

the integer here and we don't even have

724:10

to mention a bracket when you have only

724:12

one variable

724:13

makes sense right

724:16

let's see if this works after doing all

724:18

those changes compile and run there's no

724:19

problem now what we are doing is we are

724:22

assigning this expression to con right

724:24

now whenever we have con we can actually

724:26

use this so instead of using con here I

724:30

can simply use this particular code and

724:32

replace this Con and that's it

724:35

we don't even have to create a reference

724:37

for the consumer and this is what we

724:39

have seen earlier right if you compare

724:41

the Urgent example this looks similar in

724:43

fact exact same so that's how your for

724:46

each works it gives you the value that

724:48

is your n and you can do whatever you

724:50

want to do with that and you can print

724:51

it you can add it you can save it in

724:53

database that's your choice

724:55

okay so that's how basically we use a

724:57

forage now once you understand forage

724:59

let's go for the actual stream API so

725:01

basically we were we were having this

725:03

collection or at least right where do we

725:04

have values and then we were able to

725:06

print that with the help of for each and

725:07

now we know what is happening behind the

725:09

scene right

725:10

what if you want to do this with a

725:12

stream API now let me just give you the

725:14

introduction of stream API here and how

725:16

it looks like so if we talk about stream

725:18

stream is an interface right so if you

725:20

can see uh we have seen that before as

725:22

well stream is basically an interface

725:25

right the thing is if I go back to nums

725:29

and if I say dot you can see now in the

725:31

list we got this new method called

725:33

stream

725:34

again before 1.8 the method was not

725:36

there it was introduced later but we got

725:38

this method called strain now what this

725:40

train does is it gives you or it Returns

725:43

the object of stream okay so that means

725:47

if I want to accept the values I can say

725:48

stream of integers and let me just name

725:52

this as S1 so this is our stream which

725:54

is called S1 here and we got all the

725:56

values so whatever values you have in

725:58

this list is available in this S1

726:01

now with this stream you can perform any

726:03

operation and the beauty is it will not

726:05

going to affect the nums so whatever

726:08

changes you make to this stream if you

726:10

change the values if you double the

726:12

values it will not going it is not going

726:13

to affect nums now why this is important

726:16

see sometimes when you have a data and

726:18

then if you want to perform some

726:19

operation it's not like always you want

726:21

to change the existing values remember

726:23

when we talked about threads when you

726:25

have multiple threads and then you are

726:27

performing some operation mutation it's

726:29

not good right so whatever changes you

726:32

want to do do that in this stream and

726:33

use it at least you have your original

726:35

values with you

726:37

okay now that means I can also print the

726:40

entire thing with the help of for each I

726:43

can say S1 dot for each mind you again

726:46

in this stream of S1 we got all the

726:48

values which are there in the list

726:50

and let me describe that works I will

726:52

say compile and run and you can see we

726:54

got the values here

726:56

now one of the thing about stream is

726:58

once you use the stream you can't reuse

727:00

it and that's why the name is stream

727:02

example if you talk about the flow of

727:04

water the stream right once example if

727:07

you let's say if you're sitting near the

727:08

river or the bank of river basically if

727:11

the water passes you can't retouch the

727:13

same water in the same way once you have

727:15

worked with a stream you can't reuse it

727:17

example let's say if I try to print this

727:21

multiple times let's say two times

727:23

it will not give an issue with the list

727:25

okay you can simply say nums dot forage

727:27

multiple times there's no issue but if

727:29

you try to do that with the stream once

727:31

again you can see you will get an error

727:33

compile time there is no issue run time

727:35

it will work once the second time it

727:37

will say uh the stream has already been

727:41

operated a pound or close that means you

727:43

can use stream only once okay uh that

727:47

makes sense so we can't reuse it but

727:49

then what is the benefit of using stream

727:51

here the benefit is stream provides you

727:54

a lot of methods to work with example if

727:55

I say S1 dot uh we have methods example

727:59

we have methods like filter remember

728:01

when we talked about the other example

728:04

where we were removing all the odd

728:07

values or we wanted to only get even

728:09

even numbers we can do that in stream as

728:13

well now how it looks like

728:14

I can simply say S1 dot we can use a

728:18

method called filter okay now what this

728:21

filter will do is filter will say okay

728:23

I want to apply the filter here but give

728:25

me a condition so for any particular

728:28

value let's say n what you want to do

728:30

with this so I can say I can use a

728:32

Lambda expression I can say n mod 2

728:34

equal to equal zeros I only want a even

728:37

number here so this filter and if you

728:39

jump to this filter you can see filter

728:41

returns a stream so what we are doing is

728:45

when we say S1 dot filter it will give

728:47

you basically a new stream to work with

728:52

let's say S2 again as I mentioned before

728:55

S1 is used right so when I say S1 dot

728:57

filter your S1 has been used now now you

729:00

got a new stream now this S2 basically

729:03

will have only the even numbers let me

729:05

prove that let me print S2 and let's see

729:08

what values you get here so I will say

729:11

compile and run you can see we only got

729:15

even numbers now say what we are doing

729:17

here will have a separate video on this

729:19

how Filter Works but the idea is the

729:22

filter is filtering the value based on

729:24

the even or odd you can specify whatever

729:26

logic you want to but at this point I'm

729:28

just specifying a logic of a condition

729:30

of even right now once you've got this

729:33

stream I can create another stream where

729:36

I'm doubling the value I can say stream

729:38

and I can say integer let's say S3

729:41

because it will give you a new stream I

729:44

can do that operation on the new stream

729:46

which is S2 on S2 basically I'm trying

729:49

to double the value right so you can do

729:51

that with map in this map for whatever

729:53

value you get

729:55

uh you can perform some operation on

729:57

that at this point I'm just doubling the

729:58

value right

730:00

as I mentioned you can use any logic at

730:03

this point I'm just doubling it

730:04

and now let's try to see how this stream

730:07

works I'm saying S3 now because you

730:10

can't use S2 S2 is done

730:13

and if I compile and run you can see we

730:15

got double the values we got 8 4 and 12.

730:18

so yeah stream provides you this amazing

730:21

methods on which you can perform the

730:23

operation on one stream it will give you

730:25

a new stream you can perform the

730:26

operation on the other stream it will

730:27

give you a new stream uh in fact you can

730:30

also use some methods which will not

730:32

give you a stream but a last output

730:34

example let's say if I say S3 dot

730:37

there's a method called reduce now in

730:40

this reduce if you say 0 comma

730:43

again I will explain this logic later

730:44

what I've did but let's say if I try to

730:47

do this if I say C comma e c plus e now

730:50

what this values will do it again don't

730:52

focus on the logic now just focus on my

730:54

reduce this radius will not give me a

730:56

new stream this radius will give me a

730:59

value of type whatever type you

731:01

mentioned with the integer with the

731:02

stream which is integer so it will give

731:04

me uh into values I can say in result

731:06

equal to S3 dot reduce

731:08

and instead of using a forage now why we

731:11

can't use forage because reduce will

731:13

give you a single value which is there

731:15

in the result I can simply come back and

731:17

I can print the result and I can just

731:20

compile this code and run and we can see

731:23

we got 24. again let me repeat just

731:25

ignore what is happening inside we'll

731:27

talk about this in the next video what

731:29

is happening inside this idea is stream

731:32

makes your work Easy by having this

731:36

functions if you have a function like

731:37

filter map reduce now this I don't know

731:40

if you know about this concept of Big

731:41

Data example in this world we are

731:43

getting so much of data right now if you

731:45

want to use all this data first of all

731:47

you have to apply the filter on it there

731:49

are some wanted data there are some

731:50

unwanted data so you can apply the

731:52

filter once you up once you apply the

731:53

filter the data which you got you can

731:55

transform it something meaningful you

731:57

can do that with the help of map and

731:59

then you can make a graph out of it

732:01

right with the help of something called

732:02

reduce and then we were able to print

732:04

the result let me just see if that works

732:06

compile

732:08

no issue run and if you call 24 and this

732:11

is

732:14

remember we have used a for Loop which

732:16

was giving 24. the same thing we have

732:18

done with the help of stream

732:20

now the beauty is uh you can actually

732:22

write everything in one line let me show

732:23

you something I will just comment this

732:25

entire section and here what I can do is

732:29

I can say nums dot I can say stream it

732:33

will give me a stream of values on the

732:35

same stream I can say dot I can apply a

732:37

filter

732:38

and this filter can be uh we can very

732:41

logic as I mentioned we are going for

732:42

even numbers

732:44

zero and on the same stream because see

732:48

nums dot stream num Sita at least right

732:50

it the stream method will give you the

732:52

stream on that stream we are applying a

732:55

filter again we we will get a new stream

732:57

and on the new stream we are applying a

733:01

map which is uh n into two and then this

733:05

stream will give you a new stream on

733:07

which you are applying reduce where we

733:10

are saying

733:11

0 comma C plus e or C comma e which will

733:16

be giving me c plus e

733:19

and that's it this radius will give you

733:21

one value right and that one value you

733:23

can store in a result so this nums dot

733:26

stream let me just give a tab here yeah

733:28

so this basically nums dot stream will

733:30

give you a stream on that stream you are

733:32

playing a filter on that stream it we

733:34

are playing a map on the stream you are

733:36

applying a reduce and then you got one

733:39

value at a time and this looks much

733:41

better than the for loop I know for the

733:43

newcomers this will be difficult but

733:45

once you get used to it you'll always

733:46

prefer to use stream

733:48

okay and uh one of my favorite uh

733:51

trainer omenta says venkat subramanyam

733:54

says it's not difficult it's just

733:56

unfamiliar

733:57

okay so uh this is how it works and you

734:00

can compare with the for Loop and this

734:02

looks much clearer in fact it's much

734:03

more readable right on a stream you are

734:05

applying a filter on that filter applied

734:07

you are applying a map and then you are

734:08

reducing now let's try to understand

734:10

what is happening here when you talk

734:12

about filter what it does when you talk

734:14

about map what it does we understood

734:15

what it does in the forage it takes the

734:18

object of consumer but I'm not sure what

734:20

it will do here so what I will do is

734:21

just simplify the code I will remove

734:23

everything and also let me remove all

734:26

this thing so that it will look cool you

734:29

know simple code

734:30

okay so let's go step by step if we talk

734:33

about this filter and if I say okay let

734:36

me just go to filter and you can see

734:38

filter needs an object you can see

734:40

there's a parameter it needs an object

734:42

of a predicate now what is predicate I

734:45

will just go back here and if I type

734:46

indicate here so if I say predicate and

734:48

you can see it belongs to a package

734:50

java.ital.function and if I click on

734:53

predicate here predicate is a interface

734:55

which has a method called test it

734:57

returns you a Boolean value okay now

735:00

based on this Boolean value it will say

735:02

okay it can be either true or false if

735:05

from a stream you know from a stream

735:06

when you apply a filter for a particular

735:09

value if this if this is true it will go

735:12

ahead otherwise it will stop it's

735:13

something like applying a filter on a

735:15

river so let's say if you have a reverse

735:17

flowing and then you have apply a filter

735:18

there and if you want only the pure

735:21

water to pass all the garbage will be

735:24

stuck somewhere you can do that with the

735:25

help of a filter I hope that makes sense

735:27

right let's Implement predicate here so

735:30

of course pretty ticket needs a type

735:32

you'd want to work with and then let me

735:34

say this is a

735:36

p is equal to and I will say new

735:38

predicate now this predicates the

735:41

interface so we have to of course Define

735:42

it but what method it has so if I go

735:46

back and if I say Quick Fix add the

735:48

implementation method so the method name

735:50

is test

735:51

okay remove that so method name is test

735:54

and it takes the integer value and based

735:57

on some condition which you can specify

735:59

you can specify is it true or false so

736:02

what I can do here is I can check if

736:06

the value t or whatever value you want

736:08

to have let's say the value is n naught

736:10

I don't like DQ T later let's go with n

736:13

so if n is mod and mod 2 is equal to 0

736:19

in that case

736:21

return true

736:23

I can give a tab and I can say

736:26

else return false our problem solved

736:30

right okay so if it is true if the value

736:32

of n is even it is true then if this is

736:35

true I will return true or I will turn

736:37

false and now what I can do is instead

736:39

of this condition here I can put P right

736:43

even that should work let's try compile

736:46

and run and you can say it was it is

736:48

working so basically this filter needs

736:50

the object of predicate which specifies

736:52

the logic of when to pass the value and

736:54

when to stop the value

736:56

now if I try to reduce this code don't

736:58

you think this condition here itself

737:00

returns true

737:02

then why to write such a big line we can

737:04

actually return

737:05

n mod 2 equal to 0 because ultimately we

737:09

need a Boolean value right it can be

737:10

true or false so we can do this

737:13

also the predicate itself is a

737:16

functional interface that means we can

737:17

use Lambda expression here

737:20

so we can remove from here to here we

737:22

can remove the entire part we don't even

737:24

need to specify the type and we don't

737:26

need this curly bracket we can put a

737:28

arrow here and then since we only have

737:31

one statement you don't even need curly

737:33

brackets you don't even need a new key

737:34

written keyword we can write everything

737:36

in one line and since we have only one

737:38

parameter we can remove this one and you

737:41

can see the p is equal to this

737:43

expression

737:44

and whenever you have the expression or

737:46

whenever you have P you can write this

737:48

expression and this is what we have

737:50

earlier right

737:52

so now we know how Filter Works let's

737:54

see how Map works if I go to map map

737:57

needs object of a function okay what

737:59

this function is Now function is this

738:02

functional interface which takes two

738:04

types okay makes sense and there's a

738:07

method called apply

738:08

okay so what I can do is I can have the

738:11

object of map

738:13

or not the map the object of function

738:17

now this function takes two types one it

738:19

is what it accepts and what it returns

738:23

and say let's say fun because that is

738:26

fun a new function and again this is a

738:30

function interface

738:32

which has only one method let me just

738:33

add that here so this is a method which

738:36

we want to override which is apply now

738:39

what I want to do whatever value you get

738:41

let's say instead of uh and a TV let's

738:44

say n whatever you want to do with this

738:46

n it will just apply that value so let's

738:48

say for this n

738:50

what I want to do is I want to return

738:53

n into 2.

738:56

so whatever value I'm getting I just

738:57

want to double it again as I mentioned

738:59

logic if anything but I'm doubling it

739:01

here

739:02

okay makes sense and we also know that

739:05

this is a functional interface okay

739:07

first of all let me take this fun from

739:09

here and put it here so we can say fun

739:12

and let's see if that works

739:15

I will say compile and run and you can

739:18

see we got 24 it works what we can also

739:21

do is from here to here this is a

739:23

function interface so we can remove this

739:24

part

739:25

and we can use Lambda here I'll just do

739:27

that quickly now we have mastered Lambda

739:30

and we have only one statement which is

739:32

written so we can remove that

739:34

we can remove this curly brackets we can

739:35

remove the same integer we can remove

739:36

this package

739:38

and this is what we got

739:40

and now we know wherever you have fun

739:41

you can write this and this is what we

739:44

had before so we don't even need this

739:46

right the same thing can be done for

739:48

reduce if I click on reduce you can see

739:50

reduce takes two parameters one is the

739:53

type and second is the operation okay

739:56

now what is this binary operator so

739:58

binary operator basically is

740:00

it extends by function by function as a

740:03

method called apply okay let me simplify

740:05

this for you I know this is a lot of

740:06

implementation there let me simplify

740:08

this thing now when I talk about videos

740:10

how do you reduce a value example let's

740:12

say if I ask you to add three numbers

740:14

let's say one two three how will you add

740:16

them now in your mind you are doing this

740:18

so if I say one two three and let's say

740:21

four also how will you add them of

740:23

course you cannot add all the ways at

740:24

the same time you add two values at the

740:27

same time so first of all you will add

740:29

one and two which will give you three

740:31

then you will add 3 and 3 which will

740:34

give you six and then you will add 6 and

740:37

4 which will give you ten right so you

740:39

add two values at a time

740:42

or maybe if you want to simplify this

740:44

you can start with zero so you can say 0

740:45

plus 1 is 1 then 1 plus 2 is 3.

740:49

okay so you have to start with the

740:50

initial value remember when we created

740:52

int sum we have assigned the value to of

740:55

sum is 0 that is just that is this zero

740:58

and every time you will simply add two

741:01

values

741:02

so if I if I want to add two values can

741:04

I say this is c and e

741:06

the carry because see this one is a

741:09

carry here right this three is a carry

741:10

this 6 is a carry this ten is a 10 is

741:13

the output of course but this one three

741:14

and six they are carry which can be

741:16

represented with the help of C

741:18

and then we have this e which is the

741:20

element right c and e and what you're

741:23

doing is you just have to say c plus e

741:25

and that's what we have done here the

741:27

zero is the initial value what you want

741:29

to work with this value

741:31

and then the c and e are the two

741:33

variables which is going to work with

741:35

this thing and the C plus e is the

741:38

operation

741:39

is that simple okay of course you can do

741:42

this with the implementation but I think

741:44

you got the point right so stream is

741:47

cool right it works with we can apply

741:49

multiple functions here in fact let me

741:51

show you one more thing

741:52

what if I don't want a int result in

741:55

between I want a stream itself but I

741:58

want this stream to be sorted so I will

741:59

say integer

742:01

and I will say sorted values

742:04

but I don't want to apply reduce because

742:06

it just will give you one value I only

742:08

want to filter them based on the

742:10

condition and then I want to say

742:13

sort

742:14

that's it the moment you say sorted you

742:17

will get a sorted stream

742:19

and maybe you can also print this values

742:21

directly instead of using a foliage

742:23

let's try that compile and run oh it's

742:27

you have we have to use for it okay so I

742:29

will say sorted

742:31

values dot for each

742:34

and you can say whatever value you get

742:36

just print it okay let's try compile and

742:40

run and you can see we got the value

742:41

inserted format

742:43

so basically we have multiple methods to

742:45

work with in fact there is one more

742:46

thing what if you want to do the

742:48

filtering but with multiple threads

742:51

but if I you have only one thread right

742:53

what if you want to use multiple threads

742:55

in that case there is another function

742:57

called parallel stream

743:00

which will create multiple threads for

743:02

you to make this faster the only thing

743:04

is when you say sort parallel stream

743:07

doesn't make sense because sorting will

743:09

need all the elements together uh so

743:11

let's not don't use parallel stream when

743:13

you want to sort but yes if you want to

743:14

filter parallel stream works okay so

743:17

yeah that's how we use stream API and I

743:20

hope you got the idea about map filter

743:21

and reduce

UNLOCK MORE

Sign up free to access premium features

INTERACTIVE VIEWER

Watch the video with synced subtitles, adjustable overlay, and full playback control.

SIGN UP FREE TO UNLOCK

AI SUMMARY

Get an instant AI-generated summary of the video content, key points, and takeaways.

SIGN UP FREE TO UNLOCK

TRANSLATE

Translate the transcript to 100+ languages with one click. Download in any format.

SIGN UP FREE TO UNLOCK

MIND MAP

Visualize the transcript as an interactive mind map. Understand structure at a glance.

SIGN UP FREE TO UNLOCK

CHAT WITH TRANSCRIPT

Ask questions about the video content. Get answers powered by AI directly from the transcript.

SIGN UP FREE TO UNLOCK

GET MORE FROM YOUR TRANSCRIPTS

Sign up for free and unlock interactive viewer, AI summaries, translations, mind maps, and more. No credit card required.