Saturday, July 29, 2017

JVM Architecture


JVM is a virtual machine or a program that provides run-time environment in which java byte code can be executed. JVMs are available for many hardware and software platforms.

The use of the same byte code for all JVMs on all platforms makes java platform independent.






Class loader subsystem
Class loader subsystem will load the .class file into java stack and later sufficient memory will be allocated for all the properties of the java program into following five memory locations

      Heap area
      Method area
      Java stack
      PC register
      Native stack

Heap area:

In which object references will be stored.

Method area

In which static variables non-static and static method will be stored.

Java Stack

In which all the non-static variable of class will be stored and whose address referred by object reference.

Pc Register

Which holds the address of next executable instruction that means that use the priority for the method in the execution process?

Native Stack

Native stack holds the instruction of native code (other than java code) native stack depends on native library. Native interface will access interface between native stack and native library.

Execution Engine

It is a part JVM that uses Virtual processor (for execution).which contain interpreter (for reading instructions) and JIT (Just in time) compiler (for performance improvement) for executing the instructions.

How JVM is created

When JRE installed on your machine, you got all required code to create JVM. JVM is created when you run a java program.

Lifetime of JVM

When an application starts, a runtime instance is created.
When application ends, runtime environment destroyed.
If “n”no. of applications starts on one machine then “n” no. of runtime instances are created and every application run on its own JVM instance.

Main task of JVM

1. Search and locate the required files.
2. Convert byte code into executable code.
3. Allocate the memory into ram.
4. Execute the code.
5. Delete the executable code.


Friday, July 28, 2017

Java Program Structure


Java Program Structure for developing java application.

package details
class className
{
            Data Members
            User_defined Methods

public static void main(String[] args)
{
            Block of Staments
}
}

Implementing a Simple Java Program

Creating hello java example

import java.io;
class Hello
 {
 public static void main(String[] args)
{
 System.out.println("Hello Java");
System.out.println("My First Java Program");
}

}

To compile: javac Hello.java

To execute: java Hello


Understanding Hello java program

1.      class keyword is used to declare a class in java.
2.      public keyword is an access modifier which represents visibility, it means it is visible to all.
3.      static is a keyword, if we declare any method as static, it is known as static method. The core advantage of static method is that there is no need to create object to invoke the static method. The main method is executed by the JVM, so it doesn't require to create object to invoke the main method. So it saves memory.
4.      void is the return type of the method, it means it doesn't return any value.
5.      main represents startup of the program.
6.      String[] args is used for command line argument. 
7.      System.out.println() is used to print statements. 

Valid java main method signature
  1. public static void main(String[] args)  
  2. public static void main(String []args)  
  3. public static void main(String args[])  
  4. public static void main(String... args)  
  5. static public void main(String[] args)  
  6. public static final void main(String[] args)  
  7. final public static void main(String[] args)   
Invalid java main method signature
  1. public void main(String[] args)  
  2. static void main(String[] args)  
  3. public void static main(String[] args)  
  4. abstract public static void main(String[] args)

Environment setup


Requirements to write Java Applications

  1. For executing any java program, you need to install the JDK if you don't have installed it.
  2. Set path of the jdk/bin directory. 
  3. create the java program
  4. compile and run the java program

Path

      Path variable is set for providing path for all Java tools like java, javac, javap, javah, jar, appletviewer. In Java to run any program we use java tool and for compile Java code use javac tool. All these tools are available in bin folder so we set path upto bin folder.

classpath

      classpath variable is set for providing path of all Java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar.


Java Buzzwords

  1.     Simple
  2.     Platform independent
  3.     Architectural Neutral
  4.     Portable
  5.     Multithreaded
  6.     Distributed
  7.     Robust
  8.     Dynamic
  9.     Secured
  10.     High Performance
  11.     Interpreted
  12.     Object Oriented



























1. Simple

It is simple because of the following factors:

  1.     It is free from pointer due to this execution time of application is improved. [Whenever we write a Java program without pointers then internally it is converted into the equivalent pointer program].
  2.     It has Rich set of API (application protocol interface).
  3.     It has Garbage Collector which is always used to collect un-Referenced (unused) Memory location for improving performance of a Java program.
  4.      It contains user friendly syntax for developing any applications.



2. Platform independent

A program or technology is said to be platform independent if and only if which can run on all available operating systems with respect to its development and compilation.


3. Architectural Neutral
  1.      Architecture represents processor.
  2.      A Language or Technology is said to be Architectural neutral if and only if which can run on any available processors in the real world without considering their development and compilation.
  3.      The languages like C, CPP are treated as architectural dependent

4. Portable
  1.     If any language supports platform independent and architectural neutral feature is known as portable.
  2.     The languages like C, CPP, Pascal are treated as non-portable language.
  3.      Java is a portable language.
5. Multithreaded

1.      A flow of control is known as a thread.
2.      When any Language executes multiple thread at a time that language is known as multithreaded.
3.      Java is multithreaded.
4.      Using this we can perform multiple tasks at time.

6. Distributed

  1.     Using Java we can create distributed applications.
  2.     RMI and EJB are used for creating distributed applications.
  3.     In distributed application multiple client systems depends on multiple server systems so that even problem occurred in one server will never be reflected on any client system.



7. Robust
  1.     Robust simply means strong.
  2.     It is robust or strong Programming Language because of its capability to handle Run-time Error, automatic garbage collection, the lack of pointer concept, Exception Handling. All these points make It robust Language.
8. Dynamic
  1.     It supports Dynamic memory allocation due to this memory wastage is reduce and improve performance of the application.
  2.      The process of allocating the memory space to the input of the program at a run-time is known as dynamic memory allocation
9. Secure
  1.     It is a more secure language compared to other languages.
  2.     In this language, all code is covered in to byte code after compilation which is not readable by human.
10. High performance

  1.      Java is an interpreted language, so it will never be as fast as a compiled language like C or C++.
  2.      But, Java enables high performance with the use of just-in-time compiler.
11. Interpreted

      It is one of the highly interpreted programming languages.

12. Object Oriented

  1.     In java everything is Object which has some data and behavior. Java can be easily extended as it is based on Object Model.
  2.     It supports OOP's concepts because of this it is most secure language.

Evolution of Java



1.     James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project in June 1991. The small team of sun engineers called Green Team.

2.     Originally designed for small, embedded systems in electronic appliances like set-top boxes.

3.     Firstly, it was called "Greentalk" by James Gosling.

4.     Later, it was called Oak and was developed as a part of the Green project.

5.     In 1995, Oak was renamed as "Java" because it was already a trademark by Oak Technologies

Why "Java" name

1.      Java is an island of Indonesia where first coffee was produced

2.      Java is just a name not an acronym

3.      In 1995, Time magazine called Java one of the Ten Best Products of 1995.



Java Version History

There are many java versions that has been released.

1.      JDK Alpha and Beta (1995)

2.      JDK 1.0 (23rd Jan, 1996)

3.      JDK 1.1 (19th Feb, 1997)

4.      J2SE 1.2 (8th Dec, 1998)

5.      J2SE 1.3 (8th May, 2000)

6.      J2SE 1.4 (6th Feb, 2002)

7.      J2SE 5.0 (30th Sep, 2004)

8.      Java SE 6 (11th Dec, 2006)

9.      Java SE 7 (28th July, 2011)

10.  Java SE 8 (18th March, 2014)

OOPS Principles


  1.     Abstraction
  2.     Encapsulation
  3.     Inheritance
  4.     Polymorphism
1.Abstraction
Hiding internal details and showing functionality is known as abstraction.
Example: phone call, working of car
Advantages of Abstraction
1. Only show essential details to end user.
2. Hide complexity.
3. Security.
      2. Encapsulation
           Binding (or wrapping) code and data together into a single unit is known as encapsulation.
            Example: capsule
Advantages of Encapsulation:
1. A read-only (immutable) or write-only class can be made.
2. Control over the data.
3. It helps in achieving high cohesion and low coupling in the code.


3.Polymorphism

One task is performed by different ways i.e. is known as polymorphism.
Example: God.
Types of polymorphism:
1. Static/compile time polymorphism (by method overloading).
2. Dynamic/run time polymorphism (by method overriding).

4. Inheritance
When one object acquires all the properties and behaviors of parent object i.e. known as inheritance.
Why inheritance is used?
1. Code re-usability.
2. Run-time polymorphism
Types of inheritance:
  1.     Single inheritance
  2.     Multilevel inheritance
  3.     Hierarchical inheritance
  4.     multiple inheritance
  5.     Hybrid inheritance

Object-oriented programming


Computer programming Paradigms 

1. process-oriented model

In a process-oriented model, code acting on data.  Example: C .with this approach programs complexity is increased.

2. object-oriented programming
An object-oriented program can be characterized as data controlling access to code

Object-oriented programming

Object-oriented programming (OOP)is a programming language model organized around objects rather than "actions" and data rather than logic.
A program has been viewed as a logical procedure that takes input data, processes it, and produces output data.

Evolution of OOPS











History of OOPS

1.      The basis for OOP started in the early 1960s
2.      the first programming language to use objects was Simula 67
3.      Many consider that the first truly O-O language was Smalltalk, developed at the Learning Research Group at Xerox's Palo Alto Research Center in the early 1970s.
4.      C++ was implemented in 1982 under the name C with Classes.
5.      Bjarne Stroustrup  design  C++ by combining  some of the features of Simula with the syntax of C.
6.      Later Java was introduced.



Difference between Object Oriented Programming and Procedural Programming

Procedural Programming
Object Oriented Programming
1. In Procedural Programming a program is created step by step instructional format and instructions are executed in order.
2.Follow top down approach.
3. Less secure because it does not have any proper way of data hiding.
4.Does not provide code re-usability feature
5. Doesn’t provide ability to simulate real-world event much more effectively.
6. Slow development
1. In Object Oriented Programming a program is created in a way as real world works.
2. Follow bottom to top approach.
3. Secure because it have proper way of data hiding.
4. Provide code reusability feature.
5. Provide ability to simulate real-world event much more effectively.
6. Fast Development.


OOPs Concept

Object-Oriented Programming is a paradigm to design a program using classes and objects.

Object: Any entity that has state and behavior is known as an object
Example: pen, chair etc..

Class: Collection of objects is called class
Example: Pens is class and pen is object.