Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

The Java Lesson 2: Anatomy of a simple Java program

JavaFAQ Home » Java Lessons by Jon Huhtala Go to all tips in Java Lessons by Jon Huhtala


Bookmark and Share
All Java Lessons contents page | Java Lesson 1 | Java Lesson 2 | Java Lesson 3 | Java Lesson 4 | Java Lesson 5 | Java Lesson 6 | Java Lesson 7 | Java Lesson 8 | Java Lesson 9 | Java Lesson 10 | Java Lesson 11 | Java Lesson 12 | Java Lesson 13 | Java Lesson 14 | Java Lesson 15 | Java Lesson 16 | Java Lesson 17 | Java Lesson 18 | Java Lesson 19 | Java Lesson 20 | Java Lesson 21 | Java Lesson 22 | Java Lesson 23 | Java Lesson 24 | Java Lesson 25 | Java Lesson 26 | Java Lesson 27 | Java Lesson 28 | Java Lesson 29 | Java Lesson 30 | Java Lesson 31 | Java Lesson 32 | Java Lesson 33 | Java Lesson 34 | Java Lesson 35 | Java Lesson 36 | Java Lesson 37 | Java Lesson 38 | Java Lesson 39 | Java Lesson 40 | Java Lesson 41 | Java Lesson 42 | Java Lesson 43 | Java Lesson 44 | Java Lesson 45 | Java Lesson 46
In order to code and test Java programs, you need Java development software and must know how to use it. Before you go on, you MUST read and master the essential skill of running Java programs

A sample Java program

The following program is a slight variation of the program that is part of the original Test project. When executed, it will display the message "Hello World!" on the system output device (the computer console):

/**
* This class defines a program that displays a message on the console.
*
* Written by: Jon Huhtala
*/

public class App {

// This required method is the starting point for processing.

public static void main(String[] args) {
System.out.println("Hello World!"); // Display the message.
}
}

Testing the sample program

Assuming you have a Test project and have already opened a Notepad window and a Command Prompt window, the procedure to test this sample program is as follows:

  1. "Copy" the source statements (shown in blue above).

  2. "Paste" the source statements into your Notepad window being sure to replace the entire contents of the window if it contained previous code.

  3. Save the source file under the name "App.java" in your Test project.

  4. Compile the "App.java" source file by entering the following command in the Command Prompt window:

javac App.java

  1. Run the "App.class" bytecode file by entering the following command in the Command Prompt:

java App

Program analysis

Studying this sample program line-by-line will start you on the path to learning Java. The sections that follow will help you analyze the sample program. For now, it is not important that you understand everything completely. Through constant usage, you will eventually know what it all means.

Comments in Java

  • Are for documentation purposes only. The compiler ignores them when generating bytecode.

  • Three comment techniques are supported

  1. Javadoc. Begin with /** and end with */. Everything between is treated as a comment.

  2. Single-line. Begin with // and automatically continue to the end of the line.

  3. Multi-line. Begin with /* and end with */. Everything between is treated as a comment.

  • Comments make programs easier to maintain. Good programmers document well enough that a person not familiar with the program can read the comments to know what each section of code is doing.

The lesson continues here

The program's class

  • Every Java program must be defined as a class. The statement

public class App

is the class header. It declares that a class named App is to be defined and that it is to be publicly available for access from other places. In order for the Java Virtual Machine to find and use the program's class, always make it public. Other access modifiers (private, protected, and default or package access) will be presented later.

  • The words public and class are Java keywords and have special meaning. The class name is up to the programmer but must adhere to the restrictions on Java identifiers to be covered in the next lesson.

  • The name of the class must be the same as the name of the source file. If the class name is App, it must reside in a file named App.java
 

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy