Definition

  • Java is a simple, scalable (easy to integrate), object oriented (able to program real life complexities), class based, concurrent, general purpose, open source, high level 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.

Characteristics/Features

  • Simple Structure Java is considered as simple language because it is easy to learn, its syntax is quite simple, clean and easy to understand –
    • 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, storage classes, goto, and pointers.
    • Approximately half of the bugs in C and C++ programs are related to memory allocation and de-allocation process. But in java memory allocation and de-allocation is automatic.
    • In practice, java is both a simple as well as complex language depending on how you use it because Java has a wide range of applications, simple to complex.
  • Typed Language – Java is a strongly typed language i.e. they clearly distinguishes between the compile time errors (that must be detected at compile time) and run time error (that occur at run time).
  • Object Oriented –
    • In java, everything is considered as object which contains some data and its related methods/behaviors.Objects have two sections. The first is Data (instance variables) and the second is 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.
    • This object feature helps Java to extend easily as it is based on object model.
    • As we know that all the codes of the java program is written as a class and object form and follow & results oops output such as code re-usability, maintainability etc.
  • Compiled & Interpreted – Generally a computer programming language is either compiled or interpreted, but Java is both compiled as well as interpreted. 
    • A Java program/source code is first complied/translated using Java compiler (which is known as a name javac) and results a portable intermediate product/class file (not a machine code) actually called “Java byte code/Byte code”. Now this Java byte code is converted into final machine code by the Java Interpreter (which is known as a name java).Now, the Java interpreter executes the translated byte codes directly on the system that implements the Java Virtual Machine to run/execute the java program. This byte code is the actual power of Java to make it popular, platform independent and dominating over other programming languages.
    • For executing the java program successfully, first of all it is necessary to compile it to make error free and then it must be Interpreted to get related output.
  • Reliability & Robustness –
    • 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.
    • Java is robust(strong) language because it has strong memory management, automatic garbage collection, run time error/exception handling mechanism, the type checking mechanism etc. in Java. All these features make Java robust. In another words, Java eliminates pointer manipulation completely from the language, and therefore eliminates a large source of run time errors. Also, Java programmers
      need not remember to de-allocate memory in programs since there is an automatic garbage collection mechanism which handles de-allocation of memory. Thus, It provides a powerful and robust exception handling mechanism to deal with both expected and unexpected errors at run time.
  • Secure – As we know that Java is intended to be used in networking/distributing environments so it is necessary to implement several security mechanisms to protect against malicious code that might try to invade our file system.
    • The absence of pointers in Java makes it impossible for applications to gain access to memory locations without proper authorization as memory allocation and referencing model is completely opaque to the programmer and controlled entirely by the underlying run-time platform. 
    • Since java program is both compiled and interpreted hence these two steps of compilation and interpretation also allow extensive code checking and improved security.
    • Java program always runs in Java run time environment with almost null interaction with OS, hence it is more secure.
    • In Java, All the references to memory are symbolic references which is one of the security mechanism in java, i.e. 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 java applet program is loaded on its own memory space, which avoids the information interchange between applets and thus secure the information.
    • Each java applet program can be executed in its own run time environment that restricts them from viruses, deleting and modifying files in the host computer.
    • The Java enabled web browser checks the byte code of applets to ensure that it should not do anything wrong before it will run the applet.
    • Java is also a strongly typed language, which means that variables should be declared and variables should not change types. Also, type casting are strictly limited or highly sensible in java.
    • In Java, it is easier to write bug-free code then in other languages because unintended bugs are responsible for more data loss than viruses.
  • Platform Independent –
    • A platform, in computer science, is the combination of hardware or software environment both but practically software environment/operating system is considered mostly in which a program runs.
    • This means that the programs written on one platform can run on any other platform without having to rewrite or recompile them. In other words, it follows ‘WORA’ approach.
    • As we know that Java programs are compiled into byte-code format which does not depend on any machine architecture hence it can be easily translated into a specific machine by a Java Virtual Machine (JVM) for that machine. This is a significant advantage when developing applets or applications that are downloaded from the Internet and are needed to run on different systems.
    • A Java program can run on any computer system/environment (such as Linux, Windows, Mac etc.) for which a JVM (Java Virtual Machine) and some library routines have been installed.
  • Architecture Neutral – Here, architecture neutral means java produces bytecodes which are of independent of any platform/architecture i.e. it can be now easily translated into a machine code for specific platform/architecture whose JVM receives it.
  • Dynamic –
    • Java shows dynamic features which means that we can add more classes and plug in new methods to classes, creating required new classes as sub classes etc. This makes Java very easy to augment/dynamic as per our requirement.
    • Java was specially designed to adapt to an evolving environment, because of the Java compiler is smart and dynamic.
    • Java compiler 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 run time. For example, a web browser can load applets of other classes without recompilation.
    • When we are compiling a java file that depends on other non-compiled java files, then the compiler will try to find and compile them also.
    • The compiler can also handle methods that are used before they’re declared.
    • Java compiler can also determine whether a source code has been changed since the last time it was compiled.
  • Distributed –
    • java is considered as distributed programming language because the source code/program of java can be compiled onto one local machine and easily transferred(via internet) to another local or remote machine (either located near or far) where it can be executed successfully with no change because of bytecode presence.
    • To enable a Java applications to execute anywhere on the network, the compiler generates an architecture-neutral object file format/bytecode.
    • Due to distributed nature, the java program can be design to run in networking environments. For this, java have an extensive library of classes for communicating ,using TCP/IP protocols such as HTTP and FTP mainly. This makes creating network connections much easier. Thus, we can read and write objects on the remote sites via URL with the same ease that programmers are used to when read and write data from and to a file. This helps the programmers at remote locations to work together on the same project. 
    • Java’s advanced feature RMI and EJB are used for creating distributed applications.
  • Web & Network – Java is able to create network and web applications much more.
    • J2EE is used for developing network based applications.
    • Applet is used for developing client side web applications mainly.
    • JSP is used for developing server side web applications mainly.
  • Multithreading – 
    • Since Java platform is designed with multithreading capabilities, built into the language as well. Thus we can build java applications with many concurrent threads of activity, resulting in highly interactive and responsive applications.Thus, java handles more than one job at a time, so get more process done in less time than it could with just one thread.
    • By using threads, an individual program is capable to do more than one thing at the same time efficiently.
    • Java is inherently multi-threaded i.e. 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.
    • To making easy the use of threads Java offers features for synchronization between threads.
  • High Performance – 
    • Java source code is first compiled into bytecode which is highly optimized by the Java compiler, so that the Java virtual machine (JVM)/Interpreter can execute Java applications at full speed/efficiency hence gives an efficient output.
    • The advance version of JVM uses the adaptive and Just in time (JIT) compilation technique that improves performance by converting Java bytecodes to native machine instructions on the fly. 
    • 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.
  • Portable – Java program is a write once, run anywhere (WORA) programming language i.e. java uses WORA concept which makes it portable.The portability actually comes from architecture-neutrality concept.
  • Some other features of Java are – 
    • Java is strictly case sensitive programming language.
    • They are considered as pure object oriented programming language because of all the codes in java are included inside the class, even main method.

Java Virtual Machine(JVM)

  • A Java virtual machine is a virtual/abstract machine that enables a computer to run compiled Java programs or java bytecodes as well as programs written in other languages that are also compiled to Java bytecode.
  • When a Java program is compiled it is converted into intermediate byte code which is then executed by the Java interpreter by translating the byte code into machine instructions.
  • Program in Java run time environment, which is used to interpret byte code, is called Java Virtual Machine (JVM). The Java compiler reads Java language source files, translates the source into Java byte codes, and places the byte codes into class files.
  • Java interpreter is part of Java runtime environment. Byte code is an intermediate code independent of any machine and any operating system.
  • They are designed by Sun MicroSystems and introduced in 1994.
  • JVM is an abstract computing model i.e.like a real machine, it has its own instruction set, execution engine, and serves to manipulate memory areas at run time.
  • The JVM is called virtual because it is generally implemented in software, on top of a real hardware platform and operating system.
  • The objective of JVM is to provide an execution environment for applications built to run on it.
  • The JVM has two primary functions – to allow Java programs to run on any device or operating system, and to manage and optimize program memory.
  • Sun JVM is written in C whereas Oracle JVM is written in C++.
  • JDK is for development purpose whereas JRE(Java Runtime Environment) is for running the java programs. JDK and JRE both contains JVM so that we can run our java program. The JRE is the environment within which the virtual machine runs. JRE is the container, JVM is the content. Java Runtime Environment contains JVM, class libraries, and other supporting files. It does not contain any development tools such as compiler, debugger, etc.
  • JVM is the heart of java programming language that provides platform independence.
  •  JVM is a platform-independent execution environment that converts Java bytecode into machine language and executes it.
  • JVM is the main component of Java architecture, and it is the part of the JRE (Java Runtime Environment). A program of JVM is written in C Programming Language, and JVM is Operating System dependent. JVM is responsible for allocating the necessary memory needed by the Java program.
  • 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.
  • Any machine for which Java interpreter is available can execute the compiled byte code. That’s why Java is called Machine independent and Architecture neutral. 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.

Simple Java Program Example

Create and Run Java Program – 
  • STEP 1 – First of all write Java Program in any simple Text Editor Programs(such as Notepad, dos editor, Brief, vi editor of linux or Unix etc.)  as below –
           Line 1:           /* Sample of Simple Java Application Program */      
      
           Line 2 :          import java.io.*;   
           Line 3 :          class Example
                                    {
           Line 4 :                public static void main (String args[ ] )
                                         {
           Line 5 :                      System.out.println(“Simple Java Application Program Examples”);
                                          } 
                                      }
  • STEP 2 – Save the created java program in a particular computer drive (say c drive) with certain name having extension .java such as program1.java. 
  • STEP 3 – Now, compile the created java program(program1.java) with java compiler(javac) by opening dos prompt(run – cmd) –              c:\>javac program1.java (Press Enter button)
  • STEP 4 – Remove the errors in the program, if any, and finally save the program.
  • STEP 5 – Now, Interpret the compiled class/bytecode file(as class name, here it is Example.class) with java Interpreter(java) as –  c:\>java Example  (Press Enter button)
  • STEP 6 – Result displayed on the DOS screen.

NB 1 : – Java program can also be written and run using Java IDE applications(such as netbeans, eclipse, bluej etc)without involving DOS.

NB 2 : – We can not use heavy text editor to write java program because this editor such as word processor like Microsoft Word save their files in a proprietary format and not in pure ASCII text.

NB 3 : – Before compiling a java program in a computer system we must first ensure that our Java environment is correctly configured. To set this PATH and CLASSPATH is properly set.

Explanation about Java Program – 

Line 1 : Comments Line – In java, two types of comment line is applicable called Sinlge line(//) and Multiple line( /* – – –  */ ). Here, comment line is used to describe the details of the program which is useful for developing the readability/understandability of the program. This comment line is ignored by the compiler .

Line 2 : Required packages are included to provide useful system defined methods of a particular class, applied in the codes using import keyword.

Line 3 : To define a java class(here is Example) using a keyword class which includes everything inside it.

Line 4 : Definition of Java Main() methods as public static void main (String args[ ] ). This is the starting point from where the java program started for execution calling main() method.Here each keyword has unique meaning – 

public – This keyword makes the main() method public so that main method can be accessed/invoked from outside (of that class) by other class codes when program is executed.
static This keyword allows the main () method to be executed without creating an object
of that class.
void – This keyword means main () method does not return any value after processing.
         Braces { } – Defines blocks of code.
         Square bracket [ ] – Declares array/string types.
Line 5 : This line is used to print the string inside it as output“Simple Java Application Program Examples”. In this line, println() method is used to display the output on the computer screen/console. The println () function accepts any string and display its value on console. The java in built method println () is available in io package under System class with the help of out object.
Create and Run Applet Program –
  • To create an applet program, 3 steps can be followed –
    1. Create an applet program file in a simple text editor program say notepad
    import Java.applet.Applet;
    import Java.awt.Graphics;

    public class Program1 extends Applet
    {
           public void paint (Graphics g)
            {
                  g.drawString(“Hello India!”, 150, 200);
             }
     } 

    2. Compile the source/program file as java application program

    Now the ready Java applet program is compiled using Java compiler named ‘Javac’.
    To compile this we have –
    C:\> Javac Program1.Java
    After successful compilation, Program1.class file is created as an output of compilation.

    3. Run the applet program 

    To execute the applet program finally, we now create a HTML file with .html extension again which actually includes the given applet class file Program1.class, and then run this html file either using java enabled web browser(such as Internet Explorer, Netscape Navigator etc) or applet viewer, a java plug-in tool. The html file is –

    <HTML>
          <HEAD>
               <TITLE> A Simple Applet </TITLE>
          </HEAD>
          <BODY>
                 The output of the applet program is
                 <APPLET CODE=” Program1.class” WIDTH=150 HEIGHT=50></APPLET>
           </BODY>
    </HTML>

    Simply run this created html file(say Program2.html) by double click on it that opens in web browser and display the output.

    NB : Both the files of applet program(say here Program1.java and Program2.html) must be kept in the same directory or folder.

    Explanation of Applet Program – 
    • Here, in the code java.applet.Applet package is required to create an applet program and java.awt.Graphics package is required to display/paint the output of the applet program on the screen.
    • Next, Program1 is the executable class which is public in nature and it extends from applet class which means that a programmer builds the applet code on the standard Applet class.
    • In the Program1 applet class we have defined paint method inside which we are using
      object of Graphics class. Graphics class is available in Java.awt package.
    • The drawString method is used to write/display “Hello India!” message on the screen. It is a method of Graphic class. This drawString method takes three arguments. First argument is a message and the other two are pixel positions or location on X-axis and Y-axis respectively, from where the printing/output of the string on the screen will begin.

    For more details about applet theory & programs, click this link.

       718 total views,  1 views today

      Categories: Java

      0 Comments

      Leave a Reply

      Your email address will not be published. Required fields are marked *

      This site uses Akismet to reduce spam. Learn how your comment data is processed.