Download Java: Minimal Console Program Introduction Concepts

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Structured programming wikipedia , lookup

Scala (programming language) wikipedia , lookup

Library (computing) wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Class (computer programming) wikipedia , lookup

Interpreter (computing) wikipedia , lookup

Java syntax wikipedia , lookup

Object-oriented programming wikipedia , lookup

Java (programming language) wikipedia , lookup

One-pass compiler wikipedia , lookup

Java ConcurrentMap wikipedia , lookup

Covariance and contravariance (computer science) wikipedia , lookup

C++ wikipedia , lookup

C syntax wikipedia , lookup

Java performance wikipedia , lookup

Go (programming language) wikipedia , lookup

Name mangling wikipedia , lookup

C Sharp syntax wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Transcript
minimal console program > console > Java
Java: Minimal Console Program
Introduction
This article explains how to create a minimal console program and compile it using Java.
The minimal program forms the simplest possible program. It runs, but does not appear to do
anything.
Concepts
program
A computer program is a collection of instructions, or statements, to
perform a task or set of tasks to be carried out by a computer. The
instructions that comprise the minimal program are contained in the listing
in the Source Code section.
A program is commonly compiled into a file which can be executed from
the computer’s operating system (e.g. Unix, Windows, DOS), and is thus
known as an executable. In Java, executables consist of classes made up of
Java bytecode and have an extension of .class. The minimal program
compiles into an executable with the filename minimal.class.
language
A programming language is an artificial language that can be used to
control the behaviour of a machine, particularly a computer. Programming
languages, like human languages, are defined through the use of syntactic
and semantic rules, to determine structure and meaning respectively.
This article demonstrates construction of a program in the Java
programming language.
console program
A console program is a computer program designed to be used via a textonly computer interface.
The minimal program runs entirely as a text-only program and must be
started from a text-only command prompt. It is therefore a console
program.
main
If a program is executable, it requires a main method that serves as the
entry point.
The main method for the minimal program is declared as follows:
public static void main( String[] args )
minimal console program > console > Java
source code
Source code (commonly referred to as source or code) is a sequence of
statements written in a human-readable programming language.
The source code of the minimal program is stored in a file named
minimal.java.
bytecode
Java bytecode is the form of instructions that the Java virtual machine
executes. When a Java program is executed the program consists of
bytecode which is interpreted when the program is run.
compile
When a program is compiled the source code is translated into another
language (the target language).
Source code is commonly compiled into programs which can be executed
from the computer’s operating system (e.g. Unix, Windows, DOS), and are
thus known as executables. In Java, executables consist of classes made up
of Java bytecode and have an extension of .class.
The minimal program
minimal.class.
is
compiled
Source Code
The source code listing is as follows:
class minimal
{
public static void main( String[] args ) {}
}
into
an
executable
named
minimal console program > console > Java
Compiling and Running
Instructions
1. Save the source code listing into a file named minimal.java.
2. Launch a Windows command prompt.
Click the start button, All Programs, Accessories and Command Prompt.
minimal console program > console > Java
3. Navigate to the directory minimal.java was saved in.
4. To compile the program, type:
> javac minimal.java
(Do not type the > character – it is the command prompt.)
5. To test the program, type:
> java minimal
Notes
Before compiling, the source code must be saved in a text file. Java source code should be saved
with a filename extension of .java.
After the source code has been saved, the program can be compiled into Java bytecode. The
command to do this is javac minimal.java.
javac
is the compiler.
minimal.java
is the name of the source file to compile.
To run the compiled program, enter java minimal. Because there are no instructions in the
main method, nothing appears to happen.
minimal console program > console > Java
Code Explanation
class minimal
{
...
}
A class can be described as a template for objects used by the program. A Java program must be
composed of at least one class. Classes are declared using the class keyword. The format of a
class is as follows:
class identifier { members }
where:
class
is the keyword indicating that a class is being declared.
In this class declaration, as in all class declarations, the keyword is the word
class.
identifier
is the identifier by which it will be possible to refer to the class.
The identifier of the class declared here is minimal.
members
contains the members of the class, which describes its characteristics.
The only member of the class declared in this program is the main method.
The name of the class declared above is minimal. Because it is the only class declared, it
describes all the characteristics of the program. In this article, the only characteristic of the
minimal class, and therefore the only characteristic of the program, is that it contains a main
method.
public static void main( String[] args ) {}
If a program is executable one of the classes available must have a static main method that serves
as the entry point.
The main method is the entry point and is thus called when the program starts. The format of a
method declaration is as follows:
modifiers type identifier( parameters ) { statements }
minimal console program > console > Java
where:
modifiers
is a list of keywords that modify the method.
modifiers also include the type.
Strictly speaking, the
Other than the type modifier, the main method declaration contains two
modifiers: public and static.
The private, protected and public modifiers control accessibility.
The public modifier declares the method to be visible to everyone. The
main method is declared as public because it must be visible to everyone
in order to allow the program to be called.
A method that has been modified with the static keyword is accessible
without requiring an instance of the containing class. What this means is
that the method can be called without declaring any objects based on the
class All that is required is for the method to be declared as a member of the
class, as in the example above, and it can be called directly. The main
method is declared using the static modifier to provide a global
mechanism for calling the program, since at the time of calling no instances
of the class will have been declared.
type
is the data type specifier of data returned by the method. This specifier
indicates the type of the method's return value, which can be assigned.
A type specifier specifies the type of an entity. Examples of types are int
for integer or String for text string.
The main method must be declared using a return value type of void,
which indicates that the method does not have a return value.
identifier
is the identifier by which it will be possible to call the method.
The identifier of the main method is main.
parameters
(as many as needed) consist of a data type specifier followed by an identifier
for each parameter. They make it possible to pass arguments to the method
when it is called. Parameters are separated by commas.
The main method has one parameter – args, which is a String array
representing arguments passed when running the program from the
command line. The minimal program does not use the args parameter.
statements
is the method's body. It is a block of statements surrounded by braces {}.
The statements form the instructions for what the method should do.
The main method has no statements, and thus does nothing and nothing
appears to happen when the program runs.
minimal console program > console > Java
Terms
class
A class defines the abstract characteristics of a thing (object), including its
characteristics and what it can do.
From a programming point of view a class can be described as a template
from which direct instances are generated.
The class in the minimal program is identified by minimal.
member
When a class is declared, the declaration includes its members, which
describe its characteristics. The members of a class can include variables
(often referred to as properties) and functions (often referred to as methods)
which are specific to the class.
In the minimal program, the main method is a member of the minimal
class.
method
A subroutine is a portion of code within a larger program that performs a
specific task and is relatively independent of the remaining code. A
function is a subroutine that returns a value. A method is a function which
is a member of a class.
The only method in the minimal program is the main method.
data type
A data type is a classification of a particular kind of information. It can be
defined by its permissible values and operations.
The main method declaration defines a return data type of void.
type specifier
The type of an entity is specified using a data type specifier, sometimes
called a type specifier.
The type specifier for the main method is void, which indicates that the
main method does not return any data.
parameter
A parameter is a variable that takes on the value of the corresponding
argument passed in the call to a method.
The main method has one parameter – args, which is a String array
representing arguments passed when running the program from the
command line. The minimal program does not use the args parameter.
minimal console program > console > Java
argument
An argument is the actual value assigned to a parameter when a method is
called.
The main method has one parameter - args, which could be used to pass
arguments, but the minimal program does not use the args parameter and
therefore does not accept any arguments.
identifier
An identifier is a token that names an entity.
An identifier is the programming equivalent of a name. It is a textual token
that can be used to refer to an entity, such as a data type, a class or a
method.
The identifier of the minimal class is minimal. The identifier of the
main method is main.
keyword
A keyword is a word or identifier that has a particular meaning to its
programming language.
The declaration of the main method uses three keywords: public,
static and void.
modifier
A modifier keyword, or modifier, is a keyword that modifies an entity.
The main method declaration contains three modifiers: public that
declares the method to be visible to everyone, static that makes the
method accessible without requiring an instance of the containing class and
type modifier void that indicates that the method does not return a value.
string
A string is a sequence of symbols.
Strings are often used to represent readable text. For example, "hello" can
be represented by a string of character symbols.
The parameter in the main method is an array of strings.
minimal console program > console > Java
array
When working with closely related data of the same type and scope, it is
often convenient to store them together in a data structure instead of in
individual variables. The most common data structure is the array. Arrays
are fixed-length structures for storing multiple values of the same type.
In Java, an array is declared in the same way any variable is declared. The
type is the type of the elements contained in the array. The [] notation is
used to indicate that the variable is an array. For example:
String
single
;
String[] multiple ;
In this example single is a single String variable and multiple is
an array of String variables.
The parameter in the main method is an array of strings.