Java powerful notes by Ankit Kumar
notify:
`notify` एक single thread को notify करता है
jo thread object ke monitor par wait karte h
Ye sirf ek thread ko un threads mein se jagata hai
notifyAll:
`notifyAll` सभी waiting threads को notify करता है
Sabhi thread object ke monitor par wait karte h ye unko jagata hai.
Java में , `notify` और `notifyAll` methods का उपयोग threads को synchronized context में notify
करने के लिए किया जाता है फिर से execute कर सकते हैं
*Throw
Throw ek exception ko program se bahar nikalta hai,
ka istemaal exception ko explicitly throw karne ke liye kiya jaata hai
**Throws
ka istemaal method signature mein jyaada exceptions throw karne k liye kiya jata h
throws method signature mein exceptions ko list karta hai.
Throw:
● Method body ya block code ke andar use hota hai.
● Checked ya unchecked exception throw kar sakta hai.
● Sirf ek exception ek baar throw kar sakta hai.
Throws:
● Method signature mein use hota hai.
● Checked aur unchecked dono exceptions declare kar sakta hai.
● Multiple exceptions comma se separate karke declare kar sakte hain.
*Java io streams
Sequence of data hota hai jo source se read karta hai Aur destination se write karta hai
input stream is used to read data from the source
And output stream is used to write data to the destination.
class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}
Types of streams
*Byte stream
Single byte(8 bits) of data ko read aur write karta h
Saari byte stream classes ko base classes se derived kiya jata hai
Jise inputStream aur outputStream class khete hai
Character stream
Single character of data ko read or write karta h
Saari character stream classes ko base classes se derived kiya jata hai
Jise reader aur writer class khete hai
Datagram socket
data packets ko connection ke bina send aur receive kiya jata hai
Connectionless hota hai
networking concept hai
One way communication hota h
stream socket
data packets ko ek connection k sath send aur receive Kiya jata hai
Connection oriented hota h
networking concept hai
Two way communication hota h
C. Datagram socket aur stream socket dono communication endpoints hote h
jo computer networks mein use hote h
**Java Applet mein main method kyun नहीं होता?
[Link] Environment
Isme main method की जरूरत नहीं padti kyuki
वेब ब्राउज़र या applet viewer applet ko चलाते हैं.
ये applet ko चलाते और बंद करते हैं aur applet ka use karte h
[Link] Cycle Methods
main method की जगह पर, applets के लिए
Applet class में कुछ खास methods होते हैं
ये methods applet के परू े (lifecycle) को कंट्रोल करते हैं.
कुछ ज़रूरी lifecycle methods hote hai:
init (शरूु करने के लिए), start (चलाने के लिए), stop (रोकने के लिए), destroy (सबकुछ बंद करने के लिए).
3. Security
अगर applet में main method होता h
To ब्राउज़र के लिए अच्छा नहीं hota h
Kyuki applet k paas कंप्यट ू र k saare resource use karne ki permisssion hoti h
4. Focus on Functionality
Jab main method nahi hota h
tab developer सिर्फ applet के असली काम पे ध्यान deta h
Jese graphics, animation, ui
**Stream tockenizer
ek class jo input stream ko tokens me divide karta h
Log file ko extract karke information nikalta hai
Text files ya stream ko parse karne mein kaam aata h
Alag types k tokens ko specify karke input stream se retreive karta h
*Dynamic binding
program chalate waqt decide hota hai ki kaunsa function call hoga
jisse code flexible ho jata hai.
*String class
1. Immutable hota h
2. Character k sequence ko change nhi kiya ja sakta h
Jiske content ko change nhi kiya ja sakta h
3. Ek baar set karne ka baad change nhi kar sakte
3. Thread safety - no
4. Storage - String pool
*String Buffer
1. Mutable hota h
2. Character k sequence ko change kiya ja sakta h
Jiske content ko change kiya ja sakta h
3. Baar baar change kar sakte
3. Thread safety - yes
4. Storage - heap memory
*AWT
Abstract Window Toolkit
jo (GUI) waale applications banane ke liye use hota hai
Ex. window-based applications,
ek original (GUI) toolkit ya component hai
jaise buttons, windows, aur menus.
*Swing
jo (GUI) waale applications banane ke liye use hota hai
Swing ज्यादा advanced और platform-independent features दे ता है ।
jo AWT ke functionality ko extend karta hai.
Ex. window-based applications,
ek modern aur powerful (GUI) toolkit ya component hai
*Session handling
1. Ek server side mechanism h
2. user aur web application k beech k temprory connection ko maintain karta h
3. Ex. login status, user ko har request me login karne ki jaroorat nhi h
1. Http session
Ek server side object h
Ek browser session k doran user k data ko store aur manage karta h
2. Session id
Java server se ek unique no. nilalta h jo user k session ko identify karta h
3. Methods
SetAttribute(), getAttribute, aur inValidate() attributes session ko control karte h
Ye reusable hota h jise dubara use kiya ja sakta h
setAttribute()
Session me dnata ko store karne k liye hota h
getAttribute()
Session se data ko retrieve karne k liye hota h
Invalidate()
User k session ko end karne k liye hota h
persistance
User k interaction k dauran Data ko maintain karta h
Taki personalised information bani rahe
**Java file class
Files par operations perform karne k liye [Link] ki file class ka use kiya jata h
[Link] is a Java file that contains information about the Java program
Operation Method Package
To create file createNewFile() [Link]
To read file read() [Link]
To write file write() [Link]
To delete file delete() [Link]
**Container
1. Defi.
1. Ek container class java me gui programming class hoti h
2. Jo graphical components ko hold aur organize karti h
3. Like buttons aur panels
2. Examples
Kuch Common Container classes hote h
jframe, jpanel, jdialog hote h
3. Organisation components
Container developers ko allow karta h ki
Graphical elements ko User k liye structure aur arrange kare
4. Layout management
Container layout manager provide karta h
Ye batata h kese components positioned h aur sized h
Aur gui me contribute karta h
5. Modularity
Container class code modularity ko enhance karta h
Ise asani se manage aur update, kiya jata hai
Aur gui app ko maintain rakhta h
`transient
keyword का उपयोग किसी फील्ड को serialization से बाहर रखने के लिए किया जाता है ,
`volatile`
keyword का उपयोग किसी फील्ड की value को सभी threads के लिए तरु ं त अपडेटेड बनाए रखने के लिए
किया जाता है ।
Java mein transient aur volatile do alag-alag keywords hain jo object serialization aur
multithreading ke liye upyogi hote hai
JDBC
Se database connection banane k steps
1. Import jdbc packages
2. Register JDBC driver
3. Database connection establish
4. Create statement
5. Execute sql queries
6. Process result (for select queries)
7. Resource close
serializable
*Layout managers
[Link] components ko visually arrange karte Hain
Greed layout or grid bag layout do layout manager hote Hain
[Link] layout
Jo Java mein GUI banane ke liye components ko
ROw aur columns ke barabar grid mein arrange karta hai
Greed layout ek layout manager hai
Example image gallery ya table
1. Ye simple hota hai
2. Isme components ko kam control karte hai
3. Ye aasan layout ke liye achcha hota h
2. Grid bag layout
Gridbag layout ek powerful layout manager hai
aur flexible hota hai
Jo Java mein GUI banane ke liye components ko grid ke bhiter arrange karta hai
1. Ye jyada complex hota hai
2. Isme har component ke liye alag constant set hota hai
3. Iska use complex layout banane mein hota hai
Exception in Java
1. Exception ek unexpected event hai
2. Jo program ke normal flow ko disrupt(rukavat, baadhit,) karta hai
3. Exception ek runtime error hai
4. jo program ke execution Ko rokata hai
Exception ke causes:
Logical error - Program mein koi galti Jaise division by zero
Runtime error - Program ke execution ke dauran koi galti jaise ki file note found
External error - Program ke bahar koi galti jaise ki network error
Try - ek code block h jisme exception throw kiya jata h
Catch - exception ko handle karta h
Finally - optional block h jo hamesha execute hota h fir chaye exception ho ya na ho
Final, finally, finalize
Final
Ek access modifier h jiska use class, methods aur variables par restriction lagate h
1. variable
1. Isme किसी वेरिएबल को `final` डिक्लेयर किया जाता है
2. तो उसकी वैल्यू को बदला नहीं जा सकता
3. इसे केवल एक बार असाइन किया जा सकता है ।
2. Method
1. यदि किसी मेथड को `final` declare किया जाता है
2. तो उसे सबक्लास में ओवरराइड नहीं किया जा सकता।
3. Class
1. यदि किसी क्लास को `final` declare किया जाता है
2. तो उसे इनहे रिट नहीं किया जा सकता h
Finally
try-catch` स्टे टमें ट के साथ उपयोग किया जाता है
3. यह ब्लॉक हमेशा execute होता है , चाहे exception हो या न हो
1. एक ब्लॉक है जो important codes ko execute karta h
Finalize
. जिसे किसी ऑब्जेक्ट के garbage collected होने से पहले JVM द्वारा कॉल किया जाता है
3. Iska use cleanup processing me kiya jata h
4. Aur finalize() keyword se declare karte h
1. `finalize` एक मेथड है
Applet
1. Ek Chhota program hota h jisse webpage me dalkar internet par chalaya jata h
Ye html me embed hote h jisme tags ka use kiya jata h
2. Aur ise browser ke Java interpreter se execute Kiya jata hai
4. Aaj applet jyada use nahi kiya jata hai kyunki iski security pahle weak thi
Aur JavaScript aur modern technology famous hai
3. Applet ka use web pages mein intercity aur animation jodne ke liye Kiya jata hai
5. Environment - browser me chalana
Security - html embedded, ye os k har browser par chalta h
Dis - isme plugin requied hota h
Ex. JavaScript web me coding5
Applet in container
1. Applet एक छोटा प्रोग्राम होता है
jise ek कंटे नर me integrated karke वेब ब्राउज़र में chalaya jata h
Servlets
1. Chote java program hote h jinhe web server par daalkar internet chalaya jata h
Aur website ko dynamic banata h
4. Data ko process karta h aur html content generate karta h
2. Aur request ka jawab ek request response model se deta h
Servlets http request ka response deta h
3. Servlet container k andar apache, tomcat, servlets k execution ko manage karta h
Java program server site par chalte h
Ex. JavaScript local server par coding
JAP
Java application program
1. Independent program hote Hain
Jo operating system per directly run hote Hain Bina kisi browser k
Java me likhi application hoti h
2. inhe byte code mein compile Kiya jata hai
aur Java runtime environment se execute karte h
3. Application ka use desktop tools, enterprise application and mobile apps mein hota hai
4. ye secure hota hai aur computer per application pahle se install hota hai
Ex. Nokia Mobile s40 me java games aur apps directly install Kiya ja sakta h .jar
Get
1. Ek process h jo server se data mangta hai
2. Limited data amount bhejta hai
kyunki data header mein bheja jata hai
3. Yah secure nahi hota kyunki data URL mein exposed ho jata hai
4. Iski request bookmarked hoti hai
5. Get ki request jyada efficient hoti hai
6. Get post se jyada use Kiya jata hai
7. Get request idempotent hota hai
Post
1. Jo server ko data bhejta hai
2. Post large amount of data bhejta hai
kyunki data ko body mein bheja jata hai
3. Post secured hota hai
kyunki data URL mein expose nahin hota
4. Post ki request bookmarked nahin Hoti
5. Post ki request kam efficient hoti hai
6. Post get se kam use Kiya jata hai
7. Post require non idempotent hota hai
Structured programming
1. Ismein code ko functions mein organise Kiya jata hai
2. Ismein programs ko small program aur functions mein divide Kiya jata hai
3. Yah top down approach follow karta hai
4. Yah code ko jyada importance deta hai
5. Ismein structure, union, aur functions aate Hain
6. Advantages - user friendly and easy to understand hota hai
easier to learn, require less time to write, easy to maintain hota hai
Object oriented programming
1. Ismein code ko objects aur classes me organise Kiya jata hai
2. Ismein program Ko object aur entities mein divide Kiya jata hai
3. Yah bottom up approach follow karta hai
4. Yah data ko jyada importance deta hai
5. Ismein classes object aur oop k concept aate Hain
6. Advantages - reusability of code, effective problem solving
Rmi
Remote method invocation
1. Rmi ek API hai jo JvM mein chal rahe method k object ko dusre JvM mein chal rahe
method k object ko call karta hai
2. Rmi stub or skeleton objects me use kiya jata hai
3. Rmi distributing computing me use kiya jata h
applications banane ke liye
stub for client and skeleton for server
Stub and skeleton
1. Distributing computing se stub aur skeleton create karte h
2. Client program server par remote object ka request bhejta h
3. Aur iske method ko invoke karne ki koshish karta h
1. Stub aur skeleton generate karne k liye
Javac xstubs xskeleton command ka use karte h
2. Ye commands stubs aur skeleton files generate karta h
3. Aur rmic compiler ka use karte h jese
Corba idl, jidl, etc
Java bean
Ek reusable software ya object hota h
jo data ko encapsulate Karta hai
aur ek method provide Karta hai jise data ko access ya manipulate Kiya jata hai
Advantages
Reusability - Java beans ko applications ke different parts mein
dobara istemal Kiya jata hai jise code kam likhna padta hai
Encapsulation - Java bean data aur function ko ek sath pack karta hai
Jise data ko protect ya secure aur object manipulation aasan ho jata h
Maintainability - integration, standardisation Java bean ki platform independent
Default constructor, getter setter, serializable
Constructor
2. objects ko intialise karne k liye use kiya jata h
3. Jab object create hota h to constructor automatically call hota h
4. Class k naam se start hota h
5. Parameter le sakta h
6. Return nahi ho sakta h
Method
1. Ek normal function hota h
4. Parameter le sakta
5. Return type ho sakta h
2. Object par action perform karta h
3. Class k naam k alawa koi bhi naam le sakta h
Cookie
1. वेबसाइट me छोटी information ki text file hoti h
Jo computer me store hoti h
2. User ki activity ko track karta h
Aur user preference ko store karta h
3. Ek session mein thode time k liye hota h
Storage - client side
Security - weak
Size limited - 4 kb
Ex. cookie in the browser
Session variable
1. वेबसाइट पर डाटा स्टोर करने वाला temprory variable होता है
Jo server par store hota h
2. User ki activity ko session tak track karta h
Aur user preference ko store karta h
3. Session mein khatam hone par automatic clear hojata h
Storage - server side
Security - higher
Size - larger capacity
Ex. session option in termux
Synchronisation
ek samay par multiple threads ko access karne se rokta h
jisse data inconsistency aur race conditions se bacha ja sake
1. Declaration
Synchronised keyword se declare karte
Method ko execute karne se pehle method ko mark karte h
Ex. playstore me ek time par do apps update nhi hote h
Play Store me sirf ek app ko update karte h
taaki sab kuch sahi tareeke ho
JDK (Java Development Kit)
1. ek software development kit hoti h
2. Ek complete toolset hai jisse java applications aur applets ko develop aur run kiya jata h
3. jisme compiler javac, archiver jar, documentation javadoc
interpreter/loader java hote h
Ex. Android studio
*Jac
1. java abstract class ek blueprint h
2. Isme direct object nhi banaya jata
3. lekin dusri class se inherit karke object banaya ja sakta h
4. Isme abstract methods aur concrete methods hote h
5. subclass mein implementation karna hota hai
Ex. jese vs code khud c lang. ka code run nhi kar sakta iske liye minigw ko vs code me install
kiya jata h jisse c lang. ka code run kiya ja sakta h aur
Vs code aur minigw aapas mein information share karte h
Oops
Code ko objects me organise Kiya jata h
Oops classes aur objects se bana hota h
Principle - encapsulation, polymorphism, abstraction, inhert.
jisme class aur object ka concept hota h
Ex. ek topic k baare me multiple information batana
Advantages :
1. Reusability
oop se code ko reuse kiya jata h
2. Maintainability
Oop se code maintain karna aasan hota h
3. Flexiblity
Oop se code ko flexible banaya jata h
Features:
class
1. class ek blueprint होता hai object ko create karne k liye
2. Isme Attribute or method ki definition hoti h
3. aur objects(attribute) k properties(methods) aur behaviour define karta h
Ex. animals(oop) me dogs ki species(class) h
object
1. ek real-world entities ko represent karta h
2. Object me data aur code ya attribute aur method dono hote h
3. object mein data aur us data ke liye behavior होता है
Ex. car, person, book
Encapsulation
1. Class me data aur function ko eksaath bind karta h
aur data ko control, protect karta h
2. Aur direct access ko rokta h
3. Data aur uske kaam karne wale funcions ko ek package mein band karta h
Ex. check bank balance
Abstraction
abstraction internal , complex details chupata hai aur
simple user interface user provide karta hai user k liye
Ex. only car driving skill
Key Differences
Purpose
Abstraction: Complexity ko chhupana aur high-level interface provide karna.
Encapsulation: Data ko secure aur data integrity maintain karna.
Implementation:
Abstraction: Interfaces aur abstract classes ka use karke achieve hota hai.
Encapsulation: Access modifiers (jaise private, protected, public) ka use karke achieve hota
hai
Focus
Abstraction: Kya show karna hai (what to show).
Encapsulation: Kaise protect karna hai (how to protect).
Dono ek dusre par depend hote h aur dono milkar software ko organised aur manage karte h
Polymorphism
1. ek method ko different tarikon se implement Kiya jata hai
2. Ek method se अलग-अलग object ko call karte hain
3. Ka matlab hota h many forms yani ek se jyada form
Ex. jese ek aadmi father, husband, employee ho sakta
Compile time polymorphism
jab program compile hota h compiler decide karta hai ki kis method ko call karna hai
ise method overloading k through implement kiya jata h
ye static hota h jisko change nhi kiya ja sakta h
run time poly.
jab program run hota h tab object ko change kar sakte h
isse method overriding k trough implement kiya jata h
ye dyamic hota h
aur iske function ya method ko call kar sakte h
InHeritance
Ek base class se derived class banate hain
Ek class k data ko dusre class se access karte h
Ex. father aur son aapas me chije share karte h
Inheritance kitne type ke hote Hain java me
single, multilevel, hierarchical
java me multiple aur hybrid inheritance nhi hote kyuki interface ko support h
single inhe.
Ek base class se ek se derive class banate hain
Ek class k data ko dusre class se access karte h
Ex. father se son jo aapas me chije share karte h
Hierarchical Inheritance
Ek base class se ek se jyada derive class banate hain
Ex. science se physics chemistry biology bante Hain
Multi level inheritance
Base Class se derived class banate hain
aur derive class se hi derive class bana sakte hain
Ex. class a se class b banayi aur class b se class c banayi
interface
1. ye multiple inheritance ko support karta h
2. ek blueprint h jisme sirf abstract methods hote h
3. Ex. Remote control ek interface ki tarah kaam karta hai
4. jo TV ko commands, methods bhejta hai jaise "volume up" ya "channel down", aur TV in
commands ko implement karta hai
5. Isme do interface se jyada declare kar sakte h aur unke abstract methods ko bhi
Multiple inheritance
Do base class se ek derive class banate hain Jaise mother father se children banta hai
Hybride
single inheritance aur multiple inheritance ka combination.
package
1. java mein package classes ka ek group होता है
2. jise code ko organise aur conflicts se bachane mein madad करता है
3. jise aap folders ki tarah manage kar sakte hain.
4. Java mein package classes ka ek folder jaisa होता है
do types k packages hote h-
user defined packages
in built packages
Ex.
1. Import package_name *
2. Import package_name.class_name
3. package_name.class_name ob = new package_name.class name();
keywords
1. java me 52 reserved words hote h
2. keywords are reserved words jinka meaning compiler janta h that have a specific meaning
within the programming language itself
access specifier
keywords hote h
class, method, constructor aur variable ki accessibility ya scope ko control karte hain.
types-
private, protected, public, default(no specifier)
JDBC
(Java Database Connectivity)
jo Java programs ko databases se connect होने में मदद करता hai.
JRE
**Java runtime environment
ek software layer hoti h jo os ki top layer par chalti h
Java Runtime Environment जावा प्रोग्राम चलाने के लिए ek जरूरी software होता है ।
JRE वो वातावरण है जो आपके कंप्यट ू र पर जावा प्रोग्राम चलाने दे ता है ।
jvm
java virtual machine
java program chalane k liye ek environment banaya jata h, ye platfoarm independence hota h
ye runtime environment provide karta h
jo java bytecode ko machie lang me convert karta h
jvm jre ka part h
rmi
(Remote Method Invocation) ek machine पर चल रहे program ke methods को dusri machine se
call kar sakte hain
exception kese hota h
try - catch throw se exception to handle kiya jata h
Bytecode
bytecode intermediate code hota h
Jo JVM mein run hota h
Aur platform independence enable hota h
applet
ye html me embed hote h
ye dynamic content ko generate karta h
lifecycle:
intialize, stated, painted, stoped, destroyed
jap
java application program
java me likha hota h aur jvm ya os par chalaya jata h indepedely
ek calculator, ek text editor, ek web browser, ek game
Structure programming
Code ko functions mein organise Kiya jata hai Large aur complex program Ko small piece of
code aur more manageable mein convert karta ha
GET, POST
GET aur POST HTTP request bhejte H aur data ko receive karte hain
error
Java mein error program चलने par problem को show karta h
error ko recover nhi kiya ja sakta h
ex. memory error, hardware error, jvm error, etc
java
java high level, oop aur secaure programming lang. H
Jiska use large scale applications ko develop karne k liye kiya jata h
java k features:
high level language, object oreinted, platform independence, robust(strong), secure
local variable ki default value kya hoti h
kuch nhi hoti,
local variable me koi value assigned nhi hoti h
java is not pure oop lang.
because ye primitive data types ko support karta h
jese int, float, double, byte, short, aur boolean
byte ki default value kya h?
0 hoti h
relational operator in java
relational operator ko comparison operator bhi kha jata h jese
>,>=,<,<=,==,!=
java code k sum ki value kya h?
byte num1, num2, num3;
num1 = 10;
num2 = 10
sum = num1 + num3;
ans ye code compile nhi hoga kyuki byte ko int me promote kara jata h
right code:
sum = (byte)(num1+num2);
now sum gives 20.
primitive data types kya hote h
int, short, byte, long float, double, char, boolean
Who developed java
java ko james gosling ne developed kiya tha
Method overloading
1. M o mein class ke andar same Naam ke multiple methods hote per different parameter
hote Hain
ye compile time polymorphism hota h
Method overrding
1. M o mein class ke andar same Naam ke multiple methods hote aur sam naam k
parameter hote Hain
Default constructor
Me objects banate h bina data members ko define kiye
constructor k paas return value nhi hota h
Parameterized constructor
Me objects banate h aur parameters define h
Copy constructor
Exisiting objtect ka duplicate banata h
Java Basic Input and Output
[Link](); or
[Link](); or
[Link]();