Anatomy of a Java Program - James Madison University
Download
Report
Transcript Anatomy of a Java Program - James Madison University
Anatomy of a Java Program
AnotherQuote.java
1 /** A basic java program
2 *
3 * @author Nancy Harris, James Madison University
4 * @version V1 6/2010
5 */
6 public class AnotherQuote
7 {
8
public static void main(String args[])
9
{
10
// Quote
11
System.out.println("\"Computers are useless. They can only give you answers\"");
12
13
// Author
14
System.out.println("\tPablo Picasso");
15
}
16 }
When executed, this program displays:
"Computers are useless. They can only give you answers“
Pablo Picasso
Comments
1 /** A basic java program
2 *
3 * @author Nancy Harris, James Madison University
4 * @version V1 6/2010
5 */
6 public class AnotherQuote
7 {
8
public static void main(String args[])
9
{
10
// Quote
11
System.out.println("\"Computers are useless. They can only give you answers\"");
12
13
// Author
14
System.out.println("\tPablo Picasso");
15
}
16 }
Lines 1 – 5 are block comments. Block comments begin with a /* and end with a */.
Everything in between is ignored by the compiler and is not turned into byte code.
Comments are used to document the code for the programmers who maintain it.
A specialized block comment has the /** start and identifies comments that may
become part of the documentation of the code. These are called javadoc style comments.
You will learn more about javadoc comments later.
Class Header
1 /** A basic java program
2 *
3 * @author Nancy Harris, James Madison University
4 * @version V1 6/2010
5 */
6 public class AnotherQuote
7 {
8
public static void main(String args[])
9
{
10
// Quote
11
System.out.println("\"Computers are useless. They can only give you answers\"");
12
13
// Author
14
System.out.println("\tPablo Picasso");
15
}
16 }
Line 6 is the opening line of the application or the class. Classes are containers that enclose
applications.
“public” and “class” are reserved words. “public” means that the application may be used by
other java classes and the “class” reserved word means that this is the opening of the class
declaration. The class name follows, “AnotherQuote”. This is a name made up by the
programmer. By convention, class names are written in title case (first letter of each word is
capitalized.)
Program to Source file
AnotherQuote.java
1 /** A basic java program
2 *
3 * @author Nancy Harris, James Madison Univ
4 * @version V1 6/2010
5 */
6 public class AnotherQuote
7 {
8
public static void main(String args[])
9
{
10
// Quote
11
System.out.println("\"Computers are us
13
// Author
14
System.out.println("\tPablo Picasso");
15
}
16 }
This class source program will be stored in a file of the same name as the class,
AnotherQuote.java. Java is case sensitive, so the case of the class name must match exactly the
case of the source file. Java source files are plain text files and can be edited by any text editor.
Curly braces { }
1 /** A basic java program
2 *
3 * @author Nancy Harris, James Madison University
4 * @version V1 6/2010
5 */
6 public class AnotherQuote
7 {
8
public static void main(String args[])
9
{
10
// Quote
11
System.out.println("\"Computers are useless. They can only give you answers\"");
12
13
// Author
14
System.out.println("\tPablo Picasso");
15
}
16 }
Line 7 is the opening curly brace and Line 16 is the closing curly brace for the AnotherQuote
program. Curly braces enclose blocks of java code. In this case, they are enclosing the definition
of the AnotherQuote class.
main method
1 /** A basic java program
2 *
3 * @author Nancy Harris, James Madison University
4 * @version V1 6/2010
5 */
6 public class AnotherQuote
7 {
8
public static void main(String args[])
9
{
10
// Quote
11
System.out.println("\"Computers are useless. They can only give you answers\"");
12
13
// Author
14
System.out.println("\tPablo Picasso");
15
}
16 }
Line 8 is a method header. A method is a task within a program. For now, all of your programs
will consist of a single method called “main”. All java applications must have a main method
which must look like line 8. The curly braces enclose the code that is part of the main task.
“public”, static, void are more reserved words. You will learn their meaning later this semester.
main is the identifier used for the main method and the parameter contains the identifiers
String and args. You will learn more about parameters later in this course.
Inline comments
1 /** A basic java program
2 *
3 * @author Nancy Harris, James Madison University
4 * @version V1 6/2010
5 */
6 public class AnotherQuote
7 {
8
public static void main(String args[])
9
{
10
// Quote
11
System.out.println("\"Computers are useless. They can only give you answers\"");
12
13
// Author
14
System.out.println("\tPablo Picasso");
15
}
16 }
Lines 10 and 13 illustrate another kind of comment. These are inline comments and are
designated by //. The // symbol can occur anywhere on the line; everything following the line is
treated as comment and ignored by the compiler. You see here another convention; for full line
inline comments, there is one blank line before the start of the comment.
System.out.println
1 /** A basic java program
2 *
3 * @author Nancy Harris, James Madison University
4 * @version V1 6/2010
5 */
6 public class AnotherQuote
7 {
8
public static void main(String args[])
9
{
10
// Quote
11
System.out.println("\"Computers are useless. They can only give you answers\"");
12
13
// Author
14
System.out.println("\tPablo Picasso");
15
}
16 }
Line 11 is a Java statement. This statement carries out one piece of the overall task. It is a call to
a method in another class. The method call, System.out.println sends data to the display. The
data it sends is in parentheses following the println name. The text that is sent to the display is
in quotes to signify that this data is textual data.
All java statements end with the semi-colon symbol (;).
Escape characters
1 /** A basic java program
2 *
3 * @author Nancy Harris, James Madison University
4 * @version V1 6/2010
5 */
6 public class AnotherQuote
7 {
8
public static void main(String args[])
9
{
10
// Quote
11
System.out.println("\"Computers are useless. They can only give you answers\"");
12
13
// Author
14
System.out.println("\tPablo Picasso");
15
}
16 }
The backslash (\) that you see on line 11 tells the system that we want to display a special
character. In this case, we want to display quotes around the quotation so we use the \” to
differentiate what we want printed from the quotes used to enclose the text. You’ll learn more
about these symbols and manipulating text data later.
Line 14 is the final statement which displays the author of the quotation.