Java Programming Tutorial 3: Writing Your First Program

The file that you created in the previous tutorial should look like this:

public class Program {

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

 

}

 

}

 

  • COMMENTS – Anything on a line that is to the right of “//” is ignored by Eclipse.  Also, anything in the program after a “/*” and before a “*/” is ignored.  These are tools for you to write comments in your program that help other programmers (and yourself) to remember what’s going on in your program – to take notes, you might say.
  • FUNCTIONS – Functions are small parts of your program.  The line that says “public static void main(String[] args)” starts a function called “main”.  The following “{” marks the beginning of the function, and the next “}” marks the end of the function.  As you can see, nothing actually happens in the function right now.  When you run a Java program, it starts by looking for a function called “main” and runs it.  We’ll talk more about the words that come before the function name (“public static void”), and the stuff between the parenthesis, later.
  • CLASSES – Classes are groupings of functions and data.  They always have the word “class” before the name of the class.  They also start and end with “{” and “}”.

THE BIG PICTURE – As you can see, we have a class (called “Program”) containing one function (called “main”) that does absolutely nothing.  Our first task will be to make the program display text to the user.  To do that, write the following somewhere between the main function’s “{” and “}” markers:

System.out.println(“Hello World!”);

This command runs another function called “System.out.println”.  When running another function, everything inside of the parenthesis is sent as information to that function.  All text messages (called “Strings”) must be surrounded in quotation marks.  Finally, all commands end with a semi-colon.

You’re final program should look like this:

 public class Program {

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println(“Hello World!”);

}

 

}

Run your program by clicking on the “Run” menu and selecting “Run” or “Run Last Launched.”  Notice, in the “Console” tab, that the message “Hello World!” was displayed!

Leave a Comment