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, page 2

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

Statement blocks

  • Are modules of code consisting of one or more statements

  • Begin with an opening brace "{" and end with a closing brace "}"

  • May be entirely nested within another statement block. For example, the block for the main method is nested within the block for the App class in the sample program.

The main method

  • A required method (program module) in all Java programs except applets

  • The first method executed by the JVM when processing begins

  • In a small program, it defines application processing. In larger programs, it is the highest-level module and calls other methods to perform detailed processing.

  • Should have a method header coded as follows:

public static void main(String[] args)

This states that the method has public access, is a class (static) method not to be associated with any objects of the class, does not return a value to the caller (void), has the name main, and receives an array of string references (String[]) named args as parameters. It is not important that you understand all this now, just code the statement exactly as shown. The only flexibility you have in coding the header is the name of the array. The sample program will run just fine if you re-code the method header as

public static void main(String[] x)

Writing to the system output device (the console)

  • The system output device is already open and may be referenced by the identifier System.out where out is a public reference to a PrintStream defined within the System class. Again, it is not important that you understand all this now.

  • println is a public method of PrintStream objects that accepts a string parameter and copies it to the output device. In the sample program, the string is coded as a string literal enclosed within quotation marks.

Compiler requirements

  • All Java statements are delimited by a semicolon ";"

  • The compiler ignores "white space" characters (spaces, tabs, and the use of the return key). A good programmer, however, will use "white space" liberally to make their code easier to write, read, and maintain.

DO NOT write code like this

public class App {public static void main(String[] args){
System.out.println("Hello World!");}}

even though it compiles and runs just fine. Try it and see!

Review questions

  1. Which of the following are invalid headers for the main method (choose three)

  1. public static void Main(String[] args)

  2. public static void main(String[] parms)

  3. public static void main(String args)

  4. public static void main(string[] args)

  5. public static void main(String[] args)

  1. What will result from attempting to compile and execute the following?

public class App
{
public static void main(String[] parms)
{
// System.out.println("Java is fun");
}
}

  1. it will not compile

  2. it will compile but an error will occur at run time

  3. it will compile and run but nothing will display

  4. it will compile and run to display the message "Java is fun" on the console

  1. What will result from attempting to compile and execute the following?

public class App {
public static void main(String[] parms) {
System.out.println("Java is fun");
}
}

  1. it will not compile

  2. it will compile but an error will occur at run time

  3. it will compile and run but nothing will display

  4. it will compile and run to display the message "Java is fun" on the console

  1. What will result from attempting to compile and execute the following?

public class App
{
public static void main(String[] args)
{
System.out.println("Java is fun")
}
}

  1. it will not compile

  2. it will compile but an error will occur at run time

  3. it will compile and run but nothing will display

  4. it will compile and run to display the message "Java is fun" on the console

go to previous page of java lesson Previous page: A sample Java program:  

 


 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