Anda di halaman 1dari 30

Java Presentation

Java was developed by a team lead by James Gosling at Sun Microsystems and released in 1995. It was originally intended as a lanuage for creating embedded software in consumer electronic devices. The internet and WWW made it a popular lanuage for creating platform independent programs. Java is a pure objected language. Java is case sensitive.The class name in the program and the filename should match in case. Java is available in many editions:Standard Edition, Enterprise Edition and micro Edition. All java Keywords must be typed in lowercase.

Simple: Java is simple compared to C++.Java has

omitted complex and confusing features of C++. Object Oriedent: Java has been designed as a pure Object-Oriented inheritance. Distributed:Java programs can be distributed and accessed over the using TCP/IP,UDP and FTP Robust:Major causes of failures are run-time errors and memory is strongly typed language, type checking is carried out at both compile and runtime . java uses automatic garbage collection . Secure: In Java security is an impoytant concern.

Architecture Neutral:Java compiler compiles the

source code and generates bytecode. which is intermediate between source and native machine code.using JVM . Protable: porthing an interpreter for JVM to any system all code compiled for it will run on that system. Interprted: The bytecode is then interpreted by a java interpreter which converts the bytecode into platfrom specific code and executes it. Multithreaded: Multithreaded is the ability of an application to execute more than one task at same time. High Performance:Java language supports many high performance features such as multithreading &compilation

javac: The java converts source code into bytescodes. java: The java application launcher.converts & executes bytecode. javadoc: The java commenting tool.creates HTML help file form
java source code. jdb: The java debugger.steps through programs as they executes,sets breakpoints and monitors behavior. javap: Disassembling classes.lists methods and class members. javah: Header file generator.used when combining java with c/c++ programs. jar: Java archive (JAR) manager.combines and compresses multiple files and directories into one. appletviewer: The java applet launcher.

Comments: Java supports three types of comments. 1-Single line comment :-It begin with a // and ends with new line
charater. Eg : // everything to the end of line is comments. 2-Multiline comment :- It starts with /* and ends with */ . Eg: /* this is multiline comments */ . 3-Javadoc comments :- It starts with /** and ends with */. Eg: /** everything here is a javadoc comment */ . Java Keywords :keywords are the predefined words of language. Data Types: It provides a set of built-in types as well as allows the user to create user defined types. Total 8 primitive data types in java. 1-Integer types : byte , short , int, long . 2-Floating points types : float , double . 3-Character typs : char . 4-Boolean type : boolean .

Variables: variables hold value in a program.all variable must be

declared before they can be used. syntax-type identifier [=value ..] 1-Instance variables (Non static fields) : It belong to an object.object store their states in non static fields,it declared without the static keyword .its values may be different for different objects . 2-Class variables (Static fields) : a class variable belongs to a class.its declared with the static modifier.the compiler that there is exactly one copy of this variable, how many objects of the class are created. 3-Local Variables : It belong to a maethod .they are declared and can be used in that method and are not accessible in the rest of program. 4-parameters: Recall the signature for the main mathod : public static void main (String[] args). Here the variable args is the parameter to this methode. A methode accepts inputs in parameters. Literals : A literal is value assigned to a variable. Eg int a=10 .

Constant :variable whose value cannot be changed is constant.

In java a constant is created using the final keyword. Syntax final type identifier = value ; Eg final float pi = 3.142 ; Operators: Java provides a rich set of oprators for manipulating program data.that requires one operand is called unary operator. Two requires is called binary .and three requred is called ternary. 1- Arithmatic Operators: + , - , *, / , % . 2- Relational Operators: < > ,<= ,>= ,== ,!= . 3-Logical Operators: && , || , ! . 4-Assignment Operators: = , += ,- = . 5-Increment /Decrement Operators: ++ , - - . 6-Conditional Operators: ?: . 7-Bitwise Operators: & , | , ~ ,^, << , >> , >>> . 8-Special Operators: instanceof , Dot Operator (.) , new .

Control statements are used to control the flow of execution of statements in program. These statements are two categorized 1- Selection Statements : if , if-else ,Switch . 2-Loop Control Statements : while , do-while ,for . if statement : this statement executes a statement or group of statements only if the condition is true. otherwise false.not execute. Syntax- if(condition ) ststement ; Eg- if(errorcondition == 1) display() ; if-else Statement: This statement executes a statement or group of statement if the condition is true and another statement or group if the condition is false. it is a two way decision making statement. Syntax if (condition ) statement1;else statement2 ; Eg- if (n%2==0 ) System.out.println (Even Number); else System.out .println (Odd Number) ;

Switch Stetement:this is multi-way decision making statemaent

the value of an expression is matched with case values. When a match is found the statement belonging to that case is executed.if none of case values match the default case is executed. SyntaxEg- char ch =r switch (expression) switch (ch) { { case const expr1://statements caseb:System.out.println(Blue ); break; break; case const expr2://statements caser:System.out.println(Red); break; break; default : // statements default:System.out.println(Invalid); } //end of switch }

Loop Control Statement : These statement are used to repeat the execution of code for a fixed number of times or till some condition is satisfied . Condition should be boolean. Three loop statements. while loop : The while loop is a top-tested or entry controlled loop.this loop executes statement as the condition is true .as soon as the condition becomes false control comes out of the loop. Syntax Eg- i=0; while (condition) while (i<=10) { { System.out.println (value = +i); loop statement i=i+1; } } do-while loop: The do-while loop is bottom tested or exit controlled loop . this loop executes statement as the condition is true . the condition becomes false control comes out of the loop.

Syntax Eg int i=10 d0 d0 { { loop stetements System.out.println (value = +i); }while (condition) } while (--i>0); for loop : The for combines initialization,condition and loop expression in a single statement. this loop executes statement as the condition is true . the condition becomes false control comes out of the loop. Syntax Eg int i=1; for(initialization;condition;loop-expression) for(i=0;i<=10;i++) { { loop stetements System.out.println (value = +i); } }

Arrays: An array in java is an object which stores a group of

variable s of the same type,referred to by a comman name. it may be of primitive or non primitive types.elements accessed by an index pr subscript . These are two types of array. 1- One-Dimensional Arrays: Syntax type array-name []; 2- Multi-Diamensional Arrays: Syntax type [][].[] array-name. Command Line arguments: Recall the defination of the main mathod .main is defined as a method which accepts an array of string a as command line arguments.these are the arguments passed to the main function when its starts executing.all stored as strings. Syntax: java classname arg1 arg2 . Input from console: Input values can be passed to program using command line arguments or from the console. and System.out are predefined stream objects.

Classes &objects are at the heart of object oriented programming. A class is blueprint for an object and an object is an instance of class. Defining Classes : A class define a new type.this new type can be used to create objects of that type.there are three type of classes 1-Concreate Class:concrete class is a class which can be instantiated. It is a complete class which defines data member,constractors, Methods and can also be suc-classed. 2-Abstract Class:It is incomplete class which cannot be instantiated. It defines data members,constructors,methods, declares sub-class methods. 3- Interface or Pure Abstract Class: an interface is a pure abstract declares methods and define constants as well. Creating Objects:A class define a new type.this new type can be used to create objects .To create objects of class ,two steps followed.

1-Declare a variable of class type . This variable does not create an Object.instead,it is simply a reference to an object. Syntax: ClassName object Name; 2- Acquire an actual physical copy of object and assign it to that variable using the new is dynamically allocates memory. Syntax: ClassName ObjectName = new ClassName (); Array Of Objects : create an array of objects.two steps follows 1- create an array of refernces using new. Syntax: ClassName [] arrayName =new ClassName [size]; 2- create an object for each refernce using new. Syntax: for(i=0; i<size; i++) arrayName [i] = new ClassName(); Eg:- MyRectangle [] r= new MyRectangle[5]; for (int i= 0; i<5 ; i++) r[i] = new MyRectangle();

A Constructors is special method of class which has the same name and is used for initializing the object member.this are two types. 1-Defolat Constructor :This is constructor with no is automatically created by the compiler .it is no longer automatically. 2-Parameterized Constructor : This is constructor with parameter. It is assign specific values to member.this means that two or more methods can have the same signature within the same scope. Eg: class MyRectangle { int length ,breadth ; MyRectangle() // defolat constructor { length=0; breadth=0; } MyRectangle(int length,int breadth)//parameterized constructor { this.length=length ; this.breadth=breadth; } }

this keyword: The this keyword always refers to current or

implicit objects. Uses of this is four ways like 1-To resolve ambiguity between a local and instance variable: when the local variable has the same name as the instance variable,the local variable hides the instance variable.its used to specify instance variables. 2-To return the implicit object : a methods may want to return the implicit object itself.insuch case this can be returned. 3- To invoke methods and instance members: Instance members and methods can be accessed using this inside a method. 4-To invoke one constructor within another:from within a costructor you can also use the this keyword to call another constructor in the same class.doing so is called an explicit constructor invocation.

Defining Methods: a method is a self-contained block of code

that performs specific operation on data. Syntax: [Modifiers] returntype methodeName ([parameters]) { [local declarations ] [method body] } Static member: it is also called class member.they do not belong to an object but to the class.are accessed by ClassName.staticMember Static methods: Static methods are declared static.its do not belong to an object but belong to the class. Syntax : ClassName.methodName(args) Static Blocks: its belong to the class.this block will be executed even before main is used to initialize static variables.

The Object Class: the Object class defines the basic state and
behavior that all objects must have such as the ability to compare to another objects to convert to a string. 1- equals method: is used to compare two objects for return true if objects are equals and false otherwise. Eg- string s1= hello; string s2=hello; if(s1.equals s2) system.out.println (they are Equals); 2-getClass method: it return this class object.we can get a variety of information about the class.Eg- Class c = obj.getClass(); 3-to String method: this method gives the String representation of an returns a string that textually represents on objects. Eg- System.out.println (s.toString());

Wrapper classes: java is pure object oriented language.but

primitive data types cannot participate in methods which require objects as parameters or return objects .as solution to this problem java provides Wrapper classes in the lang package,which allows us to wrap primitive data type into an object.there are two ways of creating wrapper class objects. 1-Using new : An instance of a wrapper class may be created by calling the corresponding constructor and by passing argument. Eg- Interger intwrapper =new Interger(20); 2-Using valueOf : the static valueOf() method can be invoked to create a wrapper class objects. It can be invoked directly on the class it retruns the corresponding wrapper class object. Eg- Integer intwrapper = Integer.valueOf(20);

Package is a namespace that organizes a set of related classes and interface.the package which is imported by defaults is the java.lang package which contains language related classes. Creating Packages: to create a user define package .the package statement should be written in the source code file.only one package statement can be written in a file. Syntax : package <top_pkg_name> [.<sub_pkg_name>] * ; Eg- package maths.algebra; Accessing Packages : to use classes of a package in another package the classname must be qualified using the package name. Syntax : Package.className Eg- mypackage.MyClass. An easier method is to use the import allows us to access a specific class or all classes form a package. Syntax : import <pkg_name>[.<sub_pkg_name>].*.<class-name>;

Garbage collection is a machanism to remove objects from memory when they are no longer needed.Garbage collection is carried out by grabage runs as a separated invoked in two ways 1-Using System.gc() method. 2- Using an instance of the Runtime class and invoke gc (). Syntax : Runtime rt =Runtime.getRuntime(); Eg- rt.gc () ; finalize () : the purpose of finalization is to perform clean-up operations for the object i.e freeing resources ,deallocating memory,releasing connections, closing files etc. Stntax: protected void finalize () { // finalization code here }

Inheritance allows creation of new classes using existing java inheritance using the extends keyword.The class being inherited is called super,base or parent class and the class inherites properties is called sub ,derived or child class.its is-a relationship. Types are like 1-Single Inheritance: A class can have only one superclass. 2-Multilevel Inheritance: a subclass is further used to derive more classes. Eg- class B extends A and C extends class B. 3- Hierarhical Inheritance : A single superclass class ha many subclass.these are further used to create more subclasses. Member Access: A subclass inherits the member of its superclass but it cannot access all the members directly. Directly accessible: those superclass member which is declared public or protected. And no access speifier .and in same package. Not accessible: private data members and methods have same name

The Super Keyword: the keyword super is very important

keyword used in inheritance .it is used for twp purposes: 1-Invoking superclass constructor: if super class has default constructor ,the subclass need not define a constructor .if superclass defines parameterized constructor ,it is manadatory subclass to define a parameterized constructor and pass parameter to superclass Syntax: super (parameters); 2-Invoking superclass member hidden by subclass : if the subclass defines member having the same name as the superclass,it hides the superclass member.superclass members cannot be accessed in the subclass .if you want to acces them the super keyword should used. Syntax : super.member Method Overriding: it means two or more methods having the same name but differing in number of parameter or types.

An abstract class is a class which h cannot be instantiated .i.e we cannot create objects of an abstract can only used to create subclass. Syntax :abstract class CalssName {} An abstract class contain data member,constructor,method ,static . Abstract Methode: An abstract class provides all the functionality required by all its subclass.An abstract method has to be overridden in the subclass .a class having an abstract method must declared as abstract. Syntax : abstract returntype methodName (parameters); Eg : abstract int area () ; Final Keyword: The use of final to create constant. The final keyword is used for these two purpose. 1-Preventing method overriding : can not override in subclass. 2- Preventing Inheritance : class is declared final cannot be inherited in subclass.

It is an fully abstract class which does not contain any instance member but only method declarations. These are implicity static and final .it is used two or more classes have the same behavior. Syntax: modifier interface <interfaceName>[extends <super interfacename>] { type methodName (parameters) .} Tagging Interfaces: some interface are defined without any methods or constant is called as tagging or marker onterface. Impementing Interface : one or more classes can implements that interface .using implement keyword in its declaration. Synyax : modifier class ClassName extends SuperClassName [implements InterfaceName1 [InterfaceName2.]] { // class body } Object Cloning : it means creating a copy of an object.object cloning create a new object which is identical to one being cloned. Eg: MyRectangle r1 =new myrectangle (); r2=r1;

In java it is possible to define a class within another class;such classes are called as nested classes. Syntax : class outerClass { // variable and methods of outer class class NestedClass { // variable and mrthods of nested class } } Since a nested class does not exit outside the outer class,an object of the nested class should be created inside the outer class.the nested class object stores a reference to the outer class object and can access all its member. Two type of nested classes. 1- Static nested : static nested class is also called as outer class. 2-Non static nested:non static nested class is also called as inner class

Local Inner Class: An inner class which id declared locally

inside a methode is called a local inner class. Outer.inner m =new outer.inner() ; Anonymous inner class: class which has no name is an anonymous inner class. A n anonymous class is defined a local inner class .to create an anonymous inner class it must either implement an interface or extend one class. Syntax : new SuperType (parameters) { inner class method and data } Herr supertype can be: an interface or a class. Note: If the SuperType is an Interface ,no parameters should be passed.

An Exception is an abnormal condition that arises in code at run time.exception is a runtime ia inbuilt exceptin handling. There are two types of exception 1-Checked Exception :Except for RuntimeException ,error and their subclasses all other exception are called checked exceptions. Eg- IO 2-Unchecked Excepyion : Exception defined by error and Runtime Exception classes and their subclasses are called as unchecked exception. Eg- NullPointerException . Exception Handaling :exception handling in java is using 5 keyword 1-try:Exception are written within try block. 2-catch : that secifices the exception type that you wish to catch. 3- throw : is used to explicity throw an exception object. 4-throws : is used in a method header to declare exception. 5-finally : is executed whether or not an exceptions.

An applet is small java program which runs in a web browser. An applet is created by inherting from the java.applet.Applet class or the javax.swing.Japplet class. Applet Lifecycle : the Applet class defines five methods that control the creation and execution of an applet.this are 1- init () : Automatically called to perform frist-time initialization of the applet. 2- start() :every time the applet moves into sight on the web browser to allow the applet to start up its normal operations.called after init() 3-stop() : every time the applet moves into sight on the web browser to allow the applet to shut off its expensive operations.befor destroy() 4-destroy() : called when the applet is being unloaded from the page to perfrom final release of resources when applet is no longer used. 5-paint() : called each time the applet output needs to be redrawn.