Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Java Basics


Published on

Published in: Technology, Education
  • Be the first to comment

Java Basics

  2. 2. WHAT IS THREADING ? 2  Any single path of execution within a program that can be executed independently, is called a thread.  Java provides built-in support for multithreaded programming.  A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution.  A multithreading is a specialized form of multitasking. Multithreading requires less overhead than multitasking processing. A thread cannot exist on its own; it must be a part of a process. When a standalone application is run, a thread is automatically created to execute the main() method. This thread is called the main thread. The program terminates when the main thread finishes executing. All other thread is called child thread.  Threads subdivide the runtime behavior of a program into separate, independently running subtasks. Even in a single processor system, thread provides an illusion of carrying out several tasks simultaneously.  This is possible because switching between threads happen very fast.
  3. 3. THREAD PRIORITIES: 3  Every Java thread has a priority that helps the operating system determine the order in which threads are scheduled.  Java priorities are in the range between MIN_PRIORITY (a constant of 1) and MAX_PRIORITY (a constant of 10). By default, every thread is given priority NORM_PRIORITY (a constant of 5).  Threads with higher priority are more important to a program and should be allocated processor time before lower-priority threads. However, thread priorities cannot guarantee the order in which threads execute and very much platform dependentant.
  4. 4. ABOVE MENTIONED STAGES ARE EXPLAINED HERE:  New: A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread.  Runnable: After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task.  Waiting: Sometimes a thread transitions to the waiting state while the thread waits for another thread to perform a task. A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing.  Timed waiting: A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs.  Terminated: A runnable thread enters the terminated state when it completes its task or otherwise terminates. 4
  5. 5. EXAMPLE OF THREADING 5 // Create a new thread. class NewThread implements Runnable { Thread t; NewThread() { // Create a new, second thread t = new Thread(this, "Demo Thread"); System.out.println("Child thread: " + t); t.start(); // Start the thread } // This is the entry point for the second thread. public void run() { try { for(int i = 5; i > 0; i--) { System.out.println("Child Thread: " + i); // Let the thread sleep for a while. Thread.sleep(500); } } catch (InterruptedException e) { System.out.println("Child interrupted."); } System.out.println("Exiting child thread."); } } public class ThreadDemo { public static void main(String args[]) { new NewThread(); // create a new thread try { for(int i = 5; i > 0; i--) { System.out.println("Main Thread: " + i); Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println("Main thread interrupted."); } System.out.println("Main thread exiting."); } } Here is an example that creates a new thread and starts it running: Output: Child thread: Thread[Demo Thread,5,main] Main Thread: 5 Child Thread: 5 Child Thread: 4 Main Thread: 4 Child Thread: 3 Child Thread: 2 Main Thread: 3 Child Thread: 1 Exiting child thread. Main Thread: 2 Main Thread: 1 Main thread exiting.
  6. 6. WHAT IS INTERFACES ? 6 An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. An interface is not a class. Writing an interface is similar to writing a class, but they are two different concepts. A class describes the attributes and behaviors of an object. An interface contains behaviors that a class implements. Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class. An interface is similar to a class in the following ways: a. An interface can contain any number of methods. b. An interface is written in a file with a .java extension, with the name of the interface matching the name of the file. c. The bytecode of an interface appears in a .class file. d. Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.
  7. 7. DECLARING INTERFACES: 7 The interface keyword is used to declare an interface. Here is a simple example to declare an interface: /* File name : */ import java.lang.*; //Any number of import statements public interface NameOfInterface { //Any number of final, static fields //Any number of abstract method declarations } Let us look at an example that depicts encapsulation: Interfaces have the following properties: An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface. Each method in an interface is also implicitly abstract, so the abstract keyword is not needed. Methods in an interface are implicitly public.
  8. 8. WHAT IS POLYMORPHISM ? 8 Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOPS occurs when a parent class reference is used to refer to a child class object. Any java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object. It is important to know that the only possible way to access an object is through a reference variable. A reference variable can be of only one type. Once declared the type of a reference variable cannot be changed. The reference variable can be reassigned to other objects provided that it is not declared final. The type of the reference variable would determine the methods that it can invoke on the object. A reference variable can refer to any object of its declared type or any subtype of its declared type. A reference variable can be declared as a class or interface type.
  9. 9. WHAT IS INHERITANCE ? 9 A class can be built on another class that is already defined and is existing. The already existing class is called base class or parent class. The newly built class is called derived class or child class. The child class inherits all the properties of the parent class, i.e. it inherits all the member variables and methods of the parent class. In addition the child class can have its own member variables and methods. Inheritance may take different forms.
  10. 10. BASIC OF INHERITANCE 10 Reusability is one of the most important aspects of OOPS paradigm. Java supports the concept of reusing the code that already exists. Inheritance allows the creation of hierarchical classification. Using inheritance, you can create a general class that defines traits common to a set of related items. The characteristics of this class can then be inherited by other, more specific classes, each adding those aspects that are unique to it. A class that is inherited is called superclass or parent class. The class that does the inheriting is called a subclass or child class.
  11. 11. FORMS OF INHERITANCE  A class in java can only extend one other class, i.e. a class can only have one immediate superclass. This is sometimes called linear inheritance. The inheritance relationship can be depicted as an inheritance hierarchy. Inheritance defines is-a relationship between a superclass and its subclass.  This means that an object of a subclass can be used whenever an object of the superclass can be used. This is-a relationship does not hold between peer classes. Multiple inheritance, i.e. a subclass inheriting from more than one superclass is not supported in java. 11
  12. 12. WHAT ARE APPLET’S ? 12  An applet is a Java program that runs in a Web browser. An applet can be a fully functional Java application because it has the entire Java API at its disposal. There are some important differences between an applet and a standalone Java application, including the following: 1. An applet is a Java class that extends the java.applet.Applet class. 2. A main() method is not invoked on an applet, and an applet class will not define main(). 3. Applets are designed to be embedded within an HTML page. 4. When a user views an HTML page that contains an applet, the code for the applet is downloaded to the user's machine. 5. A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser or a separate runtime environment. 6. The JVM on the user's machine creates an instance of the applet class and invokes various methods during the applet's lifetime. 7. Applets have strict security rules that are enforced by the Web browser. The security of an applet is often referred to as sandbox security, comparing the applet to a child playing in a sandbox with various rules that must be followed.  Other classes that the applet needs can be downloaded in a single Java Archive (JAR) file.
  13. 13. LIFE CYCLE OF AN APPLET: 13  Four methods in the Applet class give you the framework on which you build any serious applet:  init: This method is intended for whatever initialization is needed for your applet. It is called after the param tags inside the applet tag have been processed.  start: This method is automatically called after the browser calls the init method. It is also called whenever the user returns to the page containing the applet after having gone off to other pages.  stop: This method is automatically called when the user moves off the page on which the applet sits. It can, therefore, be called repeatedly in the same applet.  destroy: This method is only called when the browser shuts down normally. Because applets are meant to live on an HTML page, you should not normally leave resources behind after a user leaves the page that contains the applet.  paint: Invoked immediately after the start() method, and also any time the applet needs to repaint itself in the browser. The paint() method is actually inherited from the java.awt.
  14. 14. A "HELLO, WORLD" APPLET: 14  The following is a simple applet named import java.applet.*; import java.awt.*; public class HelloWorldApplet extends Applet { public void paint (Graphics g) { g.drawString ("Hello World", 25, 50); } }  These import statements bring the classes into the scope of our applet class: java.applet.Applet. java.awt.Graphics.  Without those import statements, the Java compiler would not recognize the classes Applet and Graphics, which the applet class refers to.
  15. 15. WHAT IS PUBLIC STATIC VOID MAIN ? Every word in the above statement has got a meaning to the JVM. 1. public: It is a keyword and denotes that any other class (JVM) can call the main() method without any restrictions. 2. static: It is a keyword and denotes that any other class (JVM) can call the main() method without the help of an object. More about static is available at static Keyword – Philosophy. 3. void: It is a keyword and denotes that the main() method does not return a value. 4. main(): It is the name of the method. 15
  16. 16. THANK YOU….. 16