Full Transcript

HISTORY Java is the general purpose, true object oriented programming language and is highly suitable for modeling the real world and solving the real world problems. In the company Sun Microsystem, James Goslings(an employee there), started a project named „GREEN‟. He begun his work by atte...

HISTORY Java is the general purpose, true object oriented programming language and is highly suitable for modeling the real world and solving the real world problems. In the company Sun Microsystem, James Goslings(an employee there), started a project named „GREEN‟. He begun his work by attempting to extend C++ features for developing embedded software for electronic companies to automate the electronic devices such as microwave. But this could not be accomplished by C++. So James Goslings started to develop a new language known as “OAK”. In may 1995, Sun officially announced this language at Sunworld 95.Due to some reasons, the name was changed to “JAVA”. The prime motive of Java was the need for a platform independent language. The second motive was to design a language which is „Internet Enable‟. To run the programs on internet, Green project team come up with the idea of developing “Web Applet”. The team developed a web browser called “Hot Java” to locate and run applet programs on internet. The most striking feature of Java is „Platform neutralness‟. Java is the first language that is not tied to any particular hardware or O.S. JAVA MILESTONES YEAR DEVELOPMENT 1990 Sun Microsystem started to develop special software for electronic devices. 1991 James Goslings developed a new language “OAK”. 1992 New language Oak was demonstrated in hand held devices. 1993 World Wide Web(WWW)appeared on internet and transformed the text based internet to graphical environment. Sun developed Applets. 1994 Green project team developed a web browser “Hot Java” to run applets on internet. 1995 Oak was renamed to JAVA. 1996 Java established itself as the leader of internet programming. Sun released Java development kit 1.0. 1997 Sun released Java development kit 1.1(JDK 1.1). 1998 Sun released Java2 with version 1.2(SDK 1.2). 1999 Sun released Java2 standard edition (J2SE) and Enterprise Edition (J2EE). 2000 J2SE with SDK 1.3 was released. 2002 J2SE with SDK 1.4 was released. 2004 J2SE with JDK 5.0 was released. CHARACTERISTICS OF JAVA 1. SIMPLE: Java was designed to be easy for the professional programmers to learn and use effectively. Java uses C/C++ syntax. So, it is very simple to work in Java. 2. OBJECT ORIENTED: Java is true object oriented language. Almost everything in Java is an object. All program code and data reside within class and object. Java has a lot of classes arranged in packages that we can directly use in our programs. 3. ROBUST AND SECURE: Java is a robust language. Here robust means, it provides many safeguards to ensure reliable code. It has strict compile-time and run-time checking for data types. It also has the concept of exception handling to capture errors and remove the fear of crashing the system. Security is the major issue, when you work on internet. That‟s why, it not only verify all memory access but also ensure that no viruses are entering through the applet. That‟s why it doesn‟t use pointers. 4. PLATEFORM INDEPENDENT AND PORTABLE: Java is portable that means Java programs can be easily moved from one system to another, anywhere, anytime. Java ensures portability in two ways. First, Java compiler generates bytecode instructions that can be implemented on any machine. Second, the size of primitive data types are machine dependent. Java is platform independent that means changes and upgrades in operating system, processors and system resources will not force any change in java programs. This is why Java is famous for programming on internet which interconnects different kinds of system worldwide. 5. COMPILED AND INTERPRETED: Usually, a language is either compiled or interpreted. Java combines both the approaches. First, java compiler translates the source code into bytecode. Bytecode is intermediate form, not the machine instruction. In the second stage, Java interpreter translate this bytecode to the machine code, which can be directly executed by the machine that is running the Java program. 6. MULTITHREADED AND INTERACTIVE: Multithreaded means handling multiple tasks simultaneously. Java supports multithreading programming. For example: we can listen an audio while typing or scrolling a page. Java comes with tools that support multiprocess synchronization and construct smooth running interactive system. 7. DISTRIBUTED: Java applications can open and access remote objects on internet as they easily do in a local system. This enables multiple programmers at multiple remote locations to collaborate and work together on a single project. JAVA VIRTUAL MACHINE(JVM) Java compiler translates the source code into intermediate code, known as byte code for a machine, that is called “Java virtual machine” and it exists only inside the computer memory. Then the computer interpreter convert the intermediate code into red machine code and it makes the java “Platform Independent” or “Architecture Neutral”. Java Program --compiler---- Intermediate Code interpreter Machine Code For virtual machine For real machine All interpreters are different for different machines. JAVA DEVELOPMENT KIT: Java development kit comes with a collection of tools used for developing and running java programs. They include- 1. APPLET VIEWER: To view Java applets. 2. JAVAC: To compile the java programs. 3. JAVA: To run java programs (interpreter). 4. JAVAP: It is java deassembler, which converts bytecode files into program description. 5. JAVAH: It produces header files for use. 6. JAVADOC: It is used for HTML documents. 7. JDB: It is java program debugger. API (APPLICATION PROGRAMMING INTERFACE) API (Application programming interface) is a document that contains description of all the features of a product or software. It represents classes and interfaces that software programs can follow to communicate with each other. An API can be created for applications, libraries, operating systems, etc. It is a java standard library. API includes hundreds of classes and methods grouped into several packages. Most common packages are: 1. INPUT/OUTPUT PACKAGE(java.io.*): It contains collection of classes required for input/output. 2. AWT PACKAGE(java.awt.*): Abstract window toolkit contains classes required for GUI. 3. APPLET PACKAGE(java.applet.*): It contains classes for applets. 4. NETWORKING PACKAGE(java.net.*): It contains classes for networking. 5. UTILITY PACKAGE(java.util.*): It contains classes for different utilities. Ex; date/time. A PROGRAM TO DEMONSTRATE A SIMPLE PROGRAM TO PRINT SOMETHING ON THE SCREEN: class example1 { public static void main(String args[]) { System.out.print(“Hello”); System.out.println(“Good morning”); } } A PROGRAM TO DEMONSTRATE THE USE OF VARIABLE: class Example2 { public static void main(String args[]) { int num; num=10; System.out.println(“The value is:” + num); } } HOW TO SAVE AND RUN THE PROGRAM:  Save this program with the name Example2.java.  Click on start menu and go to run option.  Write cmd.  Now command pompt will be opened.  Go to the location where program is saved.  Set the path of that location in command prompt.  Give command to compile the program: javac example2.java  Give the command to run the program. : java example2 Here compilation is compulsory before running the program. When you compile a program, if there is a mistake in program, it shows on the screen with line and location also. JAVA KEYWORDS There are 50 keywords defined in language java. The keywords const and goto are reserved but not used. Abstract Continue for new Switch Assert Default Goto package synchronize boolean Do if private This break Double implements protected throw byte Else import public transient case Enum Instance of return try catch Extends int short void char Final interface static throws class Finally long strictfp volatile const Float native super while JAVA DATATYPES java datatypes aredivided into four categories: 1. Integer: NAME WIDTH RANGE 1. Byte 8 -128 to 127 2. Short 16 -32768 to 32767 3. Int 32 -2147483648 to 2147483648 4. Long 64 -9223372036854775808 to +ve 2. FLOATING POINT: NAME WIDTH RANGE 1. Double 64 4.9e-3.24 to 1.8e+308 2. Float 32 1.4-045 to 3.4e+038 3. CHARACTER: Char ----16 bit----0 to 65536 4. BOOLEAN: It provides “true” or ”false” result. It is used with all relational operators. CLASS: class is the combination of data and its associated functions together. The data means the variables. These functions and variables are collectively called “class members” i.e data members and function members. A class is defined by class keyword. Class contains all the features of OOPS. Ex: class classname { datatype var_name; returntype function_name( ) { ……….. ……… } } OBJECT: An object is essentially a block of memory that contains space to store all the instance variables. Creating an object is called “instantiating” a object. Objects are created using the keyword “new”. The new keyword creates an object of the specified class and allocates memory and returns a reference to that object. Ex: Suppose Box is a class- Box b=new Box( ) Here b is an object of class Box. PACKAGE: When java interfaces and classes are grouped together in to single unit that is called “Package”. Programs are organized as set of packages. Each package has its own names. The naming structure for packages is hierarchical. Packages also help us to avoid class name collision when we use the same class as that of others. To create a package, simply include a package command as the first statement in the java source file. Any class declared within that file will belong to the specified package. Syntax: Package To use a package in your program, you must use following syntax- Syntax: import java.,*; ex: import java.util.*; The classes and interface of util package will be accessible to the program. INHERITANCE: The mechanism of deriving a new class from an old name is called “Inheritance”. The old class is known as base class or super class or parent class and the new one is called derived class or sub class or child class. The inheritance allows subclasses to inherit (reuse) all the variables and methods of their parent class. Inheritance can be of following types in java- 1. SINGLE INHERITANCE: (Only one super class) In single inheritance, only one super class is created that means there is only one super class and derived class. A //Super class or Base class // Derived class B The keyboard “extends” is used to call a super class in derive class. e.g class room { int length, breadth; void show( ) { System.out.println(“L:”+ length+ “B:” + breadth); } } Class bedroom extends room { int height; void display( ) { System.out.println(“Height:”+h); } void volume( ) { System.out.println(“L * B * H:” + (length * breadth * height)); } } class simpleinheritance { public static void main(String args[]) { room t=new room( ); bedroom m=new bedroom( ); t.length=10; t.breadth=20; System.out.println(“Controls of Super class”)); t.show( ); m.height=50; System.out.println(“Controls of Derive class”)); m.volume( ); } } 2. MULTIPLE INHERITANCE: Java does not implements multiple inheritance directly. This concept is implemented using a secondary inheritance path in the form of interfaces. INTERFACE: An interface is actually a kind of class. Like classes, interface contains methods and variables but they define only abstract mehods and final fields. Abstract methods are those methods which do not specify any code to implement these methods and data fields(variables) contain only constants. In this way interface contains methods which do not have the code to implement them and variables having only constant value. Therefore, it is the responsibility of that class which implements them to define the code for implementation of these methods. Syntax: To define an interface- interface interface_name { Variable declaration; Methods declaration; } Interface is used as “super class” whose properties are inherited by other classes. “implements” keyword is used to inherit an inherit an a class. e.g interface Area { float pi=3.14; float compute(float x, float y); } class Rectangle implements Area { float compute(float x, float y) { return (x*y); } } class Circle implements Area { float compute(float x, float y) { return(pi*x*y); } } class interface_test { public static void main(String args[]) { Rectangle r=new Rectangle( ); Circle c=new Circle( ); Area a; a=r; System.out.println(“AREA OF RECTANGLE:”+ a.compute(10,20) ); a=c; System.out.println(“AREA OF RECTANGLE:”+ a.compute(10,20) ); } } 3. MULTILEVEL INHERITANCE: A B c When a class A serve as a base class for the derive class B, which again serve as base class for the derive class C, this is called Multilevel Inheritance” and this chain A->B->C is called “Inheritance Path”. Ex: class Car{ public Car() { System.out.println("Class Car"); } public void vehicleType() { System.out.println("Vehicle Type: Car"); } } class Maruti extends Car{ public Maruti() { System.out.println("Class Maruti"); } public void brand() { System.out.println("Brand: Maruti"); } public void speed() { System.out.println("Max: 90Kmph"); } } public class Maruti800 extends Maruti{ public Maruti800() { System.out.println("Maruti Model: 800"); } public void speed() { System.out.println("Max: 80Kmph"); } public static void main(String args[]) { Maruti800 obj=new Maruti800(); obj.vehicleType(); obj.brand(); obj.speed(); } } 4. HIERARICHAL INHERITANCE: A B B B As you can see in the above diagram that when a class has more than one child classes (sub classes) or in other words more than one child classes have the same parent class then such kind of inheritance is known as hierarchical. Example: class A { public void methodA() { System.out.println("method of Class A"); } } class B extends A { public void methodB() { System.out.println("method of Class B"); } } class C extends A { public void methodC() { System.out.println("method of Class C"); } } class D extends A { public void methodD() { System.out.println("method of Class D"); } } class MyClass { public void methodB() { System.out.println("method of Class B"); } public static void main(String args[]) { B obj1 = new B(); C obj2 = new C(); D obj3 = new D(); obj1.methodA(); obj2.methodA(); obj3.methodA(); } } CONSTRUCTOR: Constructor is a special type of method that enables an object to initialize itself when it is created. Constructors have the same name as the class itself. They do not specify a return type not given void because they are used for creating objects (instances) only. Ex: class Rectangle { int length, width; rectangle(int x, int y) { length=x; width=y; } void area( ) { System.out.println(“Area is:”+(x*y)); } } class RectangleArea { public static void main(String ars[]) { Rectangle r=new Rectangle(15,10); r.area( ); } } TYPES OF CONSTRUCTORS: 1. Default Constructor- The constructor which has no parameters. 2. Parameterized constructor- The constructor which has some parameters. 3. Multiple constructor- A class can contain multiple constructors with different parameters. 4. Copy constructor- A constructors which copies the value of one object to another. COPY CONSTRUCTOR: A copy constructor is a constructor that takes only one parameter that is the same type as the class in which the copy constructor is defined. A copy constructor is used to create an object that is the copy of its parameter but it is an actually independent copy. Example: class Complex { private double re, im; public Complex(double re, double im) { this.re = re; this.im = im; } Complex(Complex c) { System.out.println("Copy constructor called"); re = c.re; im = c.im; } public String toString() { return "(" + re + " + " + im + "i)"; } } public class Main { public static void main(String[] args) { Complex c1 = new Complex(10, 15); Complex c2 = new Complex(c1); Complex c3 = c2; System.out.println(c2); // toString() of c2 is called here } } GARBAGE COLLECTION: Java deallocates the objects automatically, this technique is called “Garbage collection”. It is similar t C++ delete operator option but it is done manually in C++ while garbage collection is automatic. When no reference to an object exist that object is assumed to be no longer needed and memory occupied is freed by garbage collector automatically. THIS KEYWORD: This keyword is used to resolve the ambiguity between instance variables(class variables) and parameters, when the names of instance variables and parameters are same. This can be also used inside any method to refer to the current object. This is always a reference to the object on which the method was invoked. Example: class Rectangle { int length, width; void calculate(int length, int width) { this.length=x; this.width=y; } void area( ) { System.out.println(“Area is:”+(x*y)); } } class RectangleArea { public static void main(String ars[]) { Rectangle r=new Rectangle(15,10); r.calculate( ); r.area( ); } } ABSTARCT CLASS: A class that contains one or more abstract methods (methods which have no definition), is called “Abstract Class”. In other words, it is a superclass that declares the structure of a given abstraction without providing a complete implementation of every method. To declare a class abstract, you just need use simply “abstract” keyword infront on class keyword. There can be no objects of abstract class. An abstract class can not be directly instantiated with the new operator, you can not declare abstract constructors. Example: abstract class A { abstract void callme( ); void callmetoo( ) { System.out.println(“THIS IS NOT ABSTRACT METHOD”); } } class B extends A { void callme( ) { System.out.println(“ CALLME ABSTRACT METHOD IMPLEMENTATION IN B”); } } class abstarctdemo { Public static void main(String args[]) { B b=new B( ); b.callme( ); b.callmetoo( ); } } FINALIZE METHOD ( ): Finalization is just the opposite process of initialization. Constructor is used to initialize an object when it is declared; this process is called “Initialization”. Java automatically frees up the memory resources used by the objects but objects may hold other non object resources like file descriptors or window system fonts. The java run-time (Garbage Collector) cannot free these resources. In order to free these resources we must use “finalize method ( )”. This is similar to destructor in C++. The finalize method ( ) is simply finalize ( ) and can be added to any class. Java calls that method whenever it is required. ARRAY An array is a group of like-typed variables that are referred by a common name a specific element in an array is accessed by its index. Array offers a convenient way of grouping related information. Java array is an object the contains elements of similar data type. It is a data structure where we store similar elements. We can store only fixed set of elements in a java array. Array in java is index based, first element of the array is stored at 0 index. Advantage of Java Array o Code Optimization: It makes the code optimized; we can retrieve or sort the data easily. o Random access: We can get any data located at any index position. TYPES OF ARRAY IN JAVA There are two types of array. o Single Dimensional Array o Multidimensional Array One-dimensional array A one dimensional array is essentially a list of like typed variable to create an array, you must use following syntax- Type variable-name [ ]; Ex- int month_days [ ]; This means, all the month_days which are about to insert in this month_days array must be integer. Still actually no array exists. The value of month_days is set to NULL, which means an array with no value. To make an array actual, physical array of integers, you must use new operator.New is a special operator that allocates memory. Ex- month_days= new int ; On execution month_days will refer to an array of 12 integers and all elements will be initialized to zero. Ex- class array { public static void main (String args [ ]) { int month_days [ ]; month_days= new int ; month_days =1; month_days =2; month_days =3; System.out.println (“Days:”+ month_days ); } } MULTIDIMENSIONAL ARRAY Data is stored in row and column based index (also known as matrix form). Syntax 1. dataType[][] arrayRefVar; (or) 2. dataType [][]arrayRefVar; (or) 3. dataType arrayRefVar[][]; (or) 4. dataType []arrayRefVar[]; Example: class doublearray { public static void main(String args[]) { int arr[][]={{1,2,3},{2,4,5},{4,4,5}}; for(int i=0;i

Use Quizgecko on...
Browser
Browser