0% found this document useful (0 votes)
37 views11 pages

50 Java Programs for Beginners

The document contains a practical file with 50 Java programs, each accompanied by code and sample output. The programs cover various concepts such as basic syntax, control structures, object-oriented programming, exception handling, file operations, and more. These programs are intended for educational purposes and may require minor adjustments based on the environment.

Uploaded by

cobrasnack3421
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views11 pages

50 Java Programs for Beginners

The document contains a practical file with 50 Java programs, each accompanied by code and sample output. The programs cover various concepts such as basic syntax, control structures, object-oriented programming, exception handling, file operations, and more. These programs are intended for educational purposes and may require minor adjustments based on the environment.

Uploaded by

cobrasnack3421
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Practical File – Krishu Yadav

50 Java Programs (Code + Sample Output). Programs are intended for learning and may need small
environment-specific changes (e.g., file paths, DB credentials).

1. Hello World
class Hello {
public static void main(String[] args) {
[Link]("Hello World!");
}
}

Sample Output:
Output:
Hello World!

2. Sum of Two Numbers


import [Link];
class Sum {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter two numbers: ");
int a = [Link]();
int b = [Link]();
[Link]("Sum = " + (a + b));
}
}

Sample Output:
Example Input: 5 10
Output:
Sum = 15

3. Even or Odd
import [Link];
class EvenOdd {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
if(n%2==0) [Link]("Even");
else [Link]("Odd");
}
}

Sample Output:
Input: 9
Output:
Odd
4. Factorial
import [Link];
class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
int fact = 1;
for(int i=1;i<=n;i++) fact *= i;
[Link]("Factorial = " + fact);
}
}

Sample Output:
Input: 5
Output:
Factorial = 120

5. Fibonacci (first 10)


class Fibonacci {
public static void main(String[] args) {
int n1=0, n2=1, n3;
[Link](n1 + " " + n2);
for(int i=2;i<10;i++){
n3 = n1 + n2;
[Link](" " + n3);
n1 = n2; n2 = n3;
}
}
}

Sample Output:
Output:
0 1 1 2 3 5 8 13 21 34

6. Largest of Three
class Largest {
public static void main(String[] args) {
int a=10,b=25,c=15;
if(a>b && a>c) [Link]("Largest = " + a);
else if(b>c) [Link]("Largest = " + b);
else [Link]("Largest = " + c);
}
}

Sample Output:
Output:
Largest = 25

7. Palindrome Number
class Palindrome {
public static void main(String[] args) {
int n=121, rev=0, temp=n;
while(n>0){ rev = rev*10 + n%10; n /= 10; }
[Link](temp==rev ? "Palindrome" : "Not Palindrome");
}
}

Sample Output:
Output:
Palindrome

8. Prime Check
class Prime {
public static void main(String[] args) {
int n=7, count=0;
for(int i=1;i<=n;i++) if(n%i==0) count++;
[Link](count==2 ? "Prime Number" : "Not Prime");
}
}

Sample Output:
Output:
Prime Number

9. Reverse Number
class Reverse {
public static void main(String[] args) {
int n=1234, rev=0;
while(n>0){ rev = rev*10 + n%10; n/=10; }
[Link]("Reverse = " + rev);
}
}

Sample Output:
Output:
Reverse = 4321

10. Multiplication Table


class Table {
public static void main(String[] args) {
int n=5;
for(int i=1;i<=10;i++) [Link](n + " x " + i + " = " + (n*i));
}
}

Sample Output:
Output:
5 x 1 = 5
...
5 x 10 = 50
11. Class & Object
class Student {
String name; int age;
void display(){ [Link]("Name: "+name+", Age: "+age); }
public static void main(String[] args){
Student s = new Student();
[Link]="Krishu"; [Link]=20; [Link]();
}
}

Sample Output:
Output:
Name: Krishu, Age: 20

12. Inheritance (Single)


class Animal { void eat(){ [Link]("Eating..."); } }
class Dog extends Animal { void bark(){ [Link]("Barking..."); } }
class Test { public static void main(String[] args){ Dog d=new Dog(); [Link](); [Link](); } }

Sample Output:
Output:
Eating...
Barking...

13. Multi-level Inheritance


class A{void a(){[Link]("A");}} class B extends A{void b(){[Link]("B");}} class C

Sample Output:
Output:
A
B
C

14. Method Overriding


class Bank{int rate(){return 0;}} class SBI extends Bank{int rate(){return 8;}} class Test{public static

Sample Output:
Output:
8

15. Method Overloading


class Demo{ void show(int a){[Link](a);} void show(String s){[Link](s);} public s

Sample Output:
Output:
5
Hi
16. Abstract Class
abstract class Shape{ abstract void draw(); } class Circle extends Shape{ void draw(){ [Link]

Sample Output:
Output:
Circle

17. Interface Example


interface Drawable{ void draw(); } class Rect implements Drawable{ public void draw(){ [Link]

Sample Output:
Output:
Rect

18. Exception Handling (try-catch-finally)


class ExDemo{ public static void main(String[] args){ try{ int a=10/0; } catch(ArithmeticException e){ Sy

Sample Output:
Output:
Cannot divide by zero
Done

19. Custom Exception


class MyException extends Exception{ MyException(String s){ super(s); } } class Test{ static void check(i

Sample Output:
Output:
Age < 18

20. File Write & Read


import [Link].*;
class FileExample{
public static void main(String[]args)throws Exception{
FileWriter fw=new FileWriter("[Link]");
[Link]("Hello File");
[Link]();
FileReader fr=new FileReader("[Link]");
int c; while((c=[Link]())!=-1) [Link]((char)c);
[Link]();
}
}

Sample Output:
Output:
Hello File
21. File Copy (byte)
import [Link].*;
class CopyFile{ public static void main(String[]args)throws Exception{ FileInputStream in=new FileInputSt

Sample Output:
Output:
Copied

22. String Reverse


class StrRev{ public static void main(String[]args){ String s="Hello"; String rev=""; for(int i=[Link](

Sample Output:
Output:
olleH

23. StringBuilder usage


class SB{ public static void main(String[]args){ StringBuilder sb=new StringBuilder(); [Link]("Hi"); s

Sample Output:
Output:
Hi Java

24. Collections - ArrayList


import [Link].*;
class AL{ public static void main(String[]args){ ArrayList<String> a=new ArrayList<>(); [Link]("A"); [Link]

Sample Output:
Output:
A
B

25. Collections - HashMap


import [Link].*;
class HM{ public static void main(String[]args){ HashMap<Integer,String> m=new HashMap<>(); [Link](1,"One"

Sample Output:
Output:
Two

26. Comparable example (sort custom objects)


import [Link].*;
class Person implements Comparable<Person>{ String name; int age; Person(String n,int a){name=n;age=a;} p
class Test{ public static void main(String[]args){ ArrayList<Person> l=new ArrayList<>(); [Link](new Perso

Sample Output:
Output:
B-20
A-25

27. Thread by extending Thread


class MyThread extends Thread{ public void run(){ for(int i=1;i<=3;i++) [Link]("T:"+i); } pub

Sample Output:
Output (example):
T:1
T:2
T:3

28. Thread by Runnable


class R implements Runnable{ public void run(){ for(int i=1;i<=3;i++) [Link]("R:"+i); } publi

Sample Output:
Output:
R:1
R:2
R:3

29. Synchronized block


class Counter{ int c=0; public void inc(){ synchronized(this){ c++; } } }
class TestSync{ public static void main(String[]args)throws Exception{ Counter ct=new Counter(); Thread t

Sample Output:
Output (expected):
2000

30. Lambda expression


import [Link].*;
class LambdaDemo{ public static void main(String[]args){ List<Integer> l=[Link](1,2,3); [Link](

Sample Output:
Output:
1
2
3

31. Stream API - filter


import [Link].*;
import [Link].*;
class StreamDemo{ public static void main(String[]args){ List<Integer> l=[Link](1,2,3,4,5); [Link]

Sample Output:
Output:
2
4

32. Date and Time (LocalDate)


import [Link].*;
class DateDemo{ public static void main(String[]args){ [Link]([Link]()); } }

Sample Output:
Output:
(prints current date)

33. JDBC Example (connection skeleton)


// Requires JDBC driver and DB; this is a skeleton
import [Link].*;
class JDBCDemo{
public static void main(String[]args)throws Exception{
Connection con = [Link]("jdbc:yourdb","user","pass");
Statement st = [Link]();
ResultSet rs = [Link]("SELECT * FROM table");
while([Link]()) [Link]([Link](1));
[Link](); [Link](); [Link]();
}
}

Sample Output:
Output:
(Depends on DB rows)

34. Serialization Example


import [Link].*;
class Emp implements Serializable{ String name; int id; Emp(String n,int i){name=n;id=i;} }
class SerDemo{ public static void main(String[]args)throws Exception{ Emp e=new Emp("A",1); ObjectOutputS

Sample Output:
Output:
Serialized

35. Deserialization Example


import [Link].*;
class SerDemoRead{ public static void main(String[]args)throws Exception{ ObjectInputStream in=new Object

Sample Output:
Output:
Deserialized: Emp

36. Properties File


import [Link].*;
import [Link].*;
class PropDemo{ public static void main(String[]args)throws Exception{ Properties p=new Properties(); p.s

Sample Output:
Output:
Saved

37. Regex Example


import [Link].*;
class RegexDemo{ public static void main(String[]args){ Pattern p=[Link]("\\d+"); Matcher m=[Link]

Sample Output:
Output:
123

38. Binary File Read/Write


import [Link].*;
class BinDemo{ public static void main(String[]args)throws Exception{ FileOutputStream out=new FileOutput

Sample Output:
Output:
1 2 3

39. Map with computeIfAbsent


import [Link].*;
class MapDemo{ public static void main(String[]args){ Map<String,Integer> m=new HashMap<>(); [Link]

Sample Output:
Output:
1

40. Optional Example


import [Link].*;
class OptDemo{ public static void main(String[]args){ Optional<String> o=[Link]("Hello"); [Link]

Sample Output:
Output:
Hello

41. Read console input (BufferedReader)


import [Link].*;
class BR{ public static void main(String[]args)throws Exception{ BufferedReader br=new BufferedReader(new

Sample Output:
Output:
Input: Hi
You: Hi

42. Command-line args


class Cmd{ public static void main(String[]args){ for(String s:args) [Link](s); } }

Sample Output:
Run: java Cmd A B
Output:
A
B

43. Exception chaining


class ExA extends Exception{ ExA(String s){ super(s); } }
class ExDemo2{ static void m() throws ExA{ try{ [Link]("a"); } catch(Exception e){ throw new Ex
public static void main(String[]args){ try{ m(); } catch(Exception e){ [Link]([Link]()

Sample Output:
Output:
Parse failed

44. Runtime Polymorphism


class Parent{ void show(){ [Link]("Parent"); } }
class Child extends Parent{ void show(){ [Link]("Child"); } }
class TestPoly{ public static void main(String[]args){ Parent p = new Child(); [Link](); } }

Sample Output:
Output:
Child

45. Static block and static variable


class StaticDemo{ static int x; static{ x=5; [Link]("Static block"); } public static void mai

Sample Output:
Output:
Static block
5

46. Final keyword example


class FinalDemo{ final int x=10; public static void main(String[]args){ FinalDemo f=new FinalDemo(); Syst

Sample Output:
Output:
10

47. Nested class (inner class)


class Outer{ int x=10; class Inner{ void show(){ [Link](x); } } public static void main(Strin

Sample Output:
Output:
10

48. Anonymous class


interface G{ void go(); }
class TestAnon{ public static void main(String[]args){ G g=new G(){ public void go(){ [Link](

Sample Output:
Output:
Running

49. DateTime formatting


import [Link].*;
import [Link].*;
class DateFmt{ public static void main(String[]args){ String f = [Link]("dd-MM-yyyy

Sample Output:
Output:
(prints formatted datetime)

50. HTTP request using HttpURLConnection (simple)


import [Link].*;
import [Link].*;
class HttpDemo{ public static void main(String[]args)throws Exception{ URL u=new URL("[Link]

Sample Output:
Output:
(HTML of [Link])

You might also like