Wednesday, 23 August 2023

Lesson 6: Simple Program

 

A Java program is executed from the main method in the class.

Let’s begin with a simple Java program that is displays the message welcome to Java! On the console (output).

The word console is an old computer term that refers to the text entry and display device of a computer. 

Console input means to receive input from the keyboard, and console output means to display output on the monitor.


Line 1 - defines a class. Every Java program must have at least one class. Each class has a
name. By convention, class names start with an uppercase letter. In this example, the class
name is Welcome.

Line 2 - defines the main method. The program is executed from the main method. A class
may contain several methods. The main method is the entry point where the program begins execution.

A method is a construct that contains statements. The main method in this program contains the System.out.println statement. This statement displays the string Welcome to Java! On the console (Line 4).

String is a programming term meaning a sequence of characters. A string must be enclosed in double quotation marks. Every statement in Java ends with a semicolon (;), known a the statement terminator.

Reserved words, or keywords, have a specific meaning to the compiler and cannot be used for other purpose in the program.
For example, when a compiler sees the word class, it  understand that the word after class is the name for the class. Other reserved words in this program are public, static, and void.

Line 3 is a comment that documents what the program is and how it is constructed. Comment help programmers to communicate and understand the program. They are not programming language and thus are ignored by the compiler.

In Java, comments are preceded by two slashes ( //) on a line, called a line comment, or enclosed between / * and */ on one or several lines, called a block comment or paragraph comment. When compiler sees //, it ignores all text after // on the same line
The following below are the example of comments:


A pair of curly braces in a program form a block that groups the program’s components. In Java, each block begins with an opening braces ({) and ends with a closing braces (}).

Every classes has a class block that groups the data and methods of the class. Similarly, every method has a method block that groups the statement in the method.

Blocks can be nested, meaning that one block can be placed within another, as shown in the following code below.

`~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~'
You have seen several special characters
(e.g., { }, //, and ;) in the program. They are used in almost every program. The most common errors you will make as you learn to program will be syntax errors.
'~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.'




.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~

Check-Point

_____________________________________________________
   ~o~ End of the lesson ~o~
_____________________________________________________

Thank you!

No comments:

Post a Comment