You are on page 1of 37

INTRODUCTION TO JAVA

JAVA
• Java is related to C++, which is a direct
descendant of C.
• Much of the character of Java is inherited
from these two languages.
• From C, Java derives its syntax.
• Many of Java’s object-oriented features were
influenced by C++.
JAVA
• Java was conceived by James Gosling, Patrick
Naughton, Chris Warth, Ed Frank, and Mike
Sheridan at Sun Microsystems, Inc. in 1991.
• This language was initially called “Oak,” but
was renamed “Java” in 1995.
JAVA
The primary motivation for Java

1. Need for a platform-independent (that is,


architecture-neutral) language
2. To create software to be embedded in
various consumer electronic devices, such as
microwave ovens and remote controls.
Java Applets

1. An applet is a special kind of Java program


2. It is designed to be transmitted over the
Internet and automatically executed by a
Java-compatible web browser.
3. An applet is downloaded on demand,
without further interaction with the user.
Java Applets
4. Applets are intended to be small programs.
5. They are typically used to display data
provided by the server, handle user input, or
provide simple functions
6. Example: A loan calculator, that execute
locally, rather than on the server.
7. The applet allows some functionality to be
moved from the server to the client.
Security
• Every time you download a “normal” program,
there is a risk, because the code might
contain a virus, Trojan horse, or other harmful
code.
• The ability to download applets with
confidence that no harm will be done and that
no security will be breached is considered to
be the single most innovative aspect of Java.
Portability

• Portability is a major aspect of the Internet


because there are many different types of
computers and operating systems connected
to it.
• If a Java program is to be run on virtually any
computer connected to the Internet, there
needed to be some way to enable that
program to execute on different systems.
Bytecode
• The output of a Java compiler is not executable
code.
• It is called bytecode
• Bytecode is a highly optimized set of instructions
designed to be executed by the Java run-time
system called the Java Virtual Machine (JVM).
• JVM was designed as an interpreter for
bytecode.
Bytecode
• Although Java was designed as an interpreted
language, there is nothing about Java that
prevents on-the-fly compilation of bytecode
into native code in order to boost
performance.
• Sun provided HotSpot technology that
provides a Just-In-Time (JIT) compiler for
bytecode.
servlet
• A servlet is a small program that executes on
the server.
• Just as applets dynamically extend the
functionality of a web browser, servlets
dynamically extend the functionality of a web
server.
• Servlets are used to create dynamically
generated content that is then served to the
client
The Java Buzzwords

• Simple
• Secure
• Portable
• Object-oriented
• Robust
• Multithreaded
• Architecture-neutral
• Interpreted
• High performance
• Distributed
• Dynamic
Robust

• The ability to create robust programs was


given a high priority in the design of Java.
• Consider two of the main reasons for program
failure:
1. memory management mistakes
2. mishandled exceptional conditions.
Memory management
• It can be a difficult, tedious task in traditional
programming environments.
• Java virtually eliminates these problems by
managing memory allocation and de-
allocation.
• De-allocation is completely automatic,
because Java provides garbage collection for
unused objects.
Exceptional conditions
• Exceptional conditions - such as
– division by zero
– “file not found”
• Java helps by providing object-oriented
exception handling.
Multithreaded

• Java was designed to meet the real-world


requirement of creating interactive,
networked programs.
• To accomplish this, Java supports
multithreaded programming, which allows
you to write programs that do many things
simultaneously.
Multithreaded
• The Java run-time system comes with an
elegant yet sophisticated solution for
multiprocess synchronization that enables you
to construct smoothly running interactive
systems.
Architecture-Neutral

• A central issue for the Java designers was that


of code longevity and portability.
• One of the main problems facing
programmers is that no guarantee exists that
if you write a program today, it will run
tomorrow—even on the same machine.
Architecture-Neutral
• Operating system upgrades, processor
upgrades, and changes in core system
resources can all combine to make a program
malfunction.
• The Java Virtual Machine is the solution to
this situation. Their goal was
“write once; run anywhere, any time, forever.”
Quiz
• Define Byte code.
• What is a compiler?
• What is an interpreter?
• Discuss different features of JAVA.
Compiling and running of
sample programs in JAVA
Java SE 6

• The newest release of Java is called Java SE 6,


and the material in this book has been
updated to reflect this latest version of Java.
• With the release of Java SE 6, Sun once again
decided to change the name of the Java
platform.
• First, notice that the “2” has been dropped.  
Java SE 6
• Thus, the platform now has the name Java SE,
and the official product name is Java Platform,
Standard Edition 6.
• As with J2SE 5, the 6 in Java SE 6 is the product
version number.
• The internal, developer version number is 1.6.
Netbeans IDE 8.0.2
• The software Netbeans IDE 8.0.2 will be used
in this course.
• NetBeans IDE 8.0.2 provides out-of-the-box
code analyzers and editors for working with
the latest Java 8 technologies
– Java SE 8
– Java SE Embedded 8
– Java ME Embedded 8.
A First Simple Program

• let’s look at some actual Java programs.


• Let’s start by compiling and running the short
sample program shown here.
• As you will see, this involves a little more work
than you might imagine.
LTC: A First Simple Program
/*
This is a simple Java program.
Call this file "Example.java".
*/
class Example {
// Your program begins with a call to main().
public static void main(String args[]) {
System.out.println("This is a simple Java program.");
}
}
Entering the Program

• Entering the Program


• In Java, a source file is officially called a
compilation unit.
• It is a text file that contains one or more class
definitions.
• The Java compiler requires that a source file
use the .java filename extension.
Entering the Program

• The name of the class defined by the program is


also Example.
• All code must reside inside a class.
• By convention, the name of that class should
match the name of the file that holds the
program.
• The capitalization of the filename matches the
class name. The reason for this is that Java is
case-sensitive.
Compiling a JAVA program.
• The javac compiler creates a file called
Example.class
that contains the bytecode version of the program.
• The Java bytecode is the intermediate
representation of the program that contains
instructions the Java Virtual Machine will execute.
• Save the program.
• Click on
To run the program
• Go to file and then go to src.
• Then you find Example.java.
• Right click on Example.java.
• Then you will find run file.
• Click on it. Then your program will be
executed.
• You can see the output at the bottom of your
program.
Display of output
• When the program is run, the following
output is displayed:

This is a simple Java program.


Explanation
class Example {
• This line uses the keyword class to declare
that a new class is being defined.
• Example is an identifier that is the name of
the class.
• The entire class definition, including all of its
members, will be between the opening curly
brace ({) and the closing curly brace (}).
Explanation

public static void main(String args[]) {


• This line begins the main( ) method.
• The keyword public is an access specifier,
which allows the programmer 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.
Explanation
• The keyword 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 Virtual Machine before any objects are
made.
• The keyword void simply tells the compiler
that main( ) method does not return a value.
Explanation
• In main( ), there is only one parameter-
String args[ ]
declares a parameter named args, which is an
array of instances of the class String.
• Objects of type String store character strings.
• In this case, args receives any command-line
arguments present when the program is
executed.
Explanation
System.out.println("This is a simple Java
program.");
• Notice that it occurs inside main( ).
• This line outputs the string
“This is a simple Java program.”
followed by a new line on the screen.
Output is actually accomplished by the built-in
println( ) method.
• In this case, println( ) displays the string which
is passed to it.
• The line begins with System.out.
– System is a predefined class that provides
access to the system
– out is the output stream that is connected
to the console.
• The first } in the program ends main( )
method, and the last } ends the Example class
definition.

End of Class

You might also like