0% found this document useful (0 votes)
18 views268 pages

Java Programming - Second Year - Bharathiyar University Sylabbus

The document outlines a comprehensive curriculum on Object-Oriented Programming (OOP) and Java, covering fundamental concepts, principles, and applications. It details lessons on various topics including Java's evolution, features, and comparisons with other programming languages. Additionally, it discusses the benefits and limitations of OOP, emphasizing its role in managing complexity in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
18 views268 pages

Java Programming - Second Year - Bharathiyar University Sylabbus

The document outlines a comprehensive curriculum on Object-Oriented Programming (OOP) and Java, covering fundamental concepts, principles, and applications. It details lessons on various topics including Java's evolution, features, and comparisons with other programming languages. Additionally, it discusses the benefits and limitations of OOP, emphasizing its role in managing complexity in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 268
CONTENTS UNITI LESSON 1 OBJECT ORIENTED PROGRAMMING LESSON 2 EVOLUTION OF JAVA AND FEATURES OF JAVA LESSON 3 COMPARISO! C++, JAVA, JAVA AND THE WEB COMPARISON OFC, C++, JA LESSON 4 ARCHITECTURE AND STRUCTURE OF JAVA PROGRAM LESSON 5 JAVA TOKENS, JAVA STATEMENT, JAVA VIRTUAL MACHINE UNIT Ir LESSON 6 . CONSTANTS, VARIABLES, DATATYPES, KEYWORDS LESSON 7 OPERATORS LESSON 8 oe DECISION MAKING AND BRANCHING STATEMENTS LESSON 9 LOOP STRUCTURES LESSON 10 CLASSES, OBJECTS AND METHODS. UNIT 1 LESSON 11 ARRAYS, STRINGS AND VECTORS 15 25 33 43 51 61 73 81 93 107 AG LESSON 12 INTERFACES LESSON 13 INHERITANCE LESSON 14 PACKAGES: PUTTING CLASSES TOGETHER LESSON 15 MULTITHREADED PROGRAMMING : UNIT IV =: LESSON 16 ea MANAGING ERRORS AND EXCEPTIONS LESSON 17 ABSTRACT WINDOWING TOOLKIT(AWT), an LESSON 18 APPLET PROGRAMMING LESSON19 ~~ nr GRAPHICS PROGRAMMING © ae ae LESSON 20 PROGRAMS ON GRAPHICS/APPLETS uMr yo LESSON 21 _ on 1/0 CLASSES ° LESSON 22 . : FILE HANDLING LESSON 23 . ‘ STREAMS AND STREAM CLASSES LESSON 24 THE CHARACTER STREAMS . nan LESSON 25 RANDOM ACCESS FILES AND BYTE STREAMS 119 127 137 149 161 175 © 191 205 213." 223. 239 249 255 267 LESSON 1 OBJECT ORIENTED PROGRAMMING CONTENTS 1.0 Aims and Objectives. 1.1 Introduction. ay anor a 1.2 Object Oriented Programming Paradigm. 1.3 Principles of OOP. 1.4 Benefits of OOP. : : 1.5 Non-Benefits of OOP." 7!" 1.6 Applications of OOP. : . 1.7 Object Oriented Programming Languages: 1.8 Let us sum up. 1.9 Check Your Progress: Model Answers 1.0 AIM AND OBJECTIVES In this lesson we will learn the Fundamentals of Object oriented programming, basic concepts of OOP and the benefits of OOP. At the end of this lesson the learner will be able to identify objects and distinguish the characteristics of classes, objects and methods. 1,1 INTRODUCTION @iawe Object-oriented programming forms the core of Java. Since the concepts of OOP is so integral to Java, it is very essential to understand its basic principles before starting to write some simple Java programs. This lesson commences with theoretical discussions on OOP, followed by the benefits and application of Object Oriented Programming (OOP). 1.2 OBJECT-ORIENTED PROGRAMMING PARADIGM ‘The need to program GUIs led to a distinct programming paradigm, called object-oriented programming. An object-oriented programming system (OOPS) consists of objects. An object comprises both data and algorithms. The data describes the state of the object. Objects communicate by sending messages to each other. When an object receives a message, it responds by executing one of its algorithms. The algorithm may change the state of the object, generate output, or send messages to other objects. 70 * . nt of a OOPS are well-suited to programming GUIs. Typically, tre ia anal the GUI is represented by one object. The object determines oot er ieortaenting a behavior of the corresponding clement. For example, an objec ser closes the Window would have data for its postion, size and tite. If the user closes the window, the OOPS sends a message to the window object telling rom the itself. The window then executes an algorithm that erases its image {rot screen. All computer programs essentially consist of two elements: cr ea data. A program can be conceptually organized around its code or data. is first way, program organized around code is called the process-oriented mode! and can be thought of as code acting on data, The second way of organizing Program around data (i.c., objects) is called Object-Oriented Programming, which was created to manage the increasing complexity. An Object oriented program is a collection of class definitions, each one wrapping up all the data and functionality associated with a single concept or entity specified in the program design. . 1.3 PRINCIPLES OF OOP All object oriented programming gives the basic mechanisms that help you implemerit the object-oriented model. Class Defines the abstract characteristics of a thing (object), including ~the thing's characteristics (its attributes, fields or properties) and the thing's behaviors (the things it can do, or methods, operations or features) One might say that a class is a blueprint or factory that describes the nature of something. Classes pro , modularity and structure in an object-oriented computer Poeen) A class should typically be recognizable to a non-programmer familiar with the problem domain, meaning that,’ the charactetistics of the class should make sense in context. \Also, the code for a class should be relatively self-contained (generally using encapsulation). Collectively, the properties and methods defined by a class are called members.) Object “A particular object. 4 Instance One can have an instance of a class or a particular ce te Method An object's abilities. In language, methods are verbs Message passing The process by which an object send object or asks the other object to in S (data to another woke a method. Abstraction Abstraction is simplifying complex reali eee nae to the problem, and working oe Y modeling’ classes level of inheritance fora given aspect of fe ables. “PPtopriate ( resccion is also achieved through conponitin) For example, a class Car would be made up of an Engine, Gearbox, Steering objects, and many more components. To build the Car class, one does not need to know how the different components work + internally, but only how to interface with them; ie., send messages to them, receive messages from them, and perhaps make the different objects composing the class interact with each other. Encapsulation (encapsnsen is the mechanism that binds together, the data und code, it manipulates. It keeps both safe from outside interference and misuse/ Encapsulation may be thought of as a , protective wrapper that prevents the code and data from being arbitrarily accessed by the other code defined outside the wrapper. Access to the code and data inside the wrapper is tightly controlled through a well-defined interface. ‘The power of an encapsulated code is that everyone knows it and thus can use it regardless of the implementation details and without fear of unexpected side effects. In Java, the basis of encapsulation is the class. A class defines the structure and behavior defined by the class. For this reason, objects are sometimes referred to as instance of a class. Thus, a class is a logical construct; an object has physical reality. Since the purpose of a class is to encapsulate complexity, there is mechanism for hiding the complexity of implementation inside the class. (‘sacs method or variable in a class may be marked private or public. The public interface of a class represents everything that may'be known to the external users of that class. The private methods and =“ only be accessed by the code that is a jember of a class. tnetanel inne is process by which one object acquires the roperties of another object. This is important because it supports the concept of inheritance hierarchical classification.) For Example, Pomeranian is the part of the classification ‘dog’-which in turn is part of the ‘mammal’ class, which is under the large class ‘animal’. Without the use of hierarchies, each object would need only define all of its characteristics explicitly. However, by use of inheritance, an object need-only define those qualities that make it unique within its class. It can inherit its general attributes from its parent. It id the inheritance mechanism that makes it possible for one objett to be specific instance of a more general case} If you wish to descnite a more specific class of animals, such as‘mammals, they would have more specific attributes, such as the type of teeth and skeletal system. This is known as su¥class of animals, where animals are referred to as “superclass”. Since mammals are simply more precisely specified animals, they inherit all attributes from animals. A deeply inherited su lass inherits all the attributes from each of its ancestors in the class hierarghy. Polymorphism (romain is a feature that allows one interface to be Used for general class of action. The specific action is determined by the exact nature of the situation More generally, the concepts of polymorphism are often expressed by the phrase ‘one interface, multiple methods’. This means that it is possible to design a generic interface to'a group of related activities, It means it is . functions that do not care which variables types are passed to them. > . Check Your Progress: 1 i List a few characteristics of Object oriented programming, an Notes: a) Write your answer in the space given below. b) Check your answer with the one given at the end of this lesson, . [ 1.4 BENEFITS OF OOP. a... greatest benefit of an objéct-oriented programming system is that it imposes a powerful structure on computer programs. This Sarat weet make complex, intricate systems, such as GUIs, manageable, Several features that contribute to this structure are: (i) Modularity The objects in an OOPS are self-contained, and have clearly defined interfaces . {o other objects. This facilitates writing ‘modular code, which reduces the inherent complexity of a program. (ii) Polymorphism An OOPS can have many different kinds of objects, and new objects can inherit ‘properties of existing objects. For example, a GUI might have several kinds of windows: document windows, dialog boxes, help screen: ° An OOPS would typically define a window object, and then define specialized sub-objects for cach parti i i is i i a P es Beles kind of window. Again, this helps to manage the complexity of (iii) Extensibility If an OOPS provides for three kinds of windows, it can as easily provide for four, or five. Adding a new feature is often simply a matter of defining an object to implement it. Furthermore, new sub-pbjects can be defined without having to modify, or even see, the original cod In principle, the same program ‘can be done in a procedural language. However, for technical reasons, it is more difficult, and it very often corrupts the original design of the program, leading to unmanageable complexity. Reusability (one of the purported benefits of object-oriented programming ig that object tan be placed in libraries for all to use. This saves the World the effort of continually rewriting the same code for every new program, Furthermore, because objects are extensible, object libraries offer the_programmer more flexibility and functionality than subroutine libraries (their counterparts in procedural languages). Check Your Progress: 2 Enumerate a few benefits of Object oriented programming. Notes: a) Write your answer in the space given below. b) Check your answer with the one given at the end of this lesson. 1,5 NON-BENEFITS OF OOP Object-oriented programming has real benefits. However, it is not a universal remedy. Here are a few difficulties: * An OOPS gives the tools to manage complexity; it doesn't give the ability to do so. If a program isn't well designed to begin with, an OOPS isn't going to help. « Not everything is an object. Numerical integration and bank ‘statements are best left to procedural languages and database systems, respectively. 58 1.6 APPLICATIONS OF OOP i . The C Applications of OOP are scene i gain nope ates Ui tne nreae Ue most popular application of object-oriented programming, up to rer > indowing in the area of user interface design such as windows. Hundreds o ve ae systems have been developed, using the OOP techniques. Real I-busini a systems are often much more complex and contain many more objects wit complicated attributes and methods. OOP is useful in these types of applications because it can simplify a complex problem. The promising areas for application of OOP inclade. * Real-time systems * Simulation and modeling * Object-oriented databases Hypertext, hypermedia and expertext Al and expert systems automation systems ; * _CAM/CAD systems programming 1.7 OBJECT ORIENTED LANGUAGES Cie lbisctoriented programming is not the 1 ike structured Programming, OOP languages such as C and Pascal. Hower may generate confusion when the pr ight of any particular language. concepts can be implemented using ver, programming becomes clumsy and Ograms grow large. A language that is them. The languages should support several of the OOP concepts to claim that they are object oriented. . Dependirig upon the features they support, they can be classified into the following two categories: 1. Object-based programming languages, and 2. Object-oriented Programming languages. Data encapsulation Data hiding and access mechanisms Automatic initialization and clear-up of objects Operator overloading 12 Languages that support programming with objects are said to be object-based programming languages. They do not support inheritance and dynamic binding. Ada is a typical object-based programming language. Object-oriented programming incorporates all of object-based programming features along with two additional features, namely, inheritance and dynamic binding. Object-oriented programming can therefore be characterized by the following statement: . Object-based features + inheritance + dynamic binding _ Languages that support these features include C++, Smalltalk, Object Pascal, Simula, Visual Basic, Python, VB.NET and Java. There are a large number of object-based and object-oriented programming languages. 1.8 LET US SUM UP In'this lesson we have discussed the basic features and benefits available in Object oriented programming. The following points have been discussed in this lesson : . + Java is an Object oriented language. + The characteristics of OOP are Class, encapsulation, Objects, Methods, Abstraction, Encapsulation * Object-based programming is the style of programming that primarily supports encapsulation and object identity. + Object-oriented programming incorporates all of object-based programming features along with two additional features,” namely, inheritance and.dynamic binding. © Object-oriented programming can be characterized by the statement: Ohject-based features + inheritance + dynamic binding. 1.9 CHECK YOUR PROGRESS : MODEL ANSWERS 1. Following are the characteristics of OOP. A few of them can be discussed. Use of Class, encapsulation, Objects, Methods, Abstraction, - Encapsulation 2., The benefits of OOP are Modularity, Polymorphism, Extensibility and Reusability 14 LESSON 2 EVOLUTION OF JAVA AND FEATURES OF JAVA CONTENTS 2.0 Aims and Objectives 2.1 Introduction 2.2 Evolution of Java 2.3 Versions of Java 2.4 Java Version History 2.5 Java Platform 2.6 Features of Java 2.7 Let us sum up 2.8 Check Your Progress: Model Answers 2.0 AIMS AND OBJECTIVES ‘This lesson aims at providing the reader with information on the history and evolution of Java language. This lesson also deals with the features available in Java and also discusses the different versions and changes it has undergone. At the,end of-the lesson the learner will be able to understand the features and highlight the different version of Java. 2.1 INTRODUCTION ‘The computer language innovation and development occurs for two fundamental reasons: * To adapt to changing environments. + To implement refinements and improvements in the art of programming. In the end of 1980s and the early 1990s, object-oriented programming using C++ took hold. Indeed, for a brief moment it seemed as if programmers had finally found the perfect language. Because C++ blended the high efficiency and stylistic elements of C with the object-oriented paradigm, it was a language that could be used to create a wide range of programs. Within few years, the World Wide Web and the Internet reached critical mass, and the necessity for architecture neutral language was felt, which resulted in the evolution of Java language. “15 = 2.2 EVOLUTION OF JAVA aes Gosling and Patrick Naughton conceived Java. This language was initially éalled “Oak” but was renamed ‘Java’ in 1995, Surprisingly, the original application area for Java was not the Internet. Instead, the primary motivation was the requirement of platform ~ independent language that Sould be used to create software to be embedded in various consumer electronic devices, such as microwave ovens and remote controls. In addition to the surface similarities, Java'shares some of the other attributes that helped make C and Gre successful. First, Java was designed, tested, and refined by real, Working programmers. Second, Java is cohesive and Logically consistent. Third, except for thece constraints imposed by the Internet Covironment, Java enables the programmer, and hee fall crcl. 2.3 VERSIONS OF JAVA jhe next major release of Java was Java 2. The first release of Java 2 carried the version number 1.2. It may seem odd that the first release of Java 2 The next release of Java was Java 2, version 1.3: This version of Java wane ® Arst major upgrade to the original Java 2 release. For the ment part it added to existing functionality and. “tightened up” the development environment. In general, programs written for version 1.3 and those written for tersion 1.3 are source-code compatible. Although version 1.3 contained a smaller set of changes than the preceding three major releases, it was nevertheless important, The current release of Java is Java 2, version 1.4, This release contains several important upgrades, enhancements, and additions. For example, it adds the new keyword assert, chained exceptions, ands channel-based 1/0 16 . Despite the ‘significant number of new features, version 1.4 maintains nearly 100 percent source-code compatibility with prior versions. 2.4 JAVA VERSION HISTORY A The Java language has undergone several changes since JDK 1.0 as well as numerous additions of classes and packages to the standard library. Since J2SE 1.4, the evolution of the Java language has been governed by the Java Community Process (JCP), which uses Java Specification Requests (JSRs) to Propose and specify additions and changes to the Java platform. The language is specified by the Java Language Specification (JLS). In addition to the language changes, much more dramatic changes have been made to the Java class library over the years, which have grown from a few hundred classes in JDK 1.0 to over three thousand in J2SE 5.0. Entire new APIs, such as’Swing and Java2D, have been introduced, and many of the original JDK 1.0 classes and methods have been deprecated. JDK 1.0 (January 23, 1996) JDK 1.1 (February 19, 1997) Os The following features were added in JDK 1.1 ‘ «an extensive retooling of the AWT event model * inner classes added to the language © JavaBeans * JDBC * RMI J2SE 1.2 (December 8, 1998) This and subsequent releases through J2SE 5.0 were rebranded and called . a8 Java 2 and the version name "J2SE" (Java 2 Platform, Standard Edition) replaced JDK to distinguish the base platform from J2EE (Java 2 Platform, Enterprise Edition) and J2ME (Java 2 Platform, Micro Edition). The following features were included: © the Swing graphical API was integrated into the core classes * Sun's JVM was equipped with a JIT compiler for the first time * Java Plug-in : g * Java IDL, an IDL implementation for CORBA interoperability J2SE 1.3 (May 8, 2000) ‘the most notable changes were: + HotSpot JVM included (the HotSpot JVM was fi for the J2SE 1.2 JVM) Me + RMI was modified to support optional compatibility with CORBA é{ released in April, 1999 + JavaSound “4 +, Java Naming and Directory Interface (JNDI)' included in core libraries (previously available as an extension) ; + Java Platform Debugger Architecture (JPDA) Section 1.02 ee Section 1.03 J2SE 1.4 (February 6, 2002) This was the first release of the Java platform developed under the Java Community Process. Major changes included : : + exception chaining allows an exception to'éncapsulate original lower-level exception + Internet Protocol version 6 (IPv6) support image I/O API for reading and writing images in formats like JPEG and PNG : + integrated XML parser . + integrated security and cryptography extensions J2SE 5.0 (September 30, 2004) ‘This version was originally numbered 1.5, which is still used as the internal version number. This version added a number of significant new language features such as : + Generics: Provides compile-time (static) type safety for collections and eliminates the need for most typecasts (type conversion). Meta data : Also called annotations, allows language constructs such as classes and methods to be tagged with additional data, which can then be processed by metadata-aware utilities. . Autoboxing//unboxing: Automatic conversions between primitive types (such as int) and primitive wrapper classes (such as Integer) Enumerations: The enum keyword creates a typesafe, ordered list of values (such as Day. MONDAY, Day. TUESDAY, etc,). Previously this could only be achieved by non-typesafe’ constant integers or manually constructed classes (typesafe enum pattern). ‘Swing: New look and feel, called synth. 18 + Varargs : The last parameter of a method can now be declared using a’ type name followed by three dots (e.g. void drawtext(String... lines)). In the calling code any number of parameters of that type can be used and they are then placed in an array to be passed to the method, or alternatively the calling code can pass an array of that type. Enhanced ‘for, loop’: The for loop syntax is extended with special syntax _for iterating over each member of either an array or any Iterable such as. the standard collection classes. Java SE 6 (December 11, 2006) In this version, Sun replaced the name “J2SE" with Java SE and dropped the ".0" from the version number: Internal numbering for developers remains 1.6.0, ; ae During the development phase, new builds including enhancements and bug fixes were’ released approximately weekly. Beta versions were released in February and June 2006, leading up to a final release that occurred on December 11, 2006. The current revision is Update 6 which was released in 2008. Rew ue we Major changes included in this version: + Support for older Win9x versions dropped. The last version for Win98 is 1.5.0.13. : * Dramatic performance improvements'for the core platform, and Swing. + Improved Web Service support. + JDBC 4.0 support “«! Java Compiler API an API allowing a Java program to select and invoke a “\' ~-“Java Compiler programimiatically. > : * Many GUI improvements, such, as integration of Swing Worker in the “ “API, table“sorting~ and filtering, and. true Swing double-buffering (climinating the gray-area effect). Java SE6 Update 10 « ‘Java SE 6 Update 10 (previously known as Java SE 6 Update N), while it does not change any public API, is meant as a major enhancement in terms of end-user usability. © °°) 77" ~ arn Major changes for this update include: * Java Deployment Toolkit, a set of JavaScript functions to ease the deployment of applets and Java Web Start applications. * Java Kemel, a small installer indluding. only the most commonly used SRE classés: Other packages are downloaded when needed. * Java Quick Starter, to improve cold start-up time. 19:; + Improved graphics performance on Windows using Direct3D. + Anew Swing look and feel called Nimbus and based on synth. « Next-Generation Java Plug-In: applets now run in a separate process and Support many features of Web Start applications. Java SE7 This is in the carl ; i sroicet as ami ly planning and development stages. This project is codenamed as Dolphii ie : - i " phin Project. Start 106 and will tentatively be released in January 2009, Started up in August 20( New features that may be integrated in Java 7 comprise: JVM support for dynamic languages, following the prototyping work currently done on the Multi Language Virtual Machine, A new library for parallel computing on Multi-core processors, Superpackages (JSR 294), which are a way to define explicitly in a library - or module which classes will be visible from outside of the library, Swing Application Framework, an ‘infrastructure common to most desktop applications, making Swing applications easier to create. Check Your Progress : 1 Write a brief note on the different versions of Java. Notes ? a) Write your answer in the space given below. b) Check your answer with the one given at the end of this lesson. ' 2.5 JAVA PLATFORM ‘The Java platform is the name for a bundle of related programs, or platform, from Sun which allow for developing and running programs written in the Java programming language. The platform is not specific to any onc processor or operating system, but rather an execution engine (called a virtual machine] and a compiler with a set of standard libraries that are implemente for various hardware and operating systems so that Java programs can run identically on all ofthem. —* ; Different "editions" of the platform are available, including: + Java ME (Micro Edition): Specifies several different sets of libraries (known as profiles) for devices which are sufficiently limited tha’ 20 supplying the full set of Java libraries would take up unacceptably large amounts of storage. 7 + Java SE (Standard Edition): For general purpose use on desktop PCs, servers and similar devices. + Java EE (Enterprise Edition): Java SE plus various APIs useful for multi-tier client-server enterprise applications. . ‘The Java Platform consists of several programs, each of which provides a distinct portion of its overall capabilities. For example, the Java compiler, which converts Java source code into Java bytecode (an intermediate language or the Java Virtual Machine (JVM), is provided as part of the Java Development * Kit (JDK). The Java Runtime Environment (JRE), complementing the JVM vith a just-in-time (JIT) compiler, converts intermediate bytecode into native nachine code on the fly. Also supplied are extensive libraries (pre-compiled into ava bytecode) containing reusable code, as well as numerous ways to deploy sava applications, including embedding them in a web page as an applet. 2.6 FEATURES OF JAVA Java is simple to use. Apart from the simplicity Java is Secure, Portable, Object-Oriented, Robust, Multithreaded, Architecture-neutral, garbage ollected, multithreaded ' programming language with a strongly typed xception-handling mechanism for writing distributed, and dynamically xtensible programs. ' \ few features of Java are : Java is Simple to code and use * Java was designed to be easy for the professional programmer to learn and use effectively. If you already understand the basic concepts of object-oriented programming experience, learning Java will be, even easier. Java is Object Oriented Java was clean, usable, pragmatic approach to objects. The object model in Java is simple and easy to extend, while simple types, such, as integers, are kept as high - performance no objects. Safe and Robust be ability to create robust programs was given a high priority in the design of Java. To gain reliability, Java restricts in a few key areas, to force to find mistakes early in program deveapmend At the same time, Java frees from having to;worry about many of the most common causes of programming errors. (Because Java is a strictly typed language, it peak ‘the code at compile time. However, it also checks the code at run time. The C or C++ programmer can make use of features called “pointers” Which led to potential hazard in runti e use of Pointers are disallowed in Java 21 Platform Independent system they're ranting on, Platform independent. Multithreaded integra” W25 designed to mect\the real-world requirement of creating interactive, networked programs’ To accomplish this, Java supports multithreaded programmin many things simultaneous! Architecture-Neutral fecture neutral.) T: it simply, Java programs don’t care what Som Platformn “independence is the ‘ability of the Platform independence is another way of saying that Java is same program to fn different operating systems; Java is completely which allows writing programs that do I. Threads represent Program to do more than one task at the same time. way for a computer A central issue for Java designers was that of code longevity and Portability, ;) One of the main problems facing programmers is that no guarante€ exists that if you wn a program today, it will run tomorrow > even on the same machine.\Designer goal was.*Write Once,. run anywhere, any time, forever. ”) Interpreted and High Performance ( While it is true that Java was engineered for interpretation, the Javabytecode was carefully designed so that it would be easy to translate directly intonative machine code for high-performance by using a just-in-time compiler, Java run-time systems that provide this feature lose none of the benefits of the platform-independent code, ‘ ‘Distributed Computing Java is designed for the distributed environment of the Internet, because it handles TCP/IP protocols. In fact, accessing a resource using a URL is ~ not much different from accessing a file. . Automatic Garbage Collection ( In C or C++, when a “pointer was used, the programmer head to “return” , it back to the OS,)If this step was neglected, too much memory was used up and this resilted in the slowin; speed.\Java brought in a featu: automatically returned to the i ‘Exception Handling (serie programming thos®languages, when a pro not be possible to find th fe: reason -for programmer a rich set of exception han why the program crashed and handle it an appropriate mann er) g down of the ‘program’s execution ire where unwanted memory was languages did not support exceptions) In gram crashes at unexpected situation it will the -error. \Java provides the idler8 that help in es Check Your Progress : 2 22 , | , | List a few feature available in Java language. _| Netes : a) Write your answer in the space given below. b) Check your answer with the one given at the end of this lesson. 2.7 LET US SUM UP In this lesson we have learnt the evolution of Java. We have also discussed a few features of the Java language. -The following points were discussed in this lesson : * James Gosling and Patrick Naughton developed Java Language © Java language was initially called “Oak” but was renamed “Java” in 1995, * The different versions of Java includes JDK 1.0 (January 23, 1996), JDK 1.1 (February 19, 1997), J2SE 1.2 (December 8, 1998), J2SE 1.3 (May 8, 2000), J2SE 1.4 (February 6, 2002), J2SE 5.0 (September 30, 2004), Java SE 6 (December 11, 2006),Java SE 6 Update 10, Java SE 7 * ‘The different editions of Java are Java ME (Micro Edition), Java SE (Standard Edition), Java EE (Enterprise Edition) 2.8 CHECK YOUR PROGRESS: MODEL ANSWERS 1. The different versions of Java includes a. JDK 1:0 (January 23, 1996) , . JDK 1.1 (February 19, 1997), . J2SE 1.2 (December 8, 1998) . J2SE 1.3 (May'8, 2000)" °°" : JQSE 1.4 (February 6, 2002), J2SE 5.0 (September 30, 2004) . Java SE 6 (December 11, 2006) . Java SE 6 Update 10, Java SE 7 .2. Few features of Java language are Java is Simple to code and use, Java is Object Oriented, Safe and Robust, Platform Independent, Multithreaded, Architecture-Neutral, Interpreted and High Performance, Distributed Computing,..Automatic Garbage Collection, Exception Handling. s ao pam oe 23 a 5 . LESSON 3 : COMPARISON OF C, C++, JAVA JAVA AND THE WEB CONTENTS 3.0 Aims and Objectives 3.1 Introduction _3.2 Comparison of C, C++ and Java 3.3 Java and the Web 3.3.1 Web surfing with Java enabled web browser 3.3.2 Third party Java tools 3.4 Let us sum up 3.5 Check Your Progress: Model Answers 3.0 AIMS AND OBJECTIVES The objective of this lesson is to compare the high level programming, languages’C, C++ and Java. The prime objective is that the reader will be able to compare the features available in each of these languages and learn Java in a easier manner. The second part of this lesson focuses on the applications of Java in the internet and Web. Ie to identify the enhanced At the end of this lesson the learner will be abl features of Java and use them in an efficient manner. 3.1 INTRODUCTION Java is more powerful and can do much more than C/C++."The Internet is the place where the little dynamo called Java was first set. free, Now thousancis of programmers, Internet developers, Web publishers, and software hotises sround the world are racing to learn everything they can about this tool “for revolutionizing the Internet and the way programming is done. After all, C++ faware industry too, yet as you will soon was supposed to revolutionize the so! , y c earn, the major difference between C++ and Java is that Java delivers on all its promises. 2 COMPARISON OF C, C++ and Java C doesn't have a graphical user interface (GUI), and C doesn't have any ramming (OOP). It's possible to write in “fe harder because . ew powerful features of Java. But that is é \_way to do object-oriented prog Java ina C style, avoiding the ni foolish.) . . « /Java either checks for errors, or makes you chéck for errors. C lets you do many things that would cause errors (for example, convert strings to integers, or do I/O}, but doesn't make you write code to handle the errors. Java makes you write try..catch statements around things that . might cause problems. Java is easier because... Java checks for errors. For example, Java checks subscripts to make sure they are in the correct range. Java does things automatically. There aréa huge number of things that Java can automatically write for the user) For example, expandable arrays, many‘data structures, ete. In C it would take a very long time to write and debug these things by yourself. * Java doesn't have the most dangerous things. The things in C which cause the most program errors are pointers, pointer arithmetic, and memory managementava has replaced these with much; much safer things: references, subscription, and garbage collection. 7 The Java language is based mostly on C and C++. A lot of the basic language elements, for example, the primitive types, operators, and statements of Java are taken directly from C. ; Primitive Java types are similar to C ‘ Java includes types which are similar to those in C, C++ hort, int Jong, float, and double). Unlike C/C++, Java nee eae how | 1ese types are implemented. For example, the length in C” might be 16 bits on a PC, 32 bit on a workstation, and 60 bite on anid . 26 supercomputer. Java defines exactly how ints are represented (32 bits, two's complement, big-endian). All Java types are completely defined, in contrast to C where NO types are well specified. Portability and types Because of the lack of complete type definitions, moving a C/C++ program from one machine to another can be a giant headache, or even impossible) For example, to preserve the range of an integer variable it is requiret'fo change shorts to ints, but then it is also necessary to change the corresponding format specifiers, union declarations, bit field declarations, shift operators, etc. This is only one example of the tremendous number of portability problems in C/C++. Additfonal primitive types Java has two additional primitive types: boolean and byte. The boolean type is a nice addition that is like Pascal boolean; it can have only the values true and false. The byte type is used for 8-bit integers because char is a Unicode character, which requires 16 Bis) Summary of the Java primitive types Java integer types are two's complement: byte (signed 8 bits), char (unsigned 16 bits), short (signed 16 bits), int (signed 32 bits), and long (signed 64 bits)) The real surprise here is that char is 16 bits) This is because the Unicode character set is used so that characters #rf all major human languages can be represented. C doesn't specify the character set for char. Jaya and C Arrays Java arrays are very similar to C arrays although they must alwavs be fynamically allocated. However, Java arrays are not just pointers into memory, but separate ott One of the good bene“t~ of this is that array subscript bounds are-¢hecked, which makes finuag bugs much easier. Java Strings are not Arrays of char In Java there are two special object types, String and StringBuffer that are used to store strings of characters. C uses arrays of char, but this causes many problems.) Of course, you can use an array of characters in Java if you need to, byt it is usually easier to use String or StringBuffer Java Operators are similar to C : Most of the Java operators are the same as in C/C++, however Java ay the exact order of evaluation in an expression, which C does not. Rather than list all of the 40+ operators that are the same, it's easier to list the differences. . . There is an additional right shift operator, >>>, to add zero bits on the left regardless of the sign of the left i) The C comma operator is 27

You might also like