OWliag
Arrays —_
_»-An array in Java is a data structure that allows you to store a collection of values of the
ame data type in a contiguous block of memory.
_* Each value'n the array is identified by an index, which is a non-negative integer that
represents its position in the array.
ane first element in the array has an index of 0) the second element has an indeoyoF?,
so on.
and
dataType[] arrayName = new ata TypelarraySize};
ip, Qea@ Gai: —
A intl] myArray =y{1. 2. 3. 4, 5 boo RY
valun (TT 2 &
dow Lola 1 ASaccessModifier returnType methociome(parameterType parameterName) {
————
// method body
return returnValue;
public int add{int a, int b) {
a oe
BrameMethods
_ecessModifier: specifies the visibility of the method (e.g., public, private, protected, or default)
_JaturniType: specifies the data type of the value retumed by the method (e.g, int, String,
Cboolean, etc.)
_inethodName: specifies the name of the method
ameterType: specifies the data type of the parameters passed to the method (e.g., int,
String, boolean, etc.)
parameterName: specifies the name of the parameters passed to the method
method body: contains the statements that are executed when the method is called
returnValue: specifies the value returned by the method (if any)presi reote Gerad
eee
Dene entree dees enn eee en eee a
eee StnClass
+ accessModifier: specifies the visibility of the class (e.g., public, private, protected, or default)
a i —!1 aa
~
* class: keyword used to define a class
* ClassName: specifies the name of the class (by convention, the name should start with a
capital letter)
* data members: variables that store data within the class
* constructors: special methods that are used to initialize objects of the class
* methods: functions that define the behavior of the class= ™
= + In Java, inheritance is a mechanism that allows one clas to inherit the properties and
= methods of another cls. *
«+The class thats being inherited from scaled the superlass or parent class andthe class
thats inheriting from itis called the subclass or cid class.
+ Types of inheritance
a {Single inheritance
‘rom one base class.
t ! ‘b Multiple inheritance: It isthe inheritance hierarchy wherein one derived class inherits
$$! | ‘trom mutiple base classes.
‘¢Hierarchical inheritance ts the innentance hierarchy wherein mutiple eubcisses
inherit from one baze clas,
<4 Multievel ieritance: itis the inhertance hierarchy wherein subclass acts as a base
‘class for other classes.
‘e-Hybrid inhertanes : The inheritance hierarchy that reflects any legal combination of
‘other four types of inheritance.
Is the inheritance hierarchy wherein one derived cass inheritsyt Elov
Inheritance
+-lava, inheritance is a mechanism that allows one class to inherit the properties and
methods of another class.
© The class that is being inherited from is called the superclass or parent class, and the class
‘that is inheriting from it is called the subclass or child class.
* Types of inheritance :
a. Single inheritance : It is the inheritance hierarchy wherein one derived class inherits
from one base class.
b.Muttiple inheritance : It is th
from multiple base classes.
c.Hierarchical inheritance : It is the inheritance hierarchy wherein multiple subclasses
inherit from one base class.
d.Multilevel inheritance : It is the inheritance hierarchy wherein subclass acts as a base
class for other classes.
e. Hybrid inheritance : The inheritance hierarchy that reflects any legal combination of
other four types of inheritance.
heritance hierarchy wherein one derived class inheritssteers i SuperclassName {
a
Me ee lo Tar)
public void speak() €
eee ee toh a ee oe ee
public class Dog extends Animal {
Coss t Ly
public void speak() €
System. out.print1n("Woof!");i EIOV
Package
‘ackage is a mechanism to encapsulate a group of classes, interfaces and subpackages.
Packages are the way to organize files into different directories according to their
functionality, usability as well as category.
* Packages also provide a way for separating “design” from “coding”.
* There are two types of packages in Java:
© User-defined package : The package we create is called user defined package.
Built-in package : The already defined package like java.io", java.lang.* etc are known
as built-in packages.Features of Object-Oriented programming
_ 2 Objects
potas
2 Mheritance
* Polymorphism —~7
* Data hiding
* Encapsulation
* Reusabilityception Handling
_77An exception is an unwanted or unexpected event, which occurs during the execution of a
program i.e., at runtime, that disrupts the normal flow of the program's instruction.
* Exception handling provides a type-safe, integrated approach for handling unusual
problems that arise while executing a program.
* To handle the exceptions, exception handling mechanism is designed.
* Java provides a mechanism for handling exceptions through the use of try-catch blocks.rg
ee ee USC
int result = divide(10, 0); // This will throw an ArithmeticException
System.out.printIn(result); // This will not be executed
a smh (ArithmeticException e) {
eee eee econ eee et sts tC
System.out.printIn("An ArithmeticException occurred: " + e.getMessage());
pecusetng
ieee eee C TE ee MU Cu OR
Oo ee te eee EC
Te ee Ut)
public static int divide(int numerator, int denominator) {
PCa MUMultithread 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.
* Each thread runs parallel to each other.
* A multithreading is a specialized form of multitasking.
* In multithread program, each thread has its own life cycle.i EIOV
Java Applet
oe applet in Java is a small program that is designed to be executed within a web browser
using the Java Virtual Machine (VM).
* It runs inside the web browser and works at client side.
* Applets are used to make the website more dynamic and entertaining.
* Life cycle of an applet use five methods which are as follows :
a.init( ) : This method is intended for whatever initialization is needed for our applet.
b.start() : This method is automatically called after the browser calls the init method.
c.stop( } : This method is automatically called when the user moves off the page on which
the applet sits.
d.destroy( ) : This method is only called when the browser shuts down normally.
e.paint( ) : Invoked immediately after the start() method, and also any time the applet
needs to repaint itself in the browser.Sees NEM ace) Tt
Sa CEE en 3
public class MyFirstApplet extends Applet {
public void psint(Graphies g) {
g.drawString("Hello, World!", 50, 25);
ed
BTC id
My First Applet
Bard
Bead
BU edi EIOV
String Handling
+ Sting handling is a process of performing different operation such as concatenation,
comparison on the string.
Java string length() : The Java string length() method returns the length of the string. It
returns count of total number of characters present in the string.
* compareTo() : The Java string compareTo() method compares the given string with current
string. It returns positive number, negative number or zero.
* concat() : The Java string concat() method combines a specific string at the end of another
string and returns a combined string.
* replace() : replace() method returns a string, replacing all the old characters to new
characters. e. Java string equals() : The Java string
* equals() : equals() method compares the two given strings on the basis of content of the
string. If all the characters are matched, it returns true else it will return false
* contains() : The Java string contains() method searches the sequence of characters in the
string. If the sequences of characters are found, then it returns true otherwise returns false.AWT and AWT Controls
©-AWT stands for Abstract Window Toolkit, which is a Java-based GUI (Graphical User
Interface) library that provides the tools and components needed to create desktop
applications with a graphical user interface.
* AWT provides a set of classes and interfaces that allow developers to create and manage
windows, buttons, text fields, menus, and other graphical user interface components.
* It contains three kinds of classes :
a.Containers class : Frame, Dialog, Panel, Applet etc.
b. Components class : TextField, Button, Checkbox, Scrollbar, Label, List etc.
c.Custom graphics class : Color, Font, Dimensions etc.
* AWT Controls refer to the set of GUI components that can be created using AWT.
+ These components include buttons, checkboxes, choice lists, labels, text fields, text areas,
and more.
* AWT Controls are implemented as Java classes and can be instantiated and customized to
fit the specific needs of a given application.import java.awt.*;
public class SimpleAWTExample {
public static void main(String[] args) {
eeu Mee al Uae
Label myLabel = new Label("Hello, World!");
Dee eC eon yy
myFrame.setSize(200, 100);
myFrame.setVisible(true) ;Layout Manageri EIOV
Layout Manager
* Layout manager is a mechanism that automatically arranges the components in a container
(such as a Frame, Panel, or Window) according to a specific layout strategy.
* Layout managers are used to control the size and position of components within a
container, and to ensure that the components are displayed in a visually pleasing and
organized way.
* There are several layout managers available in Java, includin:
© FlowLayout - arranges components in a single row or column, and wraps them to a
new row or column when there is no more space.
© BorderLayout - arranges components in five regions: north, south, east, west, and
center.
© GridLayout - arranges components in a grid of rows and columns.
© CardLayout - allows multiple components to be displayed in the same space, and
allows the user to switch between them.
© GridBagLayout - provides the most flexible and powerful layout mechanism, allowing
components to be placed in a grid of rows and columns with variable sizes and
alignments.import java.awt.
import javax.swing.*;
public class SimpleFlowlayoutExample (
public static void main(String{] args) {
iFrames frame = new JFrame("FlowLayout Example");
JPanel panel = new JPanel(new Flow Layout());
ws Mea ial ae ied 2
JButton button? = new JButton("Button 2");
Bet eM a a ac a
panel.add(button1);
eta) ed
pane] .add(button3) ;
Fee ete
frame.setDefaultCloseOperation( JFrame. EXIT _OM_CLOSE);
frame.setSize(900, 200);
frame.setVisible( true);(= BTN
Happy Ending! Ae
sy,
Drak