Download Unit-1 Introduction to Java

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Unit-1 Introduction to Java
Basics of Java programming
Introduction
James Gosling is a famous Canadian software developer who has been with Sun
Microsystems since 1984 and is considered as father of Java programming
language. Gosling did the original design of Java and implemented its original
compiler and virtual machine.
History of Java
Features of Java (CHARACTERISTICS OF JAVA)
Java and C
 Java is a lot like C but the major difference between Java and C is that Java is an










object-oriented language and mechanism to define classes and objects.
Java does not include the C unique statement keywords goto, sizeof, and
typedef.
Java does not contain the data types struct, union and enum.
Java does not define the type modifiers keywords auto, extern, register, signed
and unsigned.
Java does not support an explicit pointer type.
Java does not have a preprocessor and therefore we cannot use # define,
#include, and # ifdef statements.
Java does not support any mechanism for defining variable arguments to functions.
Java requires that the functions with no arguments must be declared with empty
parenthesis and not with the void keyword as done in C.
Java add new operators such as instanceof and >>>.
Java adds labelled break and continue statements.
Java adds many features required for object-oriented programming.
Java and C++
 Java is a true object-oriented language while C++ is basically C with object







oriented extension.
Listed below are some major c++ features that were intentionally committed
from Java or significantly modified.
Java does not support operator overloading.
Java does not have temple classes as in C++.
Java does not support multiple inheritence of classes. This is accomplished using a
new feature called "interface".
Java does not support global variables. Every variable and method is declared
within a class and forms parts of that class.
Java does not use pointers.
Java has replaced the destructors function with a finalize() function.
There are no header files in Java.
Java compared to C++
Toolkits and platforms

Below Figure describes all major releases of Java to date starting with version 1.0 and ending with
version 1.4.
Applications, applets, and servlets
 First, you can use Java to create applications. This figure shows an application that uses a
graphical user interface, or GUI, to get user input and perform a calculation. Application
is a program that run in window.
 Applet is a special type of program that runs within web browser after it has been
retrieved from internet or intranet. One of the unique characteristics of Java is that you
can use it to create a special type of web-based application known as an applet. For
instance, this figure shows an applet that works the same way as the application above it.
The main difference between an application and an applet is that an applet can be stored
in an HTML page and can run inside a Java-enabled browser. As a result, you can
distribute applets via the Internet or an intranet.
 The Enterprise Edition of the Java 2 Platform can be used to create a special type of
server-side application known as a servlet. Servlets can access enterprise databases and
make that data available via the web.
Java and Internet
 Java is strongly associated with the internet because of the
fact that the first application program written in java was
HotJava, a Web browser to run applets on Internet. Internet
users can use Java to create applet programs and run them
locally using a "Java-enabled browser" such as HotJava.
 Internet users can also set up their Web sites containing Java
applets that could be used by Superhighway has made Java a
unique programming language for the Internet. In fact, due
to this, Java is popularly known as internet language.
Java and World Wide Web
 World Wide Web (WWW) is an open-ended information retrieval system designed to be used in
the Internet’s distributed environment. This system contains what are known as Web page that
provide both information and controls.
 Unlike a menu driven system where we are guided through a particular direction using a decision
tree structure, the Web system is open-ended and we can navigate to a new documents in any
direction as shown in fig. 2.3. This is made possible with the help of a language called Hypertext
Markup Language (HTML). Web pages contain HTML tags that enable us to find, retrieve,
manipulate and display documents worldwide.
 Java was meant to be used in distributed environments such as Internet. Since, both the web and
Java share the same philosophy, Java could be easily incorporated into the Web and systems. Before
Java , the World Wide Web Was limited to the display of still images and texts. However, the
incorporation of Java into Web pages has made it capable of supporting animation, graphics, games
and a wide a range of special effects. With the support of Java, the web has become more interactive
and dynamic. On the other hand, with the support of Web, we can run a Java program on someone
else’s computer across the Internet.
 Java communicates with a Web page through a special tag called <APPLET>.
 The figure shows the following communications steps:
 1. The user sends a request for an HTML document to the remote computer’s




Web server. The Web server is a program that accepts a request, processes the
request, and sends the required documents.
2. The HTML document is returned to the user’s browser. The document
contains the APPLET tag, which identifies the applet.
3. The corresponding applet bytecode is transferred to the user’s computer. This
bytecode had been previously created by the Java compiler using the Java source
code file for that applet.
4. The Java-enabled browser on the user’s computer interprets the bytecodes
and provides output.
5. The user may have further interaction with the applet but with no further
downloading from the provider’s Web server. This is because the bytecode
contains all the information necessary to interpret the applet.
Web Browsers
 They allow us to retrieve the information spread across the
Internet and display it using the hypertext markup language
(HTML). Example ofWeb browser, among others, include:
 • HotJava
 • Netscape Navigator
 • Internet Explorer
 HotJava
 HotJava is the Web browser from Sun Microsystems that enables
the display of interactive content on the Web, using the Java
language. HotJava is writing entirely in Java and demonstrates the
capabilities of the Java programming language.
 When the Java language was first developed and ported to the
Internet, no browsers were available that could run Java applets.
Although we can view a Web page that includes Java applets with a
regular browser, we will not gain any of Java’s benefits. HotJava is
currently available for SPARC/Solaris platform as well windows
95 and Window NT. Its biggest draw is that was the biggest Web
browser to provide support for the Java language,
 thus making theWeb more dynamic and interactive.
Netscape Navigator
 Netscape
Navigator, from Netscape Communications
Corporation, is a general-purpose browser that can run Java
applets. With versions available for Windows 95, NT, Solaris
and Apple Macintosh, Netscape navigator is one of the most
widely used browsers today.
 Netscape Navigator has many useful features such as visual
display about downloading process and indication of the
number bytes downloaded. It also supports JavaScripts, a
scripting language used in HTML documents.
Internet Explorer
 Internet Explorer is another popular browser developed
Microsoft for Window 95 and NT Workstations. Both the
Navigator and Explorer use tool bars, icons, menus and
dialog boxes for easy navigation. Explorer uses a just-in-time
(JIT) compiler which greatly increases the speed of
execution.
Java Support Systems
 Table 2.2 list the systems necessary to support Java for
delivering information on the Internet.
Java Environment
 Java environment includes a large number of development
tools and hundreds of classes and methods.
 The development tools are part of the system known as Java
Development Kit (JDK) and the classes and methods are
parts of the Java Standard Library (JSL), also known as the
Application Programming Interface (API).
Java Developing Kit
 The Java Developing Kit comes with a collection of tools that
are used for developing and running Java programs. They
includes:
table 2.3 list these tools and their descriptions.
 The way these tools are applied to build and run application programs is




illustrated in fig .2.1.
To create a Java program, we need to create a source code file using a
text editor.
The source code is then compiled using the Java compiler Javac and
executed using the Java interpreter Java.
The Java debugger jdk is used to find errors, if any, in the source code.
A compiled Java program can be converted into a source code with the
help of Java disassembler Javap.
Application Programming Interface
 The Java Standard Library (or API) includes hundreds of classes and






methods grouped into several functional packages. Most commonly
used packages are:
• Language Support Package: A collection of classes and methods
required for implementing basic features of Java.
• Utilities Packages: A collection of classes to provide utility
functions such as date and time functions.
• Input/Out Package: A collection of classes required for input/
output manipulation.
• Networking Package: A collection of classes for communicating
with other computers via Internet.
• AWT Package:The Abstract Window Tools Kit packages contains
classes that implements platform-independent graphical user interface.
• Applet Package:This includes a set of classes that allows us to
create Java applets.
Advantages of Using Java
 Java technology will help you do the following:
 Get started quickly: Although the Java programming language is a powerful
object-oriented language, it's easy to learn, especially for programmers already
familiar with C or C++.
 Write less code: program written in the Java programming language can be four
times
smaller
than
the
same
program
in
C++.
 Write better code: The Java programming language encourages good coding
practices, and its garbage collection helps you avoid memory leaks. Its object
orientation, its Java Beans component architecture, and its wide-ranging, easily
extendible API let you reuse other people's tested code and introduce fewer bugs.
 Develop programs more quickly: Your development time may be as much as
twice as fast versus writing the same program in C++.
 Avoid platform dependencies with 100% Pure Java: You can keep your
program portable by avoiding the use of libraries written in other languages.
 Write once, run anywhere: Because 100% Pure Java programs are compiled into
machine-independent byte codes, they run consistently on any Java platform.
How Java compiles and interprets code:
 Description
 Any text editor can save and edit the source code for a Java application. Source code files
use the java extension.
 The Java compiler translates source code into a platform-independent format known as
Java bytecodes. Files that contain Java bytecodes use the class extension.
 The Java interpreter executes Java bytecodes. Since Java interpreters exist for all major
operating systems, Java bytecodes can be run on most platforms.
 Any computer with a Java interpreter can be considered an implementation of a Java
virtual machine (JVM).
 Some web browsers like Netscape and the Internet Explorer contain Java interpreters.
This lets applets run within these browsers. However, both Netscape and the Internet
Explorer only provide older versions of the Java interpreter.
 Sun provides a tool known as the Java plug-in that allows the Netscape and Internet
Explorer browsers to use the most current version of the Java virtual machine.
INSTALLATIONOF JDK 1.6
 Show In Laboratory.
A summary of the directories and files of the JDK:
Some Important Term

JRE(Java Runtime Environment):

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.

runs code compiled for a JVM and performs class loading (through the class loader), code verification (through the
bytecodeverifier) and finally code execution.

The jre directory contains the Java interpreter, or Java Runtime nvironment (JRE), that’s needed to run Java applications once
they’ve been compiled.
 Java virtual machine (JVM):
 Any machine that has a Java interpreter installed on it can be
considered an implementation of a Java virtual machine
(JVM).

Class Loader:

responsible for loading all classes needed for the Java program.

Bytecodeverifier:
tests the format of the code fragments and checks the code fragments for illegal code that can violate access
rights to objects.
 Garbage collection thread:


responsible for freeing any memory that can be freed. This happens automatically during the lifetime of the Java
program.

programmer is freed from the burden of having to deallocatethat memory themselves.
 Q: How Code Security Is Maintained In Java?

Ans:Code Security Is maintained By Following Term.
(1)JRE
(2)Class Loader
(3)Bitecodeverifier
JAVAPROGRAMSTRUCTURE:

As all other programming languages, Java also has a structure.

The first line of the C/C++ program contains include statement. For example, <stdio.h> is the header file that
contains functions, like printf (), scanf () etc. So if we want to use any of these functions, we should include this
header file in C/ C++ program.

Similarly in Java first we need to import the required packages. By default java.lang.* is imported. Java has several
such packages in its library. A package is a kind of directory that contains a group of related classes and interfaces.
A class or interface contains methods.

Since Java is purely an Object Oriented Programming language, we cannot write a Java program without having at
least one class or object. So, it is mandatory to write a class in Java program. We should use class keyword for this
purpose and then write class name.

In C/C++, program starts executing from main method similarly in Java, program starts executing from main
method. The return type of main method is void because program starts executing from main method and it
returns nothing.
 Since Java is purely an Object Oriented Programming language, without





creating an object to a class it is not possible to access methods and
members of a class. But main method is also a method inside a class, since
program execution starts from main method we need to call main method
without creating an object.
Static methods are the methods, which can be called and executed without
creating objects.
Since we want to call main () method without using an object, we should
declare main ()method as static.
JVM calls main () method using its Classname.main () at the time of
running the program.
JVM is a program written by Java Soft people (Java development team) and
main () is the
method written by us. Since, main () method should be
available to the JVM, it should be declared as public. If we don’t declare
main () method as public, then it doesn’t make itself available to JVM and
JVM cannot execute it.
JVM always looks for main () method with String type array as parameter
otherwise JVM
cannot recognize the main () method, so we must
provide String type array as parameter to main () method.
 A class code starts with a {and ends with a}. A class or an object





contains variables and methods (functions). We can create any number
of variables and methods inside the class. This is our first program, so
we had written only one method called main ().
Our aim of writing this program is just to display a string “Hello
world”. In Java, print () method is used to display something on the
monitor.
A method should be called by using objectname.methodname (). So,
to call print () method, create an object to PrintStream class then call
objectname.print () method.
An alternative is given to create an object to PrintStream Class i.e.
System.out. Here, System is the class name and out is a static variable
in System class. out is called a field in System class. When we call this
field a PrintStream class object will be created internally. So, we can
call print() method as: System.out.print (“Hello world”);
println () is also a method belonging to PrintStream class. It throws
the cursor to the next line after displaying the result.
In the above Sample program System and String are the classes
present in java.lang package.
Comments
 Comments are description about the aim and features of the
program.
 Comments increase readability of a program.
 Three types of comments are there in Java:
 · Single line comments: These comments start with //
e.g.: // this is comment line
 · Multi line comments: These comments start with /* and end
with */
e.g.: /* this is comment line*/
 · Java documentation comments: These comments start with
/** and end with */
These comments are useful to create a HTML file called API
(application programming Interface) document. This file contains
description of all the features of software.
Java documentation comments(Example)
 Example 3-1: FirstSample.java
/* This is my the first program in Java
I am enjoying Java.
*/
public class FirstSample
{
public static void main (String [] args)
{
System.out.println("Welcome to the world of Java");
}
}
Escape Sequence
 Java supports all escape sequence which is supported by C/
C++.
 A character preceded by a backslash (\) is an escape sequence
and has special meaning to the compiler. When an escape
sequence is encountered in a print statement, the compiler
interprets it accordingly.
How to create identifiers
 As you code a Java program, you need to create and use
identifiers.
keyword
 A keyword is a word that’s reserved by the Java language. As a
result, you can’t use keywords as identifiers.