16
Jan

Java Tutorial – 02 – Hello World Example


Before you can program in Java you need to
download and install the JDK, or Java Development Kit, from Sun’s website. Among other things
the JDK includes the Java compiler, class libraries, and the virtual machine needed
to run Java applications. As an alternative, Sun’s download page also has a link to get
Netbeans bundled with the JDK. This is the IDE, or Integrated development environment,
that I’ll be using throughout this tutorial. It can also be downloaded from Netbeans.org.
Another great IDE’s you can use is Eclipse, or if you don’t want to use an IDE at all
any regular text-editor works fine. When you have your programming environment
setup, we’ll start off by creating a simple Hello World program. As you can see, I’ve
created an empty file called MyApp.java and opened it in the editor. The first thing we’ll
do here is create a public class that has the same name as the physical source file.
It’s legal to have more than one class per file in Java, but only one public class, and
that name must match the filename. Also keep in mind that Java is case sensitive. The brackets
({}) following the class name delimits what belongs to that class and must be included. Next we’ll add the main method inside the
class. This is the starting point of the application and must always have this form. The keywords
themselves will be looked at in a later section. The last step in our Hello World program,
is to output the text by calling the print method. This method is located inside the
built in System class, and then another level down inside the out class. The method takes
a single argument, the string we want to print out, and it ends with a semicolon (;), as
do all statements in Java. If we’re unsure of what a specific class contain,
or what arguments a method takes, we can take advantage of code hints in some IDE’s such
as Netbeans. The window normally shows up anytime you’re typing code and have multiple
predetermined alternatives, but you can also bring it up manually by pressing Ctrl+Space.
It’s a very powerful feature that quickly gives you access to all class libraries and
their members.

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

5 Comments

Leave a Reply

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