Programming in Java Learning Objectives
Total Page:16
File Type:pdf, Size:1020Kb
Programming in Java Learning objectives After undergoing the subject, students will be able to Understand the history of Java. Explain how Java programs are compiled, interpreted and executed in java environment. Explain how Java is used to develop different types of application Explain the concept of Packages and their uses Explain the concept of multithreading. Explain how we can build application with JDK. 1. History of Java Java Programming Language was written by James Gosling along with two other person ‘Mike Sheridan‘ and ‘Patrick Naughton‘, while they were working at Sun Microsystems. Initially it was named oak Programming Language. 1.1.Releases of Java Initial Java Versions 1.0 and 1.1 was released in the year 1996 for Linux, Solaris, Mac and Windows. Java version 1.2 (Commonly called as java 2) was released in the year 1998. Java Version 1.3 codename Kestrel was released in the year 2000. Java Version 1.4 codename Merlin was released in the year 2002. Java Version 1.5/Java SE 5 codename ‘Tiger’ was released in the year 2004. Java Version 1.6/Java SE 6 Codename ‘Mustang’ was released in the year 2006. Java Version 1.7/Java SE 7 Codename ‘Dolphin’ was released in the year 2011. Java Version 1.8 is the current stable release which was released this year (2015). 1.2. Five Goals which were taken into consideration while developing Java: Keep it simple, familiar and object oriented. Keep it Robust and Secure. Keep it architecture-neural and portable. Executable with High Performance. Interpreted, threaded and dynamic. 2. Java virtual Machine JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime environment in which java bytecode can be executed. JVMs are available for many hardware and software platforms (i.e. JVM is platform dependent) 2.1. What is JVM It is: A specification where working of Java Virtual Machine is specified. But implementation provider is independent to choose the algorithm. Its implementation has been provided by Oracle and other companies. An implementation Its implementation is known as JRE (Java Runtime Environment). Runtime Instance Whenever you write java command on the command prompt to run the java class, an instance of JVM is created. 2.2. What it does The JVM performs following operation: Loads code Verifies code Executes code Provides runtime environment JVM provides definitions for the: Memory area Class file format Register set Garbage-collected heap Fatal error reporting etc. 3. JIT Compiler The key of java power "Write once, run everywhere" is bytecode. The way bytecodes get converted to the appropriate native instructions for an application has a huge impact on the speed of an application. These bytecode can be interpreted, compiled to native code or directly executed on a processor whose Instruction Set Architecture is the bytecode specification. Interpreting the bytecode which is the standard implementation of the Java Virtual Machine (JVM) makes execution of programs slow. To improve performance, JIT compilers interact with the JVM at run time and compile appropriate bytecode sequences into native machine code. When using a JIT compiler, the hardware can execute the native code, as opposed to having the JVM interpret the same sequence of bytecode repeatedly and incurring the penalty of a relatively lengthy translation process. This can lead to performance gains in the execution speed, unless methods are executed less frequently. The time that a JIT compiler takes to compile the bytecode is added to the overall execution time, and could lead to a higher execution time than an interpreter for executing the bytecode if the methods that are compiled by the JIT are not invoked frequently. The JIT compiler performs certain optimizations when compiling the bytecode to native code. Since the JIT compiler translates a series of bytecode into native instructions, it can perform some simple optimizations. Some of the common optimizations performed by JIT compilers are data-analysis, translation from stack operations to register operations, reduction of memory accesses by register allocation, elimination of common sub-expressions etc. The higher the degree of optimization done by a JIT compiler, the more time it spends in the execution stage. Therefore a JIT compiler cannot afford to do all the optimizations that is done by a static compiler, both because of the overhead added to the execution time and because it has only a restricted view of the program. 3.1. How it works The Just-In-Time (JIT) compiler is a component of the Java Runtime Environment that improves the performance of Java applications at run time. Java programs consists of classes, which contain platform neutral bytecode that can be interpreted by a JVM on many different computer architectures. At run time, the JVM loads the class files, determines the semantics of each individual bytecode, and performs the appropriate computation. The additional processor and memory usage during interpretation means that a Java application performs more slowly than a native application. The JIT compiler helps improve the performance of Java programs by compiling bytecode into native machine code at run time. The JIT compiler is enabled by default, and is activated when a Java method is called. The JIT compiler compiles the bytecode of that method into native machine code, compiling it "just in time" to run. When a method has been compiled, the JVM calls the compiled code of that method directly instead of interpreting it. Theoretically, if compilation did not require processor time and memory usage, compiling every method could allow the speed of the Java program to approach that of a native application. JIT compilation does require processor time and memory usage. When the JVM first starts up, thousands of methods are called. Compiling all of these methods can significantly affect startup time, even if the program eventually achieves very good peak performance. 4. Java Features 4.1. General Purpose Java capabilities are not limited to any specific application domain rather it can be used in various application domain and hence it is called General Purpose Programming Language. 4.2. Class based Java is a class based/oriented programming language which means Java supports inheritance feature of object-oriented Programming Language. 4.3. Object oriented Java is object-oriented means software developed in Java are combination of different types of object. 4.5. Platform Independent A Java code will run on any JVM (Java Virtual Machine). Literally you can run same Java code on Windows JVM, Linux JVM, Mac JVM or any other JVM practically and get same result every time. 4.6. Architectural neutral A Java code is not dependent upon Processor Architecture. A Java Application compiled on 64 bit architecture of any platform will run on 32 bit (or any other architecture) system without any issue. 4.7. Multithreaded A thread in Java refers to an independent program. Java supports multithread which means Java is capable of running many tasks simultaneously, sharing the same memory. 4.8. .Dynamic Java is a Dynamic programming language which means it executes many programming behavior at Runtime and don’t need to be passed at compile time as in the case of static programming. 4.9. Distributed Java Supports distributed System which means we can access files over Internet just by calling the methods. 4.10. Portable A Java program when compiled produce bytecodes. Bytecodes are magic. These bytecodes can be transferred via network and can be executed by any JVM, hence came the concept of ‘Write once, Run Anywhere(WORA)’. 4.11. Robust Java is a robust programming Language which means it can cope with error while the program is executing as well as keep operating with abnormalities to certain extent. Automatic Garbage collection, strong memory management, exception handling and type checking further adds to the list. 4.12. Interpreted Java is a compiled programming Language which compiles the Java program into Java byte codes. This JVM is then interpreted to run the program. 4.13. Security Unlike other programming Language where Program interacts with OS using User runtime environment of OS, Java provides an extra layer of security by putting JVM between Program and OS. 4.14. Simple Syntax Java is an improved c++ which ensures friendly syntax but with removed unwanted features and inclusion of Automatic Garbage collection. 4.15. High Level Programming Language Java is a High Level Programming Language the syntax of which is human readable. Java lets programmer to concentrate on what to achieve and not how to achieve. The JVM converts a Java Program to Machine understandable language. 4.16. High Performance Java make use of Just-In-Time compiler for high performance. Just-In-Time compiler is a computer program that turns Java byte codes into instructions that can directly be sent to compilers. 5. Native Code Native code is computer programming (code) that is compiled to run with a particular processor (such as an Intel x86-class processor) and its set of instructions. If the same program is run on a computer with a different processor, software can be provided so that the computer emlates the original processor. In this case, the original program runs in "emulation mode" on the new processor and almost certainly more slowly than in native mode on the original processor. (The program can be rewritten and recompiled so that it runs on the new processor in native mode.) Native code can also be distinguished from bytecode (sometimes called interpreted code), a form of code that can be said to run in a virtual machine (for example, the Java Virtual Machine). The virtual machine is a program that converts the platform-generalized bytecode into the native code that will run in a specific processor.