0 ratings0% found this document useful (0 votes) 2 views14 pagesJava Code Examples
Examples for coding in java
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
© Absolutely. These are the most important Java code
examples that freshers are expected to understand and
explain during interviews.
1. Hello World
BENZ) QO
public class Main {
SoS EComeTesTECR oy Tel String[] args) {
[Link](’ y
a
} a
Interview Question: Why is main( static?
JVM can call it without creating an object.2. Constructor Example
Java O
AER msiartelooeg
String name;
Tet
peeved B
PINOLE RSE Vert
nee omc tis3. Constructor Overloading
Java oO
OCeeea OTE |
Pay ocOnt
Sees aren @ screens w
y
Employee(String name) {
[Link](name);
a
} a
4. Encapsulation
Java a’)
Crees Cra|
PN RSes te ny
PST CR aC Rn Cc Cente!
Den CC en rty
y
Pro roaseen teen retin
peter
}
}
PRSICROCESS VETER
Proce Ch OCR cinco ne
Sat Cree CaS
Reon rt Qc ctw
Sacre a semen Cec)
}
} oe5. Inheritance
EVE QO
class Animal {
SPAT RTITTLOKG
[Link]("Animal makes sound");
}
}
class Dog extends Animal {
void bark(Q {
[Link]("Dog barks");
}
}
public class Main {
public static void main(String|] args) {
Dog d = new Dog();
[Link]();
Carte c0y
y
i u6. Method Overloading
BENET
CEE Mer ean Coban §
int add(int a, int b) {
pcos Bald
}
ratte na eee
returna+b+c;
}
y
7. Method Overriding
Java
class Animal {
SOIT
Spout eset Ott escn tO 7
yi
y
OEE etcocte enn Tie|
eed
SOc
cme e sett Gt p= ce oy
}
y8. Abstract Class
Java
abstract class Vehicle {
Ele tecCol mace ORE
a
Eee seca Tae |
void start() {
[Link]("Car Started");
iy
a
9. Interface
Java
PrseCeeUTTeTTE
Droste Ratres(
v
class Dog implements Animal {
public void sound() {
[Link]("Bark");
a
i10. Exception Handling
BEV QO
pt beRO ETB UC ben |
PCR CROCE TT en TENT EG
aa
piste ch lew UPA
pero eee
Ieee) Be
[Link](’ y
} finally {
[Link](’ y
i
}
} (HySERGIE Ci Keyes [Link]-yoluleyel
REN QO
DOERR SME MO Neraae ces CM CEES eT CORG
InvalidAgeException(String msg) {
Bienes)
}
}
PIT RIES VEINS
static void check Age(int age)
ee NL escent
Cree
Parco EVP aes
Cleat lye
12. ArrayList
REN OQ
Tyee CUCRTSIW Veen DR CT
public class Main {
public static void main(String[] args) {
Neca AROSHen ESTES
Dre vec ov Abioes
eac(RE Os
[Link]("Spring’);
Sema soe riney
}
y oYeM sETyesy-i4
BEV QO
import [Link]. HashSet;
pel ybee mC bon |
public static void main(Stringl] args) {
HashSet set =
new HashSet<>();
[Link]("Java");
[Link](" Java");
[Link](set);
I
} a
Output:
Plain text O
[Java] a
Duplicate removed.14. HashMap
BENZ’
import [Link]. HashMap;
public class Main {
PCR CBC cies IT Re
PE COVES aSes Teese Te
new HashMap<>();
pet) eat Re LDH
[Link](2,"Spring”);
[Link] printIn(map);
y
}
15. String Reverse
BEV
PIO Eee Urine
public static void main(String[] args) {
fleets ae
Sienna eae
for(int i=[Link]-1;i>=0;i--) {
merece
}
uma sssttih aay16. Palindrome
Java
Praeo LESS VIG
PT eC ta OCR r i Cte
eect
vetep Cami
Onecare tN) Sree reo yy
Seema
a
if([Link](rev)) {
Seer nase @ action
}
»
}
17. Factorial
Java
Pra rRa EES UCT
PRTC CR LOG B rrr tert IEEE
intn=5;
int fact = 1;
for(int i=lji<=njit+) {
cca
i
Sums nen iN (ty18. Prime Number
Java QO
EBUEVORE
main(String] args) {
prime = true;
NCS EVES tee
(n%i==0) {
pablo
[Link](prime ?
"Prime" : "Not Prime’);19. JDBC Connection Example
RENN oO
Sone EER ia
public class Main {
po EomseceCOR TosCeBertereel(ciesnCe lI EETee(5)
eee nt
Connection con =
Na VENT Tome Keone Ton
Bloc eer oec el
Dota
eee
Sacer maerstabal(
Recomm ccnst ccc ay
Interview Favorite: Explain each step
20. Spring Boot REST API
RENN QO
Osean
pan Ros art oKocecernecaloant
@GetMapping(’/hello")
Paneer nteeaInE
return "Hello Worl
}
p oTop 10 Code Examples You Must
Master
ce
ee
Constructor
Encapsulation
Inheritance
Overloading
Overriding
Exception Handling
ArrayList
HashMap
String Reverse
10. JDBC Connection
If you can write these 10 from memory, you're already ahead of
many fresher Java candidates in interviews. 6 @@