Java - First Project: Console and Dialog I/O

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 15

Java – First Project

Console and Dialog I/O


Part 1: A Closer Look at the "Hello World!"
Application

Here is its code from page 17 of the textbook:


public class HelloPrinter
{
public static void main(String[] args)
{
// Display a greeting in the console window.
System.out.println("Hello World!");
}
}
Components of HelloPrinter
The "Hello World!" application consists of
three primary components:

• source code comments,


• the HelloPrinter class definition,
• the main method.
Source Code Comments
/**
* Write a description of class HelloPrinter here.
* @author Bunn
* @version 8-20-10
*/
public class HelloPrinter
{
public static void main(String[] args)
{
// Display a greeting in the console window.
System.out.println("Hello World!");
}
}
Comment Types
Comments are ignored by the compiler but are useful to other
programmers.
Two types of comments appear in HelloPrinter: the blue comment is
multi-line java doc comment and the gray comment is one line only.
The Java programming language supports three kinds of comments:
/* text */
The compiler ignores everything from /* to */ over mutiple lines.
/** documentation */
This indicates a documentation comment (doc comment, for short). The
compiler ignores this kind of comment, just like it ignores comments
that use /* and */. The javadoc tool uses doc comments when
preparing automatically generated documentation.
// text
The compiler ignores everything from // to the end of the line.
Java syntax rules versus style guidelines
Syntax Rules must be obeyed in order to compile the program:
• Uppercase and lowercase letters are different.
• Spaces are not allowed in any Java identifier (such as a class name).
• Identifiers (such as class names) must start with a letter of the
alphabet.
• Identifier names may contain letters, numbers, underscores, and $.
• Each begin brace { must have a matching end brace }.
• Braces delimit a body of code such as the class code or the method
code.
• More rules to come as you learn more this term…
Java syntax rules versus style guidelines
Style guidelines used by good programmers and by the people who
wrote the language:
• Use a block comment identifying its use, author, version for each
class. This is the java doc comment in HelloPrinter (blue one)
• Comments in the body of the code are used to help the programmer
make the code clearer to other programmers.
• Class files always start with uppercase letters (System, String, …)
• Method names always start with lowercase letters (main(), println(),
…).
• Each new block of code should be indented with matching { and }
braces.
• Identifier names should reflect the use of the item within the project.
• More style guidelines to come…
Class Definition
The following bold text begins the class definition
block for the "Hello World!" application:
public class HelloPrinter
{
public static void main(String[] args)
{
// Display a greeting in the console window.
System.out.println("Hello World!");
}
}
Class Definition cont’d

The most basic form of a class definition is:


public class name
{
...
}
The keyword class begins the class definition for a
class named name, and the code for each class
appears between the opening and closing curly
braces marked in bold above.
The main Method
The bold text begins the definition of the main method:
public class HelloPrinter
{
public static void main(String[] args)
{
// Display a greeting in the console window.
System.out.println("Hello World!");
}
}
Note that the end of main in indicated by bold text }
The main Method Signature
In the Java programming language, every application must
contain a main method whose signature is:

public static void main(String[] args)

The main method is used by the runtime system as the


starting point for execution of Java applications.
The modifiers public and static can be written in
either order (public static or static public),
but the conventional style is to use public static
as shown above.
The main Method Parameter
public static void main(String[] args)
The main method defines one parameter (accepts a
single argument): an array of elements of type
String. You can name the argument anything you
want, but most programmers choose "args".
This array is one mechanism through which the runtime
system passes information to your application. The
"Hello World!" application ignores its command-line
arguments, but you should be aware of the fact that
such arguments do exist.
Output to the console
System.out.println("Hello World!");
This instruction uses the System class from the
core library to print the "Hello World!" message to
standard output. Portions of this library (also
known as the "Application Programming
Interface", or "API") will be used in this course.
The System class and the String class are part of
the java.lang package, which is included by
default in all Java programs.
Application Programming Interface

The API is found online at


http://download.oracle.com/javase/6/docs/
You can also download the documents found
in FirstClass – Computer Science –
Software Downloads – Java 6 code and API
(The zipped folder contains the API)
Java Language Structure
Java’s basic language components are classes. Each
class is saved as a file with the same name as the
class.
Classes are grouped into packages with functionally
similar classes.
The two libraries which contain the packages are
java and javax.
The java.lang package in the java library is
included in all programming by default.
The classes in HelloPrinter are java.lang.System and
java.lang.String.

You might also like