Monday, 24 November 2014

Beginning Programming
Code to Execute
To repeat what we discussed in the last section, the program containing source code always has extension .java. The Java compiler always generates bytecode with same name and has an extension .class. The class file processed by the JVM and produces output. It is a rule that the source file name must match with the class name inside the file.
Source file
Class name
Byte code
HelloWorld.java
HelloWorld
HelloWorld.class
AddNumbers.java
AddNumbers
AddNumbers.class
Average.java
Average
Average.class
 In a Java application, execution starts with the main method. We have already used this method and will understand it better later in the course. You must have heard that Java is "platform independent". What does this mean? This means that you can write Java code on some system, say a Windows machine, and compile it. Then you can take the classfile generated and run it on any other system (Linux, Unix, MAC, or another Windows) and the code will run!
Beginning Programming
Creating and Running Java Application
We have been learning the basic concepts of Java and running code fragments from the text area. However, to actually develop and run a Java application, we need do something more. We must install the necessary software
  • JDK or Java Development Kit, which is software that can be used to develop Java based software. The JDK consists of software including Java Runtime Environment (JRE), Java compiler (javac), debugger, additional support software needed to write Java applications.
  • The Java Runtime Environment (JRE) is required to execute Java code. It consists of the Java Virtual Machine (JVM) plus standard classes and libraries needed to execute the code. The emulation capability of the JVM  allows execution of Java code in a platform independent way using the java command.
The typical execution cycle for a Java application, say to print Hello World is pictured below
A Java application must have the extension .java - this is a language rule. Upon compiling, it gets converted to a class file also known as bytecode. The JVM executes the class file using the java command.
Beginning Programming
Methods
We saw that methods allow us to define behaviour of objects - in this section we take a closer look at methods. In our first program, we wrote our code inside the main method. The main method is a special method in a Java application and the one where execution of a Java program starts. In object oriented programming, it is good practice to write methods to implement the business logic. Methods allow us to break a complex task into simpler ones, with methods providing the implementation of the simpler ones. Since a method must implement logic, it needs input (usually, but not necessarily) and it produces output. The inputs to a method are supplied by itsparameters or arguments and the output is reflected in its return value. This is the syntax to declare a method in Java
[access] returnType methodName([arguments…]) {
//method body
}
For example to write a method that prints "HelloWorld", instead of writing the code in the main method,
we can create a method called printString that takes a String as argument and will print the argument.
This method returns nothing, so its return type is void.
void printString(String str){
System.out.println(str);
}
The advantage of this approach is that by changing the value passed as the argument, 
I can print any message I want. This is just a basic introduction to methods, we will see more about them
by and by.
Beginning Programming
Object Oriented Programming 
We write computer programs to solve problems and there are several programming paradigms. Object oriented programming is a popular, widely used and common one. Java is an object oriented programming language. In object oriented programming, we solve a problem by defining objects that interact with each other. This is an intuitive way to model real world problems since we see objects all around in the real world - cars, stars, flowers, rooms etc. An object oriented language therefore provides us with a mechanism to
  1. Define the behavior and properties of objects we want to define
  2. Set up objects of each kind and put them to work
Most object oriented languages including Java allow programmers to create classes to address the first point. A class is a template or blueprint to create objects. It  lets us define state and behavior for objects and allows us to create objects of that class.
  • State is defined by internal data - in Object Oriented terminology these are referred to as fields or attributes
  • Behaviour is determined by methods that we write to implement logic
  • New objects can be created using constructors
Java allows us to write code to create classes and define fields, methods and constructors.

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. 
Beginning Programming
Structure of a Programming 
Java is an object oriented programming language, so any program that you write must be inside a class. To quickly get started with running Java applications, there are three basic steps
1. Create a class
public class HelloWorld {

}
2. Create a main method inside the class
public class HelloWorld {
public static void main(String[ ] args) {
}}
3. Write the code for the logic
public class HelloWorld {
/* A program to display the message "Hello World" on standard output */
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
The code above is a complete program to print the message "Hello World". Copy paste the code into scratch pad and run it.