First Java Program – Java Tutorial for Absolute Beginners #4

Hi guys Venkatash here welcome to the
next tutorial. In this tutorial we’re going to create a first program but
before we go ahead and use the Eclipse IDE I think it would be beneficial for
you to learn to use the command line to compile and run our first our program so
let’s begin by opening TextEdit and then in a new document type in public class
HelloWorld now HelloWorld is actually the class name and just make a note of that
your case being used because Java is case-sensitive followed by open brace
and closed brace just put the closed brace on a new line now just be in
between the two braces on a new line press tab and type in public static void
main open parenthesis string open square bracket close square brackets args and
then closed parenthesis followed by open brace and closed brace put the closed
brace on a new line again now this public class and static are
actually keywords in Java but for now don’t worry about that I’m going to
explain this in subsequent tutorials now if you look here this should be a
lower case P however for now leave it as it is and I will show you what does
happen when you leave it this way now go ahead and save this file save and for
the file name type in helloworld now we might notice here the file name matches
class name we entered in the file so in Java the found name must match class
name and then end this with dot Java over here
the file as utf-8 document and save next open a terminal change the directory to
where you save the Java file I’ve signed mine on the desktop
LS loops let’s do that again LS and just here type in the command
Java C the Java compiler this command is used to actually compile the Java file
to produce a class file containing the bytecode I’m going to explain all of
this in subsequent tutorials and then HelloWorld dot Java hit enter to
compile okay now you’ll see there’s an error produced here if you look over
here there’s an arrow showing where the error is as I explained Java is
case-sensitive so let’s go back into the file and change this back to a lowercase
save it and then back to the terminal to recompile the file just make a note
whenever you do make a change in the actual file
you do need to recompile it before you can run it again and every time you
compile a java file it produces a class file and I guess updated and now
that we compile no errors we can go ahead and run it now so type in Java
HelloWorld and HelloWorld by itself hit enter okay great how it’s run correctly
I’ll quickly show you the class file lay is produced over here hello world dot
class now this file can actually be run on any computer that adds the Java
Runtime environment installed ok now you’re probably wondering there’s actually no output being displayed here that’s because we actually haven’t written any
statement to be output let’s go back to the file
and then in between these two braces here hit tab twice type in system dot
out dot print Ln Ln meaning line open parentheses quotation mark now I’m going
to type in hello world but you can type in whatever you like and then quotation
mark again and then closed parentheses followed by a semicolon now this must
end with a semicolon and again I’ll explain all this in
subsequent tutorials save this go back to the terminal let’s
recompile this okay no errors now run this again okay great
as you can see it’s output hello world okay that is the end of our second
tutorial we actually created an around our first Java program on the command
line in the next tutorial we’ll go through how to use the Eclipse IDE if
you’ve got any questions or comments please leave them in the comments below
and if you enjoy the video please hit subscribe and the like button and I’ll
see you in the next video

Leave a Reply

Your email address will not be published. Required fields are marked *