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

  • High Performance:
    • As we know in Java we have to first compile the program, then execute it using Java
    • 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.