declare

Java Tutorial - 07 - Creating and Using Strings in Java



Sharing buttons:

hello welcome to lesson 7 of mastering

Java here we're going to finally begin

to talk about the topic of strings in

Java we've learned about lots of

different data types in Java thus far in

our our lessons here we've learned about

integers and floating points we've also

learned about the concept of a character

which is a single letter that you can

print out to the screen or read in from

the keyboard and recently we've talked

about arrays which are large collections

of basically similar types of data like

integers or things like that we've

talked about that now we're going to

talk about strings which is something

you'll learn in almost every program

that you will right now in Java a string

is an object and that is why we've kind

of delayed talking about it until after

we've kind of covered the concept of an

object in other programming languages

like C++ or C strings are not objects

really and so you typically cover

strings much earlier but here we had to

kind of wait a little bit because

strings are objects basically a string

is a sequence of characters so when we

have been doing before we've been doing

things like system dot out dot print Ln

and we've been doing this kind of thing

forever so I love pizzas and you know by

now that you can save this and run it

and we've been using this enough so that

you know that you can print things out

to the screen but we've always been

enclosing the text that we're printing

to the screen in between quotations what

you have done here without really

knowing it up till now is the sequence

of characters between the quotation

marks is really what you call a string

in Java in a string very simply is just

a sequence of characters

so here the sequences the capital I the

space the lowercase l.o.v.e the space

and then pizzas and then the period so

every single character you see including

any whitespace like a space bar or even

an apostrophe any kind of weird symbol

every single character that you see is

part of the string so this string has

one two three four five six seven eight

nine ten eleven twelve thirteen fourteen

if I'm doing my math right 14 characters

as part of the string now the print

statement that we use in Java all the

time understands that whenever you put

the

characters in between these quotation

marks it already knows that that's a

string and it knows how to put that out

to the screen and we didn't really go

through the trouble of telling you that

this is a string up until now because if

you know it's kind of not that important

until you get to what we're talking

about now so let me go ahead and kind of

erase that I guess what I want you to

understand is when you have characters

between quotations like that that's

what's called a string literal which is

literally just kind of a constant you

that you define there and you can print

it out to the screen now what we want to

talk about now is how to create

variables for lack of a better word that

hold these strings all right so you want

to create a name and you want to assign

some characters to it you want to use

that throughout your program so for

instance if I want to do that here's how

you pull it off first of all we know

it's a string that we're creating so you

go capital S string and we have to give

it a name so let's call it STR because

that's that's pretty common if you just

have a temporary guy you want to hold a

string you can name it STR but you can

name it anything you like

right this is telling Java that we would

like to create a string object of named

STR and we'll put an equal sign there

and if you remember this is going to

look very familiar to you we can say

it's equal to a new string object open a

parentheses put a quotation

I love let's say hamburgers right with a

period and we have to always put a

semicolon so let me once save that what

we have done here is we have created a

string and notice that the way that we

declare strings in Java is very very

similar to declaring an object in Java

if you remember back to what we talked

about objects you have the type of

object you're creating the name of it

and you put an equal sign and then

you're on this side you're telling it

hey open up and create a new object of

this type and in this case we have kind

of a constructor going on in the

background there we're passing Java the

sequence of characters that we want to

be initialized in to this variable so

we're passing basically passing that

constructor these these characters there

and that's how the thing is created so

in this case we have created something

named

and STR and we have put these characters

into that so I can do that again s

string I can name it anything I want

just like a variable I can call it Jason

I could say new string and I can say and

I also love hot dogs right and I could

put a semicolon there okay now that you

have these strings created you can use

them in all types of different

situations for instance you can go

system dot out dot print om right and I

can just literally treat this as almost

like a variable name if this were an

integer I could just put the name in

here and it would print the integer to

the screen so I can just put STR there

and on the next line dot system L dot

print L N and then over here I can put

the name of the other string object that

I have created I can save that and

basically what you're doing is you're

you're essentially taking these

characters that are now stored in this

string and sticking it in the print

statement and you're doing the same

thing for that so let's print that out I

love hamburgers and I also love hot dogs

now you for very simple programs like

this you might not quite understand yet

well when do I need to use a string when

you can think of it even without knowing

all of the details you can think of lots

of situations maybe you're creating an

address book and you may have a first

name a last name an address telephone

number where each of those different

fields are sequences of characters so

you would have to use a string to store

the first name and a string to store the

last name of the string to store the

address and so on basically it's it's

versatile when you need to store data

that's not just a simple number like an

integer can handle all right now let me

also show you this is all fine and dandy

but let me go up here let me show you

something else now the way that we have

declared these strings and initialize

the object because remember strings are

objects in Java is very similar to how

we declare any object now this is

exactly correct and you will see this in

programming if you read code out there

but it's more common to use a shorthand

notation for this because this is so

commonly done and it's a little bit

wordy another way that you can declare

as you can say string let me call this

one STR 2 2 2

make it different from the string

assignment that we have up here and I

can just put it equal now instead of

saying new string in parentheses and

then the string I can just open up the

double quotes on the side and I like to

go to Disney Land like that and I can

put a semicolon after that notice

everything goes away and basically I've

created a string without specifying new

string on the right hand side this is so

common creating strings that Java knows

that when you when you're saying string

and then the name on the right hand side

when you in closing quotes it knows what

you're trying to do create a string

object and assign this character

sequence to that object so let's create

another one string I'll just name it my

last name just to illustrate that you

can name you can do anything you want

here and I can say you know four five

eight four or four or five eight seven

eight is my favorite number right so

notice that in a string you can have

letters uppercase lowercase you can have

punctuation mark so you can also have

numbers here now these numbers are not

represented in integer form they're just

characters to print on the screen so

these are not this is not something

mathematical that I can multiply by

right here when I put a sequence of

numbers inside of quotations like this

Java is going to treat it just as a

sequence of characters to print to the

screen right then I can go down here and

I can put out dot print Ln right and I

can just create a blank line here by

doing that and I can dot out dot println

and then inside of here I can say STR to

system dot out dot print Ln and then

inside of here I can say Gibson like

this so let me go ahead and save this

and print this now it looks like we have

some kind of error here we go back and

try to scan through and we see

immediately we've got red underline and

of course I forgot to put a semicolon

there when we add that everything gets

nicely cleaned up we go ahead and hit

run and then we see here we have the

first two strings printed now we have

the second two strings printed to the

screen now the last thing I want to show

let me put another blank line there alt

pret oh and I'm just kind of it leave it

empty take the X out of there leave it

empty that'll just print a blank line to

the screen these strings that we have

created in terms of these names here can

be combined in a print statement much

like we can combine anything that we put

in these print statements for instance I

can say system dot out dot print Ln I

can combine text I can say Valon I can

say Valentine's Day and then I can put a

plus and then I can put STR one right so

you can think of this STR one it's like

a variable just like integers or

whatever but really it's an object it's

more it's more complex than a variable

but it's holding the string so in a

print statement I can mix what we call

string literals when you're spelling it

out inside of the print statement I can

mix that with what I have created here

now notice I'm referencing STR one it's

got a red underline because I named this

one STR and then in this one STR two so

just to fix that we'll take that away

we'll hit this guy and we can say

Valentine's Day I love hamburgers now

the reason there's a no space there is

because there's no space there so I can

hit this guy here and then in a similar

way system dot out dot print LM just

like I can print using a print statement

two or three integers in a single line

if I'm like I can of course do this STR

plus STR too I can do three or four

strings whatever I can just link them

with the plus sign like I always do and

Java knows that this is a string and

this is a string and it knows how to

handle those and print that stuff out to

the screen so when I run this guy I love

hamburgers I like to go to Disneyland so

the bottom line in this lesson I wanted

to illustrate to you is mostly how do

you declare and initialize strings

you'll see it two different ways

typically you'll see it this way which

emphasizes that strings are really

objects it's perfectly fine to do it

this way or you might also see it like

this which is also very common because

it's a little simpler than all of this

stuff but in either case the string is

still an object it's just a shorthand

way to get it all set up for you also I

wanted to illustrate for you whenever we

have these strings

and you can use the print statements

that we've been using all the time to -

to print them to the screen also you can

mix them with other string literals or

you can print multiple strings to the

screen at once so there's a lot of

versatility there and once you get the

hang of what a string is and how to

declare them then what we're going to do

here in the next several sections is

teach you how to use strings in your

programs for some a little bit more

complicated things than what we're doing

here so I'll show you the full power of

how we can use strings in the java

language