|
|
|
1000 Java Tips ebook
|
|
 

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

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
-
Which of the following are
invalid headers for the main method (choose three)
-
public static void Main(String[] args)
-
public static void main(String[] parms)
-
public static void main(String args)
-
public static void main(string[] args)
-
public static void main(String[] args)
-
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"); } }
-
it will not compile
-
it will compile but an
error will occur at run time
-
it will compile and run
but nothing will display
-
it will compile and run to
display the message "Java is fun" on
the console
-
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"); } }
-
it will not compile
-
it will compile but an
error will occur at run time
-
it will compile and run
but nothing will display
-
it will compile and run to
display the message "Java is fun" on
the console
-
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") } }
-
it will not compile
-
it will compile but an
error will occur at run time
-
it will compile and run
but nothing will display
-
it will compile and run to
display the message "Java is fun" on
the console
Printer Friendly Page
Send to a Friend
..
Search here again if you need more info!
|
|
|