Más contenido relacionado



  1. java
  2. 1. Overloading by changing the number of parameters
  3. 2. Method Overloading by changing the data type of parameters
  4. Let’s look at a real-world example:
  5. • Important Points • Two or more methods can have the same name inside the same class if they accept different arguments. This feature is known as method overloading. • Method overloading is achieved by either: – changing the number of arguments. – or changing the data type of arguments. • It is not method overloading if we only change the return type of methods. There must be differences in the number of parameters.
  6. Method Binding in Java First let's look at two definitions from the book: • Connecting a method call to a method body is called binding. • All method binding in Java uses late binding unless the method is static or final.
  7. • Static Binding and Dynamic Binding  Connecting a method call to the method body is known as binding.  There are two types of binding  Static Binding (also known as Early Binding).  Dynamic Binding (also known as Late Binding).
  8. Example: Static Binding
  9. Example: Dynamic Binding
  10. UNIT-3
  11. Types of Exception in Java
  12. Examples of Built-in Exception • ClassCastException • // Java Program to illustrate • // ClassCastException • class Test { • public static void main(String[] args) • { • String s = new String("Geeks"); • Object o = (Object)s; • Object o1 = new Object(); • String s1 = (String)o1; • } • }
  13. A. Arithmetic exception • // Java program to demonstrate ArithmeticException • class ArithmeticException_Demo • { • public static void main(String args[]) • { • try { • int a = 30, b = 0; • int c = a/b; // cannot divide by zero • System.out.println ("Result = " + c); • } • catch(ArithmeticException e) { • System.out.println ("Can't divide a number by 0"); • } • } • }
  14. B. NullPointer Exception
  15. C. StringIndexOutOfBound Exception • // Java program to demonstrate StringIndexOutOfBoundsException • class StringIndexOutOfBound_Demo • { • public static void main(String args[]) • { • try { • String a = "This is like chipping "; // length is 22 • char c = a.charAt(24); // accessing 25th element • System.out.println(c); • } • catch(StringIndexOutOfBoundsException e) { • System.out.println("StringIndexOutOfBoundsException"); • } • } • }
  16. D. FileNotFound Exception
  17. E. NumberFormat Exception
  18. F. ArrayIndexOutOfBounds Exception • // Java program to demonstrate ArrayIndexOutOfBoundException • class ArrayIndexOutOfBound_Demo • { • public static void main(String args[]) • { • try{ • int a[] = new int[5]; • a[6] = 9; // accessing 7th element in an array of • // size 5 • } • catch(ArrayIndexOutOfBoundsException e){ • System.out.println ("Array Index is Out Of Bounds"); • } • } • }
  19. • Multithreading in Java • is a process of executing multiple threads simultaneously. • A thread is a lightweight sub-process, the smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking. • However, we use multithreading than multiprocessing because threads use a shared memory area. They don't allocate separate memory area so saves memory, and context-switching between the threads takes less time than process. • Java Multithreading is mostly used in games, animation, etc.
  20. Thread Synchronization
  21. Interthread Communication
  22. Enumerations: Enum is short for "enumerations", which means "specifically listed".
  23. Example program enum Level { LOW, MEDIUM, HIGH } public class Main { public static void main(String[] args) { Level myVar = Level.MEDIUM; System.out.println(myVar); } } Output:MEDIUM
  24. Enum inside a Class public class Main { enum Level { LOW, MEDIUM, HIGH } public static void main(String[] args) { Level myVar = Level.MEDIUM; System.out.println(myVar); } } Output:MEDIUM
  25. Enum in a Switch Statement
  26. Autoboxing and Unboxing: The automatic conversion of primitive data types into its equivalent Wrapper type is known as boxing and opposite operation is known as unboxing. This is the new feature of Java5. So java programmer doesn't need to write the conversion code. Advantage of Autoboxing and Unboxing: No need of conversion between primitives and Wrappers manually so less coding is required.
  27. Output:50 5
  28. Java Annotations • Java annotations are metadata (data about data) for our program source code. • They provide additional information about the program to the compiler but are not part of the program itself. These annotations do not affect the execution of the compiled program. • Let's take an example of @Override annotation. • The @Override annotation specifies that the method that has been marked with this annotation overrides the method of the superclass with the same method name, return type, and parameter list. • It is not mandatory to use @Override when overriding a method. However, if we use it, the compiler gives an error if something is wrong (such as wrong parameter type) while overriding the method.
  29. Java MouseListener Interface
  30. kk
  31. Java Abstract Window Toolkit(AWT) • java AWT is an API that contains large number of classes and methods to create and manage graphical user interface ( GUI ) applications. The AWT was designed to provide a common set of tools for GUI design that could work on a variety of platforms. The tools provided by the AWT are implemented using each platform's native GUI toolkit, hence preserving the look and feel of each platform. This is an advantage of using AWT. But the disadvantage of such an approach is that GUI designed on one platform may look different when displayed on another platform that means AWT component are platform dependent. • AWT is the foundation upon which Swing is made i.e Swing is a improved GUI API that extends the AWT. But now a days AWT is merely used because most GUI Java programs are implemented using Swing because of its rich implementation of GUI controls and light-weighted nature.
  32. Component class • Component class is at the top of AWT hierarchy. It is an abstract class that encapsulates all the attributes of visual component. A component object is responsible for remembering the current foreground and background colors and the currently selected text font. Container • Container is a component in AWT that contains another component like button, text field, tables etc. Container is a subclass of component class. Container class keeps track of components that are added to another component. Panel • Panel class is a concrete subclass of Container. Panel does not contain title bar, menu bar or border. It is container that is used for holding components. Window class • Window class creates a top level window. Window does not have borders and menubar. Frame • Frame is a subclass of Window and have resizing canvas. It is a container that contain several different components like button, title bar, textfield, label etc. In Java, most of the AWT applications are created using Frame window. Frame class has two different constructors,
  33. AWT Button
  34. AWT Label
  35. AWT TextField
  36. AWT TextArea
  37. Example: In this example, we are creating checkbox that are used to get user input. If checkbox is checked it returns true else returns false. AWT Checkbox
  38. Example: In this example, we are creating drop-down menu that is used to get user choice from multiple choices. AWT Choice
  39. AWT List
  40. AWT Canvas
  41. Java applets
  42. What is Applet? An applet is a Java program that can be embedded into a web page. It runs inside the web browser and works at client side. An applet is embedded in an HTML page using the APPLET or OBJECT tag and hosted on a web server. Applets are used to make the website more dynamic and entertaining. Important points : • All applets are sub-classes (either directly or indirectly) of java.applet.Applet class. • Applets are not stand-alone programs. Instead, they run within either a web browser or an applet viewer. JDK provides a standard applet viewer tool called applet viewer. • In general, execution of an applet does not begin at main() method. • Output of an applet window is not performed by System.out.println(). Rather it is handled with various AWT methods, such as drawString().
  43. 9/12/2022 Distributed Computing, M. L. Liu 194 Summary • An applet is a Java class • Its code is downloaded from a web server • It is run in the browser’s environment on the client host • It is invoked by a browser when it scans a web page and encounters a class specified with the APPLET tag • For security reason, the execution of an applet is normally subject to restrictions: – applets cannot access files in the file system on the client host – Applets cannot make network connection exception to the server host from which it originated