1 00:00:00,240 --> 00:00:02,700 So let's take a closer look at what we just achieved 2 00:00:02,700 --> 00:00:05,050 especially around the text we printed to the screen. 3 00:00:06,240 --> 00:00:11,030 We're going to learn about two new topics, strings and variables. 4 00:00:11,030 --> 00:00:13,030 In Java, by placing letters or 5 00:00:13,030 --> 00:00:16,930 characters in double quotes, we create what is known as a String. 6 00:00:18,290 --> 00:00:21,710 A handy way to visualize strings, is by picturing a banner that you might 7 00:00:21,710 --> 00:00:24,930 hang at a party, that is stringing together individual characters. 8 00:00:26,030 --> 00:00:27,955 Strings are how we deal with text and 9 00:00:27,955 --> 00:00:30,830 you'll encounter them quite a bit in your programming adventures. 10 00:00:30,830 --> 00:00:32,880 In fact, since strings are so 11 00:00:32,880 --> 00:00:36,450 common, they're one of the basic data types that Java offers. 12 00:00:36,450 --> 00:00:41,680 A variable is a way to store data into a named location that you can use 13 00:00:41,680 --> 00:00:43,250 to reference later. 14 00:00:43,250 --> 00:00:47,881 In Java, you must specify what data type you are planning on storing so 15 00:00:47,881 --> 00:00:52,839 that it knows how to store that information, String is one of those types. 16 00:00:52,839 --> 00:00:57,460 And we'll discover more data types as this course progresses, when you need them. 17 00:00:57,460 --> 00:01:00,840 If you just can't wait, I've also added a link in the teachers' notes. 18 00:01:01,980 --> 00:01:05,680 Let's go back to our workspace, and let's learn how to use a String variable. 19 00:01:07,120 --> 00:01:10,670 All right, so let's say I asked you to write out another line to the screen that 20 00:01:10,670 --> 00:01:13,120 said, Craig is learning how to write Java. 21 00:01:13,120 --> 00:01:16,962 It looks something like this, 22 00:01:16,962 --> 00:01:23,024 it's console.printf(''Craig is learning 23 00:01:23,024 --> 00:01:30,020 how to write Java'');, now let's save that. 24 00:01:30,020 --> 00:01:32,908 Let's go ahead and compile and once again we do, 25 00:01:32,908 --> 00:01:39,093 javac Introductions.java, 26 00:01:39,093 --> 00:01:43,820 that creates the class file and we say, java Introductions. 27 00:01:46,740 --> 00:01:48,205 Well, that output looks a little bit ugly. 28 00:01:48,205 --> 00:01:51,556 It can definitely be prettied up by putting that information on different 29 00:01:51,556 --> 00:01:53,238 lines, but how are we gonna do that? 30 00:01:53,238 --> 00:01:56,769 Now obviously, if I press Enter in a line like this, 31 00:01:56,769 --> 00:01:59,590 it's gonna probably break the program. 32 00:01:59,590 --> 00:02:05,130 So what we have is there's a new thing they introduced called an escape sequence. 33 00:02:05,130 --> 00:02:06,680 And that is a \n. 34 00:02:06,680 --> 00:02:09,250 And that is the new line escape sequence. 35 00:02:09,250 --> 00:02:12,190 I'm gonna put that at the end of both of our lines and 36 00:02:12,190 --> 00:02:16,560 escape sequences are used to write characters that are not easily printable 37 00:02:16,560 --> 00:02:19,060 or may break the syntax or the structure of our code. 38 00:02:20,530 --> 00:02:23,330 Let's go down here and let's clear the screen. 39 00:02:23,330 --> 00:02:26,260 And you can do that by typing clear and we'll clear our terminal. 40 00:02:26,260 --> 00:02:29,540 See, it's reset back now and let's go ahead and let's compile. 41 00:02:29,540 --> 00:02:31,630 Now I want to show you another little terminal trick here. 42 00:02:31,630 --> 00:02:34,070 If you press the Up arrow, it's going to go and 43 00:02:34,070 --> 00:02:35,860 go through the history of what you've typed. 44 00:02:35,860 --> 00:02:39,630 So let's get back to where we said, javac Introductions.java. 45 00:02:39,630 --> 00:02:42,930 We’ll run that, whoop, look I forgot to save. 46 00:02:42,930 --> 00:02:45,608 You come up and you press Save. 47 00:02:45,608 --> 00:02:47,750 No, again let's go Up arrow. 48 00:02:47,750 --> 00:02:52,600 Introductions, and let's run it, here we go. 49 00:02:52,600 --> 00:02:57,510 That is much better looking and aside from talking in third person Tarzan speak, 50 00:02:57,510 --> 00:02:59,710 this is looking much better. 51 00:02:59,710 --> 00:03:03,180 All right, so let's assume we wanted to make this program about someone else. 52 00:03:03,180 --> 00:03:05,520 And one way to handle that would be to change every String that 53 00:03:05,520 --> 00:03:06,950 contains your name. 54 00:03:06,950 --> 00:03:09,130 But imagine that our program got much longer and 55 00:03:09,130 --> 00:03:12,620 the name Craig is in hundreds of thousands of places in the code. 56 00:03:12,620 --> 00:03:15,230 We wouldn't want to have to change that name everywhere and 57 00:03:15,230 --> 00:03:16,740 keep changing it everywhere. 58 00:03:16,740 --> 00:03:20,360 So a better way to handle this is to store that information in what is known 59 00:03:20,360 --> 00:03:21,270 as a variable. 60 00:03:22,530 --> 00:03:26,700 So the way that you declare a variable is first by defining its type. 61 00:03:26,700 --> 00:03:31,567 And since we know we want to have a string variable, say String. 62 00:03:32,860 --> 00:03:35,870 And next we want to give it a name that we can reference in the code later. 63 00:03:37,080 --> 00:03:38,330 The better we name our variables, 64 00:03:38,330 --> 00:03:42,100 the more likely someone reading our code will be able to understand its purpose. 65 00:03:42,100 --> 00:03:45,015 So in that case, let's name our variable firstName. 66 00:03:47,290 --> 00:03:51,220 Now we do an equal sign, and then double quotes, and your name, 67 00:03:51,220 --> 00:03:56,300 and then of course a semi colon to mark that this statement is over. 68 00:03:57,300 --> 00:04:02,430 This reads as assign Craig to the variable firstName of the data type String. 69 00:04:02,430 --> 00:04:04,190 In Java, while not required, the standard for 70 00:04:04,190 --> 00:04:07,480 naming variables is to use what is known as CamelCase. 71 00:04:07,480 --> 00:04:09,209 The first letter is lowercase, and 72 00:04:09,209 --> 00:04:12,158 then each new word starts with a capital letter, like this. 73 00:04:14,691 --> 00:04:21,420 thisIsAnExampleOfCamelCasing. 74 00:04:21,420 --> 00:04:24,870 Earlier when I introduced you to the printf method on the console object, 75 00:04:24,870 --> 00:04:26,602 I said that it was for printing text. 76 00:04:26,602 --> 00:04:27,859 But, to be more accurate, 77 00:04:27,859 --> 00:04:31,660 it actually prints formatted text that's what the f stands for. 78 00:04:31,660 --> 00:04:34,890 This lets us be more creative with our text, using variables and 79 00:04:34,890 --> 00:04:37,130 other special formatting instructions. 80 00:04:37,130 --> 00:04:41,840 It turns out that printf takes multiple options or parameters. 81 00:04:41,840 --> 00:04:44,660 The first parameter that is passed to the printf function 82 00:04:44,660 --> 00:04:47,370 is what is known as the format string. 83 00:04:47,370 --> 00:04:52,060 When using methods, you can add additional parameters by separating them with a coma. 84 00:04:52,060 --> 00:04:55,770 Those additional parameters are used to replace the format specifiers 85 00:04:55,770 --> 00:04:57,790 that are in the format string. 86 00:04:57,790 --> 00:05:00,117 Let me show you what I mean. 87 00:05:00,117 --> 00:05:04,995 First I'll replace Craig with the format 88 00:05:04,995 --> 00:05:09,881 specifier %s where s stands for string. 89 00:05:09,881 --> 00:05:14,879 And then, after the first parameter here, I'm gonna insert a comma, 90 00:05:14,879 --> 00:05:18,070 then I'm gonna put in a firstName variable. 91 00:05:18,070 --> 00:05:19,400 Let's do the same for the second sentence. 92 00:05:19,400 --> 00:05:22,700 So we'll replace the Craig with %s. 93 00:05:22,700 --> 00:05:24,250 And we'll get to the end of the line here, and 94 00:05:24,250 --> 00:05:30,020 we'll put a comma in the second parameter argument is firstName. 95 00:05:31,670 --> 00:05:33,270 Okay, so let's save that. 96 00:05:34,370 --> 00:05:37,880 Let's do a clear, we'll use the Up arrow to get 97 00:05:39,150 --> 00:05:41,940 back to our javac Introductions, so we'll compile the program. 98 00:05:43,885 --> 00:05:46,826 We'll run the program, great it still works. 99 00:05:49,311 --> 00:05:53,590 Okay now let's change the value of our firstName variable to a friend of yours. 100 00:05:53,590 --> 00:05:57,552 I'll change mine to a fellow teacher's name, Ben. 101 00:05:57,552 --> 00:06:03,168 And I'll save the file and we'll do clear, run javac and 102 00:06:03,168 --> 00:06:07,614 then we'll run Java and we'll see that I only 103 00:06:07,614 --> 00:06:12,650 changed the one place, but we updated both lines. 104 00:06:13,990 --> 00:06:18,440 Great job using strings and variables and formatters even, for that matter. 105 00:06:18,440 --> 00:06:23,950 So we learned, when you create or declare a variable, you first use its data type. 106 00:06:23,950 --> 00:06:29,120 In this case, we used the data type string and then we assigned with the equal sign 107 00:06:29,120 --> 00:06:32,560 and set the firstName variable to the String, Craig. 108 00:06:33,780 --> 00:06:37,010 We created that String by surrounding it in double quotes. 109 00:06:38,420 --> 00:06:40,750 And of course we end our statement with a semicolon. 110 00:06:41,860 --> 00:06:45,490 So that is the syntax or structural rules for creating a variable. 111 00:06:46,860 --> 00:06:51,327 One thing of note here is that Java is case sensitive which means the case, 112 00:06:51,327 --> 00:06:53,243 is it upper case or lower case? 113 00:06:53,243 --> 00:06:54,710 It makes a difference. 114 00:06:54,710 --> 00:06:57,990 Now let's do an exercise and make sure all this new information is sticking.