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