Java Examples
OOPS |
Exception |
RMI |
String |
Thread |
io |
Util |
Net |
AWT |
JDBC |
|
|
| 1.Command Line Arguments program |
class ComLineArg {
public static void main(String args[]) {
int count, i=0;
String string;
count = args.length;
System.out.println("Number of argument : " + count);
while(i<count) {
string = args[i];
i = i + 1;
System.out.println(i+ " Argument is : " + string);
}
}
}
|
| 2. Stack Program |
class Stk {
int stck[] = new int[10];
int tos;
Stk() {
tos = -1;
}
void push(int item) {
if(tos==9)
System.out.println("Stack is full : ");
else
stck[++tos] = item;
}
int pop() {
if (tos < 0) {
System.out.println("Stack underflow ");
return 0;
}
else
return stck[tos--];
}
}
public class Stack {
public static void main (String args[]) {
Stk mystack1 = new Stk();
Stk mystack2 = new Stk();
for(int i=0; i<10; i++) mystack1.push(i);
for(int i=10; i<20; i++) mystack2.push(i);
System.out.println("Stack in Mystack1: ");
for(int i=0; i<10; i++)
System.out.println(mystack1.pop());
System.out.println("Stack in Mystack2 : ");
for(int i=0; i<10; i++)
System.out.println(mystack2.pop());
}
}
|
| 3.Inheritance Program |
//Simple Example of Inheritance.
//create a superclass.
class A{
int i,j;
void showij() {
System.out.println("i and j:"+i+ " "+j);
}
}
class B extends A {
int k;
void showk() {
System.out.println("k:"+k);
}
int sum() {
return (i+j+k);
}
}
class Inheritance {
public static void main(String args[]){
A superob=new A();
B subob=new B();
superob.i=10;
superob.j=20;
System.out.println("Contents of superob: ");
superob.showij();
System.out.println();
subob.i=7;
subob.j=8;
subob.k=9;
System.out.println("Contents of subob: ");
subob.showij();
subob.showk();
System.out.println("Sum of i,j,k is: "+subob.sum());
}
}
|
| 4.Overload program |
// In Polymorphism one of the ways that Java implements Polymorphism.
import java.lang.*;
class Overloaddemo {
void test(){
System.out.println("No parameter Method Called : ");
}
//overload test for one integer parameter.
void test(int a)
{
System.out.println("One parameter Method Called :"+a);
}
//overload test for two integer parameters.
void test(int a,int b)
{
System.out.println("Two parameter Method Called :"+a+" "+b);
}
//overload test for a double parameters.
double test(double a)
{
System.out.println("Double parameter Method Called :"+a);
return a*a;
}
}
public class OverLoadex
{
public static void main(String args[])
{
Overloaddemo ob=new Overloaddemo();
double result;
//call all versions of test()
ob.test();
ob.test(10);
ob.test(10,20);
result = ob.test(123.25);
System.out.println("Result of ob.test(123.25): "+result);
}
}
|
| 5.Override program |
// In Polymorphism one of the ways that Java implements Polymorphism.
import java.lang.*;
class OverRide1 {
void test(int a,int b) {
System.out.println("PARENT CLASS METHOD CALLED : "+a+" "+b);
}
}
public class OverRide extends OverRide1 {
void test(int a,int b) {
System.out.println("CHILD CLASS METHOD CALLED :"+a+" "+b);
}
void callsuper(int a,int b) {
System.out.println("USING KEYWORD SUPER PARENT CLASS METHOD CALLED :");
super.test(a,b);
}
public static void main(String args[]) {
OverRide ob = new OverRide();
ob.test(10,20);
// TO CALL PARENT METHOD USE SUPER KEYWORD :
ob.callsuper(20,30);
}
}
|
|
Back to Top |