Friday, April 10, 2020

Java: Lecture2


MARWARI COLLEGE, RANCHI
(AN AUTONOMOUS UNIT OF RANCHI UNIVERSITY FROM 2009)

- Prakash Kumar, Dept. of CA
-Raju Manjhi, Dept of CA
__________________________________________________________________________________

Java:


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.


What happens at runtime?
At runtime, following steps are performed:

How to set path in Java

The path is required to be set for using tools such as javac, java etc.
If you are saving the java source file inside the jdk/bin directory, path is not required to be set because all the tools will be available in the current directory.
But If you are having your java file outside the jdk/bin folder, it is necessary to set path of JDK.
There are 2 ways to set java path:
1.   temporary
2.   permanent

 

Set Temporary Path of JDK in Windows
For Example:
set path=C:\Program Files\Java\jdk1.X.XXXXX\bin
 
 
Set Permanent Path of JDK in Windows
 
To set the temporary path of JDK, you need to follow following steps:
·         Open command prompt
·         copy the path of jdk/bin directory
·         write in command prompt: set path=copied_path
For setting the permanent path of JDK, you need to follow these steps:
Go to:
1.  MyComputer properties -> advanced tab -> environment variables -> new tab of user variable -> write path in variable name -> write path of bin folder in variable value -> ok -> ok -> ok




A Glance at JVM, JRE, JDK

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

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 is also actively released by other companies besides Sun Micro Systems.


JDK

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



JVM (Java Virtual Machine)


1.       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).
JVM is:
1.       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 Sun and other companies.
2.       An implementation Its implementation is known as JRE (Java Runtime Environment).
3.       Runtime Instance Whenever you write java command on the command prompt to run the java class, and instance of JVM is created.
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.

Internal Architecture of JVM
Let's understand the internal architecture of JVM. It contains classloader, memory area, execution engine etc.


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.

 

 JAVA TOKENS:- Smallest individual units in a program are known as tokens. Java language includes five types of tokens. They are:-

·                     Reserved Keywords
·                     Identifiers
·                     Literals
·                     Operators
·                     Separators


Variable and Datatype in Java

Variable is a name of memory location. There are three types of variables:
1.   local
2.   instance and
3.   static.

There are two types of datatypes in java, primitive and non-primitive.

Variable
Variable is name of reserved area allocated in memory.
int data=7;

 

Types of Variable
There are three types of variables in  java    
·   local variable
·   instance variable
·   static or class variable


Instance variables are used to define the attributes of a particular object.
Class or Static variables are similar to instance variables, except their values apply to all that class's instances (and to the class itself) rather than having different values for each object.
Local variables are declared and used inside method definitions, for example, for index counters in loops, as temporary variables, or to hold values that you need only inside the method definition itself. Use local variables to store information needed by a single method and instance variables to store information needed by multiple methods in the object. Although all three kinds of variables are declared in much the same ways, class and instance variables are accessed and assigned in slightly different ways from local variables.

Note:Unlike other languages, Java does not have global variables-that is, variables that are global to all parts of a program. Instance and class variables can be used to communicate global information between and among objects. Remember that Java is an object-oriented language, so you should think in terms of objects and how they interact, rather than in terms of programs.



Data Types in Java
In java, there are two types of data types:-
·         1. primitive data types
·         2. non-primitive data types



Size and Range of Integer Types:- 

Type       Size               Minimum value                         Maximum value
byte        1byte            -128                                           127
short       2bytes          -32,768                                      32,767
int          4bytes           -2,147,483,648                          2,147,483,647
long     8 byte     -9,223,372,036,854,775,808       9,223,372,036,854,775,807

Java OOPs Concepts


Object Oriented Programming is a paradigm that provides many concepts such as inheritance, data binding, polymorphism etc.
Simula is considered as the first object-oriented programming language. The programming paradigm where everything is represented as an object, is known as truly object-oriented programming language.
Smalltalk is considered as the first truly object-oriented programming language.

OOPs (Object Oriented Programming System)


Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts:
·         Object
·         Class
·         Inheritance
·         Polymorphism
·         Abstraction
·         Encapsulation
Object
The instance of a class is known as object. It mainly serve the following purpose:-
§  Understanding the real world.
§  Decomposition of a problem into objects depends upon judgment and nature of the problem
E.g.
class x
{
        ------;
        void method( )
          {
            ----------;
            ----------;
           }
     
}
class p2
{
public static void main(String args[])
{
   x obj=new x( );   //Object Creation
   obj1.method( );   //Method call
  }

 }

Class
The object with the same data structure and behavior are grouped into a class.
Each class describes possibly infinite set of individual objects. The following points can be noted:-
·         A class is a template that unites data and operations.
·         A class is an abstraction of real world entities with similar properties.
·         A class identifies a set of similar objects.

·         Class is an implementation of abstract data types (ADT).


Inheritance
When one object acquires all the properties and behaviours of parent object i.e. known as inheritance. It provides code reusability. 

Polymorphism
When one task is performed by different ways i.e. known as polymorphism.  
In java, we use method overloading and method overriding to achieve polymorphism.

Abstraction
Hiding internal details and showing functionality is known as abstraction. For example: phone call, we don't know the internal processing.
In java, we use abstract class and interface to achieve abstraction.

Encapsulation 
Binding (or wrapping) code and data together into a single unit is known as encapsulation. For example: capsule, it is wrapped with different medicines.
A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here.

Advantage of OOPs over Procedure-oriented programming language
1) OOPs makes development and maintenance easier where as in Procedure-oriented programming language it is not easy to manage if code grows as project size grows.

2) OOPs provides data hiding whereas in Procedure-oriented programming language a global data can be accessed from anywhere.

3) OOPs provide ability to simulate real-world event much more effectively. We can provide the solution of real word problem if we are using the Object-Oriented Programming language.

Java Support System:-
Support System
Description
Internet Connection                          
Local computer should be connected to the internet.
Web Server
A program that accepts requests for information and sends the required documents.
Web Browser                                  
A program that provides  access to WWW and runs java applets.
HTML                                             
A language for creating hypertext for the Web.
APPLET Tag                                   
For placing java applets in HTML document.
Java Code                                        
Java code is used for defining java applets
Bytecode                                        
Compiled java code that is referred to in the APPLET tag and transferred to the user computer
Proxy Server                                   
An intermediate server between the requesting client workstation and the original server. It is typically implemented for ensuring security.
Mail Server                                     
A computer server that facilitates exchange (sending and receiving) of electronic mails across networks. 

Java Development Kit:-   The java development kit comes with a collection of tools that are used for developing and running Java programs. They include:-
·   applet viewer(for viewing java applets)
·   javac(java compiler)
·   java (java interpreter) 
·   javap(java disassembler)
·   javah(for C header files)
·   javadoc(for creating HTML documents)
·   jdb(java debugger)

      COMPARISON   B/W JAVA AND C++
1) Java does not support operator overloading.
2) Java does not support multiple inheritance for this we use new feature called interface.
3) Java does not support global variable.
4)  Java does not support pointers.
5)  Java uses finalize () is case of destructor. There is no use of header files.

JAVA OFFERS TWO FLAVOUR OF PROGRAMMING
1)           Application programming

2)           Applet programming.

Simple application programs:

1. 
class A
{
public static void main(String args[])
{
int x;
x=10;
System.out.println("Value of x="+x);                           
}
}

2.
class Leap
{
public static void main(String args[])
{
 int y;
 y=1600;
 if(y%4==0&&y%100!=0||y%400==0)
 {
 System.out.println("Year is Leap year");
 }
 else
 {
 System.out.println("Year is not Leap year");
 }
}
}

Go through the video:





No comments:

Post a Comment