Page 57 - iPlus_Ver_2.0_class_8
P. 57

i +  STRUCTURE OF A JAVA PROGRAM
                 The program created in the previous section contains a single class named HelloWorld which
                 is declared by using the class keyword. The curly brackets ({ }) are used to start and end the
                 body of the class. The HelloWorld class has one method named main( ). The main( ) method is
                 declared by using three Java keywords, which are public, static, and void. We will discuss the Java
                 keywords later in the chapter. The main() method must look like:
                                        public static void main(String args[])
                 The only term that you can change in the syntax of the main( ) method is the name of the parameter
                 args, which can be any valid identifier name. The array of strings (String []) will contain the command-
                 line arguments, if any, when the program is executed from the command prompt. In BlueJ, we can
                 also use main() method without any argument. The main() contains a single statement:
                                     System.out.println("Hello from Touchpad");
                 This statement is used to display the message “Hello from Touchpad” on the output screen.
                 System.out represents the standard output of the device where the program is running, and the
                 println() method displays the given string along with a line feed.
                 Notice the semicolon at the end of the System.out.println() statement terminates the line. It is
                 also known as the line terminator. The Java compiler ignores all whitespace and indenting, so it
                 is necessary to use a semicolon to denote the end of all statements in Java.

                  i +  BASIC FUNDAMENTALS OF JAVA

                 Like other programming languages, Java also provides some fundamental concepts which are
                 given below:

                 •  Identifiers
                 •  Keywords

                 •  Data Types
                 •  Literals
                 •  Variables

                 •  Comments
                 Let us discuss these in detail.
                 Identifiers

                 An identifier is the name given to an object in a Java program. It is used to uniquely identify an
                 object in a program. For example, a class name or a method name.

                 Rules for Naming Identifiers

                 The rules for naming an identifier are:
                 •  An identifier can consist of letters, digits 0–9, underscore ( _ ), and dollar ($) symbol.
                 •  An identifier cannot start with a digit.

                 •  Keywords cannot be used as an identifier.
                 •  No special symbols like !, @, #, %, etc. can be used in an identifier.

                 •  Identifiers are case-sensitive, which means ‘name’ and ‘NAME’ are two different identifiers.


                                                                                                                  55
                                                                                               Program Coding
   52   53   54   55   56   57   58   59   60   61   62