Presentation is loading. Please wait.

Presentation is loading. Please wait.

GENESIS OF JAVA & AN OVERVIEW SHASHI BHUSHAN SOCIS, IGNOU.

Similar presentations


Presentation on theme: "GENESIS OF JAVA & AN OVERVIEW SHASHI BHUSHAN SOCIS, IGNOU."— Presentation transcript:

1 GENESIS OF JAVA & AN OVERVIEW SHASHI BHUSHAN SOCIS, IGNOU

2 TODAY’S AGENDA §GENESIS OF JAVA §AN OVERVIEW OF JAVA

3 GENESIS OF JAVA §SOFTWARE DEVELOPMENT BURDEN §THE WEB REVOLUTION §NEW ORDER OF COMPUTING §PHILOSOPHY OF JAVA AND DESIGN GOALS §JAVA BUZZWORDS

4 GENESIS OF JAVA (CONTD..) §MULTIPLE INCOMPATIBLE HARDWARE ARCHITECTURE §MULTIPLE INCOMPATIBLE OPEARATING SYSTEM §INCOMPATIBLE GUI SOFTWARE DEVELOPMENT BURDEN:

5 GENESIS OF JAVA(CONTD..) §GROWTH OF INTERNET, WWW AND ELECTRONIC COMMERCE §THE WEB RESOLUTION WHOSE KEYPOWER IS “WRITE ONCE AND RUN ANYWHERE” MODEL REQUIRED A NEW PARADIGM

6 GENESIS OF JAVA (CONTD..) §DATA ONLY §INFORMATION IS STATIC §HTML IS NOT FLEXIBLE §LARGE NUMBER OF COMPUTING PROTOCOLS AND STANDARDS LIMITATIONS OF WEB TECHNOLOGY:

7 GENESIS OF JAVA (CONTD..) §JAVA PHILOSOPHY AND DESIGN GOALS “WRITE ONCE, RUN ANYWHERE ON ANY PLATEFORM”. THE NETWORK BECOMES THE DISTRIBUTION VEHICLE FOR S/W APPLICATION. IT MUST ENABLE THE DEVELOPMENT OF SECURE HIGH PERFORMANCE AND HIGHLY ROBUST APPLICATION ON MULTIPLE PLATFORM IN HETEROGENEOUS & DISTRIBUTED NETWORKS. §SUPPORT OF DYNAMIC ACTIVE PROGRAMS (APPLETS) FOR TRANSMISSION BETWEEN CLIENTS AND SERVERSATTATCHED TO INTERNET.

8 GENESIS OF JAVA (CONTD..) §AN APPLICATION IS A PROGRAM WHICH RUNS ON ANY COMPUTER UNDER THE OPERATING SYSTEM OF THAT COMPUTER. IT IS SIMILAR TO ANY APPLICATION PROGRAM CREATED BY C OR C++. §AN APPLET IS AN APPLICATION DESIGN TO BE TRANSMITTED OVER THE INTERNET AND EXECUTED BY JAVA COMPATIBLE BROWSERS. AN APPLET IS LIKE TINY JAVA PROGRAM DYNAMICALLY LOADED ACROSS THE NETWORK JUST LIKE AN IMAGE, SOUND FILE OR VIDEOCLIP. APPLETS AND APPLICATIONS:

9 AN OVERVIEW OF JAVA §SIMPLE, FAMILIAR AND OBJECT ORIENTED §ROBUST §HIGH SECURITY §ARCHITECTURE NEUTRAL AND PORTABLE §MULTITHREADED §HIGH PERFORMANCE §DISTRIBUTED AND DYNAMIC JAVA BUZZWORDS:

10 AN OVERVIEW OF JAVA (CONTD..) §JAVA.LANG §JAVA.IO §JAVA.AWT §JAVA.APPLET §JAVA.SQL §JAVA.NET §JAVA.UTIL JAVA API AND CORE LANGUAGE FUCTIONS:

11 AN OVERVIEW OF JAVA (CONTD..) §NO MORE TYPEDEFS, OR PREPROCESSOR §NO MORE STRUCTURE OR UNIONS §NO MORE MULTIPLE INHERITANCE §NO MORE GOTO STSTEMENTS §NO MORE AUTOMATIC COERSIONS §NO MORE POINTERS §NO MORE POINTER ARITHMETICS FETURES REMOVED FROM C & C++

12 AN OVERVIEW OF JAVA (CONTD..) THE FIRST SAMPLE PROGRAM: /* Using a text editor create a file named HelloWorld. java with the java code shown below */ class HelloWorld { public static void main (String[ ] args) { System.out.println (“HelloWorld”); //display the string } }

13 AN OVERVIEW OF JAVA (CONTD..) Compiling the source file using the JDK Javac HelloWorld.java The compiler creates a file named HelloWorld. Class in the same directory as the java source file HelloWorld.java. The class file contains platform independent java bytecodes. Run the application java HelloWorld

14 AN OVERVIEW OF JAVA (CONTD..) The System Class Member of the Java.lang package provides access to the system functionality such as standard I/O, copying arrays, getting the current date and time provides access to externally defined properties, means of loading files and libraries. System.out is a class variable that is a reference to an object implementing the standard output stream.

15 AN OVERVIEW OF JAVA (CONTD..) The System Class System.in is a class variable that is a reference to an object implementing standard input stream. System.err implements the standard error system. System class can not be instantiated.

16 AN OVERVIEW OF JAVA CONTD.. §NUMERIC DATA TYPES & CHARACTER DATA TYPES §BOOLEAN TYPES §ARRAYS MAIN FEATURES OF THE JAVA LANGUAGE: JAVA FOLLOWS C & C++ FAIRLY CLOSELY IN ITS SET OF BASIC DATA TYPES. THERE ARE ONLY THREE GROUPS OF PRIMITIVE DATA TYPES, NAMELY:

17 AN OVERVIEW OF JAVA CONTD.. §BYTE - 8 BIT SIGNED QUANTITY §SHORT - 16 BIT SIGNED QUANTITY §INT - 32 BIT SIGNED QUANTITY §FLOAT - 32 BIT IEEE 754 SPECIFICATION §LONG - 64 BIT SIGNED QUANTITY §CHARACTER DATA TYPE - 16 BIT UNICODE CHAR N UMERIC DATA TYPES:

18 AN OVERVIEW OF JAVA CONTD.. BOOLEAN DATA TYPES AND ARITHMETIC & RELATIONAL OPEARTORS: A JAVA BOOLEAN IS A DISTINCT DATA TYPE (TRUE OR FALSE) UNLIKE COMMON ‘C’ PRACTICE. A JAVA BOOLEAN TYPE CANNOT BE CONVERTED TO ANY NUMERIC TYPE 1 OR 0.

19 BOOLEAN DATA TYPES AND ARITHMETIC & RELATIONAL OPEARTORS (CONTD..) : ARITHMETIC & RELATIONAL OPEARTORS ARE : SIMILAR TO ‘C’ OR ‘C++’ >>> INDICATES UNSIGNED (LOGICAL) RIGHT + IS USED TO COMBINE TWO SHIFTS STRINGS System.out.println (“THERE ARE” + num + “CHARS IN A FILE”)

20 AN OVERVIEW OF JAVA (CONTD...) // a single dimension array of ints Syntax in Java is much the same as in C. Arrays are (references to) objects new operator is used to create an array Indexed with any integer type primitive int numbers[ ] = new int[10]; int [ ] numbers = new int[10]; Array’s size can not be changed after it is created For dynamically sized array, java.util.Vector class is used ARRAYS :

21 AN OVERVIEW OF JAVA (CONTD...) ARRAYS : All arrays have instance variable length which hold the size of the array. class Length { public static void main(String[ ] args){ int a1[ ] = new int [10]; int a2[ ] = {3, 5, 7, 1, 8, 99, 44}; System.out.println (“length of a1 is” + a1.length) System.out.println (“length of a2 is” + a2.length); }

22 AN OVERVIEW OF JAVA (CONTD...) ARRAYS OF OBJECTS : Arrays can contain any legal java data types including reference types such as object or other arrays. For example, the following declares an array of ten String objects. String[ ] arrayofStrings = new String[10];

23 AN OVERVIEW OF JAVA (CONTD...) ARRAYS OF OBJECTS (Contd...): The element in this array are reference types that is, each element contains a reference to a String object. At this point, enough memory has been allocated to contain String references but no memory has been allocated for the String objects themselves. If you attempt to access one of the array of Strings elements at this point, you will get NullpointerException because the array is empty and contains no String objects.

24 AN OVERVIEW OF JAVA (CONTD...) ARRAYS (Command line arguments explained): Explanation: This simple application displays each of its command line arguments on a line by itself. class Echo{ public static void main (String[ ] args) { for (int i = 0; i<args.length; i++) System.out.println (args [i] ); }

25 AN OVERVIEW OF JAVA (CONTD...) STRING: A sequence of char data is called a string and is implementd in the java environment by the String class (a member of the java.lang package). The following statement explicitly declares an array named args that contains String objects. The empty bracket indicates that the lenght of the array is unknown at compilation time because the array is passed in at run time. (String[ ] args) //declared with the main( ) “IGNOU” // literal strings “IGNOU”+ ”Conducts” + ”Java” + “Seminar”

26 AN OVERVIEW OF JAVA (CONTD..) : STRINGS: STRING CLASS IS A JAVA CLASS. AN INSTANCE OF A STRING CLASS IS OF COURSE AN OBJECT. IN JAVA EACH AND EVERY STRING IS AN OBJECT. STRINGS ARE NOT ARRAY OF CHARACTERS IN JAVA AS C-LANGUAGE.

27 AN OVERVIEW OF JAVA (CONTD...) STRING: String class methods: booleans equals (String object) int length ( ) char charAt(int index)

28 AN OVERVIEW OF JAVA (CONTD...) STRING: EXAMPLE 1: String methods class Stringmethods{ public static void main ( String[ ] args){ string strobj1 = “first string”; string strobj2 = “second string”; string strobj3 = strobj1; System.out.println (“Length of Strobj1:” + Strobj1.length( )); System.out.println (“Char at index 3 in strolbj1:”+strobj1.charAt (3)); If (strobj1.equals (strobj2)) System.out.println (“strobj1 == strobj2”); else System.out.println (“strobj1!= strobj2”); }

29 AN OVERVIEW OF JAVA (CONTD...) STRING: EXAMPLE :Character counting program class CountChar{ public static void main(String[ ] args) { throws java.io.IOException { int count = 0; while(System.in.read( )!= -1) count++; System.out.println(“Input has”+counts+”chars”); }

30 AN OVERVIEW OF JAVA (CONTD...) EXPLANATION : Throws java.io.exception This statement specifies that the main method can throw an exception called java.io.IOException. An exception is an event that occurs during the execution of a program that prevents the continuation of the normal flow of instructions. Main method does not throw any exception directly. But it can throw a java.io.IOException indirectly throgh its call to System.in.read.

31 AN OVERVIEW OF JAVA (CONTD...) System.in.read This method reads a single char and returns either the char that was read or, if there are no more characters to be read, -1.

32 FLOW CONTROL STATEMENTS SWITCH SYNTAX Z SWITCH STATEMENT IS IDENTICAL TO 'C' Switch (expression) { case value1: statement(s); break; case value2: statement(s); break; default: statement(s); break; } AN OVERVIEW OF JAVA (CONTD...)

33 Breaking out of Loops  Break  Continue  Break & Continue with Label  Without the Label, they are the same as C or C++ FLOW CONTROL STATEMENTS (Contd. …) AN OVERVIEW OF JAVA (CONTD...)

34 Date tomorrow = new Date( ); This single statement actually performs three functions: Declaration : Instantiation: Initialisation Creating, Declaring & Instantiating an object: OBJECT ORIENTED PROGRAMMING IN JAVA

35 Date tomorrow; Instantiation of an object: The new operator instantiates a class by allocating memory for a new object of that type. The new requires a single argument: a call to a constructor method. Declaration of an object: OBJECT ORIENTED PROGRAMMING IN JAVA (Contd....)

36 Constructor methods are special methods provided by each Java class that are responsible for initializing new objects of that type. The new operator creates the object and the constructor initializes it. The Date( ) Constructor used in Date tomorrow = new Date( ) does not take any argument. Declaration of an object: OBJECT ORIENTED PROGRAMMING IN JAVA (Contd...)

37 A constructor that takes no argument is called default constructor. Like Date( ), most classes have at least one default constructor. If a class has multiple constructors, they all have the same name but different number or types of arguments. Date class type provides with another constructor that initialises the new Date with a year, month & day. Date HisBirthday = new(1971, 6, 30); OBJECT ORIENTED PROGRAMMING IN JAVA (Contd...)

38 §No need of keeping track of objects for destroying. §The Java runtime environment has a garbage collector that frees the memory used by objects that are no longer needed. AN OVERVIEW OF JAVA (Contd...) OOP : GARBAGE COLLECTOR

39 §An object is eligible for garbage collection when there are no more references to objects. §The garbage collector run in a low-priority thread and runs both synchronously and asynchronously depending on the situation and the system on which Java is running. AN OVERVIEW OF JAVA (Contd...) OOP : GARBAGE COLLECTOR

40 A class declaration looks like this: [modifiers] class ClassName [extends SuperClassName] [implements Interface Names]{ -------- } The items between the square are optional. §modifiers declares whether the class is public, abstract or final. §extends is used to specify a single inheritance mechanism. §Implements is used to specify multiple inheritance. AN OVERVIEW OF JAVA (Contd...) OOP : CLASS DECLARATION

41 §In Java, every class has a superclass. If you don’t specify a superclass for your class it is assumed to be the Object class (declared in Java.lang). §The keyword extends is used to specify the superclass. Class NameOfClass extends SuperClassName { ---------- } AN OVERVIEW OF JAVA (Contd...) OOP : Declaring a class’s superclass

42 §The Public modifier declares that the class can be used by objects outside the current package. §The abstract modifiers declares that the abstract class may contain methods (without any implementation). These classes are intended to be sub-classed and can not be instantiated. AN OVERVIEW OF JAVA (Contd...) OOP : Public, Abstract and Final Class

43 §The Final modifier declares that this class can not be sub-classed for security reasons & design reasons. §It does not make sense for a class to be both final and abstract. AN OVERVIEW OF JAVA (Contd...) OOP : Public, Abstract and Final Class (Contd...)

44 Object Oriented Program Package: *A package is a container of related classes and interfaces *Used to access related classes as well as to hide the internals of a package *Package is declared using package keyword *Package my-package; *Package java.awt.image; *All of Java built-in classes are put under the Java package. Six sub packages are defined under the Java package *Java.lang, Java.awt, Java.io, Java.net, Java.applet, Java.util AN OVERVIEW OF JAVA (CONTD...)

45 Object Oriented Feature (Contd. …).The Import Statement.The Import statement loads existing classes for using their definition and methods Example:.Importing the class from the package: import Java.awt.Graphics;.Importing the entire package that the class belongs to: Import Java.awt.*. AN OVERVIEW OF JAVA (CONTD...)

46 JAVA OVERVIEW(CONTD…) OOP : Access control mechanism: §PRIVATE §PROTECTED §PUBLIC §STATIC §FINAL §ABSTRACT

47 JAVA OVERVIEW (CONTD…) OOP : Access control mechanism: PRIVATE: §A PRIVATE MEMBER AND METHOD ARE ACCESSIBLE ONLY TO THE CLASS IN WHICH IT IS DEFINED. EXAMPLE: class Alpha { private int privateX; private void privateMethod( ){ System.out.println(“PrivateMethod”); } } Contd....

48 JAVA OVERVIEW (CONTD…) OOP : Access control mechanism: PRIVATE: Class Beta { void accessMethod ( ) { Alpha a = new Alpha ( ) ; a.privateX = 15; // illegal a.privateMethod ( ) ; // illegal } Beta class cannot access private variable and private methods on an object of type Alpha because Beta is not the type of Alpha.

49 JAVA OVERVIEW (CONTD…) OOP : Access control mechanism: PRIVATE: objects of same type can access to one another’s private members This is because access restriction apply at the class or type level rather than at the object level. example: class Alpha { private int privateX ; boolean isEqualTo (Alpha anotherAlpha) { if (this.privateX= =anotherAlpha.privateX) //legal return true; else return false; } this is a java language keyword that refers to the current object.

50 JAVA OVERVIEW (CONTD…) Protected § It allows the class itself, subclasses anc all classes in the same package to access the members. Package Greek ; Class Alpha { //declared in a Greek package protected int protectX ; protected void protectedMethods ( ) { system.out.println(“protectedMethods”) } }Contd....

51 JAVA OVERVIEW (CONTD…) Protected § Suppose class Gama is also declared to the member of the Greek package. The class can legally access an alpha object’s privateX member variable and protected. § Methods Package Greek ; Class Gamma{ void access Method ( ) { Alpha a = new Alpha ( ) ; a.protectX=10; // legal a.protectedMethod ( ) ; // legal }

52 JAVA OVERVIEW (CONTD…) Public § Any class in any package has access to class’s public member. Declare members to be public only if such access cann’t produce undesirable results if an outsider uses them. Package Greek ; public class Alpha { public int publicX; public void publicMethod ( ) { System.out.println (“publicMethod”); } Contd....

53 JAVA OVERVIEW (CONTD…) Public § Beta class is defined ina different package and not a sub class of Alpha. Package Roman ; import Greek.*; Class Beta { void accessMethod ( ) { Alpha a=new Alpha ( ) ; a.publicX = 10; // legal a.publicMethod ( ) ; // legal } Beta can legally inspect and modify the public X and public method in Alpha class.

54 JAVA OVERVIEW (CONTD…) OOP : What Method does subclass inherit §Subclasses inherit those superclass method declared as public or protected §SubClass inherit those superclass methods declared with no acces specifier as long as the subclass is in the same package as the superclass §Subclass don’t inherit a superclass’s method if the subclass declares a method using the same name. The method in the subclass is said to override the one in the superclass. §Subclass do not inherit the superclass private methods

55 JAVA OVERVIEW (CONTD…) Static §Used with methods and variables but not with classes §used to specify a method that can be declared only once such as main ( ) §no sub classes are allowed to implement the method of the same name §static methods are not over ridden in subclasses

56 JAVA OVERVIEW (CONTD…) Final §Used with classes, methods and variables §when used with a class this class never have any subclass §when used with any method, this method cannot overridden. §When used with any member variable the variable remains constant. Contd...

57 JAVA OVERVIEW (CONTD…) Final example 1: // final used with a class final class classFinal { : } class classInherited extends classFinal{ // illegal : } Contd...

58 JAVA OVERVIEW (CONTD…) Final (Contd...) example 2: class NeverChanging { // a Final variable Final int unchangeble = 21; // a Final method Final int unchangingMethod (int a, int b) { } It defines a complete programming interface, providing its subclasses with the method declaration for all of the methods necessary to support the interface.

59 JAVA OVERVIEW (CONTD…) Abstract classes & methods §An abstract class is a class that can only be sub- classed - it can not be instantiated. §It contains abstract methods with no implementation. It defines abstract methods. §Used to create a template class or methods. §Similar to function prototypes in C or C++.

60 JAVA OVERVIEW (CONTD…) Abstract classes & methods abstract class GraphicsObject { int x, y; : void moveTo (int newX, int newY) { : } abstract void draw ( ) ; // to be implemented by all //subclasses } Contd....

61 JAVA OVERVIEW (CONTD…) Abstract classes & methods Each non-abstract subclass of Graphic object such as circle and rectangle has to provide an implementation for the draw method. class Circle extends GraphicObject { void draw ( ) { : } class Rectangle extends GraphicObject { void draw ( ) { : } An abstract class is not required to have an abstract method in it. But any class that has an abstract method in it or does not provide an implementation, any abstract method declared in its superclass must be declared as an abstract class.

62 JAVA OVERVIEW (CONTD…) OOP : Interface §An interface is a collection of abstract methods (without implementations) and constant values §Interfaces add most of the functionality that is required for many applications which would normally resort to using multiple inheritance in a language such as C++ §Java program can use interface to make it unnecessary for related classes to share a common abstract super class to add methods to object Contd. …

63 JAVA OVERVIEW (CONTD…) OOP : Interface §Accessing multiple implementation of an interface through an interface reference variable is the most powerful way that Java achieves run-time polymorphism. §Because dynamic lookup of a method at run-time incurs a significant overhead when compared with normal method invocation in java, you should be careful not to use interfaces casually in performance critical code. Contd. …

64 JAVA OVERVIEW (CONTD…) OOP : Interface A class may be declared to directly implement one or more interfaces, meaning that any instance of the class implements all the abstract methods specified by the interface or interfaces. Contd. …

65 JAVA OVERVIEW (CONTD...) OOP : Interface Declaration §interface Countable { the interface body; } §[Public] interface InterfaceName [extends list of SuperInterfaces]{ the interface body ------- }

66 JAVA OVERVIEW (CONTD...) OOP : Interface The public access specifies indicates that the interface can be used by any class in any package. If you do not specify that your interface is public, then your interface will only be accessible to classes that are defined in the same package as the interface.

67 OOP : Interface The extends clause is similar to the extends clause in a class declaration. However an interface can extend multiple interface (a class can only extend one), and an interface cannot extend classes. JAVA OVERVIEW (CONTD...)

68 OOP : Interface Example interface Collection{ int MAXIMUM = 500; void add (Object obj); void delete (Object obj); int currentCount( ); }

69 class TestStack{ public stack void main (String [ ] args) { Stack mystack1 = new Stack(l0); Stack mystack2 = new Stack (8); // push some members onto the stack for (int i=0; i<10, i++) mystack1.push (i); for (int i=0; i<8, i++) mystack2.push (i); // pop off the stack for (int i=0; i< 10; i++) System.out.println(mystack1.pop( ) ); } Stack AN OVERVIEW OF JAVA (CONTD...)

70 Recursion class Factorial { // This is a recursive function int fact (int n) { int result, if (n==1) return 1; else result = n * fact(n- 1); return result; } AN OVERVIEW OF JAVA (CONTD...)

71 class Recursion { public static void main(String args [ ] { Factorial f = new Factorial( ); System.out.println ("factorial of 3 is" +f.fact(3)); } AN OVERVIEW OF JAVA (CONTD...)


Download ppt "GENESIS OF JAVA & AN OVERVIEW SHASHI BHUSHAN SOCIS, IGNOU."

Similar presentations


Ads by Google