Introduction to Java1.0
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:
  1. Compilation: High-Level-Language computer program (e.g. Java, C#, etc.) —→ Low-Level-Language computer program (e.g. Bytecode, Assembly code, etc.)
  2. Interpretation: Low-Level-Language computer program (e.g. Bytecode, Assembly code, etc.) —→ Machine Code (native to host CPU's architecture)
2 step translation
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:
  1. Compilation or Interpretation: High-Level-Language computer program (e.g. PHP, Python, etc.) —→ Machine Code (native to host CPU's architecture)
1 step translation
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:
Java's 2-step translation
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:
  1. Java programs are comparatively safer and more secure against malicious attacks.
  2. It is a portable programming that can executed on any Operating System platform, CPU architecture, etc.
  3. Java employs an Object-Oriented paradigm towards programming which fosters reusability of code(s).
  4. Java possesses a robust and extensive library of prewritten classes available as packages.
  5. 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:
  1. Java syntaxes are relative verbose in comparison to other High-Level Programming Languages.
  2. Java programs are relatively slower to translate due to its two-step hybrid translation procedure.
  3. 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.



1.1Installation and Working with a Java IDE
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.
  1. On your desktop, double-click on the "Eclipse IDE for Java Developers..." icon to open your Eclipse® IDE workspace.
  2. With the aid of your cursor and/or mouse, navigate to the menu bar and click on the following, viz: FileNewJava 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.
    installation process 1
    Caption: FileNewJava Project
  3. 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.
    installation process 2
    Caption: Enter Project Name and check/uncheck as depicted herewith
  4. 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: NewClass.
    installation process 3
    Caption: Right-click on the (default package) icon, and then click: NewClass
  5. 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.
    installation process 4
    Caption: Enter the name for your Java Class, and then check/uncheck as shown herewith
  6. 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: RunRun.
    installation process 5
    Caption: To execute or run program, from the menu bar, click: RunRun



1.2Anatomy of a simple Java program
An important point to note with respect to programming in Java is that its syntaxing is case-sensitive.

/**
 * @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)
  1. class segment(s) are the basic building block(s) of any Java program.
  2. A Java program (or source code) must be comprised of at least one or more class block(s) or segment(s).
  3. 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.
  4. The identifier or name of the public class must be same as the name of your .java file (or source code).
  5. From Code Snippet 1: the public class block begins from line 8 and ends on line 16.

method block(s) or segment(s)
  1. A Java program (or source code) must be comprised of at least one or more method block(s) or segment(s).
  2. In Java, method block(s) must always be encapsulated within class block(s) or segment(s) for any given Java program.
  3. Each method block encloses lines of code which implements the logic of your program.
  4. 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.
  5. The public static void main(String[] args) {...} serves as the main entry point into any Java program (or source code).
  6. From Code Snippet 1: the public ... main() {...} block begins from line 10 and ends on line 14.

System class
  1. This is an important prewritten class block (or segment) which is packaged and available in the java.lang package.
  2. The System class block encapsulates several method implementations which perform different unique tasks.
  3. 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.
  4. Some of the method implementations in this class include: print(), println(), etc.
  5. print(): prints output to the screen without any trailing line break.
  6. println(): prints output to the screen with a trailing line break.
  7. From Code Snippet 1: calls to the println() method exist on line 12 and on line 13.

Comment line(s) and block(s)
  1. 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.
  2. Comments are always ignored by translators (either compilers or interpreters), and they are treated as non-executable component of a computer program.
  3. Comments promote ease of understanding for humans with reference to the code content of a computer program.
  4. Also, comments promote reusability, reproducibility, teamwork; and they serve as an effective approach to Code Documentation.
  5. 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.
  6. From Code Snippet 1: // is used in Java programming to denote a single-line comment as shown on line 11.
  7. 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: ;



1.3Basic and Selected Terminologies in Programming
  1. Algorithm: This is simply a sequence of steps required to accomplish a task or job. Therefore, a valid algorithm must satisfy three (3) basic requirements, viz:
    1. it must be unambiguous;
    2. it must be executable; and
    3. it must be terminable.
  2. Pseudocode: This is a one of the numerous ways (e.g. Pseudocode, Flowchart, Data Flow Diagram, etc.) of implementing an algorithm which employs a simple, informal, and/or unstandardized language (e.g. English Language). Basically, pseudocodes can only be comprehended by humans and not machines or computers.
  3. Program: This is the output gotten from the implementation of an algorithm using a formal and standardized language such as a programming language. In this regard, every computer program is strictly dependent on a programming language. For example: a Java program is implemented and dependent on Java programming language; a Python program is implemented and dependent on Python programming language; etc.
  4. Programming: The art of implementing an algorithm using a specific programming language.
    Programming:    Algorithm    —Programming Language→    Program
    Essentially, (computer) programming involves the following basic five (5) steps as illustrated in the diagram below, viz:
    programming workflow
    Caption: Simplified workflow steps of (computer) programming
  5. Reserved Word: a predefined word, within the grammar(s) of a programming language, which has neither been designed to possess a unique meaning nor implement a specific functionality. They are usually left out for future design and implementation.
  6. Keyword (or Command Word): a Reserved Word which has been designed to possess a unique meaning and implement a specific functionality.
  7. Syntax: The rules that govern the use of Keywords (or Command Words) for constructing statements of code and specifying identifiers in programming.
  8. Semantics: The logical meaning and/or interpretation associated with the statements of code in a program.
  9. Error or Bug: Any illegal operation existing within a computer program.
  10. Debug: To detect and resolve error(s) or bug(s) within a computer program.



1.4Types of Errors (or Bugs) in Java Programming
  1. Syntax Error: A type of error/bug that occurs when the syntactic rules of a specific programming language (e.g. Java) were not adhered to during the design and implementation of a program.
    1. It is also known as Compile-time or Compilation error.
    2. It always hinders a program from successful execution or run.
    3. Examples: misspelling a keyword and/or a reserved word, omitting special symbols (like (), ;, [], etc.) for completing a code statement, using a variable prior to its declaration, etc.
  2. Semantic Error: A type of error/bug that occurs when the semantic rules of a specific programming language were not adhered to during the design and implementation of a program.
    1. It is also known as Logical error.
    2. It does not hinder a program from excuting or running successfully.
    3. Examples: an operation that does not adhere to the arithmetic rule of BODMAS/PEMDAS, infinite loops, incorrect usage of arithmetic or boolean operators, etc.
  3. Runtime Error: A type of error/bug that may occur during the execution of a program.
    1. It is also known as Execution error.
    2. It always hinders a program from excuting or running successfully.
    3. Examples: arithmetic division by absolute zero (0), square-root of a negative number, accessing a non-existent index in a array/list, etc.
  4. Exception: A special type of error that may occur at the Runtime (or Execution time) of a Java program; and which may result in an event/scenario that hinders the normal execution workflow of the Java program.
    1. It is exclusive to Java programming language and some other high-level languages.
    2. It usually triggers an impromptu break event.
    3. Examples: failure in a computer-network connection, inaccessible files (or inputs, data, etc.) during input/output operations, hardware failure and/or incompatibility (e.g. RAM speed, CPU architecture, etc.).



1.5Practice Exercise
  1. What is the expected output with reference to the code snippet below?
    /**
     * @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
     * @course ProgrammingLessons (proglessons).
     * @title Introduction to Java.
     * @author Dr. Bonaventure Chidube Molokwu.
     */
      
    System.out.print("The product of 5 and 6 is: ");
    System.out.println("5 * 6");
    

  2. What is the expected output with reference to the code snippet below?
    /**
     * @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
     * @course ProgrammingLessons (proglessons).
     * @title Introduction to Java.
     * @author Dr. Bonaventure Chidube Molokwu.
     */
      
    System.out.print(6 + 6);
    System.out.print(12);
    

  3. What type of error (as discussed earlier) exists in the following code snippet?
    /**
     * @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
     * @course ProgrammingLessons (proglessons).
     * @title Introduction to Java.
     * @author Dr. Bonaventure Chidube Molokwu.
     */
      
    system.out.println("Hello World");
    

  4. What type of error (as discussed earlier) exists in the following code snippet?
    /**
     * @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
     * @course ProgrammingLessons (proglessons).
     * @title Introduction to Java.
     * @author Dr. Bonaventure Chidube Molokwu.
     */
      
    System.out.println(5/0)
    

  5. Is the following code snippet valid in Java programming? If yes, state the exact screen output; and if otherwise, expantiate on the existent errors/bugs.
    /**
     * @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
     * @course ProgrammingLessons (proglessons).
     * @title Introduction to Java.
     * @author Dr. Bonaventure Chidube Molokwu.
     */
      
    ;;;;;;
    System.out.print("Welcome to ProgrammingLessons");;;;;;
    

  6. TRUE or FALSE: An exception is a special type of Execution Error?

  7. TRUE or FALSE: An algorithm might be ambiguous, but the algorithm must be executable and terminable?

  8. TRUE or FALSE: Java employs a 1-step Translation strategy for the execution of its program(s)?

  9. What is the expected output with reference to the code snippet below?
    /**
     * @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
     * @course ProgrammingLessons (proglessons).
     * @title Introduction to Java.
     * @author Dr. Bonaventure Chidube Molokwu.
     */
      
    System.out.println(3 + 3 / 3 - 3);
    

  10. What of the following lines, in the code snippet below, prints out an entire empty line to the user's screen?
    /**
     * @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
     * @course ProgrammingLessons (proglessons).
     * @title Introduction to Java.
     * @author Dr. Bonaventure Chidube Molokwu.
     */
     
    System.out.print();
    System.out.print(" ");	  
    System.out.println();	
    System.out.Println(" ");
    

Sponsored Ads