Uploaded by Patricia Capinpin

Introduction to Java Programming Language

advertisement
What is Java?
• Java is an object-oriented programming language.
• Created in 1991 by James Gosling of Sun Microsystems.
Initially called Oak, in honor of the tree outside Gosling's window, its name was changed to Java
because there was already a language called Oak.
• The fastest growing programming language in the history of computing
• Originally designed in 1991 for use in embedded consumer applications
• Redesigned in early 1995 with Internet application capabilities
• Introduced in May, 1995 and immediately supported by the Netscape Web browser
• Rapidly overtaking C++ in popularity among commercial software developers
• Threatened by Microsoft's .NET initiative and C# programming language. The future is uncertain
as the legal battle between Microsoft and Sun Microsystems may last for years.
Benefits of Java
• Architecturally neutral. Once written, the same Java program can be run on any platform
(computer and operating system) that supports Java.
• Entirely object-oriented. Existing code can be easily re-used and maintained.
• Secure. Dangerous program actions are prohibited.
• Supports Internet programming. Java applets are run by Web browsers.
• Robust. Language features and packaged code support graphical programming, exception
handling, networking, multi-threading, I/O, and more.
Java Technology
• A programming language
As a programming language, Java can create all kinds of applications that you could create
using any conventional programming language.
• A development environment
As a development environment, Java technology provides you with a large suite of tools: a
compiler, an interpreter, a documentation generator, a class file packaging tool, and so on.
• An application environment
Java technology applications are typically general-purpose programs that run on any machine
where the Java runtime environment (JRE) is installed.
• A deployment environment
There are two main deployment environments: First, the JRE supplied by the Java 2
Software Development Kit (SDK) contains the complete set of class files for all the Java
technology packages, which includes basic language classes, GUI component classes, and so on.
The other main deployment environment is on your web browser. Most commercial browsers
supply a Java technology interpreter and runtime environment.
Features of Java
• The Java Virtual Machine
The Java Virtual Machine is an imaginary machine that is implemented by emulating software
on a real machine. The JVM provides the hardware platform specifications to which you compile
all Java technology code. This specification enables the Java software to be platformindependent because the compilation is done for a generic machine known as the JVM.
A bytecode is a special machine language that can be understood by the Java Virtual
Machine (JVM). The bytecode is independent of any particular computer hardware, so any
computer with a Java interpreter can execute the compiled Java program, no matter what type
of computer the program was compiled on.
• Garbage Collection
Many programming languages allows a programmer to allocate memory during runtime.
However, after using that allocated memory, there should be a way to deallocate that memory
block in order for other programs to use it again. In C, C++ and other languages the
PSU-Lingayen Campus
jmcervantes
programmer is responsible for this. This can be difficult at times since there can be instances
wherein the programmers forget to deallocate memory and therefore result to what we call
memory leaks.
In Java, the programmer is freed from the burden of having to deallocate that memory
themselves by having what we call the garbage collection thread. The garbage collection
thread is responsible for freeing any memory that can be freed. This happens automatically
during the lifetime of the Java program.
• Code Security
Code security is attained in Java through the implementation of its Java Runtime
Environment (JRE). The JRE runs code compiled for a JVM and performs class loading
(through the class loader), code verification (through the bytecode verifier) and finally code
execution.
The Class Loader is responsible for loading all classes needed for the Java program. It adds
security by separating the namespaces for the classes of the local file system from those that
are imported from network sources. This limits any Trojan horse applications since local classes
are always loaded first. After loading all the classes, the memory layout of the executable is
then determined. This adds protection against unauthorized access to restricted areas of the
code since the memory layout is determined during runtime.
After loading the class and layouting of memory, the bytecode verifier then tests the format of
the code fragments and checks the code fragments for illegal code that can violate access rights
to objects.
After all of these have been done, the code is then finally executed.
Advantages of Java
• Java is simple
No language is simple, but Java considered a much simpler and easy to use object-oriented
programming language when compared to the popular programming language, C++. Partially
modeled after C++, Java has replaced the complexity of multiple inheritance in C++ with a
simple structure called interface, and also has eliminated the use of pointers.
The reason that why Java is much simpler than C++ is because Java uses automatic memory
allocation and garbage collection where else C++ requires the programmer to allocate memory
and to collect garbage. Also, the number of language constructs in Java is small for such a
powerful language. The clean syntax makes Java programs easy to write and read.
• Java is Object-oriented
Object-oriented programming models the real world. Everything in the world can be modeled as
an object. Java is object-oriented because programming in Java is centered on creating objects,
manipulating objects, and making objects work together.
Object-oriented programming provides greater flexibility, modularity and reusability. For years,
object-oriented technology has been perceived as an elitist, requiring substantial investments in
training and infrastructure. Java has helped object-oriented technology enter the mainstream of
computing, with its simple and clean structure that allows the programmer to write easy to read
and write programs.
• Java is Distributed
Distributed computing involves several computers on a network working together. Java is
designed to make distributed computing easy with the networking capability that is inherently
integrated into it. Writing network programs in Java is like sending and receiving data to and
from a file.
• Portability: Program once, Run anywhere (Platform Independence)
PSU-Lingayen Campus
jmcervantes
One of the most compelling reasons to move to Java is its platform independence (Java
programs can run on any platform without having to be recompiled). Java runs on most major
hardware and software platforms, including Windows 95 and NT, the Macintosh, and several
varieties of UNIX. Java applets are supported by all Java-compatible browsers. By moving
existing software to Java, you are able to make it instantly compatible with these software
platforms. JAVA programs become more portable. Any hardware and operating system
dependencies are removed.
• Java is Interpreted
An interpreter is needed in order to run Java programs. The programs are compiled into Java
Virtual Machine code called bytecode. The bytecode is machine independent and is able to run
on any machine that has a Java interpreter. Normally, a compiler will translate a high-level
language program to machine code and the code is able to only run on the native machine. If
the program is run on other machines, the program has to be recompiled on the native machine.
For example, if you compile a C++ program in Windows, the executable code that is generated
by the compiler can only be run on a Windows platform. With Java, the program need only be
compiled once, and the bytecode generated by the Java compiler can run on any platform.
• Security
Java is one of the first programming languages to consider security as part of its design. The
Java language, compiler, interpreter, and runtime environment were each developed with
security in mind. The compiler, interpreter, and Java-compatible browsers all contain several
levels of security measures that are designed to reduce the risk of security compromise, loss of
data and program integrity, and damage to system users.
• Reliability
Security and reliability go hand in hand. Security measures cannot be implemented with any
degree of assurance without a reliable framework for program execution. Java provides multiple
levels of reliability measures, beginning with the Java language itself.
• Multimedia
The sizzle of JAVA is MULTIMEDIA - Sounds, Images, Graphics and Video. In this growing age of
multimedia, new computers are known as "multimedia ready" with CD/DVD-Rom drives, sound
cards, 3D accelerator cards and other new special sound or graphic technology capabilities.
• Java is Robust
Robust means reliable and no programming language can really assure reliability. Java puts a lot
of emphasis on early checking for possible errors, as Java compilers are able to detect many
problems that would first show up during execution time in other languages. Java eliminates
certain types of programming constructs in other languages that are prone to errors.
• Java is Multithreaded
Multithreaded is the capability for a program to perform several tasks simultaneously within a
program. For instance, downloading a mp3 file while playing the file would be considered
multithreading. In Java, multithreaded programming has been smoothly integrated into it, while
in other languages, operating system-specific procedures have to be called in order to enable
multithreading. Multithreading is especially useful in graphical user interface (GUI) and network
programming. In GUI programming, many things can occur at the same time. For example, a
user is able to listen to a mp3 file and surf the Web at the same time. In network programming,
a server can serve multiple clients at the same time. Multithreading is a necessity in visual and
network programming.
• Java is Architecture Neutral
The most remarkable feature of Java is that it is architecture neutral. Architecture neutral means
that it is platform independent. A Java program can be run on any platform with a Java Virtual
Machine. Many major operating system vendors have adopted the Java Virtual Machine, and
soon Java will be able to run on all machines. Java's initial success lies in its Web programming
capability. Java applets can be run from a Web browser, but Java is in actual, more than just a
Web applet. Stand-alone Java applications can also be run directly from operating systems using
a Java interpreter. Using Java, the developers need to only to write one version, and this one
version will be able to run on all of the platforms.
PSU-Lingayen Campus
jmcervantes
• Java is Dynamic
The Java programming language was designed to adapt to an evolving environment. New
methods and properties can be added freely in a class without affecting their clients. Also, Java
is able to load classes as needed at runtime. As an example, you have a class called 'Square'.
This class has a property to indicate the color of the square, and a method to calculate the area
of the square. You can add a new property to the 'Square' class to indicate the length and width
of the square, and a new method to calculate the perimeter of the square, and the original client
program that uses the 'Square' class remains the same.
How Java works
•
To develop and distribute a Java program
o Programmer codes Java source statements
o Java compiler converts source statements into bytecode (platform-independent object
language)
o Bytecode is copied to the target platform
•
To execute a Java program
o Platform-dependent Java Virtual Machine (JVM) software must be installed
o A copy of the JVM is started
o Each bytecode statement is interpreted (translated) by the JVM into platform-dependent
machine language and executed under JVM control
Java source
statements
->
Java compiler ->
Bytecode
Phases of a Java Program
Task
Write the program
Compile the program
Tool to use
Any text editor
Java Compiler
Run the program
Java Interpreter
--->
Platform
Java
Virtual
Machine
Output
File with .java extension
File with .class extension
(Java bytecodes)
Program Output
Sample Java Program
import javax.swing.JOptionPane;
public class Hello {
public static void main( String args[] )
{
String name;
name=JOptionPane.showInputDialog("Enter your name");
JOptionPane.showMessageDialog( null, "Hello "+name + "\nWelcome to Java Programming!" );
}
System.exit( 0 );
}
PSU-Lingayen Campus
jmcervantes
Download