A Simple Elucidation of a java program
The Idea
What is a java program?
A java program is a written source-code in java programming language intended to perform a desired task. The source-code is written using programming language constructs such as class.
The following figure shows a simple java program written to display a welcome message on screen:
Figure 1: A simple java program that display a welcome message on screen
Elucidation
Line numbering
In the above figure, we see some colored words and familiar symbols in numbered lines. The line numbers we see are not part of the program itself, but are used to refer to the parts of the program in a text-editor program. Line numbers assist a programmer in highlighting programming and execution errors while debugging the program.
Syntax Coloring
Most text-editor programs offer syntax coloring feature to differentiate pieces of the program that have different meaning in the source-code.
Language Syntax
Computer programming languages share some common features with our natural languages (e.g. English and Swahili) in that they use rules that govern how the language is implemented to produce logical meaning. The rules of a language are the syntax of that language that you use to compose logical sentences and statements. Computer languages use a set of rules comprising keywords, character symbols, and placement rules.
Language Comments
Computer programming languages offer a method of describing the pieces of the code you write in the programming language. The description you write are what referred to as comments. Java programming language offers two syntaxes or ways to make comments within a java source-code. The first syntax is by using two forward slash (//) to indicate an inline-comment, a comment that may only span one line. The second syntax is by using a pair of one forward slash (/) followed by an asterisk (*) to indicate multi-line comment, a comment that may span multiple lines. For example between the /* */ you write your comments that span multiple lines in your java source-code. Line number 1 in Figure 1 indicates an inline-comment describing the file name of the program.
Computer programming language keywords
Keywords in a programming language are reserved words used to denote behavior and characteristics of a given portion within a source-code of a language. In the java source-code in Figure 1 at line 2, are two keywords followed by the word “Welcome” and the symbol “}”. The next section talks about the meaning of the two keywords in java programming language.
Object Oriented Programming (OOP)
Computer programming languages use different methodologies to structure programs that behave in a given way. Object oriented methodology allows a programmer to structure a program in terms of objects that can communicate among each other to offer services. A programmer conceptualizes the behavior (referred to as methods) and characteristics (referred to as fields) of a piece of code as an object that responds to messages to offer its services. For example, a piece of code written to function as a customer that defines its behavior (what it does) and attributes (what it has) can be conceptualized as a customer object. Java language offers a mechanism for defining a structure that produces similar objects with differing values.
The “class” Keyword
The keyword class in Figure 1 at line 2, is a java command you use followed by a capitalized name you choose and a pair of curly-braces “{}” to denote the structure and scope of the code that conceptualizes an object blueprint. Therefore, a class is denoted as an object’s blueprint that gives it its classification from which a number of similar objects can be instantiated. All java code must be defined within a class declaration otherwise the compiler will produce syntax errors for not complying.
The “public” Keyword
OOP languages impose a protection mechanism known as access modifiers. An access modifier is a word denoting a command used to characterize a piece of code as where can the code be accessed or who can access the code. The protection seeks to forbid any modification to the code by other code as to provide consistency to objects themselves.
Java Class naming rules
The name you choose for your class must comply with java language class naming rules. A class name by convention should begin with a capitalized letter and if more than one word is required, you should capitalize each word. The class name should be a noun reflecting an object (e.g. Customer). Class names may only be letters, numbers, underscore and a dollar sign. In addition, a class name may not begin with a digit; only underscore and a dollar sign are allowed at the beginning.
Java Source File
A java source file is the file that contains one main or top-level class declared public and a possible number of inner-classes. The file must have the same name as the top-level class name within the file itself. The file name must end with the .java file extension. The class must be declared public so that other code in other classes (outside this class) can access it.
Program execution thread
When you start a program in your computer, each program you start is loaded into the memory and each program in memory is called a process. The processor has to switch among processes so that each process has chance to run or execute. Each process has its own execution starting point that acts as a line in which the processor has to follow while executing the program. The starting point line is the main thread of the process and this thread may contain other threads as well that do other work.
All java code and other threads must execute within the main thread. The main thread is accessed by declaring the pre-named method (main) with the public and static modifiers. This method is declared void so that it does not return anything after its execution. In the subsequent topics we shall talk about the static keyword.
Class Methods
In OOP languages, a method is a defined block of code which exposes certain behavior of an object by using a technique called algorithm.
Java methods are declared by indicating the return type followed by the method name you choose (by complying with method naming conventions) and a pair of parenthesis (), followed by a possible number of exception declarations (a mechanism for dealing with errors that we shall talk about in other topics), and a pair of curly-braces that encloses the code within the method itself.
Method Parameters
A parameter is extra information that a method requires in order to perform its task properly. A method can declare a possible number of parameters separated by a comma (,). The parameters are simply named variables and their corresponding types, (java offers eight data types from which a programmer can declare variables).
Variables
A variable is an implicitly named space in memory a programmer use to store values for later use within the program. The value of the variable may change or vary as the program executes. The values that a programmer stores in a variable must be one of primitive types (byte, short, int, long, float, double, char, and boolean) or object types (e.g. String, File, and FileReader).
Arrays
A variable can only store one value at a time, on other hand; an array is a variable declared to store multiple values of the same type. Array variable are declared by preceding a square bracket after the type or variable name (as in the main method parenthesis at line 5 in Figure 1 ).
Method signature
In the java source-code in Figure 1 at line 5, is the main method signature that has one parameter “args” of type String.
Sending Output to the Screen
Java provides different objects for input and output in the I/O library, System.out is one of I/O libraries for sending data to output devices (e.g. the computer screen). And the method println() (as on line 7 in Figure 1 ) sends some text on the computer’s screen by using a dot (.) symbol to send a message to the object to provide its service (e.g. displaying the message).
System is a predefined class in the java class library that offers many services including sending data to output devices.
Method Invocation
A method invocation is the process by which you use a dot (.) to access a service of an object. For example the statement System.out.printl(); is a method invocation statement which accesses a print service of the object out that belongs to the class System. All java statements must end with a semicolon (;) or otherwise the compiler will complain and throw some errors.
THE END
Your feedback is very important to me!
Please stay awesome!