Page 329 - Computer_Science_F5
P. 329
Computer Science metho d. The primary benefit of static arguments. It represents the arguments
methods is that they can be invoked
accepted by the main method, forming
without needing to create an object.
an array of strings. These arguments
For instance, the “main()” method,
are utilised for passing command-line
executed by the JVM, does not
inputs to the Java program .
FOR ONLINE READING ONLY
require an object instantiation, thus
conserving memory. (g) “System.out.println()” is used to
print a statement, where “System” is
(d) The “void” return type denotes that a class, “out” is a PrintStream class
the method does not return any value. object, and “println()” is a method
(e) “main” denotes the program’s starting of the PrintStream class. You will
point. observe further details of “System.out.
(f) “String[] args” or “String args[]” println()” when you will be encounted
is employed for command line with program examples.
Basic Java syntax and constructs
(a) The main method
In any Java program, there must be a method written The main() method is required
and you will see it in every Java program:
public static void main(String[] args)
Any code within the main() method will be executed. Every Java program has a class
name that must match the filename, and each program must include the main() method.
(b) Java output or print
System.out.println(): While inside the main() method, you can use the println()
method to display a line of text on the screen:
public static void main(String[] args) {
System.out.println(“Hello Tanzanians”);
}
Double quotes: When working with texts in this method, you have to make sure you
wrap texts inside the double quotation, such as “Hello Tanzanians”. Several lines of
text can be printed, each in its own println() method. For example:
System.out.println(“Hello Tanzanians”);
System.out.println(“Tanzania has National parks for Tourism attraction”);
System.out.println(“Tanzanian is a blessed country, full of natural resources”);
The other format of printing information in Java is by using the Print() method
which works the same to println(). For example,
320
for Advanced Secondary Schools
Computer Science Form 5.indd 320 23/07/2024 12:34

