5. 5
The genesis and evolution
The genesis and evolution of the Java
programming language is an
interesting story that spans several
decades. Java was created by James
Gosling and his team at Sun
Microsystems (later acquired by
Oracle Corporation) in the early
1990s. Here's a brief overview of its
history:
6. 6
1. Origins: In the early 1990s, Sun Microsystems started a project
called the Green Project, led by James Gosling, Mike Sheridan,
and Patrick Naughton. The aim was to develop a new language
and platform for programming consumer electronic devices, like
VCRs and set-top boxes. Initially, the project used the C++
language, but they faced various challenges with portability and
reliability.
2. OAK: In 1991, the team began work on a new language called
Oak. The name Oak was chosen because of an oak tree that
stood outside Gosling's office. The language was inspired by C+
+ and other object-oriented languages, but it was designed to be
simpler and more portable. The first Oak prototype was created
in 1992.
7. 7
3. Java: In 1995, Oak was renamed "Java" and
officially released to the public. The name
"Java" was chosen due to the team's
fascination with coffee, and there was already
a trademark conflict with the name "Oak."
With the release of Java, Sun Microsystems
marketed it as a platform-independent, "write
once, run anywhere" language, thanks to its
"Java Virtual Machine" (JVM) concept.
4. Evolution: Java continued to evolve rapidly,
and Sun Microsystems released new versions
with significant updates and improvements
over the years.
8. 8
Some key milestones in Java's evolution include:
•Java 1.0 (1996): The initial version released with basic features
and the core class libraries.
•Java 1.2 (Java 2 Platform, Standard Edition (J2SE) 1.2 - 1998):
Introduced the Java Development Kit (JDK) and Java Runtime
Environment (JRE) model. Also, the Swing GUI toolkit was added.
•Java 1.5 (J2SE 5.0 - 2004, code-named "Tiger"): Introduced major
language enhancements like generics, annotations, and auto-
boxing/unboxing.
•Java 1.7 (Java SE 7 - 2011): Added support for dynamic languages
and the "try-with-resources" statement.
•Java 1.8 (Java SE 8 - 2014, code-named "Lambdas"): Introduced
lambda expressions, the Stream API, and other functional
programming features.
9. 9
•Java 9 (Java SE 9 - 2017): Introduced the modular system (Project
Jigsaw), JShell (Java's Read-Evaluate-Print-Loop), and other
enhancements.
•Java 10, 11, 12, 13, and so on: Subsequent versions brought
various improvements and new features to the language.
10. 10
5. OpenJDK and Java Community Process: In November 2006,
Sun Microsystems open-sourced the majority of Java under
the GNU General Public License (GPL), creating the
OpenJDK project. This move allowed developers from the
broader community to participate in the development of
Java.
6. Oracle's Ownership: In January 2010, Oracle Corporation
acquired Sun Microsystems, becoming the new steward of
the Java platform.
7. Continued Development: Oracle has continued to develop
and maintain Java, releasing new versions regularly.
Additionally, the Java Community Process (JCP) allows
other companies and individuals to propose and contribute to
new features and specifications for Java.
12. 12
1. Java Mobile Applications
2. Java Desktop GUI Applications
3. Java Web-based Applications
4. Java Web Servers and Application Servers
5. Java Enterprise Applications
6. Java Scientific Applications
7. Java Gaming Applications
8. Java Big Data Technologies
9. Java Business Applications
10. Java Distributed Applications
11. Java Cloud-based Applications
12. Software Tools
13. Embedded Systems
14. Trading Applications
15. J2ME Apps
16. Server Apps In Financial Industry
13. 13
1.Java Mobile Applications
Java plays a fundamental role in the
mobile app development landscape,
particularly with the widespread usage
of the Android operating system, which
heavily relies on Java. Android phones
consistently dominate the market,
boasting a market share of over 85%.
Additionally, Java serves as the
preferred programming language for
the majority of mobile app developers.
14. 14
2. Java Desktop GUI Applications
GUI stands for Graphical User Interface, and
Java empowers developers to create GUIs
using various resources such as the Abstract
Windowing Toolkit (AWT), JavaFX, and
Swing. AWT provides a collection of pre-
designed components, including buttons,
lists, menus, and additional third-party
components. Swing, on the other hand, is a
GUI widget toolkit that incorporates advanced
elements like lists, scroll panes, tables,
tabbed panels, and trees.
15. 15
3. Java web applications
Java web applications refer to software
applications that are built using the Java
programming language and designed to be
accessed over the internet through web
browsers. These applications are hosted on web
servers and interact with users through web
interfaces.
16. 16
4. Java Web Servers and Application Servers
A Server is a central place where information
and programs are stored and accessed by
applications over the network. A Web Server is
a server which accepts a request for data and
sends the relevant document in return, whereas
an Application Server contains an EJB
container component as well to run the
enterprise applications.
17. 17
5. Java Enterprise Applications
Java Enterprise Edition (Java EE) is widely support as a
favoured platform, providing comprehensive API and
runtime environments for various scripting tasks,
including web services and network applications,
among others. Oracle reports that an impressive 97%
of enterprise-owned computers run on Java.
Additionally, Java has established itself as a
fundamental technology for banking applications,
spanning from front-end user interfaces to back-end
server systems.
18. 18
6. Java Scientific Applications
The field of scientific mathematics demands the
development of applications that excel in speed,
security, maintainability, and portability. Java is a
prime choice for fulfilling these requirements.
Java plays a vital role in powering robust
scientific applications like MATLAB, serving as
an integral component of the core system and
facilitating interactions with user interfaces.
19. 19
7. Java Gaming Applications
Java and the gaming industry form an ideal partnership.
Java provides extensive support for the jMonkey engine,
which stands as one of the most powerful 3D engines in
existence today. Moreover, creating 2D games is a
breeze with Java, especially when combined with CSS.
This dynamic combination ensures that game developers
have all the tools they need, regardless of the type of
game they are designing.
20. 20
8. Java Big Data Technologies
Java is a favored choice among programmers for developing
big data applications, as exemplified by the popularity of
Hadoop, the leading big data toolset, which runs on Java.
Java's object-oriented design, coupled with its portability,
makes it well-suited for big data application development.
However, it is worth noting that there may be performance
challenges when utilizing Java for big data projects.
Some examples of big data applications include managing
NoSQL databases, developing machine learning systems,
and handling document management tasks.
21. 21
9. Java Business Applications
Java EE platform is designed to help developers create large-
scale, multi-tiered, scalable, reliable, and secure network
applications. These applications are designed to solve the
problems encountered by large enterprises.
A business application is a collection of components that provides
a business functionality that you can use internally, externally, or
with other business applications. You can create business
applications of individual components, which are related to each
other.
For example, Order Management, Inventory Management, and
Billing are business applications that might use individual
components such as a Java EE application server, LDAP, and a
database that runs on the Solaris server.
22. 22
10. Java Distributed Applications
It provides framework for development of distributed
applications and is one of the leading platform for the
deployment of service oriented architecture (SOA) based
applications using web services.
Java applications can be divided into following application
tiers.
•Client-tier components run on the client machine.
•Web-tier components run on the Java server.
•Business-tier components run on the Java server.
•Enterprise information system (EIS)-tier software runs
on the EIS server.
23. 23
11. Java Cloud-Based Applications
Cloud Computing refers to the convenient access
to computer resources on demand, without
requiring direct user management. Java has
emerged as a prominent technology for building
cloud applications, offering a cost-effective solution
for IT infrastructure. Moreover, Java serves as a
versatile platform for developing cloud servers and
applications.
24. 24
12. Software Tools
Software development tools, also known as
software tools, play a vital role in assisting
developers with building, debugging, and
maintaining software programs and applications.
Java has a wide range of software tools available.
For instance, popular Integrated Development
Environments (IDEs) like NetBeans, IntelliJ IDEA,
and Eclipse are all examples of Java-based IDEs
that are written and developed using the Java
programming language.
25. 25
13. Embedded Systems
Embedded systems are typically low-level
components that are integral to larger
electromechanical systems. They encompass
processors, microcontrollers, and small chips, among
others. Java serves as a powerful tool for developing
reliable and efficient embedded systems at the lower
levels. Notably, Java can be employed to build well-
performing embedded systems such as Blu-ray discs
and SIM cards, which are widely used and recognized
in the industry.
26. 26
14. Trading Applications
Java is extensively utilized in the development of
highly secure trading applications, including
Murex. Murex stands as one of the foremost
trading applications employed by banks to handle
risk management, treasury operations, and post-
trade operations within financial markets.
Notably, Murex is written in the Java
programming language, showcasing the
language's suitability for building robust and
reliable trading systems.
27. 27
15. J2ME Apps
Currently, the dominant operating systems for mobile phones are
iOS and Android. However, there exist various mobile phones
and personal digital assistants that rely on J2ME (Java 2
Platform, Micro Edition). J2ME is widely employed in the
development of diverse products such as Blu-ray players, set-top
boxes, and smart cards. Furthermore, it is anticipated that the
popularity of J2ME applications will continue to grow in the
coming years.
28. 28
16. Server Apps In Financial Industry
The financial industry relies on a variety of server applications to
support its daily operations, including data processing, trading
systems, and confirmation systems. Java is a popular choice for
developing these server-side applications. Major financial
institutions, such as CitiGroup, Goldman Sachs, and Barclays,
utilize Java-based applications to power their businesses
effectively.
32. Example
class Simple
{
public static void main(String args[])
{
System.out.println("Hello Java");
}
}
save this file as Simple.java
To compile:javac Simple.java
To execute:java Simple
Output:Hello Java 32
33. • class keyword is used to declare a class in java.
• public keyword is an access modifier which represents visibility, it
means it is visible to all.
• 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.
• void is the return type of the method, it means it doesn't return any
value.
• main represents startup of the program.
• String[] args is used for command line argument.
• System.out.println() is used print statement.
33
Understanding first java program
34. Command line argument
import java.util.Scanner;
class userinput
{
public static void main(String args[])
{
int num;
float fnum;
String str;
Scanner in = new Scanner(System.in);
//Get input String
System.out.println("Enter a string: ");
str = in.nextLine();
System.out.println("Input String is: "+str);
34
35. //Get input Integer
System.out.println("Enter an integer: ");
num = in.nextInt();
System.out.println("Input Integer is: "+num);
//Get input float number
System.out.println("Enter a float number: ");
fnum = in.nextFloat();
System.out.println("Input Float number is: "+fnum);
}
}
35
36. Java vs C++: Which Language is
Right for Your Software Project
• Both Java and C++ have been in production for years.
They both have similar syntax, and they both power
some of the biggest enterprise platforms on the market.
As a matter of fact, most C++ programmers will tell you
that converting to a Java project is easy for them since
style and syntax are very similar.
• Even with the similarities, however, the two languages
are worlds apart. Java is an interpreted language—”write
once, run anywhere”—while C++ is a compiled
language. This dissimilarity will play a huge role in your
future project.
36
37. C++ VS. JAVA: MAJOR SIMILARITIES
• Syntax: Looping structures, classes, defining
variables, and conditional operators are very
similar in both languages. This makes it easy for
developers to work cross-platform should you
have several projects that use both languages.
• Entry points: When your program starts, the
compiler or interpreter looks for where it needs
to begin execution. Both Java and C++ look for the
“main” entry point.
37
38. Object-oriented: The idea of object
orientation is that the languages use classes
that represent components of your program.
Each class then contains methods and
properties that define it. Both C++ and Java
are object-oriented languages, which makes
your program much more modular so you can
reuse code for other programs.
38
39. C++ AND JAVA DIFFERENCES
• A common misconception is that if a language is similar to
another, then it must be similar in functionality. While Java
and C++ are similar in syntax, they are far more dissimilar in
the way they execute and process.
• Interpreted vs. compiled: Java is an interpreted language,
which means it is “translated” to binary at the time of
execution. This allows it to run on any operating system
regardless of where it was written. C++ is a compiled language,
which means your program is compiled on a specific operating
system and runs only on that particular operating system. If
you want it compatible with another operating system, you
must compile your program on it.
39
40. • Memory safe: Java is a memory-safe language,
which means if you attempt to assign values outside
of the given array parameters, the programmer
receives an error. C++ is much more flexible, but this
comes at a price. C++ will allow the programmer to
assign values outside of the allocated memory
resources, but this can later cause bugs and serious
crashes during run-time.
• Performance: Java is a favorite among developers,
but because the code must first be interpreted
during run-time, it’s also slower. C++ is compiled to
binaries, so it runs immediately and therefore faster
than Java programs.
40
41. • Pointers: Pointers are a C++ construct that
allows you to manage values directly in
memory spaces. Java does not support
pointers, so you are only able to pass values
using value references.
• Overloading: Overloading is a concept that
“redefines” the functionality of a method or
operator. Java allows method overloading
while C++ allows you to overload operators.
41
42. WHICH ONE IS RIGHT FOR YOUR PROJECT?
• Both Java and C++ can be used to create a wide variety
of programs. However, the language you use is
determined by what you want developed.
• C++ is generally reserved for software that needs
“hardware-level” manipulation. One difference
between C++ and Java is that C++ is closest to machine
language, which makes it much more viable for
software that needs to run quickly and requires the
ability to work directly with your computer’s memory,
hard drive, CPU or other devices. C++ is also common
with gaming applications where speed is necessary.
42
43. • You can manipulate hardware with Java, but it’s not
a common language for low-level programming
since it’s a “safer” language. Because Java won’t
allow you to perform certain functions to protect the
PC, it’s preferred for higher level applications.
• Java is the foundation for Android development, so
if you want a mobile application specifically for
Android, then Java will be your language of choice.
Java is also common for web and desktop apps as
well as applications that run on servers. Java is more
widely known and versatile, so it’s also easier to find
a Java developer than a “harder” language such as
C++.
43
44. • Overall, C++ can be used for almost anything but it’s
not always necessary to use it. Java is usually
sufficient and can be much more effective for your
project. You can find more developers who know
Java, and you’ll be able to find more developers to
pick up where your former developer left off if you
part ways.
• The best way to make a firm decision is to post your
project and ask developers for their opinions. They
can tell you which language is right for your project
to help guide you to the right solution.
44
45. 45
class addition
{
public static void main(String[] args)
{
int first = 10;
int second = 20;
// add two numbers
int sum = first + second;
System.out.println("Sum = " + sum);
}
}
46. Java, Web, and Beyond
• Java can be used to develop Web
applications.
• Java Applets
• Java can also be used to develop applications
for hand-held devices such as cell phones
46
47. Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic
47
48. Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic
48
Java is very easy to learn, and its
syntax is simple, clean and easy
to understand. According to Sun
Microsystem, Java language is a
simple programming language
because:
Java syntax is based on C++
(so easier for programmers to
learn it after C++).
Java has removed many
complicated and rarely-used
features, for example, explicit
pointers, operator overloading,
etc.
There is no need to remove
unreferenced objects because
there is an Automatic Garbage
Collection in Java.
49. Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic
49
Java is an object-oriented programming
language. Everything in Java is an object.
Object-oriented means we organize our
software as a combination of different
types of objects that incorporate both data
and behavior.
Object-oriented programming (OOPs) is a
methodology that simplifies software
development and maintenance by
providing some rules.
Basic concepts of OOPs are:
1.Object
2.Class
3.Inheritance
4.Polymorphism
5.Abstraction
6.Encapsulation
50. Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic
50
Java is distributed because it facilitates
users to create distributed applications in
Java. RMI and EJB are used for creating
distributed applications. This feature of
Java makes us able to access files by
calling the methods from any machine on
the internet.
Distributed computing involves several
computers working together on a network.
Java is designed to make distributed
computing easy. Since networking
capability is inherently integrated into
Java, writing network programs is like
sending and receiving data to and from a
file.
51. Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic
51
You need an interpreter to run
Java programs. The programs are
compiled into the Java Virtual
Machine code called bytecode.
The bytecode is machine-
independent and can run on any
machine that has a Java
interpreter, which is part of the
Java Virtual Machine (JVM).
52. Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic
52
Java is robust because:
It uses strong memory management.
There is a lack of pointers that avoids
security problems.
Java provides automatic garbage
collection which runs on the Java Virtual
Machine to get rid of objects which are not
being used by a Java application anymore.
There are exception handling and the
type checking mechanism in Java. All
these points make Java robust.
53. Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic
53
Java is best known for its security. With Java,
we can develop virus-free systems. Java is
secured because:
No explicit pointer
Java Programs run inside a virtual
machine sandbox
Classloader: Classloader in Java is a part
of the Java Runtime Environment (JRE)
which is used to load Java classes into the
Java Virtual Machine dynamically. It adds
security by separating the package for the
classes of the local file system from those that
are imported from network sources.
Bytecode Verifier: It checks the code
fragments for illegal code that can violate
access rights to objects.
Security Manager: It determines what
resources a class can access such as reading
and writing to the local disk.
54. Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic
54
Write once, run anywhere
With a Java Virtual Machine (JVM),
you can write one program that will
run on any platform.
Java is architecture neutral because there
are no implementation dependent features,
for example, the size of primitive types is
fixed.
In C programming, int data type
occupies 2 bytes of memory for 32-bit
architecture and 4 bytes of memory for 64-
bit architecture. However, it occupies 4
bytes of memory for both 32 and 64-bit
architectures in Java.
55. Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic
55
Java is portable because
it facilitates you to carry
the Java bytecode to any
platform. It doesn't require
any implementation.
Because Java is
architecture neutral, Java
programs are portable.
They can be run on any
platform without being
recompiled.
56. Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic
56
Java is faster than other
traditional interpreted
programming languages (PHP,
Ruby, Python, and JavaScript )
because Java byte code is
"close" to native code. It is still
a little bit slower than a
compiled language (e.g., C++).
Java is an interpreted language
that is why it is slower than
compiled languages, e.g., C, C+
+, etc.
57. Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic
57
A thread is like a separate
program, executing
concurrently. We can write
Java programs that deal with
many tasks at once by
defining multiple threads.
The main advantage of multi-
threading is that it doesn't
occupy memory for each
thread. It shares a common
memory area. Threads are
important for multi-media,
Web applications, etc.
58. Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic
58
Java is a dynamic language. It
supports the dynamic loading of
classes. It means classes are
loaded on demand. It also
supports functions from its
native languages, i.e., C and C+
+.
Java supports dynamic
compilation and automatic
memory management (garbage
collection).
59. #include <iostream>
int main()
{
cout << "Hello C++ Programming";
return 0;
}
class Simple{
public static void main(String args[]
)
{
System.out.println("Hello Java");
}
}
59
60. 60
Java Classes
A class in Java is a set of objects which shares common characteristics/ behavior and
common properties/ attributes. It is a user-defined blueprint or prototype from which
objects are created. For example, Student is a class while a particular student named
Ravi is an object.
Properties of Java Classes
Class is not a real-world entity. It is just a template or blueprint or prototype from which
objects are created.
Class does not occupy memory.
Class is a group of variables of different data types and a group of methods.
A Class in Java can contain:
Data member
Method
Constructor
Nested Class
Interface
61. 61
Class Declaration in Java
access_modifier class
<class_name>
{
data member;
method;
constructor;
nested class;
interface; }
62. 62
class Student
{
// data member (also instance variable)
int id;
// data member (also instance variable)
String name;
public static void main(String args[])
{
// creating an object of Student
Student s1 = new Student();
s1.id;
s1.name;
}
}
63. 63
Java Objects
An object in Java is a basic unit of Object-Oriented
Programming and represents real-life entities. Objects are the
instances of a class that are created to use the attributes and
methods of a class. A typical Java program creates many objects,
which as you know, interact by invoking methods. An object consists
of :
State: It is represented by attributes of an object. It also reflects the
properties of an object.
Behavior: It is represented by the methods of an object. It also
reflects the response of an object with other objects.
Identity: It gives a unique name to an object and enables one object
to interact with other objects.
64. 64
In how many ways we can write a Java program?
There are many ways to write a Java program. The
modifications that can be done in a Java program are given
below:
1)By changing the sequence of the modifiers, method
prototype is not changed in Java.
static public void main(String args[])
2) The subscript notation in the Java array can be used
after type, before the variable or after the variable.
public static void main(String[] args)
public static void main(String []args)
public static void main(String args[])
65. 65
3) You can provide var-args support to the main() method
by passing 3 ellipses (dots)
public static void main(String... args)
4) Having a semicolon at the end of class is optional in
Java.
class A
{
static public void main(String... args)
{
System.out.println("hello java4");
}
};
66. 66
Valid Java main() method signature
public static void main(String[] args)
public static void main(String []args)
public static void main(String args[])
public static void main(String... args)
static public void main(String[] args)
public static final void main(String[] args)
final public static void main(String[] args)
67. 67
Invalid Java main() method signature
public void main(String[] args)
static void main(String[] args)
public void static main(String[] args)
abstract public static void main(String[] args)
68. JDK Editions
• Java Standard Edition (J2SE)
– J2SE can be used to develop client-side standalone
applications or applets.
• Java Enterprise Edition (J2EE)
– J2EE can be used to develop server-side applications
such as Java servlets and Java ServerPages.
• Java Micro Edition (J2ME).
– J2ME can be used to develop applications for mobile
devices such as cell phones.
68
69. JVM
• JVM (Java Virtual Machine) is an abstract machine. It is a
specification that provides runtime environment in which java byte
code can be executed.
• JVMs are available for many hardware and software platforms.
The JVM performs the following main tasks:
• Loads code
• Verifies code
• Executes code
• Provides runtime environment
69
70. JVM provides definitions for the:
• Memory area
• Class file format
• Register set
• Garbage-collected heap
• Fatal error reporting etc.
70
72. 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.
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.
72
73. Execution Engine
It contains:
1)A virtual processor
2)2) Interpreter: Read byte code stream then execute
the instructions.
3)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.
73
74. 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 are also actively released
by other companies besides Sun Micro Systems.
74
77. Creating and Editing Using NotePad
To use NotePad, type
notepad Welcome.java
from the DOS prompt.
77
78. Creating and Editing Using WordPad
To use WordPad, type
write Welcome.java
from the DOS prompt.
78
79. Creating, Compiling, and Running
Programs
79
Source Code
Create/Modify Source Code
Compile Source Code
i.e., javac Welcome.java
Bytecode
Run Byteode
i.e., java Welcome
Result
If compilation errors
If runtime errors or incorrect result
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
…
Method Welcome()
0 aload_0
…
Method void main(java.lang.String[])
0 getstatic #2 …
3 ldc #3 <String "Welcome to
Java!">
5 invokevirtual #4 …
8 return
Saved on the disk
stored on the disk
Source code (developed by the programmer)
Byte code (generated by the compiler for JVM
to read and interpret, not for you to understand)
80. Compiling Java Source Code
You can port a source program to any machine with appropriate
compilers. The source program must be recompiled, however, because
the object program can only run on a specific machine. Nowadays
computers are networked to work together. Java was designed to run
object programs on any platform. With Java, you write the program
once, and compile the source program into a special type of object
code, known as bytecode. The bytecode can then run on any computer
with a Java Virtual Machine, as shown below. Java Virtual Machine is a
software that interprets Java bytecode.
80
Java Bytecode
Java Virtual
Machine
Any
Computer
83. Anatomy of a Java Program
• Comments
• Reserved words
• Modifiers
• Statements
• Blocks
• Classes
• Methods
• The main method
83
84. Comments
Line comment: A line comment is preceded by two
slashes (//) in a line.
Paragraph comment: A paragraph comment is enclosed
between /* and */ in one or multiple lines.
84
javadoc comment: javadoc comments begin with /**
and end with */. They are used for documenting
classes, data, and methods. They can be extracted
into an HTML file using JDK's javadoc command.
Three types of comments in Java.
85. Reserved Words
Reserved words or keywords are words that have a
specific meaning to the compiler and cannot be used for
other purposes in the program. For example, when the
compiler sees the word class, it understands that the
word after class is the name for the class. Other
reserved words in Listing 1.1 are public, static, and void.
Their use will be introduced later in the book.
85
86. Modifiers
Java uses certain reserved words called modifiers
that specify the properties of the data, methods, and
classes and how they can be used. Examples of
modifiers are public and static. Other modifiers are
private, final, abstract, and protected. A public data,
method, or class can be accessed by other programs.
A private data or method cannot be accessed by
other programs.
86
87. Statements
A statement represents an action or a sequence of
actions. The statement System.out.println("Welcome to
Java!") in the program the statement to display the
greeting "Welcome to Java!" Every statement in Java
ends with a semicolon (;).
87
88. Blocks
88
A pair of braces in a program forms a block that groups
components of a program.
public class Test {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Class block
Method block
89. Classes
The class is the essential Java construct. A class is a
template or blueprint for objects. To program in Java,
you must understand classes and be able to write and
use them.
89
90. Methods
What is System.out.println? It is a method: a collection
of statements that performs a sequence of operations to
display a message on the console. It can be used even
without fully understanding the details of how it works.
It is used by invoking a statement with a string
argument. The string argument is enclosed within
parentheses. In this case, the argument is "Welcome to
Java!" You can call the same println method with a
different argument to print a different message.
90
91. main Method
The main method provides the control of program flow.
The Java interpreter executes the application by
invoking the main method.
The main method looks like this:
public static void main(String[] args) {
// Statements;
}
91