Complete Java Notes
Complete Java Notes
Java:
1996 me java language bhi thi. James Croslings was the developer
of the java language.James worked for 1.5 years and after 1.5
years he was able to develop the first version of java language.
Embedded Software:
Embedded software are those softwares which are fix and
they don’t need any memory. Once a program installed in the
device it will on work on that program no other program can be
integrated with it. Instructions are given to it and the program
will only work according to those instructions. For example,
microwave oven, Ac etc.
Advantages of Java:
This language is an open source language i.e. it doesn’t need
any kind of license to be used.
It is a machine independent language or platform system
independent .
Comparison between C language and Java:
Programs in c language only work in windows operating system but
the programs in Java have no limitation they can be executed on
linux,windows,mac etc.for example if a program is made in linux
it can run on windows.Thats why Java is a machine independent
language.
Machine Independent:
Hardware purana ho ya naya Java har kisi py chlta hy and theres
no limitation. Platform Independent:
There is no limitation for the operating system fo Java it works
on every operating system.
Development of android can also be done by using Java.
Description for class and object:
Jesi class ho gi object bhi wesa hi hoga. Class aik dafa bnti hy
or object zada martba ban skta hy.for example,
Time t1=new Time();
t1.h=15; t2.m=23;
t3.s=44;
Time t2=new Time();
t1.h=7; t2.m=34;
t3.s=54;
Class ki Pehchan:
Java me har Class ka pehla letter capital or uppercase me ho ga
agr ni to wo class unacceptable ho gi.
Variable ki Pehchan:
Variables java k andr small letters or lowercase me hn gy or in
k xth parenthesis ni hn gi. Example of class variable function:
class Time{ int
h,m,s; void
print(){
System.out.println(“h=”+h+”,m=”+m+”,s=”+s”);
}
} class
Test{
public static void main(String args[]){
Time t1=new Time();
t1.h=23; t2.m=44;
t3.s=56;
}
}
Constructor(important):
Constructor is a special type of function which is
totally different from an ordinary function because it
has some special working in the program. It is used to
solve a particular problem.
Characteristics of a constructor:
• Constructor name and class name are always same.
• Constructor has no return type.
• Constructor is used to initialize class variables.
• Contructor is called when an object is created.
Example:
class Date{
int d,m,y;
Date(){
d=12; m=7;
y=2018;
}
Void print(){
System.out.println(“d=”+d+”,m=”+m+”,y=”+y);
} } class
Test{
public static void main(String args[]){
Date d1=new Date(); d1.print();
}
}
Sequence of main class and other class doesn’t matter
in java.
Default Constructor:
Date()
{
}
Koi bhi class beghair kisi constructor k compile ni hoti.
Agr user koi constructor ni bnata to compiler us class me
khud constructor bna deta hy.
Parameterized Constructor:
Example: class
Date{ int
d,m,y;
Date(int a,int b,int c){
d=a; m=b; y=c;
}
Void print(){
System.out.println(“d=”+d+”,m=”+m+”,y=”+y);
} } class
Test{
public static void main(String args[]){
Date d1=new Date(12,3,2018); d1.print();
}
}
Examples of constructors:
Example#1: Rectangle Area.
class Rectangle{ int
w,h;
Rectangle(int a,int b){
w=a; h=b; } int
getArea(){
Return w*h;
} } class
Test{
public static void main(String args[]){
Rectangle r1=new Rectangle(5,7); int
x=r1.getArea(); System.out.println(x);
}
}
getArea(),getVolume,etc are the built in functions to
calculate area and volume.
Constructor overloading:
Creating or making multiple constructors in a class is
known as constructor overloading.
The parameters in the constructors should be different for
every constructor.
Sequence doesn’t matter for constructors.
Example:Valid
Time(){
}
Time(int x,int y,int z){
}
Time(int a,int b,int c){
}
Data Type List:
• Boolean(1 byte)(True or false).
• Byte (127 to -128).
• Short (2 bytes)(-32768 to 32767).
• Char .
• Int.
• Long.
• Float.
• Double.
• Class.
“This” keyword:
Java language doesn’t use pointers instead java use “this”
keyword which refers to the current object. Examples:
Rational Numbers:
Add two rational numbers.
Program: class
Rational{ int
n,d;
Rational(int a,int b){
n=a; d=b; }
void print(){
System.out.println(n+"/"+d);
}
Rational add(Rational r1, Rational r2){
int p=r1.d*r2.d; int a=p/r1.d *r1.n; int
b=p/r2.d *r2.n;
Rational t=new Rational(a+b,p); return
t;
} } class
Test{
public static void main(String args[]){
Rational r1=new Rational(2,3);
Rational r2=new Rational(5,6);
Rational r3=r1.add(r1,r2); r3.print();
}
}
} } class
Test8{
public static void main(String args[]){
Rational r1=new Rational(2,3);
Rational r2=new Rational(5,6);
Rational r3=r1.multiply(r2); r3.print();
Rational r4=r1.divide(r2); r4.print();
}
}
Inheritance:
Definetion:
Inheritance is a technique in which a class is based on a
existing class.
Extends:
It is a keyword which is used to inherit a class with
another class.
Principles of inheritance:
• Pehly parent class ko values milti hn or usky bad
child class ko initialize kia jata hy.
• Parent class ka constructor pehly initialize hota hyor
child class ka bad me hota hy lekin call bad me ho ya
pehly is me koi sequence ni k pehly child class ho ya
parent class.
Super keyword:
It is a keyword which is used for the parent class. It is
used to call the parent class from another class.
Multi-level Inheritence:
It is supported in every languge.
A
↓
B
↓
C
↓
D
Multiple Inheritence:
It is not supported by Java but it is supported by C++.
Examples:
Program: class
Person{ String
name; int age;
void print(){
System.out.println("Name:"+name+",Age="+age);
} }
class student extends Person{
int r; void
print(){
super.print ();
System.out.println("Roll NO."+r);
} }
class employee extends student{
int salary;
void print(){
super.print ();
System.out.println("Salary="+salary);
} } class
Test3{
public static void main(String args[]){ ,employee
e=new employee();
e.name= "Asad";
e.age=34;
e.r=3;
e.salary=20000;
e.print();
}
}
Print student name , student roll no.,employee
name,employee salary, without using inheritance.
Program: class
Person{ String
name; int age;
void print(){
System.out.println("Name:"+name+",Age="+age);
} } class
student{
int r; void
print(){
super.print ();
System.out.println("Roll NO."+r);
} }
class employee{
int salary;
void print(){
super.print ();
System.out.println("Salary="+salary);
} } class
Test4{
public static void main(String args[]){ Person
p=new Person();
p.name= "Asad";
p.age=34;
p.r=3;
p.salary=20000;
p.print();
}
}
Print values of I,j,k using inheritance.
Program:
class A{ int
i,j; void
print(){
System.out.println("i="+i+"j="+j);
} }
class B extends A{
int k; void
print(){
super.print();
System.out.println("k="+k);
}
}
class
Test2{
public static void main(String args[]){
B b=new B(); b.i=2;
b.j=3;
b.k=6;
b.print();
}
}
Interface:
Definetion:
Template of incomplete method is known as interface.
Voice of animals.
Program:
interface Animal{
public void voice();
} class Dog implements
Animal{ public void voice(){
System.out.println("Bark");
}
}
Abstract:
Definetion:
An incomplete class is known as an abstract class. Methods
which are incomplete and have no body are called abstract.
• Jis method ki body ni hoti wahan abstract likhna hota.
• It is a keyword of java which is wriiten at the start
of the body.
• It is a class which has incomplete functionality.
• There are no objects in it.
• Complete class is also known as concrete class.
• Semicolon(;) is used at the end.
• Abstract class cant be instantiated.
Examples:
Voice of animals.
Program:
abstract class Animal{ abstract
void voice();
}
class Dog extends Animal{ void
voice(){
System.out.println("Bark");
} }
class Lion extends Animal{ void
voice(){
System.out.println("Roar");
} } class
Test{
public static void main(String args[]){
Animal a; a=new Dog();
a.voice(); a=new
Lion();
a.voice();
}
}
Communication(Parent class), English(Child
class),Urdu(Child class).
Program:
abstract class Communication{ abstract
void language();
} class English extends
Communication{ void language(){
System.out.println("Hello");
} }
class Urdu extends Communication{ void
language(){
System.out.println("Salam");
}
}
class Test{
public static void main(String args[]){
Communication c; c=new English();
c.language(); c=new
Urdu();
c.language();
}
}
}
}
Devices(Parent class),Input(child class),Output(Child
class).
Program:
abstract class Devices{ abstract
void use();
}
class Input extends Devices{
void use(){
System.out.println("To Enter Data And Information");
} }
class Output extends Devices{
void use(){
System.out.println("To Display Result");
} } class
Test2{
public static void main(String args[]){
Devices d; d=new Input(); d.use(); d=new
Output(); d.use();
}
}
Agriculture(Parent class),Winter(Child class),Summer(Child
class).
Program:
abstract class Agriculture{ abstract
void crop();
}
class Summer extends Agriculture{ void
crop(){
System.out.println("Wheat,Rice");
} }
class Winter extends Agriculture{ void
crop(){
System.out.println("Orange,Sugarcane");
} } class
Test3{
public static void main(String args[]){
Agriculture a; a=new Summer();
a.crop(); a=new
Winter();
a.crop();
}
}
Packages:
Packages are just like files/directories used to group
classes of same topic.
Java me hazaroo built-in classes mojood hn.e.g.
• Text box
• Check box
• Buttons
Java me network ki bhi classes mojood hn.
Ek computer sy dusry computer contact kr skty hn.
Directory/file retrieve,review,delete etc kr skty hn.
Java me jitni bhi classes (built-in) hn unko unky
topic k mutabiq ikhata kia gya hy. Examples:
• java.io(input/output)(data read or write kr skty
hn).
• java.awt(directory).
• java.net(it is used for networking).
}
Method Overriding:
When a child class pre-defines a parent class with same
signature.i.e,name etc then this is known as method
overriding.
Ya phr…
Jb bhi child class , parent class jesy name ki ho gi ya
same ho gi to isy method overriding kehty hn.
Method Overloading:
Making multiple methods with same name but different
parameters in the same class, it is known as method
overloading.
Ya phr…
Aik class me agr aik sy zada method bany hn gy wo bhi aik
jesy name k xth to isy method overloading kehty hn.
Computer har trh k data ko binary me convert kr leta hy
storage k liay bhi or transmission k liay bhi.
• Text.
• Image.
• Video etc.
ASCII Code:
ASCII is a standard that is used in the whole world.
Is me wo characters shamil hn jo keyboard py mojood hn.
• Numeric.
• Alphabet.
• Special characters etc.
Is me puri dunya ki languages shamil ni hoti.
Unicode:
Unicode stands for universal code.
Is me sari languages shamil hn.
Every language is supported in Unicode.
String s1=s.toUpperCase();
}
}
Static keyword:
It is not possible to count the objects of class so we use
static keyword to count the objects of the class.
Static int count=0;
It indicates that how many objects are present in the
class.
Static variable pehly bnty hn or non-static variable bad me
bnty hn.
Object bnany sy pehly static variable memory me create ho
jaty hn.
Static variable ko uski class k name k xth bhi access kia
ja skta hy.
Jb hum static keyword variable k xth istemal krty hn to wo
variable objectsy belong ni krta balky object sy bhr ho ga.
Wo object variable ni rehta so wo har object me ni hota .is
liay us ki single copy hoti hy jo object sy bhr bnti hy.
Static variable :
• Only one copy in memory.
• Static variable does not belong to the objects it
belongs to the classes.
• Belong to class.
• They are created when a class is refered.
• Static variables can be accessed with class name as
well as object variables/name. Non-Static variable:
• Multiple copies in memory.
• Non-static variables belong to objects.
• They are created upon object creation.
• They can only be accessed by the object variables.
Code
Byte Code
C Compiler:
Ye srf windows operating system pr hi run hota hy. C ka prgrm linux pr bhi ni chly ga .
Java ka Byte code har operating System pr chalta hy beshak wo window ho ya linux.
JVM:
Java me agr arrays bnany hn to uska size kbhi bhi left side pr ni likhy gy e.g.
int arr[5],x; it should be
written as , int arr[
]=new int[5];
‘String args[]’ ye khud bhi aik array hy. Array ki pehchan “[ ]”hy.
Exception:
“File Not Found Exception” wo hy k jb aap koi file open krna chah rhy ho or wo
delete ho chuki hy pehly sy hi.
Important:
java.lang aik default package hy ,string ,system etc jesi classes or jo classes hum
bnaty hn wo isi package me ati hn.
Null Pointer Exception:
Time t;
Exception Handling :
Syntax:
Try{
Body;
Body;
Example:
class Test{
try{ c=a/b;
System.out.println(e);
Finally Keyword:
Example:
class Test{
try{ c=a/b;
System.out.println(e);
System.out.println(e);
}finally{
System.out.println(“Inside Finally”);
}
Checked Exceptions:
ye wo exceptions hn jinko compilation time pr hi dekha jata hy k kia ye exception
hy ya nii matlb ye k ye aye gi ya ni aye gi iska andaza ni hota or is ko catch krna
lazmi hota hy wrna program run ni ho ga.
Un-Checked Exceptions:
Ye wo exception hy jo compilation time pr ni pehchana jata k ye exception aye gi
ya ni aye gii.
Blinking cursor is the promptent jiska mtlb ye hy k screen input k liye ruki hy k
user usy input dy k btaye k kia krna hy.
For example,
Integer.parseInt([]):
Ye wo code hy jo aik string value ko integer value me convert krta hy.
Static me non-static ko use ni kia ja skta q k static value object bany beghair hi
call kia ja skta hy.
Java me jitni bhi classes hn un sb ki aik na aik parent class hy. Object ye aik super
parent class hy or ye aik built-in class hy. Or java ki jitni bhii classes hn wo
directly ya indirectly Object sy hi ati hn . Object class me job hi function ho ga wo
java ki tmam classes me chala jye ga .
toString():
ye method method override ho rha hy , hum to-String ni likhty magr jb hum
Object create krty h to wo khud hi is ko call kry ga. toString() ka function khud hi
call ho jaye ga agr class na bhi kry. Ye tb hoga jb hum main class k Object ko print
krty hn.
Hum directly integer ko string me change ni kr skty is k liye different datatypes
use kiu jati hn.e.g.
int a=Integer.parseInt(args[0]);
Throw keyword:
User defined or anyother exception Object can be thrown through this keyword.
Exception can be thrown, jb hum khud sy exception ko throw krna chahty hn tb
hum throw keyword use krty hn, yaha hum sirf exception class ko throw kr skty
hn.
Throws Keyword:
The throws keyword is used in the function handling to create a gateway
through which exception can go to the call in function.
Ye exception ko throw krta hy. Function k end py likha jata hy or ye declare krta
hy k kis type ki exception hy.
Abstract Window ToolKit(AWT):-
Ye java ka aik package hy jis sy hum graphical user interface bana skty hn.
AWT Package contains all the classes used to create a Graphical User
Interface(GUI).
Package is just like a directory file jo k grouped hoti hy aik tarah ki classes k
sath mil kr.
Name Of Classes:
Frame.
Button.
TextField.
TextArea.
Label.
Run
Border Layout:
NORTH
W E
E A
S S
T Centre T
SOUTH
Sign-in Screen Layout:-
UserName:
Password:
login
Grid Layout:-
Ye wo alignment hy jis k thorough components ko screen py layout k xth add kia
jata hy.
Anonymous Object:
Aisa Object jis ka koi name ni hy or na hi variable declare kia hy . ye time or
typing kam krny k liye banaya jata hy.
It is used by the professionals.
It can not be changed.
Can every class be anonymous?
The answer is NO.
Sirf akela border layout ya flowlayout istmal kr k hum achy programmers ni ban
skty
Ek hi screen pre k layout me dusra layout estmal kr skty hn.
What is the default layout of Frame?
Frame ka default layout border layout hy.
Panel:-
Panel invisible hota hy is ka koi Interface ni hota.
Ek layout me dusra layout istmal krny k liye panel use krty hn.
Aik panel me dusra panel or dusry me teesra panel ban skta hy.
Panel ka default layout Flowlayout hai…..