Java Programming
[Link]
Creating hello java example
class Simple{
public static void main(String args[]){
[Link]("Hello Java");
}
}
Output: Hello Java
Program of factorial number.
class Operation{
static int fact(int number){
int f=1;
for(int i=1;i<=number;i++){
f=f*i;
}
return f;
}
public static void main(String args[]){
int result=fact(5);
[Link]("Factorial of 5="+result);
}
}
Program of fibonacci series.
class Fabnoci{
public static void main(String...args)
{
int n=10,i,f0=1,f1=1,f2=0;
for(i=1;i<=n;i++)
{
f2=f0+f1;
f0=f1;
f1=f2;
f2=f0;
[Link](f2);
}
}
}
Java Programming
[Link]
Program of armstrong number.
class ArmStrong{
public static void main(String...args)
{
int n=153,c=0,a,d;
d=n;
while(n>0)
{
a=n%10;
n=n/10;
c=c+(a*a*a);
}
if(d==c)
[Link]("armstrong number");
else
[Link]("it is not an armstrong number");
}
}
Program of checking palindrome number.
class Palindrome
{
public static void main( String...args)
{
int a=242;
int n=a,b=a,rev=0;
while(n>0)
{
a=n%10;
rev=rev*10+a;
n=n/10;
}
if(rev==b)
[Link]("it is Palindrome");
else
[Link]("it is not palinedrome");
}
}
Program of swapping two numbers without using third variable.
class SwapTwoNumbers{
Java Programming
[Link]
public static void main(String args[]){
int a=40,b=5;
a=a*b;
b=a/b;
a=a/b;
[Link]("a= "+a);
[Link]("b= "+b);
}
}
Program of factorial number by recursion
class FactRecursion{
static int fact(int n){
if(n==1)
return 1;
return n*=fact(n-1);
}
public static void main(String args[]){
int f=fact(5);
[Link](f);
}
}
Simple Example of Object and Class
Public class Student{
int id; //data member (also instance variable)
String name; //data member(also instance variable)
public static void main(String args[]){
Student s1=new Student();//creating an object of Student
[Link]([Link]+" "+[Link]);
}
}
Output: 0 null
Java Programming
[Link]
Example of Object and class that maintains the records of students
class Student{
int rollno;
String name;
void insertRecord(int r, String n){ //method
rollno=r;
name=n;
}
void displayInformation(){[Link](rollno+" "+name);}//method
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
[Link](111,"Name1");
[Link](222,"Name2");
[Link]();
[Link]();
}
}
Output: 111 Name1
222 Name2
public class Rectangle{
int length;
int width;
void insert(int l,int w){
length=l;
width=w;
}
void calculateArea(){
[Link](length*width);
}
public static void main(String args[]){
Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle();
Java Programming
[Link]
[Link](11,5);
[Link](3,15);
[Link]();
[Link]();
}
}
Output: 55
45
Public class Calculation{
void fact(int n){
int fact=1;
for(int i=1;i<=n;i++){
fact=fact*i;
}
[Link]("factorial is "+fact);
}
public static void main(String args[]){
new Calculation().fact(5); //calling method with annonymous object
}
}
Output: Factorial is 120
public class Rectangle{
int length;
int width;
void insert(int l,int w){
length=l;
width=w;
}
void calculateArea(){[Link](length*width);}
public static void main(String args[]){
Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle();//creating two objects
[Link](11,5);
[Link](3,15);
Java Programming
[Link]
[Link]();
[Link]();
}
}
Output: 55
45
Example of Method Overloading by changing the no. of arguments
class Calculation{
void sum(int a,int b){[Link](a+b);}
void sum(int a,int b,int c){[Link](a+b+c);}
public static void main(String args[]){
Calculation obj=new Calculation();
[Link](10,10,10);
[Link](20,20);
}
}
Output: 30
40
Example of Method Overloading by changing data type of argument
class Calculation{
void sum(int a,int b){[Link](a+b);}
void sum(double a,double b){
[Link](a+b);
}
public static void main(String args[]){
Calculation obj=new Calculation();
[Link](10.5,10.5);
[Link](20,20);
}
}
Output: 21.0
40
Example of default constructor
class Bike{
Java Programming
[Link]
Bike(){[Link]("Bike is created");}
public static void main(String args[]){
Bike b=new Bike();
}
}
Example of default constructor that displays the default values
class Student{
int id;
String name;
void display(){[Link](id+" "+name);}
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
[Link]();
[Link]();
}
}
Output: 0 null
0 null
Example of parameterized constructor
class Student{
int id; s
String name;
Student(int i,String n){
id = i;
name = n;
}
void display(){[Link](id+" "+name);}
public static void main(String args[]){
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
[Link]();
[Link]();
}
}
Java Programming
[Link]
Output: 111 Karan
222 Aryan
Example of Constructor Overloading
class Student{
int id;
String name;
int age;
Student(int i,String n){
id = i;
name = n;
}
Student(int i,String n,int a){
id = i;
name = n;
age=a;
}
void display(){[Link](id+" "+name+" "+age);}
public static void main(String args[]){
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan",25);
[Link]();
[Link]();
}
}
Output: 111 Karan 0
222 Aryan 25
Example of static variable
class Student{
int rollno;
String name;
static String college ="ITS";
Student(int r,String n){
rollno = r;
name = n;
}
void display (){[Link](rollno+" "+name+" "+college);}
public static void main(String args[]){
Student s1 = new Student (111,"Karan");
Student s2 = new Student (222,"Aryan");
Java Programming
[Link]
[Link]();
[Link]();
}
Output: 111 Karan ITS
222 Aryan ITS
Understanding the problem without this keyword
class student{
int id;
String name;
student(int id,String name){
id = id;
name = name;
}
void display(){[Link](id+" "+name);}
public static void main(String args[]){
student s1 = new student(111,"Karan");
student s2 = new student(321,"Aryan");
[Link]();
[Link]();
}
}
Output: 0 null
0 null
Solution of the above problem by this keyword
class Student{
int id;
String name;
student(int id,String name){
[Link] = id;
[Link] = name;
}
void display(){[Link](id+" "+name);}
public static void main(String args[]){
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
[Link]();
[Link]();
}
Java Programming
[Link]
}
Output 111 Karan
222 Aryan
Program where this keyword is not required
class Student{
int id;
String name;
student(int i,String n){
id = i;
name = n;
}
void display(){[Link](id+" "+name);}
public static void main(String args[]){
Student e1 = new Student(111,"karan");
Student e2 = new Student(222,"Aryan");
[Link]();
[Link]();
}
}
Output: 111 Karan
222 Aryan
this() can be used to invoked current class constructor
class Student{
int id;
String name;
Student (){[Link]("default constructor is invoked");}
Student(int id,String name){
this ();//it is used to invoked current class constructor.
[Link] = id;
[Link] = name;
}
void display(){[Link](id+" "+name);}
public static void main(String args[]){
Student e1 = new Student(111,"karan");
Student e2 = new Student(222,"Aryan");
[Link]();
[Link]();
}
}
Output:
default constructor is invoked
Java Programming
[Link]
default constructor is invoked
111 Karan
222 Aryan
Programmer IS-A Employee
class Employee{
float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
public static void main(String args[]){
Programmer p=new Programmer();
[Link]("Programmer salary is:"+[Link]);
[Link]("Bonus of Programmer is:"+[Link]);
}
}
Output: Programmer salary is:40000.0
Bonus of programmer is:10000
Example of method overriding
class Vehicle{
void run(){[Link]("Vehicle is running");}
}
class Bike extends Vehicle{
void run(){[Link]("Bike is running safely");}
public static void main(String args[]){
Bike obj = new Bike();
[Link]();
}
Output: Bike is running safely
super is used to refer immediate parent class instance variable.
Problem without super keyword
class Vehicle{
int speed=50;
}
class Bike extends Vehicle{
int speed=100;
Java Programming
[Link]
void display(){
[Link](speed);//will print speed of Bike
}
public static void main(String args[]){
Bike b=new Bike();
[Link]();
}
}
Output: 100
Solution by super keyword
//example of super keyword
class Vehicle{
int speed=50;
}
class Bike extends Vehicle{
int speed=100;
void display(){
[Link]([Link]);//will print speed of Vehicle now
}
public static void main(String args[]){
Bike b=new Bike();
[Link]();
}
}
Output: 50
super is used to invoke parent class constructor.
class Vehicle{
Vehicle(){[Link]("Vehicle is created");}
}
class Bike extends Vehicle{
Bike(){
super();//will invoke parent class constructor
[Link]("Bike is created");
}
public static void main(String args[]){
Bike b=new Bike();
}
}
Output: Vehicle is created
Bike is created
Java Programming
[Link]
Example of instance block
class A{
A(){
[Link]("parent class constructor invoked");
}
}
class B extends A{
B(){
super();
[Link]("child class constructor invoked");
}
B(int a){
super();
[Link]("child class constructor invoked "+a);
}
{[Link]("instance initializer block is invoked");}
public static void main(String args[]){
B b1=new B();
B b2=new B(10);
}
}
Output: parent class constructor invoked
instance initializer block is invoked
child class constructor invoked
parent class constructor invoked
instance initializer block is invoked
child class constructor invoked 10
Example of final variable
class Bike{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
}
public static void main(String args[]){
Bike obj=new Bike();
[Link]();
}
}//end of class
Output: Compile Time Error
Java Programming
[Link]
Example of final method
class Bike{
final void run(){[Link]("running");}
}
class Honda extends Bike{
void run(){[Link]("running safely with 100kmph");}
public static void main(String args[]){
Honda honda= new Honda();
[Link]();
}
}
Output: Compile Time Error
Example of final class
final class Bike{}
class Honda extends Bike{
void run(){[Link]("running safely with 100kmph");}
public static void main(String args[]){
Honda honda= new Honda();
[Link]();
}
}
Output: Compile Time Error
Example of Runtime Polymorphism
class Bike{
void run(){[Link]("running");}
}
class Splender extends Bike{
void run(){[Link]("running safely with 60km");}
public static void main(String args[]){
Bike b = new Splender();//upcasting
[Link]();
}
}
Java Programming
[Link]
Output: running safely with 60km.
Real example of Java Runtime Polymorphism
class Bank{
int getRateOfInterest(){return 0;}
}
class SBI extends Bank{
int getRateOfInterest(){return 8;}
}
class ICICI extends Bank{
int getRateOfInterest(){return 7;}
}
class AXIS extends Bank{
int getRateOfInterest(){return 9;}
}
class Test{
public static void main(String args[]){
Bank b1=new SBI();
Bank b2=new ICICI();
Bank b3=new AXIS();
[Link]("SBI Rate of Interest: "+[Link]());
[Link]("ICICI Rate of Interest: "+[Link]());
[Link]("AXIS Rate of Interest: "+[Link]());
}
}
Output:
SBI Rate of Interest: 8
ICICI Rate of Interest: 7
AXIS Rate of Interest: 9
Runtime Polymorphism with Multilevel Inheritance
class Animal{
void eat(){[Link]("eating");}
}
class Dog extends Animal{
void eat(){[Link]("eating fruits");}
}
class BabyDog extends Dog{
Java Programming
[Link]
void eat(){[Link]("drinking milk");}
public static void main(String args[]){
Animal a1,a2,a3;
a1=new Animal();
a2=new Dog();
a3=new BabyDog();
[Link]();
[Link]();
[Link]();
}
}
Output: eating
eating fruits
drinking Milk
Example of static binding
class Dog{
private void eat(){[Link]("dog is eating...");}
public static void main(String args[]){
Dog d1=new Dog();
[Link]();
}
}
Example of dynamic binding
class Animal{
void eat(){[Link]("animal is eating...");}
}
class Dog extends Animal{
void eat(){[Link]("dog is eating...");}
public static void main(String args[]){
Animal a=new Dog();
[Link]();
}
}
Output: dog is eating...
Example of instanceof operator
Java Programming
[Link]
class Animal{}
class Dog extends Animal{//Dog inherits Animal
public static void main(String args[]){
Dog d=new Dog();
[Link](d instanceof Animal);//true
}
}
Output: true
instanceof operator with a variable that have null value
class Dog{
public static void main(String args[]){
Dog d=null;
[Link](d instanceof Dog);//false
}
}
Output: false
Possibility of downcasting with instanceof operator
class Animal { }
class Dog extends Animal {
static void method(Animal a) {
if(a instanceof Dog){
Dog d=(Dog)a;//downcasting
[Link]("ok downcasting performed");
}
}
public static void main (String [] args) {
Animal a=new Dog();
[Link](a);
}
}
Output: ok downcasting performed
Downcasting without the use of instanceof operator
class Animal { }
class Dog extends Animal {
Java Programming
[Link]
static void method(Animal a) {
Dog d=(Dog)a;//downcasting
[Link]("ok downcasting performed");
}
public static void main (String [] args) {
Animal a=new Dog();
[Link](a);
}
}
Output: ok downcasting performed
Understanding Real use of instanceof operator
interface Printable{}
class A implements Printable{
public void a(){[Link]("a method");}
}
class B implements Printable{
public void b(){[Link]("b method");}
}
class Call{
void invoke(Printable p){//upcasting
if(p instanceof A){
A a=(A)p;//Downcasting
a.a();
}
if(p instanceof B){
B b=(B)p;//Downcasting
b.b();
}
}
}//end of Call class
class Test{
public static void main(String args[]){
Printable p=new B();
Call c=new Call();
[Link](p);
}
}
Output: b method
Example of abstract class that have abstract method
Java Programming
[Link]
abstract class Bike{
abstract void run();
}
class Honda extends Bike{
void run(){[Link]("running safely..");}
public static void main(String args[]){
Bike obj = new Honda();
[Link]();
}
}
Output: running safely..
Understanding the real scenario of abstract class
abstract class Shape{
abstract void draw();
}
class Rectangle extends Shape{
void draw(){[Link]("drawing rectangle");}
}
class Circle extends Shape{
void draw(){[Link]("drawing circle");}
}
class Test{
public static void main(String args[]){
Shape s=new Circle();
//In real scenario, Object is provided through factory method
[Link]();
}
}
Output: drawing circle
Abstract class having constructor, data member, methods etc.
//example of abstract class that have method body
abstract class Bike{
abstract void run();
void changeGear(){[Link]("gear changed");}
}
class Honda extends Bike{
Java Programming
[Link]
void run(){[Link]("running safely..");}
public static void main(String args[]){
Bike obj = new Honda();
[Link]();
[Link]();
}
}
Output: running safely..
gear changed
//example of abstract class having constructor, field and method
abstract class Bike
{
int limit=30;
Bike(){[Link]("constructor is invoked");}
void getDetails(){[Link]("it has two wheels");}
abstract void run();
}
class Honda extends Bike{
void run(){[Link]("running safely..");}
public static void main(String args[]){
Bike obj = new Honda();
[Link]();
[Link]();
[Link]([Link]);
}
}
Output: constructor is invoked
running safely..
it has two wheels
30
Another real scenario of abstract class
interface A{
void a();
void b();
void c();
void d();
Java Programming
[Link]
}
abstract class B implements A{
public void c(){[Link]("I am C");}
}
class M extends B{
public void a(){[Link]("I am a");}
public void b(){[Link]("I am b");}
public void d(){[Link]("I am d");}
}
class Test{
public static void main(String args[]){
A a=new M();
a.a();
a.b();
a.c();
a.d();
}}
Output: I am a
I am b
I am c
I am d
Simple example of Interface
interface printable{
void print();
}
class A implements printable{
public void print(){[Link]("Hello");}
public static void main(String args[]){
A obj = new A();
[Link]();
}
}
Output: Hello
Multiple inheritance in Java by interface
interface Printable{
void print();
Java Programming
[Link]
}
interface Showable{
void show();
}
class A implements Printable,Showable{
public void print(){[Link]("Hello");}
public void show(){[Link]("Welcome");}
public static void main(String args[]){
A obj = new A();
[Link]();
[Link]();
}
}
Output: Hello
Welcome
Simple example of private access modifier
class A{
private int data=40;
private void msg(){[Link]("Hello java");}
}
public class Simple{
public static void main(String args[]){
A obj=new A();
[Link]([Link]);//Compile Time Error
[Link]();//Compile Time Error
}
}
Role of Private Constructor
class A{
private A(){}//private constructor
void msg(){[Link]("Hello java");}
}
public class Simple{
public static void main(String args[]){
Java Programming
[Link]
A obj=new A();//Compile Time Error
}
}
Example of default access modifier
//save by [Link]
package pack;
class A{
void msg(){[Link]("Hello");}
}
//save by [Link]
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();//Compile Time Error
[Link]();//Compile Time Error
}
}
Example of protected access modifier
//save by [Link]
package pack;
public class A{
protected void msg(){[Link]("Hello");}
}
//save by [Link]
package mypack;
import pack.*;
class B extends A{
public static void main(String args[]){
B obj = new B();
[Link]();
}
}
Output: Hello
Java Programming
[Link]
Example of public access modifier
//save by [Link]
package pack;
public class A{
public void msg(){[Link]("Hello");}
}
//save by [Link]
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
[Link]();
}
}
Output: Hello
Applying access modifier with method overriding
class A{
protected void msg(){[Link]("Hello java");}
}
public class Simple extends A{
void msg(){[Link]("Hello java");}//[Link]
public static void main(String args[]){
Simple obj=new Simple();
[Link]();
}
}
Simple example of encapsulation in java
//save as [Link]
package [Link];
public class Student{
private String name;
public String getName(){
return name;
}
Java Programming
[Link]
public void setName(String name){
[Link]=name
}
}
//save as [Link]
package [Link];
class Test{
public static void main(String[] args){
Student s=new Student();
[Link]("vijay");
[Link]([Link]());
}
}
Compile By: javac -d . [Link]
Run By: java [Link]
Output: vijay
Example of single dimensional java array
class B{
public static void main(String args[]){
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//printing array
for(int i=0;i<[Link];i++)//length is the property of array
[Link](a[i]);
}}
Output: 10
20
70
40
50
Declaration, Instantiation and Initialization of Java Array
class B{
public static void main(String args[]){
Java Programming
[Link]
int a[]={33,3,4,5};//declaration, instantiation and initialization
//printing array
for(int i=0;i<[Link];i++)//length is the property of array
[Link](a[i]);
}}
Output:33
3
4
5
Passing Java Array in the method
class B{
static void min(int arr[]){
int min=arr[0];
for(int i=1;i<[Link];i++)
if(min>arr[i])
min=arr[i];
[Link](min);
}
public static void main(String args[]){
int a[]={33,3,4,5};
min(a);//passing array in the method
}}
Output: 3
Example of Multidimensional java array
class B{
public static void main(String args[]){
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
[Link](arr[i][j]+" ");
Java Programming
[Link]
}
[Link]();
}
}}
Output: 1 2 3
2 4 5
4 4 5
Addition 2 matrices
class AE{
public static void main(String args[]){
//creating two matrices
int a[][]={{1,3,4},{3,4,5}};
int b[][]={{1,3,4},{3,4,5}};
//creating another matrix to store the sum of two matrices
int c[][]=new int[2][3];
//adding and printing addition of 2 matrices
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
c[i][j]=a[i][j]+b[i][j];
[Link](c[i][j]+" ");
}
[Link]();//new line
}
}}
Output: 2 6 8
6 8 10
Example of call by value in java
class Operation{
int data=50;
void change(int data){
data=data+100;//changes will be in the local variable only
}
public static void main(String args[]){
Operation op=new Operation();
Java Programming
[Link]
[Link]("before change "+[Link]);
[Link](500);
[Link]("after change "+[Link]);
}
}
Output: before change 50
after change 50
Example of call by value in java
class Operation2{
int data=50;
void change(Operation2 op){
[Link]=[Link]+100;//changes will be in the instance variable
}
public static void main(String args[]){
Operation2 op=new Operation2();
[Link]("before change "+[Link]);
[Link](op);//passing object
[Link]("after change "+[Link]);
}
}
Output: before change 50
after change 150
String comparison in Java
By equals() method
class Simple{
public static void main(String args[]){
String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
Java Programming
[Link]
String s4="Saurav";
[Link]([Link](s2));//true
[Link]([Link](s3));//true
[Link]([Link](s4));//false
}
}
Output: true
true
false
//Example of equalsIgnoreCase(String) method
class Simple{
public static void main(String args[]){
String s1="Sachin";
String s2="SACHIN";
[Link]([Link](s2));//false
[Link]([Link](s3));//true
}
}
Output: false
true
By == operator
//<b><i>Example of == operator</i></b>
class Simple{
public static void main(String args[]){
String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
[Link](s1==s2);//true (because both refer to same instance)
[Link](s1==s3);//false(because s3 refers to instance created in nonpool)
}
}
Output: true
false
Java Programming
[Link]
By compareTo() method
s1 == s2 :0
s1 > s2 :positive value
s1 < s2 :negative value
//<b><i>Example of compareTo() method:</i></b>
class Simple{
public static void main(String args[]){
String s1="Sachin";
String s2="Sachin";
String s3="Ratan";
[Link]([Link](s2));//0
[Link]([Link](s3));//1(because s1>s3)
[Link]([Link](s1));//-1(because s3 < s1 )
}
}
Output: 0
1
-1
Understanding problem without toString() method
class Student{
int rollno;
String name;
String city;
Student(int rollno, String name, String city){
[Link]=rollno;
[Link]=name;
[Link]=city;
}
public static void main(String args[]){
Student s1=new Student(101,"Raj","lucknow");
Student s2=new Student(102,"Vijay","ghaziabad");
[Link](s1);//compiler writes here [Link]()
[Link](s2);//compiler writes here [Link]()
}
}
Java Programming
[Link]
Output: Student@1fee6fc
Student@1eed786
Example of toString() method
class Student{
int rollno;
String name;
String city;
Student(int rollno, String name, String city){
[Link]=rollno;
[Link]=name;
[Link]=city;
}
public String toString(){//overriding the toString() method
return rollno+" "+name+" "+city;
}
public static void main(String args[]){
Student s1=new Student(101,"Raj","lucknow");
Student s2=new Student(102,"Vijay","ghaziabad");
[Link](s1);//compiler writes here [Link]()
[Link](s2);//compiler writes here [Link]()
}
}
Output: 101 Raj lucknow
102 Vijay ghaziabad
Java Programming
[Link]