1 / 43

The Java Programming Language

The Java Programming Language. Robust and Versatile enabling developers to: Write software on one platform and run it on another. Create programs to run within a web browser. Develop server-side applications for online forums, stores, polls, processing HTML forms, and more.

eamon
Download Presentation

The Java Programming Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Java Programming Language • Robust and Versatileenabling developers to: • Write software on one platform and run it on another. • Create programs to run within a web browser. • Develop server-side applications for online forums, stores, polls, processing HTML forms, and more. • Write applications for cell phones, two-way pagers, and other consumer devices.

  2. Java Technology • Java technology is both a programming language and a platform • The Java programming language is a high-level language that can be characterized as follows: Simple, Object Oriented, Portable, Distributed, High Performance, Interpreted, Multithreaded, Robust, Dynamic, Secure

  3. Simple • Primary characteristics of the Java programming language include a simplelanguage that can be programmed without extensive programmer training. • The fundamental concepts of Java technology are grasped quickly; programmers can be productive from the very beginning

  4. Object Oriented • Java technology provides a clean and efficient object-based development platform. Java programming language is designed to be object oriented from the ground up. • Distributed, client-server based systems coincide with the encapsulated, message-passing paradigms of object-based software. • Programmers can access existing libraries of tested objects • Providing functionality from basic data types through I/O and network interfaces to graphical user interface toolkits. • These libraries can also be extended to provide new behavior.

  5. Portable & Distributed • Java technology is designed to support applications that will be deployed into heterogeneous network environments. • Applications must be capable of executing on a variety of hardware architectures • The Java Compiler generates bytecodes (an architecture neutral) to supply the diversity of operating environments • Intermediate format designed to transport code efficiently to multiple hardware and software platforms. • The interpreted nature of Java technology solves both the binary distribution problem and the version problem • The same Java programming language byte codes will run on any platform.

  6. Architecture Neutrality • Just one part of a truly portablesystem • The programs are the same on every platform • There are no data type incompatibilities across hardware and software architectures. • The architecture-neutral and portable language platform of Java technology is known as the Java virtual machine. • Java programming language compilers generate code tospecifiy an abstract machine.Then; • Specific implementations of the Java virtual machine provide the concrete realization of the virtual machine for specific hardware and software platforms. • The Java virtual machine is based primarily on the POSIX interface specification (an industry) • Standard definition of a portable system interface.

  7. High Performance • Performanceis always a consideration. • The interpreter can run at full speed without needing to check the run-time environment. • The Java platform achieves superior performance by adopting a scheme • The automatic garbage collector runs as a low-priority background thread • Ensuring a high probability that memory is available when required, • Leading to better performance. • Applications requiring large amounts of compute power can be designed such that • Compute-intensive sections can be rewritten in native machine code as required and interfaced with the Java platform. • In general, interactive applications respond quickly even though they're interpreted.

  8. Interpreted & Threaded • The Java interpreter can execute Java bytecodesdirectly on anymachine to which the interpreter and run-time system have been ported. • Modern network-based applications, such as the HotJava Browser for the WWW, can do several things at the same time • A user working with HotJava Browser can run several animations concurrently while downloading an image and scrolling the page. • Java technology's multithreadingcapability provides to build applications with many concurrent threads of activity. • Thus, multithreading results in a high degree of interactivity for the end user. • The Java platform supports multithreading at the language level with the addition of sophisticated synchronization primitives: • The language library provides the Thread class, • The run-time system provides monitor and condition lock primitives.

  9. Dynamic • Java Language and run-time system are dynamicin their linking stages; but • The Java Compiler is strict in its compile-time static checking • Classes are linked only as needed. • New code modules can be linked in on demand from a variety of sources, even from sources across a network. • In the case of the HotJava Browser and similar applications, interactive executable code can be loaded from anywhere, • enabling transparent updating of applications. • On-line services constantly evolve; • they can remain innovative and fresh, draw more customers, and encourage the growth of electronic commerce on the Internet.

  10. The Requirements to Write Any Java Program I We need two items to write a Java program for the Java 2 Platform, Standard Edition The Java2 Platform, Standard Editionhttp://java.sun.com/j2se/1.4.2/download.html Atext editor. We can use the simple editor NotePad included with the Windows platforms.

  11. There are lots of Java resources at the following sites: • http://java.sun.com/docs/books/tutorial/ • http://www.developer.com/java/ • http://www.javaprepare.com/index.html • http://java.sun.com/docs/books/jls/second_edition/html/jTOC.doc.html Textbooks: • “Java 2: The Complete Reference - Third Edition”, Patrick Naughton & Herbert Schildt, McGraw-Hill , 1999. • “Java Tutorial, Second Edition, Object-Oriented Programming for the Internet”, Mary Campione & Kathy Walrath, Addison Wesley, 1998 • “Learning Java” Patrick Niemeyer& Jonathan Knudsen O’Reilly, 2000. • “Object-Oriented programming with Java”,Barry J.Holmes & Daniel T. Joyce, Jones and Bartlett Pub. 2001.

  12. An Alternative Choice to Write a Java ProgramII: JBuilder8 • http://homepages.borland.com/ccalvert/JavaCourse/index.htm • http://info.borland.com/techpubs/jbuilder/jbuilder8/introjb/contents.html • http://java.sun.com/docs/books/jls/second_edition/html/jTOC.doc.html

  13. Creating the First Java Application • Create a source file. A source file contains text, written in the Java programming language. We can use any text editor to create and edit source files. • Compile the source file into a bytecode file The compiler, javac, takes the source file and translates its text into instructions that the Java Virtual Machine (Java VM) can understand. In other words: • The compiler converts these instructions into a bytecode file. • Run the program contained in the bytecode file. The Java interpreter installed on thecomputer implements the Java VM. • This interpreter takes the bytecode file and carries out the instructions by translating them into instructions that the computer can understand

  14. Write Once and Run Everywhere This important and famous property of the Java programming language means that: • When we compile our program, we don't generate instructions for one specific platform. • we generate Java bytecodes, which are instructions for the Java Virtual Machine (Java VM). • Whether our platform is Windows, UNIX, MacOS, or an Internet browser, ithas the Java VM • it can understand those bytecodes.

  15. Two Paradigms for Computer Programs There are two paradigms for a program construction: • Process Oriented Model • This model can be thought of as code acting on data • Procedural languages, such as C, characterize a series of linear steps (that is, code) • Object- Oriented Programming • Aims to manage increasing complexity. • Organizes a program around its data (that is object) and a set of well-defined interfaces to that data. • This program can be characterized as data controlling access to code.

  16. Object-Oriented Programming (OOP) • OOP is the core of Java • All Java programs are object-oriented. • This isn’t the option the way that it is in C++ • OOP and Java are integrated, it is important to understand its basic principles before writing java programs

  17. Abstraction The essential element of OOP is abstraction • It is the process of focusing on those features of something that are essentials for the task at hand and ignoring those that are not. • For a personal system, we are interested only in people objects and only the ones that are employed by the company. The skiers, golfers, and cyclists are not included. • The powerful way to manage abstraction is through the use of hierarchical classifications • This allows us to layer semantic of complex systems, breaking them into more manageable pieces. • From the outside, the car is a single object. Once inside, we see that car consists of several subsystems: steering, clutch pedal, brakes, sound system, seat belts, heading,, and so on. • Each subsystem is made of more specialized units. • The sound system consists of a radio, CD player, a tape player

  18. Hierarchical Abstractions • We have to manage the complexity of system through the use of hierarchical abstractions. • The data from a traditional process-oriented program can be transformed by abstraction into its component objects. • A sequence of process steps can become a collection of messages between these objects. • Each of these objects describes its own unique behavior. • We can threat objects as concrete entites that respond to messages telling them to do something This is the essence of object-oriented programming.

  19. It is important that we understand how the object-oriented concepts, forming the heart of Java, translate into programs • All OOP languages provide mechanism that help us to implement OO model they are • Encapsulation • Inheritance • Polymorphism • A programming language is said to support OO design if itsupports these three concepts in its syntax

  20. Encapsulation Perhaps the most important of the object-oriented concepts is that of encapsulation. • Encapsulation refers to the concept of making an object a "black box." In other words; • Encapsulation is the process of hiding all the details of an object that do not contribute to it's characteristics. In other words; • Encapsulation is the concept that an object should totally separate its interface from its implementation. • All the data and implementation code for an object should be entirely hidden behind its interface.

  21. Independent Interface from the Implementation • We can create an interface as public methods in a class. • As long as that interface remains consistent, the application can interact with our objects. • This remains true even if we entirely rewrite the code within a given method. • It allows us to hide the internal implementation details of a class. • The algorithm we use to find prime numbers might be proprietary. • We can expose a simple API to the end user, but we hide all of the logic used for our algorithm by encapsulating it within our class.

  22. Encapsulation (con’t) As summary; • When we use an object, we should not know its internal workings. • We don't need to understand how an object works. • An object should expose only the absolute necessary information needed to interface with it. • It should give us a friendly interface to those limited set of methods, and properties that the designer thought might be useful for other users.

  23. Encapsulation (con’t) This means: • An object should completely contain any data it requires, and • It should also contain all the code required to manipulate that data. Programs should interact with our object through an interface, • using properties and methods. • Client code should never work directly with the data owned by the object. .

  24. Encapsulation (con’t) In terms of object-oriented concept: • Programs interact with objects by sending messages to the object The messages: • are generated by other objects, or by external sources such as the user, and • indicate which method or property would be invoked. • The way the object reacts to these messages is through methods or properties

  25. Encapsulation (con’t) • In Java the basis of encapsulation is class • Objects are sometimes referred to as instance of a class Thus: • A class is a logical construct • An object has physical reality • The class defines the structure and behavior (data and code) that will be shared by a set of objects • When we create a class, we will specify the code and data that constitute that class. • These elements are called members of the class • The data defined by the class are referred to member variables or just methods (C/C++ programmer calls this as a function)

  26. Methods and Properties • Some of the information in the object may actually be directly accessible • Other information may require us to use a methodto access it . Because: • The information stored internally may be unnecessary for the user and • Only certain things can be written into the information space, and the object needs to check whether the program is outside the limits.

  27. Methods and Properties (con’t) • The directly accessible bits of information in the object are its properties. • The difference between data accessed via properties and data accessed via methods is that: • with properties you see exactly what you're doing to the object; • with methods, unless you created the object yourself, you just see the effects of what you're doing.

  28. Encapsulation: public methods can be used to protect private data

  29. The Purpose of a Class • Since the purpose of a class is to encapsulate complexity, there are mechanisms for hiding the complexity of implementation inside the class. • Each method or variable in a class may be marked as private and public. • The public interfaceof aclass represents everythingthatexternal users of the class need to know • The private methods and data can only be accessed by code that is a member of the class. • Any other code that is not a member of the class cannot access a private method or variable.

  30. All classes in Java technology are directly or indirectly derived from the Object class. • Some of the subclasses of Object class are: Boolean, Number, Void, Math, String, StringBuffer etc. Objects in Java

  31. What is an Object? • Objects are key to understandingobject-oriented technology. • When we look around, we cansee many examples of realworld objects: dog,desk, television set, bicycle, etc…. • These real-world objects share two characteristics: • They all have stateand behavior. For example: • Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, and wagging tail). • Bicycles have state (current gear, current pedal cadence, two wheels, number of gears) and behavior (braking, accelerating, slowing down, changing gears).

  32. ObjectDefinition • Software objects are modeled after real-world objects in that they have many states and behaviors. • A software object maintains its state in one or more variables. • A variable is an item of data named by anidentifier. A software object implements its behavior with methods. • A method is a function (subroutine) associated with an object. Finally; • An object is a software bundle of variables and related methods.

  33. The Distinction between a Class and an Object • A class creates a new data type that can be used to create objects. That is, • A class creates a logical framework that defines the relationships between its members • When we declare an object of a class, we are creating an instance of that class • An object has physical reality. • That is, an object occupies space in memory.

  34. Declaring Objects • When we create a class, we are creating a new data type. • We can use this type to declare objects of that type. Obtaining objects of a class is a two-step process • Firstly, it is required to declare a variable of the class type • This variable doesn't define an object • It is simply a variable that can refer to an object. • Secondly, it is required to acquire an actual, physical copy of the object and assign it to that variable. • We can do this using the new operator. • The new operator dynamically allocates memory for an object and returns a reference to it.

  35. Declaring an Object of the Box • The first line declares mybox as a reference to an object of the type Box. After this line • executes, mybox contains the value null, and indicates that it does not point to an • actual object yet. • The second line allocates an actual object and assigns a reference to mybox. • After the second line executes, we can use mybox as if it were a Box object. • But in reality, my box simply holds the memory address of the actual Box object. • Box mybox = new Box(); is a statement that combines the two stepsabove.

  36. Building blocks of a Java application: classes class Pendulum { float mass; float lenght= 1.0; int cycles; float position (float time) { …….. } ………….} • The Pendulum class contains three variables: • mass, length, and cycles. • It also defines a method position() • It takes a float value as argument and returns a float value. • Variables and method declarations can appear in any order, but variable initializers can’t use forward references to uninitialized variables.

  37. The Difference Object References from Pointers • An object reference appears to be similar to a memory pointer in C/C++. But, the difference between them is the key to Java’s safety: • We cannot manipulate references as in actual pointers. In other words: • We can not cause an object reference to point to an arbitrary memory location or manipulate it like an integer.

  38. A Closer Look at New • The new operator dynamically allocates memory for an object (during run time). Its general form:class-var= new classname(); • class-var is a variable of the class type being created • clasname is the name of the class that is being instantiated. • The class name followed by parentheses specifies the constructor for the class. • A constructor defines what occurs when an object of a class is created. • Constructors are an important part of all classes and have many significant attributes. • Since Java’s simple types are not implemented as objects, we don’t use newfor integers or character types. • They are implemented as normal variables.

  39. Classes (con’t) • Once we‘ve defined the Pendulum class, we can create a Pendulum object (an instance of that class) as follows: Pendulum p; p= new Pendulum( ); • The declaration of the variable p does not create a Pendulum object; • it simply creates a variable that refers to an object of type pendulum. • We still have to create the object, using the keyword. • When the pendulum object is created, we can access its variables and methods many times: p.mass=5.0; Float pos= p.position(1.0);

  40. Classes (con’t) • Two kinds of variables can be defined in a class: • Instance Variables • Static Variables • Every object has its own set of instance variables; • The values of these variables in one object can differ from the values in other object. • If we don’t initialize an instance variable when we declare it, it’s given a default value appropriate for this type.

  41. //This progam includes a method inside the box class. class Box {Box is new data type of the class. double width; double height;Box is used to declare objects for this type double depth;Class declaration only creates a template, not object //display volume a boxThis code does not cause any objects of type Box void volume ( ) { System.out.print (“Volume is”); System.out.println(width*height*depth); } } class BoxDemo3 { public static void main (String args [ ]) { Box mybox1 = new Box( );After this statement executes, mybox1 Box mybox2 = new Box ( );and mybox2 will be instances of Box // assign values to mybox1’s instance variables mybox1.width=10; mybox1.height=20; mybox1.depth=15; //assign different values to mybox2’ s instance variables mybox2.width=3; mybox2.height=6; mybox2.depth=9; //display volume of first and second boxes mybox1.volume();Invokes volume( ) method on mybox1 mybox2.volume();Invokes volume ( ) method on mybox1 } }volume () method is called relative to the mybox1 or mybox2 object,

  42. The Execution of General Form of the Class • We should call the file that contains this program BoxDemo.java, because the main () method is in the class called BoxDemo, not the class called Box. • When we compile this program, we will find that two .class files have been created, one for Box and one for BoxDemo. • The Compiler automatically puts each class into its own .class file • It is not necessary for both the Box and BoxDemo class to actually be in the same source file. • We could put each class in its own file, called Box.java and BoxDemo.java. • To run this program, we must execute BoxDemo.class. When we do, we will see the following output: Volume is 3000.0 Volume is 162.0

More Related