Saturday, 5 January 2013

Creating First Java Application

Java Simple Hello World! Program

A program to print the Hello World! message on the Computer screen.


You can use the notepad as an editor to write your first program. Read the below codes and carefully write on your editor as Java is case sensitive.


Hello World! code


class HelloWorld {
    public static void main (String args[]) {
System.out.println(“Hello World!”);
    }
 }

This is the simple base program of Java. I will explain you line by line to get an idea about the unique features that constitute a Java Program. 

Class Declaration: class HelloWorld


This line declares a class, which is an object oriented construct. We must place all codes inside a class in Java. class is a keyword and declares that a new class definition follows. HelloWorld is the Java identifier that specifies the name of the class to be defined. 

Opening and Closing Braces :{  }


Every class definition in Java begins wih an opening brace "{" and ends with a matching closing brace "}", appearing in the last line in the example.

The Main Line: public static void main (String args[])


This line defines a method named main. Conceptually, this is similar to the main() fuctnion in C and C++; Every Java application program must include main() method. This is the starting point for the interpreter to begin the execution of the program. 

public : The keyword public is an access specifier that declares the main method as unprotected and therefore making it accessible to all other classes.
static:  Static declares this method as one that belongs to the entire class and not a part of any objects of the class. The main method must always be declared as static since the interpreter uses this method before any objects are created. 
void: The type modifier void states that the main method does not return any value.

The Output Line: System.out.println(“Hello World!”);


This is similar to a printf() statement in C++. Since Java is Object Oriented, every method must be part of an object. The println() method is member of the out object, which is a static data member of System class. 

Every Java statement must end with a semicolon.

Note: We must save this program in a file called HelloWorld.java ensuring that the file name contains class name properly. This file is called the source file. If the program contains multiple classes, the file name must be the class name of the class containing the main method. 

Compiling the Program


To compile the Program, we should run the Java Compiler javac with the name of then source file on the command line as shown below:
javac HelloWorld.java

Running the Program


We need to use the Java interpreter to run stand alone program. See the below line. 

java Helloworld


Output: 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HelloWorld!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Friday, 4 January 2013

Changing the PATH Environment Variable

Setting up PATH for Java Program


We may have to set the PATH for environment variables for the easier execution of the Java Program. 


We can run the JDK without setting the Environment variable. However, it is easy to compile and run the codes written in java after setting the PATH. If you do not set the PATH variable, you need to specify the full path to the executable file every time you run it. 

Eg: C:\> "C:\Program Files\Java\jdk1.7.0\bin\javac" MyProgram.java

To set the PATH variable permanently, add the full path of the jdk1.7.0\bin directory to the PATH variable. Typically, this full path looks something like C:\Program Files\Java\jdk1.7.0\bin. Set the PATH variable as follows on Microsoft Windows:

  1. Click Start, then Control Panel, then System.
  2. Select Advanced, then Environment Variables.
  3. Add the location of the bin folder of the JDK installation for the PATH variable in System Variables. The following is a typical value for the PATH variable:
C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Java\jdk1.7.0\bin

Notes: You can add multiple directories separated with semicolons(;).
      PATH Environment variables are Not Case Sensitive. 

Installing Java

How to Install Java on a Windows Computer


The following instructions show how to install Java jdk to run your first Java Program


  1. Click here to open a page where you can download Java directly from Oracle. 
  2. Find the latest version, by the time I write this, the latest available on the page is "Java SE7u10". 
  3. Click on the download tab and save a known location in your Computer. 
  4. Make sure that that its byte size provided on the download page. After the download has completed, verify that you have downloaded the full, uncorrupted software file.
  5. Double Click the downloaded file to start the installer. 
  6. Follow the instructions on the Installer Wizard to complete the installation. 
Note: The JDK installer will automatically set path for the environmental variable. No need to worry about the path as of now.

Thursday, 3 January 2013

Object Oriented Programming Vs Procedure Oriented Programming

Difference between Object Oriented Programming and Procedure Oriented Programming


What is the difference between object oriented programming and procedure oriented programming?


Procedural Oriented Programming(POP)


Procedural Oriented Programming creates a step by step program that guides the application through a sequence of instructions. Each instruction is executed in order. The program is divided into small parts called functions. Full importance is not given to data but to functions as well as sequence of actions to be done. It follows Top Down approach and it does not have any access specifier. When using Procedure Oriented Programming, data can move freely from function to function in the system. However, it is difficult to add data and function on a later point of time. The functions in the Procedure Oriented Programming uses Global data for sharing that can be accessed freely from function to function in the system. Hence the system does not have an option to hide the data and it leads to insecurity.

Object Oriented Programming (OOPs)


Object Oriented Programming is made up of many entities called objects. Objects become the fundamental units and have behavior, or a specific purpose, associated with them. Importance is given to the data rather than procedures or functions because it works as a real world. It follows Bottom Up approach and has access specifiers named Public, Private, Protected, etc. In Object Oriented Programming, objects can move and communicate with each other through member functions. This design provides easy way to add new functions. Data cannot move easily from function to function, it can be kept public or private so we can control the access of data which provides higher data security. There are so many other features available in Object Oriented Programming compared to traditional Procedure Oriented Programming; some of them are as follows. 

The ability to create GUI (Graphical User Interface) programs

Through inheritance, we can eliminate redundant code and extend the use of existing classes.

We can build programs from standard working modules that communicate with one another rather than, having to start writing the code from scratch. This leads to saving of development time and higher productivity.

Software complexity can be easily managed.

Wednesday, 2 January 2013

Basic Requirements to write a Java Program

Minimum Tools Required to Write a Program in Java


Java Compiler


A Java compiler is a compiler for the Java programming language.

Java Virtual Machine


A Java virtual machine (JVM) is a virtual machine that can execute Java bytecode.When you run a Java Program, the Computer is actually running the Java Virtual Machine. That JVM examines your byte codes, zero by zero, one by one, and carries out the instructions described in the bytecode. 


Java API


Java API refers to the Library files. Every java program, even the simplest one, calls on code in the Java API. 


An Editor


An editor is simply a window where we can write java program and save it in the disc. Notepad is one of the simple editors. There are a lot of editors available and some are specifically designed for writing java codes, which helps the programmer to format the code and also helps with few library files.  It is also known as Integrated Development Environment(IDE). 

Tuesday, 1 January 2013

Characteristics of Java

Java Characteristics and Features

Java is simple


Java was designed to be easy for the professional programmer to learn and use effectively.


Java is object-oriented


Java is true Object Oriented Language. Almost everything in Java is an object. All program code and data reside within objects and classes. 


Java is distributed


Java is designed as a distributed language for creating applications on networks. It has the ability to share both data and programs. Java applications can open and access remote objects on Internet easily as they can do in a local system. 


Java is Compiled and interpreted


Java combines both Compiled and Interpreted approaches thus making Java two stage-system. First, Java Compiler translates source code into what is known as bytecode instructions. Bytecodes are not machine instructions and therefore, in the second stage, Java Interpreter generates machine code that can be directly executed by the Machine. 


Java is robust


Java is robust language. It provides many safeguards to ensure reliable code. It has strict compile time and run time checking for data types. 


Java is secure


Security becomes an important issue for a language that is used for programming in Internet. Java Systems not only verify all memory access but also ensure that no viruses are communicated with an applet. 


Java is architecture-neutral


Operating system upgrades, processor upgrades, and changes in core system resources can all combine to make a program malfunction. The Java designers made several hard decisions in the Java language and the Java Virtual Machine in an attempt to alter this situation. Their goal was “write once; run anywhere, any time, forever.” To a great extent, this goal was accomplished.


Java is portable


Java ensures portability in two ways. First, Java Compiler generates bytecode instructions that can  e implemented on any machine. Secondly, the size of the prmitive data types are machine independent. 


Java is multithreaded


We can listen to an audio clip while scrolling a page and at the same time download an applet from a distant computer. This feature greatly improves the interactive performance of graphical applications. 


Java is dynamic


Java is capable for dynamically linking in new class libraries, methods, and objects. 

Introduction to Java



An Introduction to Java Programming.


Java is an object-oriented programming language with a built-in application programming interface (API) that can handle graphics and user interfaces and that can be used to create applications. Java is a high-level, third generation programming language, like C, FORTRAN, Perl, and many others. You can use Java to write computer applications that play games, store data or do any of the thousands of other things computer software can do. Compared to other programming languages, Java is most similar to C and C++. However, it is not mandatory to learn C or C++ to learn Java. 

One major difference is that Java does not have pointers. However, the biggest difference is that you must write object oriented code in Java. Procedural pieces of code can only be embedded in objects. In the following we assume that the reader has some familiarity with a programming language. In particular, some familiarity with the syntax of C/C++ is useful.

To actually execute Java programs, they developed Java interpreters that ran on various machines and under various operating systems. Thus, Java became a language that would execute on a number of systems and now has implementations for virtually all common computers and operating systems.The most string feature of Java is that it is platform-neutral language. Java is the first programming language that is not tied to any particular hardware or Operating System. Programs developed in Java can be executed anywhere on any system. 

Java is an Object Oriented Language. It enables us not only to organize our program code into logical units called objects but also to take advantage of encapsulation, inheritance, and polymorphism.