Sie sind auf Seite 1von 13

Core java with jsp

Unit – 1
Chapter – 1. Introduction
History
1) Java is a programming language created by James Gosling from Sun Microsystems
(Sun) in 1991. The target of Java is to write a program once and then run this program
on multiple operating systems. The first publicly available version of Java (Java 1.0)
was released in 1995. Sun Microsystems was acquired by the Oracle Corporation in
2010. Oracle has now the steermanship for Java. In 2006 Sun started to make Java
available under the GNU General Public License (GPL). Oracle continues this project
called OpenJDK.

2) Over time new enhanced versions of Java have been released. The current version of
Java is Java 1.8 which is also known as Java 8.

3) Java is defined by a specification and consists of a programming language, a


compiler, core libraries and a runtime (Java virtual machine) The Java runtime allows
software developers to write program code in other languages than the Java
programming language which still runs on the Java virtual machine. The Java
platform is usually associated with the Java virtual machine and the Java core
libraries.

The Java language was designed with the following properties:

 Platform independent: Java programs use the Java virtual machine as abstraction and do
not access the operating system directly. This makes Java programs highly portable. A
Java program (which is standard-compliant and follows certain rules) can run unmodified
on all supported platforms, e.g., Windows or Linux.

 Object-orientated programming language: Except the primitive data types, all


elements in Java are objects.
 Strongly-typed programming language: Java is strongly-typed, e.g., the types of the
used variables must be pre-defined and conversion to other objects is relatively strict,
e.g., must be done in most cases by the programmer.

 Interpreted and compiled language: Java source code is transferred into the bytecode
format which does not depend on the target platform. These bytecode instructions will be
interpreted by the Java Virtual machine (JVM). The JVM contains a so called Hotspot-
Compiler which translates performance critical bytecode instructions into native code
instructions.

 Automatic memory management: Java manages the memory allocation and de-
allocation for creating new objects. The program does not have direct access to the
memory. The so-called garbage collector automatically deletes objects to which no active
pointer exists.

 The Java syntax is similar to C++. Java is case-sensitive, e.g., variables called myValue
and myvalue are treated as different variables.

Hello world Java program


 // a small Java program
 public class HelloWorld {
 public static void main(String[] args) {
 System.out.println("Hello World");
 }
 }

Architecture
The three main components of Java language are
JVM, JDK and JRE which stands for Java Virtual Machine, Java Development Kit and Java
Runtime Environment respectively. Each components work separately here. Let’s understand each
one of them individually.

1) JVM (Java Virtual Machine)

It is an abstract machine. It is a specification that provides run-time environment in which java


bytecode can be executed.
It follows three notations:
 Specification: It is a document that describes the implementation of the Java virtual
machine. It is provided by Sun and other companies.
 Implementation: It is a program that meets the requirements of JVM specification.
 Runtime Instance: An instance of JVM is created whenever you write a java command on
the command prompt and run the class.

2) JRE (Java Runtime Environment)

JRE refers to a runtime environment in which java bytecode can be executed. It implements
the JVM (Java Virtual Machine) and provides all the class libraries and other support files
that JVM uses at runtime. So JRE is a software package that contains what is required to run
a Java program. Basically, it’s an implementation of the JVM which physically exists.

3) JDK(Java Development Kit)

It is the tool necessary to compile, document and package Java programs. The JDK
completely includes JRE which contains tools for Java programmers. The Java Development
Kit is provided free of charge. Along with JRE, it includes an interpreter/loader, a compiler
(javac), an archiver (jar), a documentation generator (javadoc) and other tools needed in Java
development. In short, it contains JRE + development tools.

Java virtual machine


A Virtual Machine is a software implementation of a physical machine.
Java was developed with the concept of WORA (Write Once Run
Anywhere), which runs on a VM. The compiler compiles the Java file into
a Java .class file, then that .class file is input into the JVM, which Loads
and executes the class file.

Java class file


A Java class file is a file (with the .class filename extension) containing Java bytecode that can
be executed on the Java Virtual Machine (JVM). A Java class file is usually produced by a Java
compiler from Java programming language source files (.java files) containing Java classes. If a
source file has more than one class, each class is compiled into a separate class file.

The Java virtual machine (JVM) is a software implementation of a computer that executes
programs like a real machine.

The Java virtual machine is written specifically for a specific operating system, e.g., for
Linux a special implementation is required as well as for Windows.

java programs are compiled by the Java compiler into bytecode. The Java virtual machine
interprets this bytecode and executes the Java program.
The three main components of the Java language:
1. JDK which stands for(Java Development Kit)
2. JRE which stands for (Java Runtime Environment),
3. JVM which stands for (Java Virtual Machine)

 To develop and run the Java application, we need an environment and that
required environment is JDK(Java Development Kit). So first we have to
install JDK first.
 At the client side, just to run the application, the required environment is
JRE(Java Runtime Environment). Once you will install JDK, automatically
JRE will create.
 In JRE, JVM is responsible to run the application. That means JVM
providing JAVA execution engine that is JAVA interpreter named as java
which executes source code.
Java API (Application Programming Interface)
Java Platforms
An application programming interface (API), in the context of Java, is a collection of prewritten
packages, classes, and interfaces with their respective methods, fields and constructors. Similar
to a user interface, which facilitates interaction between humans and computers, an API serves as
a software program interface facilitating interaction.
In Java, most basic programming tasks are performed by the API’s classes and packages, which
are helpful in minimizing the number of lines written within pieces of code.
Java Development Kit (JDK) is comprised of three basic components, as follows:

 Java compiler
 Java Virtual Machine (JVM)
 Java Application Programming Interface (API)

The Java API, included with the JDK, describes the function of each of its components. In Java
programming, many of these components are pre-created and commonly used.
There are three frames when an API starts, as follows:

 The first frame shows all API components (classes and packages).
 When a particular package is selected, the second frame shows all interfaces, classes and
exceptions of that particular package.
 The third and primary frame provides an overview of all of API packages, which can be
expanded in the main frame to show the index, class hierarchy and help sections.
The Java platform is a suite of programs that facilitate developing and running programs written in
the Java programming language. A Java platform will include an execution engine (called a virtual
machine), a compiler and a set of libraries; there may also be additional servers and alternative
libraries that depend on the requirements. Java is not specific to any processor or operating system
as Java platforms have been implemented for a wide variety of hardware and operating systems
with a view to enable Java programs to run identically on all of them. Different platforms target
different classes of device and application domains:

 Java Card: A technology that allows small Java-based applications (applets) to be run securely
on smart cards and similar small-memory devices.
 Java ME (Micro Edition): Specifies several different sets of libraries (known as profiles) for
devices with limited storage, display, and power capacities. It is often used to develop
applications for mobile devices, PDAs, TV set-top boxes, and printers.

 Java SE (Standard Edition): For general-purpose use on desktop PCs, servers and similar
devices.
 Java EE (Enterprise Edition): Java SE plus various APIs which are useful for multi-tier client–
server enterprise applications.

The Java platform consists of several programs, each of which provides a portion of its overall
capabilities. For example, the Java compiler, which converts Java source code into Java bytecode
(an intermediate language for the JVM), is provided as part of the Java Development Kit (JDK). The
Java Runtime Environment (JRE), complementing the JVM with a just-in-time (JIT) compiler,
converts intermediate bytecode into native machine code on the fly. The Java platform also includes
an extensive set of libraries.

The essential components in the platform are the Java language compiler, the libraries, and the
runtime environment in which Java intermediate bytecode executes according to the rules laid out in
the virtual machine specification.

java programming some key points


------------------------------------------------------------------------------------------------------------------------------------

An interface can extends another interface or interfaces (more than one interface) . A
class that implements interface must implements all the methods in interface. All the
methods are public and abstract. And all the fields are public, static, and final

Abstract methods, similar to methods within an interface, are declared without any
implementation. They are declared with the purpose of having the child class provide
implementation. ... They are created with the purpose of being a super class.
functional interface
A functional interface is an interface that contains only one abstract method. They
can have only one functionality to exhibit. From Java 8 onwards, lambda expressions
can be used to represent the instance of a functional interface. A functional interface
can have any number of default methods.

Lambda Expression
Me: "Sofia, let's put the toys away. Is there a toy on the ground"
Sofia: "Yes, the ball"
Me: "Ok, put the ball in the box. Is there something else?"
Sofia: "Yes, there is my doll"
Me: "Ok, put the doll in the box. Is there something else?"
Sofia: "Yes, there is my book"
Me: "Ok, put the book in the box. Is there something else?"
Sofia: "No, nothing else"
Me: "Fine, we are done"

This is exactly what we do everyday with our Java collections, but unfortunately
the biggest part of us is not 2 years old. We iterate the collection externally,
explicitly pulling out and processing the items one by one. It would be far better
for me if I could tell to Sofia just: "put inside the box all the toys that are on the
ground". There are two other reasons, why an internal iteration is
preferable: first Sofia could choose to take at the same time the doll with
one hand and the ball with the other and second she could decide to take
the objects closest to the box first and then the others. In the same way
using an internal iteration the JIT (JUST IN TIME) compiler could optimize
it processing the items in parallel or in a different order. These
optimizations are impossible if we iterate the collection externally as we
are used to do in Java and more in general with the imperative
programming.

Now we can use lambda expressions to implement functional interfaces but


the lambda expressions are not the only mechanism we can use for this
purpose. Particularly where there are preexisting code that we would like to
reuse we can simplify the implementation of the functional interface by
using method references.
-----------------------------------------------------------------------------------------------------------------------------
Java Method References
Java provides a new feature called method reference in Java 8. Method reference is used to
refer method of functional interface. It is compact and easy form of lambda expression.
Each time when you are using lambda expression to just referring a method, you can
replace your lambda expression with method reference.

Type Annotations:

Das könnte Ihnen auch gefallen