Monday 24 November 2014

Beginning Programming 
Lexical Components Of a Program
We have seen that a Java application resides in a class and at a minimum has a main method. The  method has statements that can be executed and produce the results we want. Let us now look at a few lexical conventions of Java. Our HelloWorld program uses some reserved words - public, class, static, void are some reserved words used here. This means that they have a specific meaning for the compiler and cannot be used for other purposes in the program. You can look up the complete list of reserved words in http://download.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html
statement is the smallest unit that can be executed.
System.out.println("This is a statement"); //is a statement.
A statement must be terminated with a semi-colon. A block in a program is group of statements. Blocks are delimited by { }. Comments are informative statements written in a program and are ignored by the compiler. They are meant for people to understand the program. Here is the HelloWorld application interspersed with comments. You can run this program and see that the comments do not affect the execution of the program.
public class HelloWorld { //single line comment, start of block 1
/* An example of a multi-line comment.
 It spans two lines*/
public static void main(String[] args) {//single line comment, start of block 2
System.out.println("Hello World!"); //statement
  }//single line comment, end of block 2
}//single line comment, end of block 1The Java compiler is very strict about syntax, and any code with syntax errors will not even compile. 
A class definition must be within a block (delimited by { and } ). So must a method definition. 

No comments:

Post a Comment