Tuesday, October 30, 2012

Basics of Java - Part 2


Introduction to Java Fundamentals


Two Programming Paradigms
When we are communicating with somebody what exactly we need? The most important thing that we need is communication medium. There must be some common means of interaction. This leaded to birth of language. Initially the human communicated with each other with the help of sign language. When man came to know about the ability of different sounds they can produce they started talking in voice medium. Gradually he created the different languages that we hear today and this is how the journey of language started.
This communication was restricted to human communication but till 20th century human was very powerful in the terms of technology and he developed many electric and electronic devices and one of them was COMPUTER. This device was so powerful that it was able to manage 1000 times’ faster & efficient work than man. Computer was very much accurate than a man’s calculation.


Evolution of Computer Languages
But when this machine was invented it was very difficult to communicate with it. The language used at that time was very complex to use as well as to learn. This problem put man into finding new solution for it. The solution was a language which should be easy to understand and simple to learn. But at the same time most important aspect was it should be computer friendly i.e. machine also should interpret it quickly. In the beginning many languages were found to solve the issue but they were not much effective.
In 1972, Dennis Ritchie came with a language called C. This language changed the face of whole programming world. This language got the popularity in very less time and the reason was its user-friendly structure and fast execution. But soon the drawbacks of this language came forward and we needed a more powerful language. C was very handy in small programs but as code grows beyond some thousand lines compiler crashes. Also C was not very helpful in dealing with real world problems. This turned out in evolution of C++, a new object oriented language. This language also took the attentions of all the programmers. But soon there was a drastic change in the programming world.
By 1990’s inventors found that people have started using OSs other than Windows and they wanted a language which would be platform independent or platform neutral. Till this time nobody had thought of this kind of thing. And Sun Microsystems came up with this concept first in 1991 with Star OS. But at that time this concept was super flop as the people were much more addicted to what they had with them. In 1995, this problem was encountered and people found the thing very useful. And then Patrick Naughton, James Gosling, Ed Frank, Mike Sheridan and Chris Warth came up with the same concept with different name as “JAVA”. Java was initially known as “Oak” and later in 1995 it was renamed as Java.


Introduction to Programming
What is programming? In simple words Programming is nothing but finding solutions to given problems. Computer Programming is the art of making a computer do what you want it to do. Simply Program is a sequence or a set of instructions to the computer that will achieve your objective.
Programming is planning how to solve a problem. No matter what method is used -- pencil and paper, slide rule, adding machine, or computer -problem solving requires programming. Of course, how one programs, depends on the device one uses in problem solving.
Program can be written in any language that is preferred by the programmer. We have many programming languages to write programs like C, C++, JAVA, etc.

Introduction to Computer Science using Java
Java is a simple and yet powerful object oriented programming language and it is in many respects similar to C++. Java originated at Sun Microsystems, Inc. in 1991. It was conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. It was developed to provide a platform-independent programming language. This site gives you an Introduction to Java Programming accompanied with many java examples. It’s a complete course in java programming for beginners to advanced java.


Platform independent
Unlike many other programming languages including C and C++ when Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code. This byte code is distributed over the web and interpreted by virtual Machine (JVM) on whichever platform it is being run.


Java Virtual Machine
What is the Java Virtual Machine? What is its role?
Java was designed with a concept of ‘write once and run everywhere’. Java Virtual Machine plays the central role in this concept. The JVM is the environment in which Java programs execute. It is a software that is implemented on top of real hardware and operating system. When the source code (.java files) is compiled, it is translated into byte codes and then placed into (.class) files. The JVM executes these bytecodes. So Java byte codes can be thought of as the machine language of the JVM. A JVM can either interpret the bytecode one instruction at a time or the bytecode can be compiled further for the real microprocessor using what is called a just-in-time compiler. The JVM must be implemented on a particular platform before compiled programs can run on that platform.


Object Oriented Programming
Since Java is an object oriented programming language it has following features:
  • Reusability of Code
  • Emphasis on data rather than procedure
  • Data is hidden and cannot be accessed by external functions
  • Objects can communicate with each other through functions
  • New data and functions can be easily added
Java has powerful features. The following are some of them:-

Simple
Reusable
Portable (Platform Independent)
Distributed
Robust
Secure
High Performance
Dynamic
Threaded
Interpreted

Object Oriented Programming is a method of implementation in which programs are organized as cooperative collection of objects, each of which represents an instance of a class, and whose classes are all members of a hierarchy of classes united via inheritance relationships.


OOP Concepts
Four principles of Object Oriented Programming are
Abstraction
Encapsulation
Inheritance
Polymorphism



Abstraction
Abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of objects and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer.
EncapsulationEncapsulation is the process of compartmentalizing the elements of an abstraction that constitute its structure and behavior ; encapsulation serves to separate the contractual interface of an abstraction and its implementation.
Encapsulation
* Hides the implementation details of a class.
* Forces the user to use an interface to access data
* Makes the code more maintainable.

Inheritance
Inheritance is the process by which one object acquires the properties of another object.
Polymorphism
Polymorphism is the existence of the classes or methods in different forms or single name denoting different
implementations.



Java is Distributed
With extensive set of routines to handle TCP/IP protocols like HTTP and FTP java can open and access the objects across net via URLs.
Java is Multithreaded
One of the powerful aspects of the Java language is that it allows multiple threads of execution to run concurrently within the same program A single Java program can have many different threads executing independently and continuously. Multiple Java applets can run on the browser at the same time sharing the CPU time.

Java is Secure
Java was designed to allow secure execution of code across network. To make Java secure many of the features of C and C++ were eliminated. Java does not use Pointers. Java programs cannot access arbitrary addresses in memory.
Garbage collection
Automatic garbage collection is another great feature of Java with which it prevents inadvertent corruption of memory. Similar to C++, Java has a new operator to allocate memory on the heap for a new object. But it does not use delete operator to free the memory as it is done in C++ to free the memory if the object is no longer needed. It is done automatically with garbage collector.
Java Applications
Java has evolved from a simple language providing interactive dynamic content for web pages to a predominant enterprise-enabled programming language suitable for developing significant and critical applications. Today, It is used for many types of applications including Web based applications, Financial applications, Gaming applications, embedded systems, Distributed enterprise applications, mobile applications, Image processors, desktop applications and many more. This site outlines the building blocks of java by stating few java examples along with some java tutorials.

Tuesday, September 20, 2011

Getting Started with Java

Getting Java Basics quickly has become easy with http://javapruthwirajk.blogspot.com/. This site is for the java programmers who want to use the Java programming language to create applications using the Java basics. This site is for absolute beginners to advanced java programmers who do not require any prerequisite Java knowledge. For getting started with Java you’ll have to have some basic understanding of the concepts of programming.
After going through all the tutorials in this site, you would have learnt the essential concepts and features of the Java Programming Language which include exceptions, Swing GUI programming, Collections framework etc. A lot of code examples are used throughout tutorial to make you understand the language better.
All the listings and programs in the website are compiled and run using the JDK 1.5.
Download:
JDKand JRE 1.5
Java Architecture
The Java environment is composed of a number of system components. You use these components at compile time to create the Java program and at run time to execute the program. Java achieves its independence by creating programs designed to run on the Java Virtual Machine rather than any specific computer system.

After you write a Java program, you use a compiler that reads the statements in the program and translates them into a machine independent format called bytecode.

Bytecode files, which are very compact, are easily transported through a distributed system like the Internet.

The compiled Java code (resulting byte code) will be executed at run time.

Java programs can be written and executed in two ways:

Stand-alone application (A Java Swing Application)

Applet which runs on a web browser (Example: Internet Explorer)

Java source code
A Java program is a collection of one or more java classes. A Java source file can contain more than one class definition and has a .java extension. Each class definition in a source file is compiled into a separate class file. The name of this compiled file is comprised of the name of the class with .class as an extension. Before we proceed further in this section, I would recommend you to go through the ‘Basic Language Elements’.
Below is a java sample code for the traditional Hello World program. Basically, the idea behind this Hello World program is to learn how to create a program, compile and run it. To create your java source code you can use any editor( Text pad/Edit plus are my favorites) or you can use an IDE like Eclipse.
public class HelloWorld {
        public static void main(String[] args) {
               System.out.println("Hello World");
        }//End of main
}//End of HelloWorld Class

Output
Hello World
ABOUT THE PROGRAM
I created a class named “HelloWorld” containing a simple main function within it. The keyword class specifies that we are defining a class. The name of a public class is spelled exactly as the name of the file (Case Sensitive). All java programs begin execution with the method named main(). main method that gets executed has the following signature : public static void main(String args[]).Declaring this method as public means that it is accessible from outside the class so that the JVM can find it when it looks for the program to start it. It is necessary that the method is declared with return type void (i.e. no arguments are returned from the method). The main method contains a String argument array that can contain the command line arguments. The brackets { and } mark the beginning and ending of the class. The program contains a line ‘System.out.println(”Hello World”);’ that tells the computer to print out on one line of text namely ‘Hello World’. The semi-colon ‘;’ ends the line of code. The double slashes ‘//’ are used for comments that can be used to describe what a source code is doing. Everything to the right of the slashes on the same line does not get compiled, as they are simply the comments in a program.
Java Main method Declarations
class MainExample1 {public static void main(String[] args) {}}
class MainExample2 {public static void main(String []args) {}}
class MainExample3 {public static void main(String args[]) {}}
All the 3 valid main methods shown above accepts a single String array argument.
Compiling and Running an Application
To compile and run the program you need the JDK distributed by Sun Microsystems. The JDK contains documentation, examples, installation instructions, class libraries and packages, and tools. Download an editor like Textpad/EditPlus to type your code. You must save your source code with a .java extension. The name of the file must be the name of the public class contained in the file.
Steps for saving, compiling and Running a Java
Step 1: Save the program With .java Extension.
Step 2: Compile the file from DOS prompt by typing javac <filename>.
Step 3: Successful Compilation, results in creation of .class containing byte code
Step 4: Execute the file by typing java <filename without extension>
Java Development Kit
The Java Developer’s Kit is distributed by Sun Microsystems. The JDK contains documentation, examples, installation instructions, class libraries and packages, and tools
javadoc
The javadoc tool provided by Sun is used to produce documentation for an application or program,
Jar Files
A jar file is used to group together related class files into a single file for more compact storage, distribution, and transmission.
PATH and CLASSPATH
The following are the general programming errors, which I think every beginning java programmer would come across. Here is a solution on how to solve the problems when running on a Microsoft Windows Machine.
1. ‘javac’ is not recognized as an internal or external command, operable program or batch file
When you get this error, you should conclude that your operating system cannot find the compiler (javac). To solve this error you need to set the PATH variable.
How to set the PATH Variable?
Firstly the PATH variable is set so that we can compile and execute programs from any directory without having to type the full path of the command. To set the PATH of jdk on your system (Windows XP), add the full path of the jdk<version>\bin directory to the PATH variable. Set the PATH as follows on a Windows machine:
a. Click Start > Right Click “My Computer” and click on “Properties”
b. Click Advanced > Environment Variables.
c. Add the location of bin folder of JDK installation for PATH in User Variables and System Variables. A typical value for PATH is:
C:\jdk<version>\bin (jdk<version is nothing but the name of the directory where jdk is installed)
If there are already some entries in the PATH variable then you must add a semicolon and then add the above value (Version being replaced with the version of JDK). The new path takes effect in each new command prompt window you open after setting the PATH variable.
2. Exception in thread “main” java.lang.NoClassDefFoundError: HelloWorld
If you receive this error, java cannot find your compiled byte code file, HelloWorld.class.If both your class files and source code are in the same working directory and if you try running your program from the current working directory than, your program must get executed without any problems as, java tries to find your .class file is your current directory. If your class files are present in some other directory other than that of the java files we must set the CLASSPATH pointing to the directory that contain your compiled class files.CLASSPATH can be set as follows on a Windows machine:
a. Click Start > Right Click “My Computer” and click on “Properties”
b. Click Advanced > Environment Variables.
Add the location of classes’ folder containing all your java classes in User Variables.
If there are already some entries in the CLASSPATH variable then you must add a semicolon and then add the new value. The new class path takes effect in each new command prompt window you open after setting the CLASSPATH variable.
Java 1.5
The Java 1.5 released in September 2004.
Goals
Less code complexity
Better readability
More compile-time type safety
Some new functionality (generics, scanner)
New Features
Enhanced for loop
Enumerated types
Autoboxing & unboxing
Generic types
Scanner
Variable number of arguments (varargs)
Static imports
Annotations

Friday, August 5, 2011

Programming Languages: File Handling in C Language

Programming Languages: File Handling in C Language: "What is a File? Abstractly, a file is a collection of bytes stored on a secondary storage device, which is generally a disk of some kind. T..."

Thursday, July 21, 2011

Basic of Java - Part 1



Basic of Java


Communication and Human Being
When we are communicating with somebody what exactly we need? The most important thing that we need is communication medium. There must be some common means of interaction. This leaded to birth of language. Initially the human communicated with each other with the help of sign language. When man came to know about the ability of different sounds they can produce they started talking in voice medium. Gradually he created the different languages that we hear today and this is how the journey of language started.
This communication was restricted to human communication but till 20th century human was very powerful in the terms of technology and he developed many electric and electronic devices and one of them was COMPUTER. This device was so powerful that it was able to manage 1000 times’ faster & efficient work than man. Computer was very much accurate than a man’s calculation.


Evolution of Computer Languages
But when this machine was invented it was very difficult to communicate with it. The language used at that time was very complex to use as well as to learn. This problem put man into finding new solution for it. The solution was a language which should be easy to understand and simple to learn. But at the same time most important aspect was it should be computer friendly i.e. machine also should interpret it quickly. In the beginning many languages were found to solve the issue but they were not much effective.
In 1972, Dennis Ritchie came with a language called C. This language changed the face of whole programming world. This language got the popularity in very less time and the reason was its user-friendly structure and fast execution. But soon the drawbacks of this language came forward and we needed a more powerful language. C was very handy in small programs but as code grows beyond some thousand lines compiler crashes. Also C was not very helpful in dealing with real world problems. This turned out in evolution of C++, a new object oriented language. This language also took the attentions of all the programmers. But soon there was a drastic change in the programming world.
By 1990’s inventors found that people have started using OSs other than Windows and they wanted a language which would be platform independent or platform neutral. Till this time nobody had thought of this kind of thing. And Sun Microsystems came up with this concept first in 1991 with Star OS. But at that time this concept was super flop as the people were much more addicted to what they had with them. In 1995, this problem was encountered and people found the thing very useful. And then Patrick Naughton, James Gosling, Ed Frank, Mike Sheridan and Chris Warth came up with the same concept with different name as “JAVA”. Java was initially known as “Oak” and later in 1995 it was renamed as Java.


Introduction to Programming
What is programming? In simple words Programming is nothing but finding solutions to given problems. Computer Programming is the art of making a computer do what you want it to do. Simply Program is a sequence or a set of instructions to the computer that will achieve your objective.
Programming is planning how to solve a problem. No matter what method is used -- pencil and paper, slide rule, adding machine, or computer -problem solving requires programming. Of course, how one programs, depends on the device one uses in problem solving.
Program can be written in any language that is preferred by the programmer. We have many programming languages to write programs like C, C++, JAVA, etc.

Introduction to Computer Science using Java
Java is a simple and yet powerful object oriented programming language and it is in many respects similar to C++. Java originated at Sun Microsystems, Inc. in 1991. It was conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. It was developed to provide a platform-independent programming language. This site gives you an Introduction to Java Programming accompanied with many java examples. It’s a complete course in java programming for beginners to advanced java.


Platform independent
Unlike many other programming languages including C and C++ when Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code. This byte code is distributed over the web and interpreted by virtual Machine (JVM) on whichever platform it is being run.


Java Virtual Machine
What is the Java Virtual Machine? What is its role?
Java was designed with a concept of ‘write once and run everywhere’. Java Virtual Machine plays the central role in this concept. The JVM is the environment in which Java programs execute. It is a software that is implemented on top of real hardware and operating system. When the source code (.java files) is compiled, it is translated into byte codes and then placed into (.class) files. The JVM executes these bytecodes. So Java byte codes can be thought of as the machine language of the JVM. A JVM can either interpret the bytecode one instruction at a time or the bytecode can be compiled further for the real microprocessor using what is called a just-in-time compiler. The JVM must be implemented on a particular platform before compiled programs can run on that platform.


Object Oriented Programming
Since Java is an object oriented programming language it has following features:
  • Reusability of Code
  • Emphasis on data rather than procedure
  • Data is hidden and cannot be accessed by external functions
  • Objects can communicate with each other through functions
  • New data and functions can be easily added
Java has powerful features. The following are some of them:-

Simple
Reusable
Portable (Platform Independent)
Distributed
Robust
Secure
High Performance
Dynamic
Threaded
Interpreted

Object Oriented Programming is a method of implementation in which programs are organized as cooperative collection of objects, each of which represents an instance of a class, and whose classes are all members of a hierarchy of classes united via inheritance relationships.


OOP Concepts
Four principles of Object Oriented Programming are
Abstraction
Encapsulation
Inheritance
Polymorphism



Abstraction
Abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of objects and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer.
EncapsulationEncapsulation is the process of compartmentalizing the elements of an abstraction that constitute its structure and behavior ; encapsulation serves to separate the contractual interface of an abstraction and its implementation.
Encapsulation
* Hides the implementation details of a class.
* Forces the user to use an interface to access data
* Makes the code more maintainable.

Inheritance
Inheritance is the process by which one object acquires the properties of another object.
Polymorphism
Polymorphism is the existence of the classes or methods in different forms or single name denoting different
implementations.



Java is Distributed
With extensive set of routines to handle TCP/IP protocols like HTTP and FTP java can open and access the objects across net via URLs.
Java is Multithreaded
One of the powerful aspects of the Java language is that it allows multiple threads of execution to run concurrently within the same program A single Java program can have many different threads executing independently and continuously. Multiple Java applets can run on the browser at the same time sharing the CPU time.

Java is Secure
Java was designed to allow secure execution of code across network. To make Java secure many of the features of C and C++ were eliminated. Java does not use Pointers. Java programs cannot access arbitrary addresses in memory.
Garbage collection
Automatic garbage collection is another great feature of Java with which it prevents inadvertent corruption of memory. Similar to C++, Java has a new operator to allocate memory on the heap for a new object. But it does not use delete operator to free the memory as it is done in C++ to free the memory if the object is no longer needed. It is done automatically with garbage collector.
Java Applications
Java has evolved from a simple language providing interactive dynamic content for web pages to a predominant enterprise-enabled programming language suitable for developing significant and critical applications. Today, It is used for many types of applications including Web based applications, Financial applications, Gaming applications, embedded systems, Distributed enterprise applications, mobile applications, Image processors, desktop applications and many more. This site outlines the building blocks of java by stating few java examples along with some java tutorials.