NEW JAVA UNIT1

advertisement
JAVA PROGRAMMING
SUBJECT
CLASS
UNIT
SEMESTER
STAFF
NASC-BCA
: JAVA PROGRAMMING
: III BCA
: I
:5
: S.SAVITHA & N.P.SHIJU
UNIT-I
Fundamentals of Object-Oriented Programming: Object-Oriented Paradigm – Basic
Concepts of Object-Oriented Programming – Benefits of Object-Oriented Programming –
Application of Object-Oriented Programming. Java Evolution: History – Features – How Java
differs from C and C++ – Java and Internet – Java and www –Web Browsers. Overview of Java:
simple Java program – Structure – Java Tokens – Statements – Java Virtual Machine.
PART-A
1. Object Oriented Programming is a method of implementation.
2. All programs consists of two elements code and data.
3. The most popular applications of object-oriented programming has been in the area of user
interface design
4. Java does not contain the data types struct and union.
5. void specifies that the method main() doesn’t return any value
6. Identifiers are used for class names, method names and variable names.
7. Reserved words are special identifiers.
8. Period is used to separate package names .
9.Write a Simple Java Program
SIMPLE JAVA PROGRAM
A simple Java program can be as follows
class Sample
{
public static void main(String args[ ])
{
System.out.println(“Welcome”);
}
}
JAVA PROGRAMMING
NASC-BCA
10.Define STRUCTURE OF JAVA
Documentation Section
Package section
Import statements
Interface statements
Class definition
Main method class
{ main method definition
}
PART-B
1. What are the FUNDAMENTALS OF OBJECT ORIENTED PROGRAMMING.
Object Oriented Programming is a method of implementation in which programs are organized
as a collection of objects each of which represents an instance of some class.
Principles of Object-Oriented Programming

Software Evolution
Many programming approaches have been into play from the time of inventions of the
computer. These techniques include modular programming, top-down programming, bottom-up
programming and structured programming. The primary motivation in each case has been the
concern to handle the increasing complexity of programs that are reliable and maintainable.
To build today’s complex software, it is just enough to put together a sequence of
programming statements and set of procedures and modules but to incorporate sound
construction techniques and program structures that are easy to comprehend, implement and
modify. C, structured programming was a powerful tool that enabled programmers to write
moderately complex programs fairly easily. However, as the programs grew, even the structured
JAVA PROGRAMMING
NASC-BCA
approach failed to show the desired results in terms of bug-free, easy-to-maintain and reusable
programs.
OOP is an approach to program organization and development that
attempts to eliminate some of the pitfalls of conventional programming methods by
incorporating the best of structured features with several powerful new concepts.
2. Note on PROCEDURE ORIENTED PARADIGM
Characteristics of Procedure-Oriented Programming.






Emphasis is on during things(algorithms).
Large programs are divided into smaller programs known as functions.
Most of the functions share global data.
Data move openly around the system from function to function.
Functions transform data from one form to another.
Employs top-down approach.
3. Note on OBJECT-ORIENTED LANGUAGES
Object oriented programming languages are classified into two types based on the
concepts they support. The two types are
 Object-based languages
 Object-oriented languages
Object-based languages is that style of programming that supports encapsulation and object
identity. The other features available in this type are Data hiding, Automatic initialization
and clear up of objects, operator overloading. It also supports programming with objects. The
two features that are not supported by object-based languages are dynamic binding and
inheritance. E.g. Ada
Object-oriented languages incorporates in it features like data hiding, encapsulation,
operator overloading, automatic initialization and clear-up of objects, along with inheritance and
dynamic binding. E.g. C++, Object Pascal, Small Talk
In general,
Object-oriented language=Object-based + inheritance + Dynamic binding
4. What are the BENEFITS OF OOPS
JAVA PROGRAMMING
NASC-BCA
This new technology promises greater productivity, better quality software and lesser
maintenance cost. Other advantages are








Existing classes can be extended using inheritance that avoids redundant code.
Data hiding helps the programmer to build secure programs and so it cannot be
invaded by code in other parts of the program.
Multiple instances of an object can co-exist with out any interference.
It is easy to partition work in a project based on objects
Object oriented systems can be easily upgraded from small to large systems
Communication between objects is simpler.
Software complexities are easily manageable.
Few features to be incorporated in Object oriented systems
5. Write applications of Object Oriented Programming
The most popular applications of object-oriented programming has been in the area of
user interface design such as windows. Promising areas for application of oops includes:








Real-time systems
Simulating and modeling
Object-oriented databases
Hypertext, Hypermedia and expert text
Artificial Intelligence and expert systems
Neural networks and parallel programming
Decision support and office automation systems
Computer Integrated Manufacture/Computer Aided Design and Manufacture etc
Object-oriented paradigm came from a language, matured into design and has
now moved into analysis. This technology is going to change the way software engineers think,
analyze, design and implement systems in future.
6. Short note on JAVA AND WWW
Java communicates with a web page through a special tag is called <APPLET>
The following communication steps:
1. The user sends a request for an HTML document to the remote computer’s Web sever.
The Web server is a program that accepts a request, processes the request, and sends the
required document.
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 byte code is transferred to the user’s computer. This byte code
had been previously created by the java source code file for that applet.
4. The Java-enable browser on the user’s computer interprets the byte codes and provides
output.
JAVA PROGRAMMING
NASC-BCA
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 byte code contains all the information
necessary to interpret the applet.
PART-C
1. Explain OBJECT ORIENTED PROGRAMMING
All programs consists of two elements code and data. A program can be conceptually
organized around its code or around the data. The process – oriented approach or model
characterizes a program as a series of linear steps .
This approach can be thought of as code acting on data. Procedural languages like C
employ this model to considerable success. Problems with model appear as programs grow larger
and complex. To manage the increasing complexity the second approach called object –
oriented programming was conceived. Object oriented programming organizes a program
around its data and a set of well defined interfaces to that data. An object oriented program can
be characterized as data controlling access to code.
Object-Oriented approach overcomes some of the flaws encountered in the procedural
approach. OOP treats data as a critical element in the program development and it has the
following characteristics as




Emphasis is on data rather than procedure.
Programs are divided into what are known as objects/
Data structures are designed such that they characterize objects.
Functions that operate on the data of an object are tied together in the data
structures.
 Data is hidden and cannot be accessed by external functions.
 Objects may communicate with each other through functions.
 New data and functions can be easily added whenever necessary.
 Follows bottom-up approach.
Object Oriented Programming:
Object oriented programming is a kind of innovation in programming languages and its
important because it depicts the real world situation in a much simpler form. It is more reliable,
less confusion and less complexities. C++ is one such object oriented programming language.
Object Oriented programming has a lot of new concepts like.



Any real world situation can be clubbed as objects
Class is a collection of objects which is of similar type
Object is just combining the data and the functions that operate on that data. It is
an instance of a particular class.
JAVA PROGRAMMING
NASC-BCA
Encapsulation - The Wrapping up of data and functions into a single unit (called class) is known
as encapsulation.
Data Hiding - The data is not accessible to the outside world and only those functions which are
wrapped in the class can access it. This insulation of data from direct access by the program is
called data hiding.
Polymorphism – The ability to take more than form is known as polymorphism.
Inheritance - Inheritance is the process by which objects of one class acquire the properties of
objects of another class. It provides the concept of reusability.
2. Describe the JAVA HISTORY, Features of Java
Java is related to C++. Much of the character of Java is inherited from c, and c++. Java
derives its syntax from c and many of its object-oriented features from c++. Java was influenced
by C++ and not an enhanced version of C++. Java was conceived by James Gosling, Patrick
Noughton, Chris Wrath, Ed Frank and Mike Sheridan at Sun Microsystems in 1991. It took 18
months to develop the first working version. The language was initially called Oak but was
renamed Java in 1995. The primary motivation was the need for a platform-independent
language that could be used to create software to be embedded in various consumer electronic
devices, such as microwave ovens and remote controls.
1990 –special software that could be to manipulate consumer electronic devices
1991 - ‘OAK’
1992 – home applications with touch sensitive screen
1993 –WWW -text based internet
1994- Hot Java
1995 –java – include Netscape and Microsoft
1996 – jdk1.0
1997-jdk1.1
JAVA FEATURES
Platform Independent
The concept of Write-once-run-anywhere (known as the Platform independent) is one of the
important key feature of java language that makes java as the most powerful language. Not even
a single language is idle to this feature but java is more closer to this feature. The programs
written on one platform can run on any platform provided the platform must have the JVM.
Simple
JAVA PROGRAMMING
NASC-BCA
There are various features that makes the java as a simple language. Programs are easy to write
and debug because java does not use the pointers explicitly. It is much harder to write the java
programs that can crash the system but we can not say about the other programming languages.
Java provides the bug free system due to the strong memory management. It also has the
automatic memory allocation and deallocation system.
Object Oriented
To be an Object Oriented language, any language must follow at least the four characteristics.
Inheritance:It is the process of creating the new classes and using the behavior of the existing
classes by extending them just to reuse the existing code and adding the additional features as
needed.
Encapsulation: It is the mechanism of combining the information and providing the abstraction.
Polymorphism: As the name suggest one name multiple form, Polymorphism is the way of
providing the different functionality by the functions having the same name based on the
signatures of the methods.
Dynamic binding : Sometimes we don't have the knowledge of objects about their specific types
while writing our code. It is the way of providing the maximum functionality to a program about
the specific type at runtime. As the languages like Objective C, C++ fulfills the above four
characteristics yet they are not fully object oriented languages because they are structured as
well as object oriented languages. But in case of java, it is a fully Object Oriented language
because object is at the outer most level of data structure in java. No stand alone methods,
constants, and variables are there in java. Everything in java is object even the primitive data
types can also be converted into object by using the wrapper class.
Robust
Java has the strong memory allocation and automatic garbage collection mechanism. It provides
the powerful exception handling and type checking mechanism as compare to other
programming languages. Compiler checks the program whether there any error and interpreter
checks any run time error and makes the system secure from crash. All of the above features
makes the java language robust.
Distributed
The widely used protocols like HTTP and FTP are developed in java. Internet programmers can
call functions on these protocols and can get access the files from any remote machine on the
internet rather than writing codes on their local system.
Portable
The feature Write-once-run-anywhere makes the java language portable provided that the
system must have interpreter for the JVM. Java also have the standard data size irrespective of
operating system or the processor. These features makes the java as a portable language.
JAVA PROGRAMMING
NASC-BCA
Dynamic
While executing the java program the user can get the required files dynamically from a local
drive or from a computer thousands of miles away from the user just by connecting with the
Internet.
Secure
Java does not use memory pointers explicitly. All the programs in java are run under an area
known as the sand box. Security manager determines the accessibility options of a class like
reading and writing a file to the local disk. Java uses the public key encryption system to allow
the java applications to transmit over the internet in the secure encrypted form. The bytecode
Verifier checks the classes after loading.
Performance
Java uses native code usage, and lightweight process called threads. In the beginning
interpretation of bytecode resulted the performance slow but the advance version of JVM uses
the adaptive and just in time compilation technique that improves the performance.
Multithreaded
As we all know several features of Java like Secure, Robust, Portable, dynamic etc; you will be
more delighted to know another feature of Java which is Multithreaded.
Java is also a Multithreaded programming language. Multithreading means a single program
having different threads executing independently at the same time. Multiple threads execute
instructions according to the program code in a process or a program. Multithreading works the
similar way as multiple processes run on one computer.
Multithreading programming is a very interesting concept in Java. In multithreaded programs not
even a single thread disturbs the execution of other thread. Threads are obtained from the pool of
available ready to run threads and they run on the system CPUs. This is how Multithreading
works in Java which you will soon come to know in details in later chapters.
Interpreted
We all know that Java is an interpreted language as well. With an interpreted language such as
Java, programs run directly from the source code.
The interpreter program reads the source code and translates it on the fly into computations.
Thus, Java as an interpreted language depends on an interpreter program.
The versatility of being platform independent makes Java to outshine from other languages. The
source code to be written and distributed is platform independent.
Another advantage of Java as an interpreted language is its error debugging quality. Due to this
any error occurring in the program gets traced. This is how it is different to work with Java.
Architecture Neutral
JAVA PROGRAMMING
NASC-BCA
The term architectural neutral seems to be weird, but yes Java is an architectural neutral language
as well. The growing popularity of networks makes developers think distributed. In the world of
network it is essential that the applications must be able to migrate easily to different computer
systems. Not only to computer systems but to a wide variety of hardware architecture and
Operating system architectures as well. The Java compiler does this by generating byte code
instructions, to be easily interpreted on any machine and to be easily translated into native
machine code on the fly. The compiler generates an architecture-neutral object file format to
enable a Java application to execute anywhere on the network and then the compiled code is
executed on many processors, given the presence of the Java runtime system. Hence Java was
designed to support applications on network. This feature of Java has thrived the programming
language.
3. How JAVA DIFFER FROM C AND C++
Java is differ from c
1 Java does not include the c unique statement keyword sizeof and typedef
2 Java does not contain the data types struct and union
3.Java does not define the type modifiers keyword auto,extern,register etc
4.Java does not support an explict pointer type
5 Java does not have preprocessor
6.Java adds new operator such as instanceof and >>>
7.Java adds labeled break and countinue statements
8.Java adds many features required for oops
Java differ from c++
1 Java does not support operator overloading
2 Java does not have template classes as in c++
3 Java does not support multiple inheritances of classes. This is accomplished using a
new feature called “interface”.
4 Java dose not support global variables. Every variable and methods is declared within a class
and forms part of that class.
5 Java dose not use pointers.
6 Java has replaced the destructor function with a finalize( ) function.
7 There are no header files in java.
4. Explain JAVA AND INTERNET
Java is strongly associated with 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.
Java applets have made the internet a true extension of the storage of local computer.
JAVA PROGRAMMING
NASC-BCA
Java is an object oriented language and a very simple language. Because it has no space
for complexities. At the initial stages of its development it was called as OAK. OAK was
designed for handling set up boxes and devices. But later new features were added to it and it
was renamed as Java. Java became a general purpose language that had many features to support
it as the internet language. Few of the features that favors it to be an internet language are:
Cross Platform Compatibility: The java source files (java files with .java extension) after
compilation generates the bytecode (the files with .class extension) which is further converted
into the machine code by the interpreter. The byte code once generated can execute on any
machine having a JVM. Every operating system has it's unique Java Virtual Machine (JVM) and
the Java Runtime Environment (JRE).
Support to Internet Protocols: Java has a rich variety of classes that abstracts the Internet
protocols like HTTP , FTP, IP, TCP-IP, SMTP, DNS etc .
Support to HTML: Most of the programming languages that are used for web application uses
the html pages as a view to interact with the user. Java programming language provide it's
support to html. For example. Recently the extension package jipxhtml is developed in java to
parse and create the html 4.0 documents.
Support to Java Reflection APIs: To map the functionalities, Java Reflection APIs provides the
mechanism to retrieve the values from respective fields and accordingly creates the java objects.
These objects enables to invoke methods to achieve the desired functionality.
Support to XML parsing: Java has JAXP-APIs to read the xml data and create the xml
document using different xml parsers like DOM and SAX. These APIs provides mechanism to
share data among different applications over the internet.
Support to Web Services : Java has a rich variety of APIs to use xml technology in diverse
applications that supports N-Tiered Enterprise applications over the internet. Features like
JAXB , JAXM, JAX-RPC , JAXR etc enables to implement web services in java applications. It
makes java a most suited internet language.
Support to java enabled Mobile devices: Java programming language is made in such a way so
that it is compatible with mobile devices also. Java language also works with any java enabled
mobile devices that support MIDP 1.0/2.0 including the symbian OS mobile devices.
Support to Personal Digital Assistants: Java language is compatible with Personal Java 1.1
such as chaiVM, Jeode, CrEME, and JV-Lite2 or with all the later version and it also support
PDAs like HP/Compaq, iPAQ, Fujitsu-Siemens Pocket Loox and SimPad, HHP, NEC, Samsung,
Sharp Electronics, Toshiba, psion m5, and any other device with Windows CE/Pocket PC
2002/2003/2005).
5. Explain JAVA VIRTURL MACHINE
JAVA PROGRAMMING
NASC-BCA
A Java compiler converts the Java source code into a binary program consisting of byte
codes. Byte codes are optimized set of instructions designed to be executed by the Java run-time
system, which is called the Java Virtual Machine (JVM). JVM is an interpreted for byte code.
This interpreter inspects and deciphers the byte codes and executes the actions that the byte
codes specify. A Java interpreter can run stand-alone or it can be a part of a web browser such as
Netscape Navigator or IE where it can be invoked automatically to run applets in a web page.
Translating a Java program into byte code makes it easier to run a program in a wide
variety of environments. Only JVM needs to be implemented for each platform. Although the
details of JVM differ from platform to platform, all interpret the same Java byte code. If a Java
program were compiled to native code, then different versions of the same program would have
to exist for each type of CPU connected to the Internet.
When a program is interpreted, it generally runs substantially slower than it would run if
compiled to executable code. However with Java, the difference is not great. The use of byte
code enables the Java run-time system to execute program much faster than you might expect.
Sun has just completed its Just in Time (JIT) compiler for byte code, which is included in
Java 2. JIT is a part of JVM; it compiles the byte code into executable code in real time. It is not
possible to compile a entire Java program into executable code all at once, as Java performs
various run-time checks that can be done only at run time. Instead JIT compiles the code as
needed only during execution.
Download