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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
1
00:00:05,000 --> 00:00:09,000
They will create our first application in Java language.

2
00:00:09,000 --> 00:00:10,000
Be prepared to have fun.

3
00:00:11,000 --> 00:00:16,000
Also in this lesson, you will learn what our console applications, how to compile Java code to get

4
00:00:16,000 --> 00:00:19,000
bytecode and how to execute your first program from console.

5
00:00:20,000 --> 00:00:21,000
Let's start.

6
00:00:22,000 --> 00:00:28,000
So what a console applications console application is an application that takes input and displays output

7
00:00:28,000 --> 00:00:36,000
at command line console with access to three basic data streams, standard input, standard output and

8
00:00:36,000 --> 00:00:37,000
standard error.

9
00:00:38,000 --> 00:00:42,000
These applications don't have any graphical user interface.

10
00:00:42,000 --> 00:00:45,000
They are primarily used nowadays in learning purposes.

11
00:00:46,000 --> 00:00:51,000
So console application is a lightweight application which might be easily run in console.

12
00:00:51,000 --> 00:00:57,000
During this course, we'll learn tools which we have in GDP to create powerful applications, but the

13
00:00:57,000 --> 00:00:59,000
same as learning as a human languages.

14
00:01:00,000 --> 00:01:02,000
The first thing you learn is alphabet.

15
00:01:02,000 --> 00:01:03,000
After that, no letters.

16
00:01:03,000 --> 00:01:08,000
You can create words from words who can create sentences from sentences.

17
00:01:08,000 --> 00:01:09,000
You can build a story.

18
00:01:10,000 --> 00:01:15,000
The same is here will start from console applications to learn alphabet and words.

19
00:01:16,000 --> 00:01:22,000
Here is what we are going to do today as a software engineer will create with you files with Java.

20
00:01:22,000 --> 00:01:25,000
Extension will call this files source code.

21
00:01:25,000 --> 00:01:30,000
This is exactly files where programming code written in Java will be stored.

22
00:01:31,000 --> 00:01:35,000
After that will ask Java Compiler to compile source code to bytecode.

23
00:01:35,000 --> 00:01:38,000
Java program will do this task.

24
00:01:38,000 --> 00:01:44,000
Java will produce files with class extension and Java will execute our program.

25
00:01:45,000 --> 00:01:46,000
What do you need to understand?

26
00:01:46,000 --> 00:01:50,000
And remember that Shivam doesn't execute Java files.

27
00:01:51,000 --> 00:01:54,000
Jivin executes class files properly.

28
00:01:54,000 --> 00:01:58,000
This will help you in the future when you will troubleshoot issues in your real life project.

29
00:01:59,000 --> 00:02:05,000
So now let's take a look how to create your first program, how to compile it and run theme.

30
00:02:05,000 --> 00:02:07,000
Please don't be afraid.

31
00:02:07,000 --> 00:02:11,000
You won't use common line to write your programs on a regular basis.

32
00:02:11,000 --> 00:02:16,000
In the next, lessons will run special desktop applications designed to create programs.

33
00:02:17,000 --> 00:02:22,000
But now I'd like to ask you again to open a common line wherever you want on your computer.

34
00:02:22,000 --> 00:02:27,000
For example, for this lesson I created folder where I will create a program together with you.

35
00:02:28,000 --> 00:02:32,000
Let's type seemed easier to open a common line probably using right now.

36
00:02:32,000 --> 00:02:34,000
And you must be kidding me.

37
00:02:35,000 --> 00:02:36,000
Why do we do this?

38
00:02:36,000 --> 00:02:41,000
Why don't we just start work with applications which help us to create programs?

39
00:02:41,000 --> 00:02:44,000
The answer is there are a few points to consider.

40
00:02:45,000 --> 00:02:52,000
The first one you need to learn how to work with console at least some basics because sometimes not

41
00:02:52,000 --> 00:02:57,000
always, but sometimes depending on which built tool you will use, probably you would need to work

42
00:02:57,000 --> 00:02:58,000
with console.

43
00:02:58,000 --> 00:03:04,000
And sometimes as software engineers you would need to write scripts which will call command line comments.

44
00:03:04,000 --> 00:03:06,000
Real life example.

45
00:03:06,000 --> 00:03:11,000
You would like to compile all Java files in your project in one archive, which will be deployed to

46
00:03:11,000 --> 00:03:13,000
the Web server afterwards.

47
00:03:13,000 --> 00:03:19,000
Right after compiling, you would like to run specific files, desk classes to be sure that tests are

48
00:03:19,000 --> 00:03:22,000
not failing in case tests are succeed.

49
00:03:23,000 --> 00:03:27,000
You finish your build process in case there are failures in the tests.

50
00:03:27,000 --> 00:03:28,000
You don't proceed with your build.

51
00:03:28,000 --> 00:03:33,000
That is one of the examples where knowledge of common line comments might come in handy.

52
00:03:33,000 --> 00:03:36,000
The second point to consider Guice.

53
00:03:36,000 --> 00:03:38,000
You had just started learning Java.

54
00:03:38,000 --> 00:03:44,000
You need to understand what compilation process is, what is compilation error and how it is different

55
00:03:44,000 --> 00:03:46,000
from runtime exception.

56
00:03:46,000 --> 00:03:53,000
That's why I ask all my students to create program in Notepad and to run it and console at least once

57
00:03:53,000 --> 00:03:59,000
in their lives in some conference on the first level of interview process of junior software engineers

58
00:03:59,000 --> 00:04:04,000
where you have quizzes and tests, sometimes there are also questions related to common line.

59
00:04:06,000 --> 00:04:12,000
We opened the common line to create file with Java extension, why we cannot just do mouse right click

60
00:04:12,000 --> 00:04:19,000
and create file because you need to enable showing the file extension first in different Windows version.

61
00:04:19,000 --> 00:04:21,000
It is done in a slightly different way.

62
00:04:21,000 --> 00:04:26,000
And from default options, you can create file with the extension, which doesn't work for us.

63
00:04:27,000 --> 00:04:32,000
So let's create file from console because it is easier and it's the same way to create file from the

64
00:04:32,000 --> 00:04:35,000
terminal on different Windows versions actually.

65
00:04:35,000 --> 00:04:36,000
Now at least a few ways.

66
00:04:36,000 --> 00:04:38,000
How to create file from command prompt.

67
00:04:38,000 --> 00:04:40,000
But let me show one to you.

68
00:04:41,000 --> 00:04:43,000
Type the following notepad.

69
00:04:44,000 --> 00:04:46,000
Hello World Dot Java.

70
00:04:47,000 --> 00:04:50,000
When dialogue is popped up, click yes.

71
00:04:53,000 --> 00:05:00,000
Now we see file with Java extension created here in case you have Mac just type like this.

72
00:05:01,000 --> 00:05:04,000
Dutch Hello, World Dot Jalaa.

73
00:05:05,000 --> 00:05:06,000
The file will be created.

74
00:05:07,000 --> 00:05:15,000
Let's write our first program, we'll cover right now basic syntax so each Java file can have only one

75
00:05:15,000 --> 00:05:17,000
public class or interface.

76
00:05:18,000 --> 00:05:21,000
We'll cover more modifiers during all be topic.

77
00:05:21,000 --> 00:05:28,000
But for now, just try to understand that in case class is public, client can access it from anywhere,

78
00:05:29,000 --> 00:05:32,000
including our game, which needs to access this class.

79
00:05:32,000 --> 00:05:36,000
The name of public class should be the same as the name of a Java file.

80
00:05:36,000 --> 00:05:37,000
This is true.

81
00:05:38,000 --> 00:05:42,000
In either case you would have compilation error Gervais's case sensitive.

82
00:05:43,000 --> 00:05:49,000
So hello world written with Capital H at the beginning is completely different from Hello World with

83
00:05:49,000 --> 00:05:50,000
H lowercase.

84
00:05:51,000 --> 00:05:55,000
Java is a case sensitive because it uses a C style syntax case.

85
00:05:55,000 --> 00:06:01,000
Sensitivity is useful because it lets you infer what the name means based on its case.

86
00:06:01,000 --> 00:06:07,000
For example, the Java standard for class names is uppercase in the first letter of each word, for

87
00:06:07,000 --> 00:06:11,000
example, like integer string, etc..

88
00:06:12,000 --> 00:06:18,000
The standard for variable and method names is lowercase, the first letter of the first word and uppercase

89
00:06:18,000 --> 00:06:19,000
and all other first letters.

90
00:06:21,000 --> 00:06:26,000
No print, learn, Czech error, etc.

91
00:06:28,000 --> 00:06:35,000
And the standard for constants is all uppercase was on this course, some constant like this.

92
00:06:36,000 --> 00:06:43,000
So after hello, world, let's put curly braces here in Java, curly braces define SCOP.

93
00:06:44,000 --> 00:06:51,000
Here is the scope of the class, in a few seconds, you'll see the scope of the method each otoscope

94
00:06:51,000 --> 00:06:57,000
includes in their scope, zazz variable declared in the class scope is visible in the method scope,

95
00:06:58,000 --> 00:07:02,000
but variables declared in the method scope not visible in Glasgow.

96
00:07:03,000 --> 00:07:07,000
Now we need to create an entry point for our program by convention.

97
00:07:07,000 --> 00:07:12,000
This method is publicly accessible, so let's type here public.

98
00:07:12,000 --> 00:07:13,000
It is static.

99
00:07:14,000 --> 00:07:17,000
We'll cover more what static is during or P topic.

100
00:07:18,000 --> 00:07:19,000
It does return nothing.

101
00:07:19,000 --> 00:07:24,000
That's why return type is void and it has name main by convention.

102
00:07:25,000 --> 00:07:32,000
This method takes as a parameter array of strings in the next lessons will cover in details what arrays

103
00:07:32,000 --> 00:07:33,000
are as of now.

104
00:07:33,000 --> 00:07:36,000
Consider array of strings as a sequence of strings.

105
00:07:37,000 --> 00:07:40,000
A parameter is a variable in the method definition.

106
00:07:40,000 --> 00:07:46,000
When the method is called, the arguments are the data you pass into the methods parameters.

107
00:07:46,000 --> 00:07:49,000
Hope this doesn't bring even more confusion.

108
00:07:49,000 --> 00:07:54,000
I just want you also to learn the right wording to make your colleagues understand you.

109
00:07:55,000 --> 00:08:02,000
Public static void main method, which takes array of strings, is our entry point to each Java program.

110
00:08:02,000 --> 00:08:09,000
JVM knows that it should call exactly this method and begin program execution from this point.

111
00:08:10,000 --> 00:08:17,000
Let's define scope of this method here in this method will write code which will print hello world text

112
00:08:17,000 --> 00:08:24,000
to console output will write a system not out how to represent output.

113
00:08:24,000 --> 00:08:31,000
Stream output stream has such behavior as printing to console print, to learn method prints to console

114
00:08:31,000 --> 00:08:33,000
and terminate the line.

115
00:08:33,000 --> 00:08:41,000
Each statement in Java ends with semicolon Prindle and can take strings as arguments that's postering

116
00:08:41,000 --> 00:08:42,000
Hello World into it.

117
00:08:43,000 --> 00:08:48,000
In Java, string literals are written inside double quotes and one more new word.

118
00:08:49,000 --> 00:08:49,000
Literal.

119
00:08:50,000 --> 00:08:54,000
Literal is a notation for representing a fixed value in source code.

120
00:08:55,000 --> 00:08:57,000
Here we have string literal.

121
00:08:57,000 --> 00:08:58,000
Now let's save this file.

122
00:09:01,000 --> 00:09:09,000
Let's go back to the console and now let's compile our program, Javadoc Hello, world, that Java.

123
00:09:10,000 --> 00:09:17,000
If you don't see any messages, that means compilation was successful and you can notice that new file

124
00:09:17,000 --> 00:09:24,000
is appeared in our folder, this file has the same name as our Java file, but has different extension.

125
00:09:24,000 --> 00:09:28,000
This file contains byte code for our Java virtual machine.

126
00:09:29,000 --> 00:09:31,000
Now let's execute bytecode on our game.

127
00:09:32,000 --> 00:09:33,000
Let's step Java.

128
00:09:34,000 --> 00:09:36,000
Hello, world and press enter.

129
00:09:37,000 --> 00:09:43,000
In this case, you don't need to specify extension for the file since Java expects only class files

130
00:09:44,000 --> 00:09:45,000
and you see output here.

131
00:09:46,000 --> 00:09:48,000
Hello world congratulations.

132
00:09:49,000 --> 00:09:51,000
You have just written your first console application.

133
00:09:52,000 --> 00:09:53,000
What are common mistakes?

134
00:09:54,000 --> 00:10:00,000
You can see compilation error after calling Java and you can see runtime errors.

135
00:10:00,000 --> 00:10:02,000
Let me reproduce it with you.

136
00:10:02,000 --> 00:10:05,000
Let's pretend your name your public class incorrectly.

137
00:10:06,000 --> 00:10:08,000
Not the same as Java filename.

138
00:10:09,000 --> 00:10:15,000
Let's put capital G at the end and also let's pretend you did a typo in main method name.

139
00:10:16,000 --> 00:10:17,000
Let's save the file.

140
00:10:19,000 --> 00:10:20,000
Let's call Jack now.

141
00:10:23,000 --> 00:10:31,000
We see compilation error, so code is not compiled to bytecode, always read these error messages,

142
00:10:32,000 --> 00:10:34,000
they can help you to fix issues.

143
00:10:35,000 --> 00:10:41,000
For example, like here, public class, hello world should be declared in the file with the same name.

144
00:10:41,000 --> 00:10:44,000
We will just rename our public class.

145
00:10:45,000 --> 00:10:51,000
Let's compile it one more time, but not forget to save the file before compilation process.

146
00:10:52,000 --> 00:10:58,000
So now everything is OK and we don't see any compilation errors, and by the way, to call comments,

147
00:10:58,000 --> 00:11:01,000
which I just used, I use up Arrow.

148
00:11:02,000 --> 00:11:03,000
Let's run it again.

149
00:11:04,000 --> 00:11:06,000
And here we have one time exception.

150
00:11:07,000 --> 00:11:09,000
What does it mean, compile it?

151
00:11:09,000 --> 00:11:12,000
But error happened during the program execution.

152
00:11:13,000 --> 00:11:20,000
You try to find humane method, but couldn't hope this information will help you to troubleshoot issues

153
00:11:20,000 --> 00:11:21,000
in case you will have once.

154
00:11:22,000 --> 00:11:25,000
Let's recap one more time what we have learned today.

155
00:11:26,000 --> 00:11:28,000
And now you know what console applications are.

156
00:11:29,000 --> 00:11:31,000
You understand basic Java syntax.

157
00:11:31,000 --> 00:11:34,000
You learn how to create hello world application in Java.

158
00:11:34,000 --> 00:11:38,000
Also, you saw what compilation errors and the runtime errors are.

159
00:11:39,000 --> 00:11:41,000
You can compile and execute your programs from the console.

160
00:11:42,000 --> 00:11:43,000
And here's your homework.

161
00:11:44,000 --> 00:11:48,000
Try to repeat everything what we did during the lesson by yourself.

162
00:11:48,000 --> 00:11:54,000
Also, I'd like you to write that or shall file, which will compile your source code and we'll start

163
00:11:54,000 --> 00:11:59,000
your Java application, try to do it by yourself with help of the Internet if needed.

164
00:12:00,000 --> 00:12:06,000
And we'll check your homework in the next lesson together and read Java Code Convention from the link

165
00:12:06,000 --> 00:12:06,000
here.

166
00:12:06,000 --> 00:12:09,000
Pay extra attention to naming convention.

167
00:12:10,000 --> 00:12:16,000
Java naming convention is a rule to follow as you decide what to name your intent to file, such as

168
00:12:16,000 --> 00:12:22,000
class baggage, variable, constant method, etc. but it's not forced to follow.

169
00:12:22,000 --> 00:12:25,000
So it's known as convention, not neutral.

170
00:12:26,000 --> 00:12:32,000
These conventions, as suggested by several Java communities such as Sun Microsystems and Netscape,

171
00:12:33,000 --> 00:12:39,000
all the classes, interfaces, packages, methods and fields of Java programming language and given,

172
00:12:39,000 --> 00:12:45,000
according to the Java naming conventions, if you fail to follow these conventions, it may generate

173
00:12:45,000 --> 00:12:46,000
confusion.