Programming in Java Unit-I an Overview Of
Total Page:16
File Type:pdf, Size:1020Kb
PROGRAMMING IN JAVA UNIT-I AN OVERVIEW OF JAVA: James Gosling, Patrick Naughton, Chris Worth, ED Frank and Mike Sheridan conceived Java at Sun Microsystems, Inc in 1991. It took 18 months to develop the first Working version. This language was initially called ‘OAK’ but was renamed “JAVA” in 1995. The primary motivation was the need for a platform-independent (i.e. architecture- neutral) language that could be used to create software to be embedded in various consumer electronic devices such as microwave oven and remote controls. As you can probably guess, many different types of CPU’s are used as controllers. The trouble with C and C++ (and most other languages) is that they are designed to be compiled for a specific target. Although it is possible to compile a C++ program for just about any type of CPU, to do so requires a full C++ compiler together for that CPU. The problems are that compilers are expensive and time consuming to create. An easier- more cost efficient solution was needed. In an attempt to find such a solution, Gosling and others began work on a portable, platform-independent language that could be used to produce code that would run on a variety of CPU’s under different environments. This effort ultimately led to the creation of Java. In 1993, the World Wide Web (WWW) appeared on the Internet and transformed the text-based Internet into a graphical rich environment. The project team came up with the idea of developing web applets (tiny programs) using the new language that could run on all types of computers connected to the Internet. In 1994, the team developed a web browser called “Hot java” to locate and run applet program on Internet. Hot java demonstrated the power of new language, thus making it instantly popular among the Internet users. So, while the device for an architecture-neutral programming language provided the initial spark, the Internet ultimately led to Java’s large-scale success. Java is influenced by C++ it is not an enhanced version of C++. Difference between JAVA and C++ JAVA does not support operator overloading. JAVA does not have template classes as in C++ JAVA does not support multiple Inheritances 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 part of that class. JAVA does not use pointers. There are no header files in JAVA. JAVA has replaces the destroy function with a finalize () function. FUNDAMENTALS OF OBJECT ORIENTED PROGRAMMING OBJECT ORIENTED PROGRAMMING is an approach that provides a way of modularizing programs by creating partitioned memory area for both data and functions that can be used as templates for creating copies of such modules on demand. Object oriented program organizes a program around its data and a set of well-defined interfaces to that data. An object oriented programming can be characterized as data controlling access to code. Some of the features of object-oriented paradigm are: (i) Emphasis is on data rather than procedure. (ii) Programs are divided into what are known as “objects”. (iii) Data structures are designed such that they characterize the objects. (iv) Methods that operate on the data of an object are tied together in the data structure. (v) Data is hidden and cannot be accessed by external functions. (vi) Objects may communicate with each other through methods. (vii) New data and methods can be easily added whenever necessary. (viii) Follows bottom-up approach in program design. Method Method Data Method Method Object=Data+Methods BASIC CONCEPTS OF OBJECT ORIENTED PROGRAMMING OBJECTS Objects are the basic runtime entities in an object-oriented system. This may represent a person, a place, a bank account or any item that the program may handle. When a program is executed, the objects interact by sending messages to one another. For example ‘customer’ and ‘account’ are two objects in a banking program, then the customer object may send a message to the account object requesting for the balance. Each object contains data and code to manipulate the data. PERSON ----------- OBJECT NAME DATA ROLLNO MARK1 MARK2 TOTAL( ) METHODS CLASSES: Class is a collection of objects. Once a class has been defined, we can create any number of objects belonging to that class. Fruit(class)----> mango(object) DATA ABSTRACTION AND ENCAPSULATION The wrapping up of data and methods into a single unit (class) is known as Encapsulation. These methods provide the interface between the object’s data and program. The insulation of the data from direct access by the program is called data hiding. ABSTRACTION refers to the act of representing essential features without including the background details or explanations. INHERITANCE It is the process by which one object acquires the properties of another object.. In oop, the concept of inheritance provides the idea of reusability; this means that we can additional features to an existing class without modifying it. This is possible by deriving a new class from the existing one. The new class will have the combined features of both the classes. POLYMORPHISM Polymorphism is another important oop concept. It means the ability to take more than one form. More generally, the concept of polymorphism is often expressed by the phrase “ one interface multiple methods”. Single Function name perform different task is called function overloading. Shape Draw() Circle Box Triangle Draw(circle) Draw(box) Draw(triangle) DYNAMIC BINDING Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding means that the code associated with a given procedure call is not know until the time of the call at runtime. MESSAGE COMMUNICATION An object-oriented program consists of a set of objects that communicate with each other. 1. Creating classes that defines objects and their behaviour. 2. Creating objects from class definitions. 3. Establishing communication among objects. Message Sending Object Receiving Object BENEFITS OF OOPS 1. Through inheritance, we can eliminate redundant code and extend the use of existing classes. 2. We can build programs from the standard modules that communicate with each other, rather than having to start writing the code from the scratch. This leads to saving development time and higher productivity. 3. The principle of data hiding helps the programmer to build secure programs that cannot be invaded by code in other parts of the program. 4. It is possible to have multiple objects to co-exist without any interference. 5. It is possible to map objects in the problem domain to those objects in the program. 6. It is easy to partition the work in a project based on objects. 7. The data centered design approach enables us to capture more details of a model in an implement able form. 8. Object oriented systems can be easily upgraded from small to large systems. 9. Software complexity can be easily managed. APPLICATIONS OF OOP (i). Real systems (ii). Simulation and modeling (iii). Object oriented databases. (iv). Hypertext, hypermedia and expertext (v) AI and expert systems (vi). Neural networks and parallel programming. (vii). Decision support and office automation system. (viii). CAD/CIM systems JAVA FEATURES 1.simple. 2.object oriented 3.Compiled and interpreted. 4.robust. 5.secured. 6.architecture neutral. 7.portable 8.high performance 9.multi threaded. 10.Dynamic. Simple: o Java is a small and simple language. o Java inherit c syntax and object oriented features of c++. Object oriented: o Java is a true object oriented language. o All program code and data should be within classes and object. Complied and interpreted: o A computer language is either complied or interpreted.java combines both these approaches. o Java compiler translates source code into bytecode. o Java interpreted translate bytecode into machine code. Robust: o Java is a robust language. o It is designed for relieving memory management problem and mishandling exception. Secured: ARCHITECTURE NEUTRAL. o Platform independent- Java programs can be easily moved from one computer system to another, anywhere and anytime. o Changes and Upgrades in operating systems, processors and system resources will not force any changes in Java programs. PORTABLE Java ensures portability in two ways. First java compiler generates Byte code instructions that can be implemented on any machine. Secondly, the sizes of the primitive data types are machine independent. Multithreaded and Interactive Multithreaded means handling multiple tasks simultaneously. Java supports multithreaded programs. This means that we need not wait for the application to finish one task before beginning another. Dynamic and Extensible Java is a Dynamic language. Java is capable of dynamically linking in new class libraries, methods and objects. Memory allocation and deallocation in run time Java Environment Java environment includes a large number of development tools, and hundreds of classes and methods. The development tolls are part of the system known as Java Development Kit (JDK) and the classes and methods are part of the Java Standard Library (JSL) also known as API (Application Programming Interface). E.g. AppletViewer (for viewing java applets). javac (java compiler) java (java Interpreter, which runs applets and application by reading and interpreting byte code files) javah (produces header files for use with native methods) Text Editor Java Source HTML Code javadoc Files javac Header Java Class File javah Files java jdb Java Program Output javap(java dissembler, which enables us to convert bytecode files into a program description) jdb (java debugger, which helps us to find errors in our programs) DATA TYPES: o The datatype is used for storing the values. o If a value become too big for variable type,it is truncated.