0% found this document useful (0 votes)
0 views

Java J2EE-Unit-1

The document provides a comprehensive overview of Java, detailing its history, features, platforms, and architecture, including the Java Virtual Machine (JVM) and Java Runtime Environment (JRE). It outlines the evolution of Java from its inception as the OAK language to its current status as a widely-used programming language, highlighting its object-oriented nature and key principles such as portability and security. Additionally, it covers Java's error handling, memory management, and object-oriented programming concepts, making it a valuable resource for understanding Java's fundamentals.

Uploaded by

waben61343
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Java J2EE-Unit-1

The document provides a comprehensive overview of Java, detailing its history, features, platforms, and architecture, including the Java Virtual Machine (JVM) and Java Runtime Environment (JRE). It outlines the evolution of Java from its inception as the OAK language to its current status as a widely-used programming language, highlighting its object-oriented nature and key principles such as portability and security. Additionally, it covers Java's error handling, memory management, and object-oriented programming concepts, making it a valuable resource for understanding Java's fundamentals.

Uploaded by

waben61343
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

JSS Science and Technology

University
MSc 1st Semester
Course: Java & J2EE Unit-
Presented By
1 Chaithra C S
Course Code: 23MSCS110 Assistant Professor
Dept of Computer Applications
JSSSTU Mysuru
History of Java
• Java started out as a research project.

• Research began in 1991 as the Green Project at Sun Microsystems, Inc.

• Research efforts birthed a new language, OAK. ( A tree outside of the


window of James Gosling’s office at Sun).

• It was developed as an embedded programming language, which would


enable embedded system application.

• It was not really created as web programming language.

• Java is available as jdk and it is an open source s/w.


History of Java
Language was created with 5 main goals:
Ø It should be object oriented.
Ø A single representation of a program could be executed on
multiple operating systems. (i.e. write once, run anywhere)
Ø It should fully support network programming.
Ø It should execute code from remote sources securely.
Ø It should be easy to use.

Oak was renamed Java in 1994.

Now Sun Microsystems is a subsidiary of Oracle Corporation.


History of Java Green Team

James
Gosling
Java Logo
Versions of Java
Version Codename Year Features Added
JDK 1.0 Oak Jan23,1996 -
Rebirth of Inner classes JavaBeans, JDBC, RMI, Reflection, AWT.
JDK 1.1 Feb19,1997
Java
JIT compiler, Collections, IDL & CORBA, strictfp, Java
J2SE 1.2 Playground Dec8, 1998
Plug-in.
HotSpot JVM, JavaSound, Java Naming and Directory
J2SE 1.3 Kestrel May8, 2000 Interface, Java Platform Debugger Architecture.

Preferences API, Logging API, assert, image I/O API,


J2SE 1.4 Merlin Feb6, 2002
security and cryptography extensions.
Generics, annotations, Autoboxing, Enumerations, Varargs,
J2SE 5.0 Tiger Sep30, 2004 Enhanced for each.

JDBC 4.0, JVM improvements, Improved JAXB, Improved


JAVA SE 6 Mustang Dec11, 2006 web services, Support for older Win9x versions dropped.

JAVA SE 7 Major updates to Java


Dolphin July28, 2011

JAVA SE 8 - Mar, 2014 -


Java Platforms

There are three main platforms for Java:

• Java SE (Java Platform, Standard Edition) – runs on desktops and


laptops.

• Java ME (Java Platform, Micro Edition) – runs on mobile devices such


as cell phones.

• Java EE (Java Platform, Enterprise Edition) – runs on servers.


Java Terminology
Java Development Kit:
• It contains one (or more) JRE's along with the various development tools
like the Java source compilers, bundling and deployment tools, debuggers,
development libraries, etc.

Java Virtual Machine:


• An abstract machine architecture specified by the Java Virtual Machine
Specification.

• It interprets the byte code into the machine code depending upon the
underlying OS and hardware combination. JVM is platform dependent. (It
uses the class libraries, and other supporting files provided in JRE)
Java Terminology (contd…)
ØJava Runtime Environment:
A runtime environment which implements Java Virtual Machine, and
provides all class libraries and other facilities necessary to execute
Java programs. This is the software on your computer that actually
runs Java programs.

JRE = JVM + Java Packages Classes (like util, math, lang, awt, swing
etc) +runtime libraries.
Class Loader Java Class
Java Byte Code
Libraries
Source Verifier Java
Execution
Java Byte
codes Procedure
move Java Just-in-time
Java
locally or Interpreter Compiler
Compiler
through
n/w
Run Time System

Java Java OS Win 32 Solaris


Byte codes

MAC Others

Hardware
Architecture of JVM
Java Virtual Machine

Class loader subsystem: A mechanism for loading types (classes and


interfaces) given fully qualified names.

The Java virtual machine organizes the memory it needs to execute a


program into several runtime data areas.

Each Java virtual machine also has an execution engine: a mechanism


responsible for executing the instructions contained in the methods of
loaded classes.
Class loader subsystem
• The Java virtual machine contains two kinds of class loaders: a bootstrap class
loader and user-defined class loaders.
• The bootstrap class loader is a part of the virtual machine implementation, and
user-defined class loaders are part of the running Java application.
• Loading: finding and importing the binary data for a type
• Linking: performing verification, preparation, and (optionally) resolution
• Verification: ensuring the correctness of the imported type
• Preparation: allocating memory for class variables and initializing the memory
to default values
• Resolution: transforming symbolic references from the type into direct
references.
• Initialization: invoking Java code that initializes class variables to their proper
starting values.
Interpreter vs JIT
• Java Bytecodes were originally designed to be interpreted by JVM meaning bytecode are
translated to machine code without it being stored anywhere.
• Since bytecode verifier (which is part of JVM) performs runtime checks, line by line
execution was important.
• Since speed became an issue, Just-in-Time Compilation (JIT) came into being. JIT
converts chunks of code, stores it temporarily in memory and then executes the
converted code.
• JIT compilers are typically bundled with or are a part of a virtual machine and do the
conversion to native code at runtime, on demand.
• The compiler also does automatic register allocation and some optimization when it
produces the bytecodes.
• Therefore, JIT is hybrid compiler.
Error handling in java
• Compilation error : generated by the compiler. Examples of situation when it arises:
• incorrect syntax, bracket mismatch, if keywords are used as variables
• Using uninitialized variables
• Unreachable code: while(false){…}
• Strong type checking
• Run-time error : generated by the JVM.
• Examples of situation when it arises:
• Attempt to divide an integral value by 0 or access array index beyond the
defined range.
• Trying to access a class that does not exist at runtime. (What happens if you
delete Hello.class and then run Hello.
• Java has a strong exception handing mechanism that allows programmers to handle
such errors and come out of the situation gracefully.
When the virtual machine loads a class file, it parses information about
a type from the binary data contained in the class file.
It places this type information into the method area.

As the program runs, the virtual machine places all objects the program
instantiates onto the heap.

As each new thread comes into existence, it gets its own pc register
(program counter) and Java stack.

Byte code is a highly optimized set of instructions designed to be


executed by the Java run-time system, which is called the Java Virtual
Machine (JVM).The JVM is an interpreter for byte code.
Execution
Compiler Byte
Java Code Code
JAVAC

Libraries
JVM
JRE

Operating System

Hardware
Java Program Execution
• A Java program requires JVM (part of JRE) to execute Java code. When java
application starts to executes, Java Virtual Machine also starts.
• Bytecode has instructions that Java Virtual Machine can understand and
execute.
• JVM converts the Bytecode to machine specific code.
• Java Bytecode can be copied on to any machine that has JVM and
executed. This is what makes Java Platform Independent.
• “Write Once, Run any where”
Binary form of a .class file
public class Hello
{
public static void main(String[] args)
{
System.out.println("Hello, World!");
}
}
0000: cafe babe 0000 002e 001a 0a00 0600 0c09 ................
0010: 000d 000e 0800 0f0a 0010 0011 0700 1207 ................
0020: 0013 0100 063c 696e 6974 3e01 0003 2829 .....<init>...()
0030: 5601 0004 436f 6465 0100 046d 6169 6e01 V...Code...main.
0040: 0016 285b 4c6a 6176 612f 6c61 6e67 2f53 ..([Ljava/lang/S
0050: 7472 696e 673b 2956 0c00 0700 0807 0014 tring;)V........
0060: 0c00 1500 1601 000d 4865 6c6c 6f2c 2057 ........Hello, W
0070: 6f72 6c64 2107 0017 0c00 1800 1901 0005 orld!...........
0080: 4865 6c6c 6f01 0010 6a61 7661 2f6c 616e Hello...java/lan
0090: 672f 4f62 6a65 6374 0100 106a 6176 612f g/Object...java/
00a0: 6c61 6e67 2f53 7973 7465 6d01 0003 6f75 lang/System...ou ...
Features of Java Language
ØSimple
ØObject Oriented
ØCompile, Interpreted and High Performance
ØPortable
ØReliable
ØSecure
ØMultithreaded
ØDynamic
ØDistributed
ØArchitecture-Neutral
Java Features
• Simple
• No pointers
• Automatic garbage collection
• Rich pre-defined class library

• Object Oriented
• Focus on the data (objects) and methods manipulating the data
• All methods are associated with objects
• Potentially better code organization and reuse
Java Features
• Compile, Interpreted and High Performance
• Java compiler generate byte-codes, not native machine code
• The compiled byte-codes are platform-independent
• Java byte codes are translated on the fly to machine readable instructions in
runtime (Java Virtual Machine)
• Easy to translate directly into native machine code by using a just-in-time compiler.
• Portable
• Same application runs on all platforms
• The sizes of the primitive data types are always the same
• The libraries define portable interfaces
Java Features
• Reliable/Robust
• Extensive compile-time and runtime error checking
• No pointers but real arrays. Memory corruptions or unauthorized
memory accesses are impossible
• Automatic garbage collection tracks objects usage over time
• Secure
• Java’s robustness features makes java secure.
• Access restrictions are forced (private, public)
Java Features
• Multithreaded
• It supports multithreaded programming.
• Need not wait for the application to finish one task before beginning
another one.
Dynamic
Libraries can freely add new methods and instance variables without any
effect on their clients
Interfaces promote flexibility and reusability in code by specifying a set
of methods an object can perform, but leaves open how these methods
should be implemented .
Java Features
• Distributed
• Java is designed for the distributed environment of the Internet, because
it handles TCP/IP protocols.
• Allows objects on two different computers to execute procedures
remotely by using package called Remote Method Invocation (RMI).

• Architecture-Neutral
• Goal of java designers is “write once; run anywhere, any time, forever.”
JVM: perspectives
• JVM can be looked as
• a runtime instance: JVM life cycle begins when applications starts
to run ( that is when main method is called) and ends when the
application ends.
• the abstract specification: Specification that Java team at Sun
(Oracle) provides which tells JVM makers how they must design
JVM for their OS.
• a concrete implementation: JVM that is built specifically targeted
for an OS based on abstract specification .
Automatic garbage collection
• The garbage collector is a tool that attempts to free unreferenced memory
(memory occupied by objects that are no longer in use by the program) in
programs.
• Automatic garbage collection is an integral part of Java and its run-time
system.
• Java technology has no pointers. So there is no question of allocation and
freeing by programmers.
• Does that mean Java does not support dynamic memory allocation?
• No. It means that Java takes care of memory and relieves the programmers
from memory-related hassles.
• java –verbose:gc can be used to get more information about garbage
collection process.
Object Oriented Programming Concepts
•Objects
•Classes
•Data abstraction and Encapsulation
•Inheritance
•Polymorphism
•Dynamic Binding
A class is collection of objects of similar type or it is a template.
Ex: fruit mango;

Objects are instances of the type class.

The wrapping up of data and functions into a single unit ( called class) is known
as encapsulation. Data encapsulation is the most striking features of a class.

Abstraction refers to the act of representing essential features without including


the background details or explanations

Inheritance is the process by which objects of one class acquire the properties
of another class. The concept of inheritance provides the reusability.
Polymorphism:
It allows the single method to perform different actions based on the
parameters.

Dynamic Binding: When a method is called within a program, it


associated with the program at run time rather than at compile time is
called dynamic binding.
Object-Oriented Languages Types
1.Object-based programming language: It supports
Data encapsulation
Data hiding and access mechanisms
Automatic initialization and clear-up of objects
Operator overloading
Disadvantage : They do not support inheritance and dynamic binding
Ex: Ada

2.Object-oriented programming languages:

OOP = Object-based + inheritance + dynamic binding

Ex: C++, Java, Smalltalk, Object Pascal


Benefits of OOP
•Through inheritance, we can eliminate redundant code and extend the use
of existing classes.

•The principle of data hiding helps the programmer to build secure programs.

•It is easy to partition the work in a project based on objects.

•Object oriented system easily upgraded from small to large systems.

•Software complexity can be easily managed.


Applications of oop
• Real-time systems

• Object-oriented databases

• Neural networks and parallel programming

• Decision support and office automation systems

• CAD/CAM systems
ØWhat is the Difference b/w OO and OB Languages?
In Object based languages inheritance is not supported so that
dynamic polymorphism also not supported.
E.g. VB,VC++.

ØIs C++ partial OOP?


Yes, C++ is a partial OOP because without using class also we can
able to write the program.

ØIs Java total OOP or partial OOP?


Java is a total oop language because with out object orientation we
can’t able to write any program.
ØJava is a pure oop or not ?

By default java is not pure object oriented language.

Java is called as Hybrid language.

Pure oop languages are “small talk”, ”ruby”, “Eiffel”.


Differences b/w C++ and Java
C++ Java
Global variable are supported No Global variables. Everything
must be inside the class only
Multiple inheritance is No direct multiple Inheritance
supported
Constructer and destructors Java supporting constructors
supported only & instead of destructors
garbage collection is supported
In C++ pointers are supported No printer arithmetic in Java
C++ supporting ASCII character Java supports Unicode
set character set
Sample Program
class HelloWorld {
public static void main (String args []) {
System.out.println (“Welcome to Java Programming…..”);
}
}
public allows the program to control the visibility of class
members. When a class member is preceded by public, then that
member may be accessed by code outside the class in which it is
declared. In this case, main ( ) must be declared as public, since it
must be called by code outside of its class when the program is
started.
static allows main( ) to be called without having to instantiate a
particular instance of the class. This is necessary since main ( ) is
called by the Java interpreter before any objects are made.

void states that the main method will not return any value.

main() is called when a Java application begins. In order to run


a class, the class must have a main method.

string args[] declares a parameter named args, which is an array of


String. In this case, args receives any command-line arguments
present when the program is executed.
System is a class which is present in java.lang package.

out is a static field present in system class which returns a


PrintStream object. As out is a static field it can call directly with
classname.

println() is a method which present in PrintStream class which


can call through the PrintStream object return by static field out
present in System class to print a line to console.
class sample{
public static void main(String args[]){
System.out.println("sample:main");
sample s=new sample();
s.display();

}
void display(){
System.out.println("display:main");
}
}

You might also like