Introduction to Java
A first simple program
A First Simple Program
Entering the Program
- the name you give to a source file is very important.
- For this example, the name of the source file should be Example.java
- In Java, a source file is officially called a compilation unit.
- It is a text file that contains (among other things) one or more class definitions.
- (For now, we will be using source files that contain only one class.)
- The Java compiler requires that a source file use the .java filename extension
- In Java all code must reside inside a class
- By convention, the name of the main class should match the name of the file that holds
the program
- Java is case-sensitive
Compiling the Program
- To compile the Example program, execute the compiler, javac, specifying the name of the
source file on the command line, as shown here
- C:\>javac Example.java
- The javac compiler creates a file called Example.class that contains the bytecode version
of the program
- To run the program, you must use the Java application launcher called java
- To do so, pass the class name Example as a command-line argument, as shown here:
- C:\>java Example
A Closer Look at the First Sample Program
- /*
This is a simple Java program.
Call this file "Example.java".
*/
- This is a comment
- The contents of a comment are ignored by the compiler
- Java supports three styles of comments
- The one shown at the top of the program is called a multiline comment
- This type of comment must begin with /* and end with */.
- Anything between these two comment symbols is ignored by the compiler
A Closer Look at the First Sample Program
- The next line of code in the program is shown here:
- class Example {
- This line uses the keyword class to declare that a new class is being defined.
- Example is an identifier that is the name of the class
- The entire class definition, including all of its members, will be between the opening curly
brace ({) and the closing curly brace (})
- The next line in the program is the single-line comment, shown here:
- // Your program begins with a call to main().
- A single-line comment begins with a // and ends at the end of the line
- The third type of comment is a documentation comment
A Closer Look at the First Sample Program
- The next line of code in the program is shown here:
- public static void main(String[] args) {
- This line begins the main( ) method
- this is the line at which the program will begin executing
- a Java program begins execution by calling main( )
- The public keyword is an access modifier, which allows the programmer to control the
visibility of class members
- public member may be accessed by code outside the class in which it is declared
- The opposite of public is private, which prevents a member from being used by code
defined outside of its class
- The keyword static allows main( ) to be called without having to instantiate a particular
instance of the class.
- main( ) is called by the Java Virtual Machine before any objects are made
- The keyword void simply tells the compiler that main( ) does not return a value
A Closer Look at the First Sample Program
- In main( ), there is only one parameter, a complicated one.
- String[ ] args declares a parameter named args, which is an array of instances of the
class String
- Objects of type String store character strings.
- In this case, args receives any command-line arguments present when the program is
executed
- The next line of code is shown here
- System.out.println("This is a simple Java program.");
- Output is actually accomplished by the built-in println( ) method.
- In this case, println( ) displays the string which is passed to it
- As you will see, println( ) can be used to display other types of information, too
- System is a predefined class that provides access to the system, and out is the output
stream that is connected to the console