Strings and Variables6:58 with Craig Dennis
In this video, we will learn what a variable is, and when they should be used. We will modify our introduction to dynamically use a String variable.
It's not an error!
Not to worry, the Picked up JAVA_TOOL_OPTIONS: -Xmx128m is not an error on your part, we recently just tweaked our setup to have more memory allocated. Don't fret that mine looks different!
So let's take a closer look at what we just achieved 0:00 especially around the text we printed to the screen. 0:02 We're going to learn about two new topics, strings and variables. 0:06 In Java, by placing letters or 0:11 characters in double quotes, we create what is known as a string. 0:13 A handy way to visualize strings, is by picturing a banner that you might 0:18 hang at a party, that is stringing together individual characters. 0:21 Strings are how we deal with text and 0:26 you'll encounter them quite a bit in your programming adventures. 0:27 In fact, since strings are so 0:30 common, their one of the basic data types that Java offers. 0:32 A variable is a way to store data into a named location that you can use 0:36 to reference later. 0:41 In Java, you must specify what data type you are planning on storing so 0:43 that it knows how to store that information, string is one of those types. 0:47 And we'll discover more data types as this course progresses, when you need them. 0:52 If you just can't wait, I've also added a link in the teachers' notes. 0:57 Let's go back to our workspace, and let's learn how to use a string variable. 1:01 All right, so let's say I asked you to write out another line to the screen that 1:07 said, Craig is learning how to write Java. 1:10 It looks something like this, 1:13 it's console.printf(''Craig is learning 1:16 how to write Java'');, now let's save that. 1:23 Let's go ahead and compile and once again we do, 1:30 javac Introductions.java, 1:32 that creates the class file and we say, java Introductions. 1:39 Well, that output looks a little bit ugly. 1:46 It can definitely be prettied up by putting that information on different 1:48 lines, but how are we gonna do that? 1:51 Now obviously, if I press Enter in a line like this, 1:53 it's gonna probably break the program. 1:56 So what we have is there's a new thing they introduced called an escape sequence. 1:59 And that is a \n. 2:05 And that is the new line escape sequence. 2:06 I'm gonna put that at the end of both of our lines and 2:09 escape sequences are used to write characters that are not easily printable 2:12 or may break the syntax or the structure of our code. 2:16 Let's go down here and let's clear the screen. 2:20 And you can do that by typing clear and we'll clear our terminal. 2:23 See, it's reset back now and let's go ahead and let's compile. 2:26 Now I want to show you another little terminal trick here. 2:29 If you press the Up arrow, it's going to go and 2:31 go through the history of what you've typed. 2:34 So let's get back to where we said, javac Introductions.java. 2:35 We’ll run that, whoop, look I forgot to save. 2:39 You come up and you press Save. 2:42 No, again let's go Up arrow. 2:45 Introductions, and let's run it, here we go. 2:47 That is much better looking and aside from talking in third person Tarzan speak, 2:52 this is looking much better. 2:57 All right, so let's assume we wanted to make this program about someone else. 2:59 And one way to handle that would be to change every string that 3:03 contains your name. 3:05 But imagine that our program got much longer and 3:06 the name Craig is in hundreds of thousands of places in the code. 3:09 We wouldn't want to have to change that name everywhere and 3:12 keep changing it everywhere. 3:15 So a better way to handle this is to store that information in what is known 3:16 as a variable. 3:20 So the way that you declare a variable is first by defining its type. 3:22 And since we know we want to have a string variable, say String. 3:26 And next we want to give it a name that we can reference in the code later. 3:32 The better we name our variables, 3:37 the more likely someone reading our code will be able to understand its purpose. 3:38 So in that case, let's name our variable firstName. 3:42 Now we do an equal sign, and then double quotes, and your name, 3:47 and then of course a semi colon to mark that this statement is over. 3:51 This reads as assign credit to the variable first name of data type string. 3:57 In Java, while not required, the standard for 4:02 naming variables is to use what is known as CamelCase. 4:04 The first letter is lowercase, and 4:07 then each new word starts with a capital letter, like this. 4:09 ThisIsAnExampleOfCamelCasing. 4:14 Earlier when I introduced you to the printf method on the console object, 4:21 I said that it was for printing text. 4:24 But, to be more accurate, 4:26 it actually prints formatted text that's what the f stands for. 4:27 This lets us be more creative with our text, using variables and 4:31 other special formatting instructions. 4:34 It turns out that printf takes multiple options or parameters. 4:37 The first parameter that is passed to the printf function 4:41 is what is known as the format string. 4:44 When using methods, you can add additional parameters by separating them with a coma. 4:47 Those additional parameters are used to replace the format specifiers 4:52 that are in the format string. 4:55 Let me show you what I mean. 4:57 First I'll replace Craig with the format 5:00 specifier %s where s stands for string. 5:04 And then, after the first parameter here, I'm gonna insert a comma, 5:09 then I'm gonna put in a firstName variable. 5:14 Let's do the same for the second sentence. 5:18 So we'll replace the Craig with %s. 5:19 And we'll get to the end of the line here, and 5:22 we'll put a comma in the second parameter argument is firstName. 5:24 Okay, so let's save that. 5:31 Let's do a clear, we'll use the Up arrow to get 5:34 back to our javac Introductions, so we'll compile the program. 5:39 We'll run the program, great it still works. 5:43 Okay now let's change the value of our firstName variable to a friend of yours. 5:49 I'll change mine to a fellow teacher's name, Ben. 5:53 And I'll save the file and we'll do clear, run javac and 5:57 then we'll run Java and we'll see that I only 6:03 changed the one place, but we updated both lines. 6:07 Great job using strings and variables and formatters even, for that matter. 6:13 So we learned, when you create or declare a variable, you first use its data type. 6:18 In this case, we used the data type string and then we assigned with the equal sign 6:23 and set the firstName variable to the string, Craig. 6:29 We created that string by surrounding it in double quotes. 6:33 And of course we end our statement with a semicolon. 6:38 So that is the syntax or structural rules for creating a variable. 6:41 One thing of note here is that job is case sensitive which means the case, 6:46 is it upper case or lower case? 6:51 It makes a difference. 6:53 Now let's do an exercise and make sure all this new information is sticking. 6:54
You need to sign up for Treehouse in order to download course files.Sign up