A
Computer Program (or
Code) is simply a sequence of instruction(s) and decision(s) used to instruct a computer's CPU to perform certain and/or specified action(s);
and
Programming (or
Coding) is essentially the art of designing and implementing computer programs. Thus, a collection of computer programs is known as:
Software.
A
Translator (or
Language Processor) is a software that is capable of translating and/or transcoding a computer program from one form/language into another form/language.
On one hand, a
Compiler is a translator that translates an entire computer program (or
source code) into machine code (or
target code) prior to executing/running the machine code which can be done immediately or thereafter.
During a compilation process, the entire source code must be devoid of syntax errors; otherwise, the compilation process fails.
On the other hand, an
Interpreter is a translator that translates each line (line-by-line) of computer program (or
source code) into machine code (or
target code), and this machine code is executed/run almost immediately.
During an interpretation process, each line of the source code must be devoid of syntax errors; otherwise, the interpretation process fails.
Some High-Level-Language computer programs usually require a two-step hybrid translation procedure involving a compilation process and an interpretation process, viz:
- Compilation: High-Level-Language computer program (e.g. Java, C#, etc.) —→ Low-Level-Language computer program (e.g. Bytecode, Assembly code, etc.)
- Interpretation: Low-Level-Language computer program (e.g. Bytecode, Assembly code, etc.) —→ Machine Code (native to host CPU's architecture)
Caption: 2-step Translation procedure
Other High-Level-Language computer programs require a one-step translation procedure involving either a compilation process or an interpretation process, viz:
- Compilation or Interpretation: High-Level-Language computer program (e.g. PHP, Python, etc.) —→ Machine Code (native to host CPU's architecture)
Caption: 1-step Translation procedure
To this end, Java, as High-Level Programming Language, employs a two-step hybrid translation procedure as depicted in the diagram below:
Caption: Java's 2-step Translation procedure
Origin of Java Programming, viz:
- June 1991: Java Programming Language project began.
- January 23, 1996: The first public implementation of Java Programming Language was released as Java 1.0.
- Design and Development Team: James Gosling (Team Leader), Mike Sheridan, and Patrick Naughton.
- Design and Development Company: Sun Microsystems, Inc. which has already been acquired by Oracle Corporation in January 27, 2010.
- Latest Implementation: Available via accessing the official website here
The following highlights the merits of learning how to program in Java Programming Language, viz:
- Java programs are comparatively safer and more secure against malicious attacks.
- It is a portable programming that can executed on any Operating System platform, CPU architecture, etc.
- Java employs an Object-Oriented paradigm towards programming which fosters reusability of code(s).
- Java possesses a robust and extensive library of prewritten classes available as packages.
- It is a flexible programming language that is capable of implementing programs which targets desktop, web, and mobile (user) platforms.
Irrespective of the aforementioned advantages attributed to programming in Java, there still exist some demerits associated with the Java Programming Language, viz:
- Java syntaxes are relative verbose in comparison to other High-Level Programming Languages.
- Java programs are relatively slower to translate due to its two-step hybrid translation procedure.
- The Integrated Development Environment (IDE) software necessary to design, develop, debug, etc., requires significant memory and processing requirements. In other words, programming using the Java IDE is computationally expensive.
However, the merits of programming in Java outweighs its demerits.
There exist several IDEs which can be used to design, develop, implement, debug, etc., programs in Java. In this regard, we shall be using Eclipse
® IDE which is one of the popular IDEs that support Java Programming Language.
Eclipse Foundation provides several multi-platform installation packages for public access and usage via its official webpage
here.
After the download and respective installation of your Eclipse
® IDE, the following annotated steps exemplifies how to launch and create your first simple program in Java.
- On your desktop, double-click on the "Eclipse IDE for Java Developers..." icon to open your Eclipse® IDE workspace.
-
With the aid of your cursor and/or mouse, navigate to the menu bar and click on the following, viz: File → New → Java Project. Also, if this is your first time of opening your Eclipse® IDE, navigate to the Project Explorer panel (leftmost panel),
and simply click on the "Create a Java project" menu link as shown below.
Caption: File → New → Java Project
-
Next, type in a suitable filename (e.g. "HelloWorld") for your new Java Project, and check the radio button: Use an execution environment JRE. Also, ensure that this checkbox is unchecked: Create module-info.java file as shown below;
and click on the Finish button.
Caption: Enter Project Name and check/uncheck as depicted herewith
-
By default, every new project created via Eclipse® IDE links to the JRE System Library and contains a (default package) in your Java project's source folder (i.e. src).
Now, to program in Java, your lines/statements of code must be written within block(s) of
class
which are stored in a .java file within your Java project's source folder.
Hence, to create a new class
block in a .java file, right-click on the (default package) icon and then click: New → Class.
Caption: Right-click on the (default package) icon, and then click: New → Class
-
In the new "Java Class" dialog window that appears, enter a suitable filename (e.g. "HelloWorld") for your .java file (NB: the filenames of your Java Project and Java Class do not have to be the same).
Thereafter, you may check the following checkboxes: public static void main(String[] args) and Generate comments as shown in the diagram below.
Caption: Enter the name for your Java Class, and then check/uncheck as shown herewith
-
Finally, in the code editor window that appears, you should write your lines of code within the
public class ...{...}
block and within the public static void main(String[] args) {...}
sub-block.
As shown below, our simple program only prints text to the screen using the Java System.out.println
statement. Thereafter, to execute or run your simple program, click on the: Run icon
() present in the toolbar; or from the menu bar, click: Run → Run.
Caption: To execute or run program, from the menu bar, click: Run → Run
An important point to note with respect to programming in Java is that its syntaxing is
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title Introduction to Java.
* @author Dr. Bonaventure Chidube Molokwu.
*/
public class HelloWorld {
public static void main(String[] args) {
//TODO Auto-generated method stub
System.out.println("Hello World!");
System.out.println("Object Oriented Programming I");
}
}
Code Snippet: 1
class
block(s) or segment(s)
class
segment(s) are the basic building block(s) of any Java program.
- A Java program (or source code) must be comprised of at least one or more
class
block(s) or segment(s).
- There can exist at most one (1)
class
block (or segment) tagged as public
(i.e. public class
) in any given Java source code or .java file.
- The identifier or name of the
public class
must be same as the name of your .java file (or source code).
- From Code Snippet 1: the
public class
block begins from line 8 and ends on line 16.
method
block(s) or segment(s)
- A Java program (or source code) must be comprised of at least one or more
method
block(s) or segment(s).
- In Java,
method
block(s) must always be encapsulated within class
block(s) or segment(s) for any given Java program.
- Each
method
block encloses lines of code which implements the logic of your program.
- There can exist at most one (1)
method
block (or segment) tagged as public
and identified/named main
(i.e. public ... main() {...}
) in any given Java source code or .java file.
- The
public static void main(String[] args) {...}
serves as the main entry point into any Java program (or source code).
- From Code Snippet 1: the
public ... main() {...}
block begins from line 10 and ends on line 14.
System
class
- This is an important prewritten
class
block (or segment) which is packaged and available in the java.lang
package.
- The
System
class block encapsulates several method
implementations which perform different unique tasks.
- The
java.lang
package is automatically and implicitly imported into any Java program (or source code); hence, there is no need for any explicit import statement for this package.
- Some of the
method
implementations in this class include: print()
, println()
, etc.
print()
: prints output to the screen without any trailing line break.
println()
: prints output to the screen with a trailing line break.
- From Code Snippet 1: calls to the
println()
method exist on line 12 and on line 13.
Comment line(s) and block(s)
- Generally, in programming, a comment is usually a human-readable (explanation) text written alongside the line statements of a computer program, and which expatiates on what certain lines (or blocks or segments) of code accomplish.
- Comments are always ignored by translators (either compilers or interpreters), and they are treated as non-executable component of a computer program.
- Comments promote ease of understanding for humans with reference to the code content of a computer program.
- Also, comments promote reusability, reproducibility, teamwork; and they serve as an effective approach to Code Documentation.
- Depending on the context and purpose of a computer program, comments (in programming) can exist as either a single-line comment or multi-line comment.
- From Code Snippet 1:
//
is used in Java programming to denote a single-line comment as shown on line 11.
- Moreover, from Code Snippet 1:
/* */
is used in Java programming to denote a multi-line comment as shown from line 1 to line 6.
As earlier mentioned, the line(s) of code that constitute a Java program is/are usually written within one or more
method {...}
blocks, which are in turn, encapsulated within one or more
class {...}
blocks, respectively.
Also, every complete line statement of code in Java must be suffixed with a semicolon:
;