Java 2018 - Basic Theory

JAVA
Java is a programming Language and a platform.
Platform: Any environment in which a program runs is called a platform. Since Java has its own JRE and API, it is also a platform.
The latest version is Java 9, released on September 21, 2017.

Versions

Major release versions of Java, along with their release dates:
  • JDK 1.0 (January 23, 1996)
  • JDK 1.1 (February 19, 1997)
  • J2SE 1.2 (December 8, 1998)
  • J2SE 1.3 (May 8, 2000)
  • J2SE 1.4 (February 6, 2002)
  • J2SE 5.0 (September 30, 2004)
  • Java SE 6 (December 11, 2006)
  • Java SE 7 (July 28, 2011)
  • Java SE 8 (March 18, 2014)
  • Java SE 9 (September 21, 2017)

Oracle Corporation is the current owner of the official implementation of the Java SE platform, following their acquisition of Sun Microsystems on January 27, 2010. This implementation is based on the original implementation of Java by Sun.

Java is used in Desktop application, web application, Mobile application, embedded system, robotics, games, enterprise application, etc.

There are many 4 types of application that can be created using Java:
1)Standalone, 2)Web, 3)Enterprise, 4)Mobile

There are 4 Java platforms/editions:
1) Java SE, 2) Java EE, 3) Java ME, 4)JavaFx

Java SE  includes Java programming APIs such as java.lang, java.io, java.net, java.util, java.sql, java.math etc. It includes core topics like OOPs, String, Regex, Exception, Inner classes, Multithreading, I/O Stream, Networking, AWT, Swing, Reflection, Collection etc.

Java EE is an enterprise platform which is mainly used to develop web and enterprise applications. It is built on the top of Java SE platform. It includes topics like Servlet, JSP, Web Services, EJB, JPA etc.

Java ME is used for mobile application and JavaFx is used for light-weight user interface API.
----------------------------------------------------------------------------------------------------------
Difference between JDK, JRE and JVM:
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. JVM, JRE and JDK are platform dependent because configuration of each OS differs. But, Java is platform independent.
The JVM performs following main tasks:
  • Loads code
  • Verifies code
  • Executes code
  • Provides runtime environment
JRE is an acronym for Java Runtime Environment.It is used to provide runtime environment.It is the implementation of JVM. It physically exists. It contains set of libraries + other files that JVM uses at runtime. Implementation of JVMs are also actively released by other companies besides Sun Micro Systems.

JDK is an acronym for Java Development Kit.It physically exists.It contains JRE + development tools.
jdk

java -version // this will check your jre version
javac -version // this will check your java compiler version if you installed 

The JDK has as its primary components a collection of programming tools, including:
  • appletviewer – this tool can be used to run and debug Java applets without a web browser
  • apt – the annotation-processing tool
  • extcheck – a utility that detects JAR file conflicts
  • java – the loader for Java applications. This tool is an interpreter and can interpret the class files generated by the javac compiler. Now a single launcher is used for both development and deployment. The old deployment launcher, jre, no longer comes with Sun JDK, and instead it has been replaced by this new java loader.
  • javac – the Java compiler, which converts source code into Java bytecode
  • javadoc – the documentation generator, which automatically generates documentation from source code comments
  • jar – the archiver, which packages related class libraries into a single JAR file. This tool also helps manage JAR files.
  • JConsole – Java Monitoring and Management Console
  • jdb – the debugger
  • jrunscript – Java command-line script shell..
  • VisualVM – visual tool integrating several command-line JDK tools and lightweight performance and memory profiling capabilities
  • xjc – Part of the Java API for XML Binding (JAXB) API. It accepts an XML schema and generates Java classes.
  • As well as many more check wikipedia.
Experimental tools may not be available in future versions of the JDK.
The JDK also comes with a complete Java Runtime Environment, usually called a private runtime, due
 to the fact that it is separated from the "regular" JRE and has extra contents. It consists of a Java Virtual Machine and all of the class libraries present in the production environment, as well as additional libraries only useful to developers, such as the internationalization libraries and the IDL libraries.
Copies of the JDK also include a wide selection of example programs demonstrating the use of almost all portions of the Java API.

Ambiguity between a JDK and an SDK

The JDK forms an extended subset of a software development kit (SDK). It includes "tools for developing, debugging, and monitoring Java applications".Oracle strongly suggests to now use the term JDK to refer to the Java SE Development Kit. The Java EE SDK is available with or without the JDK, by which they specifically mean the Java SE 7 JDK. 

I need to know where JDK is located on my machine.
On running Java -version in cmd, it shows the version as '1.8.xx'. To find the location of this SDK on my machine I tried using echo %JAVA_HOME% but it is only showing 'JAVA_HOME' (as there is no 'JAVA_PATH' var set in my environment variables).
If you are using Windows:
c:\> for %i in (java.exe) do @echo.   %~$PATH:i 
This gives: C:\ProgramData\Oracle\Java\javapath\java.exe
JVM provides definitions for the:
  • Memory area
  • Class file format
  • Register set
  • Garbage-collected heap
  • Fatal error reporting etc.
Jvm Internal

1) Classloader

Classloader is a subsystem of JVM that is used to load class files.

2) Class(Method) Area

Class(Method) Area stores per-class structures such as the runtime constant pool,
field and method data, the code for methods.

3) Heap

It is the runtime data area in which objects are allocated.

4) Stack

Java Stack stores frames.It holds local variables and partial results, and plays a part in
 method invocation and return.
Each thread has a private JVM stack, created at the same time as thread.
A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes.

5) Program Counter Register

PC (program counter) register. It contains the address of the Java virtual machine instruction currently being executed.

6) Native Method Stack

It contains all the native methods used in the application.

7) Execution Engine

It contains:
1) A virtual processor
2) Interpreter: Read bytecode stream then execute the instructions.
3) Just-In-Time(JIT) compiler: It is used to improve the performance.JIT compiles parts
 of the byte code that have similar functionality at the same time, and hence reduces the
amount of time needed for compilation.Here the term ?compiler? refers to a translator from
 the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.

What happens at compile time?

At compile time, java file is compiled by Java Compiler (It does not interact with OS) and converts
 the java code into bytecode.
compilation of simple java program

What happens at runtime?

what happens at runtime when simple java program runs
Classloader: is the subsystem of JVM that is used to load class files.
Bytecode Verifier: checks the code fragments for illegal code that can violate access right
 to objects.
Interpreter: read bytecode stream then execute the instructions.

Why java uses Unicode System?

Unicode is a universal international standard character encoding that is capable of representing
 most of the world's written languages.
Before Unicode, there were many language standards:
  • ASCII (American Standard Code for Information Interchange) for the United States.
  • ISO 8859-1 for Western European Language.
  • KOI-8 for Russian.
  • GB18030 and BIG-5 for chinese, and so on.

Problem

This caused two problems:A particular code value corresponds to different letters in the various language standards.
The encodings for languages with large character sets have variable length.
Some common characters are encoded as single bytes, other require two or more byte.
  1. Solution

    To solve these problems, a new language standard was developed i.e. Unicode System.
    In unicode, character holds 2 byte, so java also uses 2 byte for characters.
    lowest value: U+0000
    highest value: U+FFFF
    Example: A is U+0041
    Here is the link: Unicode Link

    Features of Java
    Java Features

    Simple

    According to Sun, Java language is simple because:
     syntax is based on C++ (so easier for programmers to learn it after C++).
      removed many confusing and/or rarely-used features e.g., explicit pointers,
    operator overloading etc.
     No need to remove unreferenced objects because there is Automatic Garbage
     Collection in java.

    Object-oriented

    Object-oriented means we organize our software as a combination of different types of
     objects that incorporates both data and behaviour.
    Object-oriented programming(OOPs) is a methodology that simplify software
     development and maintenance by providing some rules.
    Basic concepts of OOPs are:
    1. Object
    2. Class
    3. Inheritance
    4. Polymorphism
    5. Abstraction
    6. Encapsulation

    Platform Independent

    A platform is the hardware or software environment in which a program runs.
    There are two types of platforms software-based and hardware-based.
    Java provides software-based platform.
    The Java platform differs from most other platforms in the sense
    that it is a software-based platform that runs on the top of other
     hardware-based platforms. It has two components:
    1. Runtime Environment
    2. API(Application Programming Interface)
    java is platform independent

























    Java code can be run on multiple platforms e.g. Windows, Linux, Sun Solaris,
    Mac/OS etc. Java code is compiled by the compiler and converted into bytecode.
     This bytecode is a platform-independent code because it can be run on multiple
     platforms i.e. Write Once and Run Anywhere(WORA).

    Secured

    Java is secured because:
    • No explicit pointer
    • Java Programs run inside virtual machine sandbox
    how java is secured
    Classloader: adds security by separating the package for the classes of the
     local file system from those that are imported from network sources.
     Bytecode Verifier: checks the code fragments for illegal code that can violate
     access right to objects.
     Security Manager: determines what resources a class can access such as
     reading and writing to the local disk.These security are provided by java language. Some security can also be provided by application developer
    through SSL, JAAS, Cryptography etc.

    Robust
    Robust simply means strong. Java uses strong memory management.
    There are lack of pointers that avoids security problem.
    There is automatic garbage collection in java. There is exception handling and type checking mechanism in java. All these points makes java robust.

    Architecture-neutral

    There is no implementation dependent features e.g. size of primitive types is fixed.
    In C programming, int data type occupies 2 bytes of memory for 32-bit architecture
    and 4 bytes of memory for 64-bit architecture. But in java, it occupies
     4 bytes of memory for both 32 and 64 bit architectures.

    Portable

    We may carry the java bytecode to any platform.

    High-performance

    Java is faster than traditional interpretation since byte code is "close"
    to native code still somewhat slower than a compiled language (e.g., C++)

    Distributed

    We can create distributed applications in java. RMI and EJB are used for creating
     distributed applications. We may access files by calling the methods from any machine
     on the internet.

    Multi-threaded

    A thread is like a separate program, executing concurrently. We can write Java programs
     that deal with many tasks at once by defining multiple threads. The main advantage
    of multi-threading is that it doesn't occupy memory for each thread.
    It shares a common memory area. Threads are important for multi-media,
     Web applications etc.

Comments

Popular posts from this blog

Jersey JAX RS Jar

Tomcat Installation Steps for Windows & add tomcat to eclipse

REST API