Java write once, run anywhere Nishad Abdulkareem, Lecturer MES College Marampally

advertisement
Java
write once, run anywhere
Nishad Abdulkareem, Lecturer
MES College Marampally
Index
• Java Programming language
• History
• Java Varieties
• Java world
• Buzz words
• Classpath
• Program
Department of MCA
MES College Marampally
Java Programming Language
• Java is a programming language originally developed by
James Gosling at Sun Microsystems (which is now a
subsidiary of Oracle Corporation) and released in 1995 as a
core component of Sun Microsystems' Java platform.
• The language derives much of its syntax from C and C++ but
has a simpler object model and fewer low-level facilities.
• Java applications are typically compiled to bytecode (class
file) that can run on any Java Virtual Machine (JVM)
regardless of computer architecture.
• It is intended to let application developers "write once, run
anywhere".
Department of MCA
MES College Marampally
Once upon a time…
• The original and reference implementation Java compilers,
virtual machines, and class libraries were developed by Sun
from 1995.
• As of May 2007, in compliance with the specifications of the
Java Community Process, Sun relicensed most of its Java
technologies under the GNU General Public License.
•
• James Gosling initiated the Java language project in June 1991
for use in one of his many set-top box projects.
• Java was originally designed for interactive television, but it
was too advanced.[ The language, initially called Oak after an
oak tree that stood outside Gosling's office, also went by the
name Green and ended up later renamed as Java, from a list of
random words.
Department of MCA
MES College Marampally
…and grown up like…
• Sun Microsystems released the first public implementation as
Java 1.0 in 1995. It promised "Write Once, Run Anywhere"
(WORA), providing no-cost run-times on popular platforms.
• Fairly secure and featuring configurable security, it allowed
network- and file-access restrictions. Major web browsers
soon incorporated the ability to run Java applets within web
pages, and Java quickly became popular.
• With the advent of Java 2 (released initially as J2SE 1.2 in
December 1998–1999), new versions had multiple
configurations built for different types of platforms.
• The different platforms are J2EE,J2ME and J2SE
Department of MCA
MES College Marampally
Behind the names…
• Java Platform, Enterprise Edition or Java EE is a widely
used platform for server programming in the Java
programming language.
• The Java platform (Enterprise Edition) differs from the Java
Standard Edition Platform (Java SE) in that it adds libraries
which provide functionality to deploy fault-tolerant,
distributed, multi-tier Java software, based largely on modular
components running on an application server.
• For marketing purposes, Sun renamed new J2 versions as Java
EE. The current version is called Java EE 6.
• Java EE includes several API specifications, such as JDBC,
RMI, e-mail, JMS, web services, XML, etc, and defines how
to coordinate them.
Department of MCA
MES College Marampally
JSE
• Java Standard Edition or Java SE is a widely used platform
for programming in the Java language.
• It is the Java Platform used to deploy portable applications for
general use.
• In practical terms, Java SE consists of a virtual machine,
which must be used to run Java programs, together with a set
of libraries (or "packages") needed to allow the use of file
systems, networks, graphical interfaces, and so on, from within
those programs.
• The "SE" is used to distinguish the base platform from Java
EE and Java ME. The "2" was originally intended to
emphasize the major changes introduced in version 1.2, but
was removed in version 1.6.
Department of MCA
MES College Marampally
JME
• Java Platform, Micro Edition, or Java ME, is a Java
platform designed for mobile devices and embedded systems.
• Target devices range from industrial controls to mobile phones
(especially feature phones) and set-top boxes. Java ME was
formerly known as Java 2 Platform, Micro Edition (J2ME).
Department of MCA
MES College Marampally
JEE
• Java EE also features some specifications unique to Java EE
for components. These include Enterprise JavaBeans,
Connectors, servlets, portlets (following the Java Portlet
specification), JavaServer Pages and several web service
technologies.
• This allows developers to create enterprise applications that
are portable and scalable, and that integrate with legacy
technologies.
• A Java EE application server can handle transactions, security,
scalability, concurrency and management of the components
that are deployed to it, in order to enable developers to
concentrate more on the business logic of the components
rather than on infrastructure and integration tasks
Department of MCA
MES College Marampally
Java World
Provides a simplified, fast way to
create dynamic web content. JSP
technology enables rapid
development of web-based
applications that are server- and
platform-independent.
 Java Server Pages
 Java Servlets
A servlet class
extends
the capabilities of
Messaging
is a method
of communication
servers
that
host
applications
are
between software components orthat
applications.
accessed
byclient
way can
of asend
request-response
a messaging
messages to and
programming
model.
servlets
receive messages
from Although
any other client.
Each
can
type of request,
they
clientrespond
connectstotoany
a messaging
agent that
are
commonly
used
to extendsending,
the
provides
facilities
for creating,
applications
hosted
by
web
servers
receiving, and reading messages.Eg Car
inventory
 Java Messaging Services
Web applications can use
the JavaMail API to send
email notifications. The API
has two parts: an
application-level interface
that the application
components use to send
email and a service provider
interface. Service providers
implement particular email
protocols, such as SMTP
 Java mail API
 Java API for XML Processing
 JDBC API
Department of MCA
The Java API for XML
Processing (JAXP), part of the
Java SE platform, supports the
processing of XML documents
using the Document Object
Model (DOM), the Simple API
for XML (SAX),
MES College Marampally
JRE
• Java Runtime Environment
 The java programming language adds the portability by
converting the source code to byte code version which can
be interpreted by the JRE and gets converted to the
platform specific executable ones.
 Thus for different platforms one has corresponding
implementation of JRE. But JRE has to meet the
specification JVM (Java Virtual Machine) Concept that
serves as a link between the Java libraries and the platform
specific implementation of JRE.
 Thus JVM helps in the abstraction of inner implementation
from the programmers who make use of libraries for their
programmes.
Department of MCA
MES College Marampally
• Science Behind Java
Department of MCA
MES College Marampally
JDK
 Java Developer Kit contains tools needed to develop the Java
programs, and JRE to run the programs. The tools include
compiler (javac.exe), Java application launcher (java.exe),
Appletviewer, etc…
 The JDK(Java Developmental Tool kit) comes along with java
libraries and JVM embedded in it.
 Apart from these it comes along with the utility tools for byte
code compilation "javac", Executing the byte codes through java
programmes through "java" and many more utilities found in the
binary directory of java.
 Speaking practically JDK is essential for developers, which
comes along with library packages to develop Software
programmes.
 JRE is minimal set of programmes which executes the java class
files developed by the software developers.
 Without jre one can only compile the application but cannot run
the application Since jre contains the JVM which executes the
byte code generated from the javac compiler.
Department of MCA
MES College Marampally
• JRE = JVM + Java Packages Classes(like util, math, lang,
awt,swing etc)+runtime libraries.
• Java Runtime Environment contains JVM, class libraries, and
other supporting files. It does not contain any development
tools such as compiler, debugger, etc. Actually JVM runs the
program, and it uses the class libraries, and other supporting
files provided in JRE. If you want to run any java program,
you need to have JRE installed in the system
• The Java Virtual Machine provides a platform-independent
way of executing code; programmers can concentrate on
writing software, without having to be concerned with how or
where it will run
Department of MCA
MES College Marampally
•
•
•
As we all aware when we compile a Java file, output is not an 'exe'
but it's a '.class' file. '.class' file consists of Java byte codes which are
understandable by JVM. Java Virtual Machine interprets the byte
code into the machine code depending upon the underlying operating
system and hardware combination. It is responsible for all the things
like garbage collection, array bounds checking, etc… JVM is
platform dependent.
The JVM is called "virtual" because it provides a machine interface
that does not depend on the underlying operating system and machine
hardware architecture. This independence from hardware and
operating system is a cornerstone of the write-once run-anywhere
value of Java programs.
There are different JVM implementations are there. These may differ
in things like performance, reliability, speed, etc. These
implementations will differ in those areas where Java specification
doesn’t mention how to implement the features, like how the garbage
collection process works is JVM dependent, Java spec doesn’t define
any specific way to do this.
Department of MCA
MES College Marampally
• JVM
• ByteCode
• JIT
• Applets
Department of MCA
MES College Marampally
Java Buzzwords
• Simple
 Java inherits the C/C++ syntax and many of the
object oriented features of C++ ,most programmers
have little trouble learning java.
• Object Oriented
 The object model in java is Simple and easy to
extend, while primitive types ,such as integers ,are
kept as high performance nonobjects.
• Robust
 Memory management
 Exception Handling
• Multithreaded
 Multiprocess synchronization
• Architectural Neutral
• Distributed
 of
RMI
Department
MCA
MES College Marampally
Classpath
• The Classpath is an argument - set either on the commandline, or through an environment variable - that tells the Java
Virtual Machine where to look for user-defined classes and
packages when running Java programs.
• When we invoke Java, we specify the name of the application
to run: org.mypackage.HelloWorld. However we must
also tell Java where to look for the files and directories
defining our package. So to launch the program, we use the
following command:
 java -classpath D:\myprogram
org.mypackage.HelloWorld
Department of MCA
MES College Marampally
Say hello to Java
• First Program
 All program starts by executing by calling main()
 Public is the access specifier.main must be declared as
public so that it can access from outside the class
 static allows main to be called without instantiating a
particular class.
 Any information that are passed in to the method is
received by variables specified within the parenthesis
called parameters .
 String args[] declares a parameter named args,
which is an array of instances of string.
 System is predefined class that provides access to
system, out is the output stream that is connected to the
console
 Any information need to pass to a method is
specified within the set of parameters that follow the
name of method_called parameters
Department of MCA
MES College Marampally
• Swap two numbers without using a third variable
• Reverse Number
• Expression
Department of MCA
MES College Marampally
javap - The Java Class File Disassembler
• The javap command disassembles a class file. Its output
depends on the options used. If no options are used, javap
prints out the package, protected, and public fields and
methods of the classes passed to it. javap prints its output to
stdout. For example, compile the following class declaration:
• help Prints out help message for javap.
 -l Prints out line and local variable tables.
 -b Ensures backward compatibility with javap in
JDK 1.1.
 -public Shows only public classes and members.
 -protected Shows only protected and public
classes and members.
 -package Shows only package, protected, and
public classes and members. This is the default.
 -private Shows all classes and members.
•
Department
of MCA
MES College Marampally
 -Jflag Pass flag directly to the runtime system.
Some examples: javap -J-version javap -JDjava.security.manager -JDjava.security.policy=MyPolicy MyClassName
 -s Prints internal type signatures.
• -c Prints out disassembled code, i.e., the instructions that
comprise the Java bytecodes, for each of the methods in the
class. These are documented in the Java Virtual Machine
Specification.
• -verbose Prints stack size, number of locals and args for
methods.
• -classpath path Specifies the path javap uses to look
up classes. Overrides the default or the CLASSPATH
environment variable if it is set. Directories are separated by
semi-colons. Thus the general format for path is:

Department of MCA
MES College Marampally
References
•
•
•
•
www.wikipedia.com
http://www.sun.com
http://www.java-examples.com/
The complete Reference Java
Department of MCA
MES College Marampally
Download