1 00:00:00,390 --> 00:00:01,790 This video is for Mac. 2 00:00:01,800 --> 00:00:05,190 If you're using a Windows computer, please go to the next lesson. 3 00:00:07,080 --> 00:00:07,710 So far. 4 00:00:07,710 --> 00:00:13,740 You installed Visual Studio Code, a text editor where you can write code and you installed Java inside 5 00:00:13,740 --> 00:00:14,490 your computer. 6 00:00:14,490 --> 00:00:16,650 So now you can run Java code. 7 00:00:17,380 --> 00:00:18,220 In this lesson. 8 00:00:18,220 --> 00:00:20,170 That's exactly what you're going to do. 9 00:00:20,200 --> 00:00:23,140 You will write and run your first Java code. 10 00:00:25,110 --> 00:00:27,120 Step one is to create a folder. 11 00:00:28,990 --> 00:00:31,210 Simple enough inside of your desktop. 12 00:00:31,210 --> 00:00:32,980 Make a new folder. 13 00:00:33,880 --> 00:00:36,310 And call the folder section. 14 00:00:37,830 --> 00:00:38,580 One. 15 00:00:40,340 --> 00:00:43,310 This folder is going to contain your Java file. 16 00:00:45,480 --> 00:00:48,960 Step two is to launch the folder from Visual Studio Code. 17 00:00:49,380 --> 00:00:51,120 Here I have Visual Studio code. 18 00:00:51,120 --> 00:00:51,700 Open. 19 00:00:51,720 --> 00:00:53,070 Go to file. 20 00:00:54,030 --> 00:00:54,690 Open. 21 00:00:55,830 --> 00:00:58,260 Find the section one folder that you just created. 22 00:00:58,260 --> 00:01:03,510 Mine is inside of desktop and here select the folder section one. 23 00:01:03,510 --> 00:01:06,990 Once you select it, press open in order to launch it. 24 00:01:14,910 --> 00:01:17,220 Step three is to create a Java file. 25 00:01:19,370 --> 00:01:24,560 When creating your Java file, you must name it using the camel case format where the first letter of 26 00:01:24,560 --> 00:01:28,670 every word is capitalized and the words themselves are joined together. 27 00:01:29,670 --> 00:01:30,510 And your file. 28 00:01:30,510 --> 00:01:35,610 For it to be considered a Java file, it needs to end with the extension Java. 29 00:01:37,530 --> 00:01:38,580 Let's do just that. 30 00:01:38,580 --> 00:01:45,000 I'm going to create a new file by pressing the plus icon, and the file is going to be called Hello 31 00:01:45,000 --> 00:01:51,360 Java again using Camel case, where the first letter of every word is capitalized, followed by the 32 00:01:51,360 --> 00:01:53,040 DOT Java extension. 33 00:01:53,700 --> 00:01:56,160 And here we've created a hello Java file. 34 00:01:56,160 --> 00:01:56,880 Beautiful. 35 00:01:58,080 --> 00:02:01,680 Step four is to create a Java class inside the Java file. 36 00:02:03,150 --> 00:02:03,750 In Java. 37 00:02:03,750 --> 00:02:06,420 You will write all of your code in a class. 38 00:02:08,590 --> 00:02:11,470 And there are two parts to a class the class keyword. 39 00:02:12,700 --> 00:02:14,320 Followed by the class name. 40 00:02:15,840 --> 00:02:18,420 Inside brackets is where you would write your code. 41 00:02:20,830 --> 00:02:25,000 And furthermore, the class name needs to always match the file name that it's in. 42 00:02:26,320 --> 00:02:32,110 So if the file name is called Hello, Java and Java, the corresponding class is Hello, Java. 43 00:02:34,440 --> 00:02:37,290 Inside your Java file, you can create your Java class. 44 00:02:37,290 --> 00:02:39,300 So start with the class keyword. 45 00:02:40,040 --> 00:02:41,820 Followed by the class name itself. 46 00:02:41,840 --> 00:02:43,070 Hello, Java. 47 00:02:44,660 --> 00:02:47,900 And inside brackets is where you're going to write all of your code. 48 00:02:48,700 --> 00:02:49,200 Perfect. 49 00:02:49,210 --> 00:02:50,650 We made a lot of progress. 50 00:02:50,650 --> 00:02:52,510 Let us delete all of our progress. 51 00:02:52,510 --> 00:02:54,340 We're going to delete our Java file. 52 00:02:54,340 --> 00:02:55,300 But wait a second. 53 00:02:55,300 --> 00:02:55,750 Ran. 54 00:02:55,750 --> 00:02:58,840 We made all of this progress, and now we're just going to delete it. 55 00:02:58,930 --> 00:02:59,980 Just trust me. 56 00:02:59,980 --> 00:03:04,120 So go here, delete your file, Move to trash. 57 00:03:04,770 --> 00:03:07,050 Because we already created a Java file. 58 00:03:07,080 --> 00:03:11,430 Now these code knows that this workspace is meant for Java development. 59 00:03:11,430 --> 00:03:15,660 So the second time we create a Java file, remember to use Camel case. 60 00:03:16,200 --> 00:03:20,670 Now these code is ready and it's going to auto generate the class for you. 61 00:03:20,910 --> 00:03:27,780 By default, it adds a public keyword which makes our Hello Java class publicly accessible to other 62 00:03:27,780 --> 00:03:30,810 classes that may exist inside of this workspace. 63 00:03:31,260 --> 00:03:35,910 But there are no other classes in this workspace, so public is not really doing anything. 64 00:03:36,150 --> 00:03:38,250 You can keep it, you can remove it. 65 00:03:38,250 --> 00:03:39,240 It doesn't really matter. 66 00:03:39,240 --> 00:03:40,140 It's up to you. 67 00:03:40,320 --> 00:03:41,760 I'm just going to keep it. 68 00:03:45,110 --> 00:03:48,140 Step five Your class needs a main method. 69 00:03:50,110 --> 00:03:52,870 Maine is the entry point of a Java application. 70 00:03:52,870 --> 00:03:55,030 This is where Java starts running your code. 71 00:03:55,030 --> 00:04:01,240 When you run your application, Java starts by looking for the main method so inside of your class. 72 00:04:01,940 --> 00:04:07,310 Press enter after the first bracket to get proper indentation and then right main. 73 00:04:07,910 --> 00:04:09,380 Choose the first option. 74 00:04:10,430 --> 00:04:14,960 And by clicking this option vs code automatically writes the main method for you. 75 00:04:15,320 --> 00:04:20,779 Now this might look like a lot of code and it might look a bit scary, but rest assured, the main method 76 00:04:20,779 --> 00:04:22,430 always looks like this. 77 00:04:22,550 --> 00:04:25,420 All you need to know is that this is the entry point. 78 00:04:25,430 --> 00:04:27,890 This is where Java starts running your code. 79 00:04:28,160 --> 00:04:33,260 Inside of Main, we're going to print a message and you can do this using the print line function. 80 00:04:33,350 --> 00:04:34,520 So system. 81 00:04:35,200 --> 00:04:37,990 Dot out dot print line. 82 00:04:39,690 --> 00:04:42,260 Instead of double quotes, I'm going to write. 83 00:04:42,270 --> 00:04:44,220 Hello, Java. 84 00:04:48,890 --> 00:04:51,980 Before we do anything else, let's break this line of code down. 85 00:04:52,010 --> 00:04:55,280 Print line is a function that prints a message to the console. 86 00:04:56,990 --> 00:05:00,440 In parentheses is where you place the message that you want to print. 87 00:05:02,110 --> 00:05:04,630 And the semi-colon is very important. 88 00:05:04,660 --> 00:05:07,300 The semicolon means end of statement. 89 00:05:07,330 --> 00:05:11,680 Every statement in Java, every line of code needs a semicolon at the end. 90 00:05:13,410 --> 00:05:15,390 If you forget your semicolon. 91 00:05:17,460 --> 00:05:19,980 Java is going to throw a big red error. 92 00:05:19,980 --> 00:05:24,630 If you hover on the error, it tells you that you forgot a semicolon, so you need to insert it. 93 00:05:27,080 --> 00:05:28,400 On the topic of errors. 94 00:05:28,400 --> 00:05:31,700 If you misspell the function, you're going to get an error. 95 00:05:31,910 --> 00:05:34,250 Java has no idea what function this is. 96 00:05:34,250 --> 00:05:36,320 It has no idea what you're talking about. 97 00:05:37,160 --> 00:05:41,720 And last but not least, code and any programming language is sensitive. 98 00:05:43,980 --> 00:05:48,780 If you capitalize a letter that shouldn't be capitalized, you're also going to get an error. 99 00:05:48,810 --> 00:05:53,520 Java does not recognize the print capital T function, so please be careful. 100 00:05:53,520 --> 00:05:56,010 Your syntax needs to always be exact. 101 00:05:56,010 --> 00:05:57,810 It needs to always be perfect. 102 00:05:59,420 --> 00:06:03,110 And you'll remember that the class name needs to always match the file name. 103 00:06:03,500 --> 00:06:09,710 So if I were to misspell the class by capitalizing a random letter that I shouldn't here, Java tells 104 00:06:09,710 --> 00:06:13,560 us that this class name does not match the file name that it's in. 105 00:06:13,580 --> 00:06:15,140 So we need to fix it. 106 00:06:16,630 --> 00:06:20,080 So please make sure that your syntax is always exact. 107 00:06:21,250 --> 00:06:23,680 Step six is to turn on autosave. 108 00:06:24,890 --> 00:06:27,860 The white dot means that you have unsaved changes. 109 00:06:27,860 --> 00:06:30,440 So what you would do is go to file. 110 00:06:31,230 --> 00:06:32,820 And then press save. 111 00:06:33,600 --> 00:06:35,370 Now your file is saved. 112 00:06:35,370 --> 00:06:40,200 And if I were to change the string, for example, if I were to add a bunch of exclamation marks. 113 00:06:43,440 --> 00:06:45,180 The white dot reappears. 114 00:06:47,980 --> 00:06:50,230 And now I have to save my code again. 115 00:06:50,470 --> 00:06:53,950 Now, this can be a bit annoying, so just turn on auto save. 116 00:06:55,300 --> 00:06:59,560 And now every time I make changes, the application saves by itself. 117 00:07:01,560 --> 00:07:03,660 The final step is to run your application. 118 00:07:03,660 --> 00:07:09,450 So once you're certain that your code is good, there are no syntax errors, there are no red underlines. 119 00:07:09,450 --> 00:07:12,330 You can execute it the run button. 120 00:07:12,330 --> 00:07:13,470 Execute your code. 121 00:07:16,760 --> 00:07:18,980 Let's dismiss the debug console. 122 00:07:19,370 --> 00:07:20,180 And there you go. 123 00:07:20,180 --> 00:07:21,740 It prints our message. 124 00:07:24,510 --> 00:07:29,140 Let's have a closer look at what's going on when you run your code, when you execute your code. 125 00:07:29,160 --> 00:07:34,260 Java looks for the entry point, it looks for main tries to find it, and once it does, it's going 126 00:07:34,260 --> 00:07:35,640 to run the code inside. 127 00:07:35,850 --> 00:07:38,310 The first line of code inside main prints. 128 00:07:38,310 --> 00:07:39,390 Hello Java. 129 00:07:39,390 --> 00:07:44,190 Then it looks for more code inside main, but there isn't any so it reaches the end. 130 00:07:48,310 --> 00:07:53,170 If you have any syntax errors, your code is not going to run. 131 00:07:58,400 --> 00:07:58,730 Here. 132 00:07:58,730 --> 00:08:03,680 It's telling me that I have an error inside of line number four and I need to fix it. 133 00:08:06,490 --> 00:08:08,110 If I rerun my code now. 134 00:08:09,610 --> 00:08:11,050 It works beautifully. 135 00:08:11,050 --> 00:08:14,650 And now I can keep running my code to my heart's content. 136 00:08:16,130 --> 00:08:17,060 All right. 137 00:08:23,260 --> 00:08:25,300 With that being said, congratulations. 138 00:08:25,300 --> 00:08:28,090 You wrote and ran your first Java code. 139 00:08:29,640 --> 00:08:33,860 In this lesson, you learn that all of your code must exist in a Java class. 140 00:08:33,870 --> 00:08:35,500 You created a class called Hello. 141 00:08:35,520 --> 00:08:38,100 Java Main is the entry point. 142 00:08:38,100 --> 00:08:40,679 This is where Java starts running your code.