Monday 6 July 2015

Java Sample Program

In this post, I will tell you about how to work with Java.

Getting Started - First Program of Java

You should have already installed Java Development Kit (JDK). Otherwise download it and install JDK to your system. 

Let's start by writing our first Console application that prints a message "Hello World".

  • Write the Source Code: Enter the following source codes using a programming text editor (such as Notepad or NotePad++ for Windows; jEdit or gedit for Mac OS X; gedit for Ubuntu); or an Interactive Development Environment (IDE) (such as Eclipse or NetBeans).
Source Code :

class PrintMessage
{
                     public static void main(String args[])
                     {
                                      System.out.println("Hello World!!!");
                      }
}
  • Compile the Source Code: Compile the source code "Hello.java" into portable bytecode "Hello.class" using JDK Compiler "javac". Start a CMD Shell (Windows) or Terminal (UNIX/Linux/Mac OS X) and issue this command:
shell > javac PrintMessage.java
There is no need to explicitly compile the source code under IDEs (such as Eclipse or NetBeans), as they perform incremental compilation implicitly.
  • Run the Program: Run the program using Java Runtime "java", by issuing this command:
shell > java PrintMessage
Hello World!!!
 

Monday 29 June 2015

Java Virtual Machine Concepts

When a Java program is compiled it is converted to byte code which is then executed by the Java interpreter by translating the byte code into machine instructions.

Java interpreter is part of Java runtime environment. Byte code is an intermediate code independent of any machine and any operating system. Program in Java run time environment, which is used to interpret byte code, is called Java Virtual Machine (JVM). A Java Virtual Machine is an abstract computing machine. The Java compiler reads Java language source files, translates the source into Java byte codes, and places the byte codes into class files.

Any machine for which Java interpreter is available can execute this byte code. That’s why Java is called Machine independent and Architecture neutral. Figure shows that Java compiler is accepting a Java program and producing its byte code. This byte code can be executed on any operating system (Window-98, Macintosh, Linux etc.) running on any machine with suitable Java interpreter of that machine.

 
The JVM plays the main role to making Java portable. It provides a layer of abstraction between the compiled Java program and the hardware platform and operating system. The JVM is central to Java’s portability because compiled Java programs run on the JVM, independent of whatever hardware is used.

Wednesday 17 June 2015

Introduction To Java

There is no big history of Java, but there are some interesting things behind the scene.
Java was developed by a team of computer professionals under the guidance of James Gosling at Sun Microsystems in 1991. They wanted to give a suitable name. Initially they called it “oak” after seeing an oak tree from their window, but after some weeks they were discussing another name and were sipping Java coffee, so one of them suggested the name “Java”. Other than this there is no interesting story/reason about
its name Java.
The only reason I can say is that its designers wanted to give a beautiful name to their beautiful language, just as all parents want to give a sweet name to their sweet child.
Java is a simple, scalable, object oriented, general purpose programming language with powerful features, which can be used to develop a variety of applications from simple web animations to high-end business applications that program hand-held devices, microwave appliances, cross platform server applications, etc.
Java is a strongly typed language. This specification clearly distinguishes between the compile time errors that must be detected at compile time and those that occur at run time.

Generally, a language is either compiled or interpreted, but java is both compiled as well as interpreted. First a Java program is complied by JIT (Just - in - Time) compiler and comes in the form of “Java byte code” (which is an intermediate code). Then this Java byte code is run on interpreter to execute a program. This byte code is the actual power of Java to make it popular and dominating over other programming languages.

Basic Features

  • Platform Independent: 
    • Java is Platform independent.
    • Java is compiled to an intermediate form called Java byte-code or simply byte code.
    • A Java program never really executes immediately after compilation on the host machine.
    • Rather, this special program called the Java interpreter or Java Virtual Machine reads the byte code, translates it into the corresponding host machine instructions and then executes the machine instruction.
    • A Java program can run on any computer system for which a JVM (Java Virtual Machine) and some library routines have been installed.
    • The second important part which makes Java portable is the elimination of hardware architecture dependent constructs.
  • Object Oriented:
    • As we know, object represents data (instance variable) and methods.
    • Data represents what an object is. A method represents what an object does.
    • The Data and methods are closely related to the real world structure and behavior of objects. Object oriented programming has a number of advantages like. Simpler to read program,
      efficient reuse of programming segments, robust and error-free code.
    • Java is a true object-oriented language, which provides a platform to develop an effective and efficient application and program real life complexities.
    • Java does not allow methods without class, thus an application consists of only the object which
      makes it true OOP.
    • Most of the Object-oriented concepts in Java are inherited from C++ which makes it easy for traditional programmers to understand it.
  • Easy to Learn:
    • Java is easy to learn for programmers because it is (syntax) similar to C and C++ and most of the complex parts of C/C++ have been excluded including operator overloading, multiple inheritance and pointers. Approximately half of the bugs in C and C++ programs are related to memory allocation and de-allocation.
    • Java automatically allocate and de - allocate memory as per the need of object or instance variable.
    • But don't think Java is very easy, it is both simple as well as complex depending on how you use it. because Java has a wide range of application from simple to complex.
  • Robust:
    • Java provides checking for possible problems at two levels, one at the compile time and the other at the run time, so programs are highly reliable and eliminate situations that are error-prone compared to C/C++.
  • Secure:
    • Java is intended to work in networked and distributed environments by providing security.
    • All the references to memory are symbolic references, meaning that the user is not aware where in the memory program is present, it totally depends on the JVM and machine on which the program is running.
    • Each applet is loaded on its own memory space, which avoids the information interchange between applets. 
    • Java applets can be executed in run time environment that restricts them from introducing viruses, deleting and modifying files in the host computer.
    • In Java it is easier to write bug-free code then in other languages. 
  •  Multi - Threaded:
    • Before answering what is multithreading, let me explain you what ‘thread’ is.
    • Simply, a thread is a program’s path of execution.
    • In your problems, when multiple events or actions need to occur at the same time, how you will handle it?
    • The best solution to this problem is the execution of two or more sections of a program at the same time and this technique is known as multithreading.
    • Multithreaded applications deliver their potent power by running many threads
      concurrently within a single program.
    • Java is inherently multi-threaded, for example garbage collection subsystem runs as a
      low-priority thread.
    • A single Java program can have many different threads executing independently and continuously, for example, different Java applets on the same web page can run together with getting equal time from the processor.
    • Because multithreaded applications share data and all threads of an application exists in the
      same data space therefore to maintaining reliability is sometime difficult. To making easy the use of threads Java offers features for synchronization between threads.

  • Dynamic:
    • Java was designed to adapt to an evolving environment, therefore the Java compiler is
      smart and dynamic.
    • If you are compiling a file that depends on other non-compiled files, then the compiler will try to find and compile them also.
    • The compiler can handle methods that are used before they’re declared.
    • It can also determine whether a source code has been changed since the last time it was compiled.
    • In Java classes that were unknown to a program when it was compiled can still be loaded into it at runtime. For example, a web browser can load applets of other classes without
      recompilation.

  • High Performance:
    • As we know in Java we have to first compile the program, then execute it using Java
      interpreter.
    • In general, interpreters are slow, because an interpreter executes programs instruction by instruction while Java is a fast-interpreted language.
    • Java has also been designed so that the run-time system can optimize their performance by compiling bytecode to native machine code on the fly (execute immediately after compilation).
    • This is called “Just In Time” (JIT) compilation.
    • According to ‘Sun’ with JIT compilation, Java code can execute nearly as fast as
      native compiled code and maintain its transportability and security.
    • Java offers two flavors of programming, Java applets and Java application. Applets
      are small Java programs (mostly) that can be downloaded over a computer network
      and run from a web page by using a Java enabled browser like Netscape / Microsoft
      Internet Explorer.
    • Applets used to add dynamic features like animation, sound etc. to web pages.

Saturday 22 November 2014

Main Components of Object Oriented Technology

When we learn the Object Oriented Programming using Java, we have the knowledge of main components:

  • Objects and Classes
  • Data Abstraction and Encapsulation
  • Inheritance
  • Polymorphism
These are the components of Object Oriented Technology. Let's understand a detailed description of the above component.

Objects

Let's start with an Object. The first thing that we should do in the Object Oriented approach is to start thinking in terms of Objects because in Object Oriented programming object is center point or we can say that the heart of OOP.

Definition: An object is a software bundle of variables and related methods.

Look around right now and you will find many real - world objects such as, your laptop, any dog, your television, your scooter bike, your mobile etc.
Real - world objects share two characteristics: they all have State and Behavior. so, we can think like,
My Dog (My Object) Dog have state (name, color, hungry) and behavior (barking, fetching, wagging tail).

Software Objects are conceptually similar to real world objects. they also consist of state and behavior. An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (function in some programming language).

Class

In the real world, you often have many objects of the same kind. For example, your dog is just one of many dogs in the world.  In object-oriented terminology, we say that your dog object is an instance of the class known as animal. Dogs have some state and behavior in common.

In object-oriented software, it is also possible to have many objects of the same kind that share characteristics.

Definition: A class is a blueprint, or prototype, that defines the variables and the methods common to all objects of a certain kind.


Data abstraction and encapsulation

The wrapping up of data and functions into a single unit is known as encapsulation. This is one of the strong features of the object oriented approach. The data is not directly accessible to the outside world and only the functions, which are wrapped in the class, can access it. Functions are accessible to the outside world. These functions provide the interface to access data. If one wants to modify the data of an object, s/he should know exactly what functions are available to interact with it. This insulation of the data from direct access by the program is known as data hiding.

Abstraction refers to the act of representing essential features without including the background details to distinguish objects/ functions from other objects/functions. In case of structured programming, functional abstraction was provided by telling, which task is performed by function and hiding how that task is performed. A step further, in the Object Oriented approach, classes use the concept of data abstraction. With data abstraction, data structures can be used without having to be concerned about the exact details of implementation. As in case of built-in data types like integer, floating point, etc. The programmer only knows about the various operations which can be performed on these data types, but how these operations are carried out by the hardware or software is hidden from the programmer. Similarly in Object Oriented approach, classes act as abstract data types. Classes are defined as a set of attributes and functions to operate on these attributes. They encapsulate all the essential properties of the objects that are to be created.

Inheritance

Inheritance is the process by which objects of one class acquire the properties of objects of another class in the hierarchy. For example, the scooter is a type of the class two-wheelers, which is again a type of (or kind of) the class motor vehicles.

Definitions: A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or parent class).
Types of Inheritance

Polymorphism

Polymorphism means the ability to take more than one form of the same property. The dictionary definition of polymorphism refers to a principle in biology in which an organism or species can have many different forms or stages. This principle can also be applied to object - oriented programming and languages like the Java language. Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.


Polymorphism

Wednesday 15 October 2014

Concept OOP

Hey Guys,
You are familiar with Structured Programming Language.....
Now, We have new concept that is Object Oriented Programing Language i.e., OOP.

OOP is a Programming Paradigm that represent concept of 'Objects' that have 'Data Fields' (attributes that describe the object) and associated procedures known as Methods.

Now you have a Question i.e. , What is Paradigm?
The term paradigm describes a set of techniques, methods, theories and standards that together represent a way of thinking for problem solving. According to [Wegner, 1988], paradigms are “patterns of thought for problem solving”.

Language paradigms were associated with classes of languages. First the paradigms are defined. Thereafter, programming languages according to the different paradigms are classified. The language paradigms are divided into two parts, imperative and declarative paradigms as shown in the Figure.

  • Imperative languages: can be further classified into procedural and object oriented approach.
  • Declarative languages: can be classified into functional languages and logical languages.

Imperative paradigm: The meaning of imperative is “expressing a command or order”, so the programming languages in this category specify the step-by-step explanation of command.

Procedural paradigm: Procedural languages are statement oriented with the variables holding values. In this language the execution of a program is modeled as a series of states of variable locations.

Object Oriented paradigm: The Object Oriented paradigm is centered on the concept of the object. Everything is focused on objects.

Declarative paradigm: In this paradigm programs declare or specify what is to be computed without specifying how it is to be achieved. Declarative programming is also known as Value-oriented programming.

Functional paradigm: In this paradigm, a program consists of a collection of functions. A function just computes and returns a value.

Logic paradigm: In this paradigm programs only explain what is to be computed not how to compute it. Here program is represented by a set of relationships, between objects or property of objects known as predicate which are held to be true, and a set of logic/clauses (i.e. if A is true, then B is true). Basically logic paradigm integrates data and control structures.


Object Oriented Programming

Object Oriented Programming is a real world programming.

The world and its applications are not organized as functions and values separate from one another. The problem solvers do not think about the world in this manner. They always deal with their problems by concentrating on the objects, their characteristics and behavior.

The world is Object Oriented, and Object Oriented programming expresses programs in the ways that model how people perceive the world. there are many real world objects around us which we often used to performing different functions. such as, our compass box, fans etc. through this real world example we can understand that problem solving using OO approach  is very close to our real life problem solving techniques.

The major benefit of using OO approach is that the program is organized around the data being operated upon rather than the operations performed. The basic idea behind OOP is to combine both, data and its functions that operate on the data into a single unit called object.