declare

Java 2 - Variables(int, double, String)



Sharing buttons:

hello and welcome to my second java

tutorial in this tutorial you will be

covering how to make variables and use

them so in last tutorial we talked about

this and you should still have this

program here if you don't um pause the

video and type it up so we're gonna move

this down and start talking about um

variables there are three types of

variables there are integers doubles and

strings each variable has a code word

for it

integer is int no capitals double is

just double without any capitals and

string is string with s capitalized so

to make a variable um you first type the

type of a variable you want then the

name of your variable so you know

variables can be like Y and acts of

variables for x and y coordinate planes

well suppose I want to name my variable

Henry it can be more than one letter so

you don't need or then you add a

semicolon just like with these and that

is a variable Henry but right now the

variable Henry doesn't have a um doesn't

have a meaning it doesn't have a value

and so we have to set a value so Henry

equals well for an integer it's

basically negative a very very small

number to a very high number it but I

can't go over like fourteen digits or

something so what we will do is we will

set it equal to 16 now this is a good

way to assign variables and is often

used but another thing you can do is you

can just take this step out and equals

16 you can declare what a variables

value is while creating so now we're

we've got the integers down it's a

number can't go above like a hundred

billion can't go below negative 100

billion

now we're gonna do doubles so a double

so we'll call our double Henry two and a

double is the same as an integer except

it has a decimal place it can go up to

like twelve decimal places or something

if you try to put an integer with a

decimal place like 16.47 you'll get an

error see right there

can't convert from double to int so now

if we set our double equal to sixteen

point four three we don't have an error

that is perfectly fine because you

double is meant to have a decimal

decimal place lastly string with a

capital S um we'll call it Henry

three and we'll set it equal to PI the

word high um so when declaring a string

just like when putting text in you all

need ving these symbols on either side

of your text so now we're going to type

in the text hi

by the way this thing I showed you in

the beginning where you declare the

variable then later set the variable to

another about you you can do if you

already have a variable like suppose you

have Henry is 16 now you can put Henry

equals 12 and job no sorry Henry two

equals six point three seven so by the

end of this string you will have said

Henry to 16 said Henry to two sixteen

point four three set Henry three to high

but then it's like but wait Henry needs

to be 12

I guess you lied oh well and then Henry

2 needs to be 6 points 3 7 now here's a

cool thing you can replace these with a

variable so instead of saying hello

world I might want to say hi Jo type in

Henry 3 which if you look up here stands

for high and doesn't get changed later

on

and now we're gonna run that we don't

really need so we're just gonna run it

and see right there hi

it ran this it says do all this stuff

okay I need to print something I need

and it says I need a print Henry three

Henry three equals high you can do the

same thing with a double like Henry to

which is it goes it starts out as this

then changes to this then prints itself

out so this equals that mean do the same

thing with Henry very well and if you

run that right here 12 henry equals 12

so that is the basics for variables you

can I suppose I want another integer

that equals I mean another double needs

to be double for this that is called

Henry four and I want to say equal to

Henry two times Henry Henry you can do

that you just need to make sure that it

is a double because when you multiply

these together you're going to get

something that has a decimal place and

you want to double to store that so I'm

just gonna copy this paste it Henry to

and paste it again Henry for so

basically it's saying Henry one is 12

Henry two is six point three well we

already said it's something else but

we're changing Henry to twelve or

changing Henry to six point three seven

now it's saying I'm gonna print out

Henry which equals twelve

I'm gonna print out Henry to which

equals six point three seven and then

I'm gonna multiply them together and

store it in Henry four and then I'm

gonna print out how many four so see

what happens when I run this it says 12

six point three seven and then it

multiplies them together and

and Henry for and that gives and then it

prints it out and that is the value so

keep in mind that an integer cannot have

a value like that it will give you an

error always so doubles are more useful

but if you're going to make a complex

program it is takes up less RAM if you

use integers which is their main use um

is to take up less RAM than doubles else

like strings would automatically be like

50 bazillions bases along with stuff so

that is the end of this tutorial

in the next tutorial we will be going

over system dot out dot print format so

see you in the next tutorial