Monday, January 13, 2014

Java JDBC programming Example 15

//Note: create  a DSN in the name of sql that is pointing the table name limbs in the database with three String column  names things,legs,arms





//rs is not scrollable
import java.sql.*;
class Jdbc15{
public static void main(String...arg) throws Exception{
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         Connection c =  DriverManager.getConnection("jdbc:odbc:sql");
         Statement  st = c.createStatement();
         ResultSet r = st.executeQuery("select * from limbs");
 ResultSetMetaData rsmd = r.getMetaData();
        int noofcol=rsmd.getColumnCount();
        try{  
 
while(r.next()){
        for(int i =1;i<=noofcol-1;i++){

System.out.print(r.getString(i)+"    ");
}
                        
              System.out.println();
   
        }
        
         }catch(Exception e){System.out.println("Error :");e.printStackTrace();}
      r.close();
      st.close();
      c.close();
}

}

Java JDBC programming Example 14

//Note: create  a DSN in the name of sql that is pointing the table name limbs in the database with three String column  names things,legs,arms







import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class Jdbc14 implements ActionListener{

JFrame fr1;
JLabel l1,l2,l3;
JTextField tf1,tf2,tf3;
JButton b1,b2;
Jdbc14() {
l1 = new JLabel("Enter Your thing ");
l2 = new JLabel("Enter no of legs ");
l3 = new JLabel("Enter no of arms ");

tf1=new JTextField(10);
tf2=new JTextField(10);
tf3=new JTextField(10);

b1 = new JButton("Submit");
b2 = new JButton("Cancel");
fr1=new JFrame("Insert");
}

public void display() {
fr1.setLayout(new FlowLayout(FlowLayout.LEFT));

fr1.add(l1);
fr1.add(tf1);

fr1.add(l2);
fr1.add(tf2);

fr1.add(l3);
fr1.add(tf3);

fr1.add(b1);
fr1.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);


fr1.setSize(400,500);
fr1.setVisible(true);
fr1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void actionPerformed(ActionEvent e){
String s = e.getActionCommand();
if(s.equals("Submit")){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         Connection c =  DriverManager.getConnection("jdbc:odbc:sql");
         PreparedStatement pstmt = c.prepareStatement
                                                ("insert into limbs (things,legs,arms) values (?,?,?)");
         pstmt.setString(1,tf1.getText()); //legs 8
         pstmt.setInt(2,Integer.parseInt(tf2.getText()));
         pstmt.setInt(3,Integer.parseInt(tf3.getText()));
         int i = pstmt.executeUpdate();
         pstmt.close();
         c.close();
}catch(Exception ee){}

}

if(s.equals("Cancel")){
tf1.setText("");
tf2.setText("");
tf3.setText("");
}

Java JDBC programming Example 13

//Note: create  a DSN in the name of sql that is pointing the table name limbs in the database with three String column  names things,legs,arms





import java.sql.*;


public class Jdbc13{
     public static void main(String s[]) throws Exception
     {
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        
Connection c =  DriverManager.getConnection("jdbc:odbc:sql");

Statement stmt = c.createStatement();

ResultSet rs = stmt.executeQuery("SELECT * FROM limbs");
     
ResultSetMetaData rsmd = rs.getMetaData();

         


        
System.out.println("No of columns :"+rsmd.getColumnCount());
        
System.out.println("is auto incrementing the columns ? :"+rsmd.isAutoIncrement(1));

System.out.println("is a column is case sensitive  ? :"+rsmd.isCaseSensitive(1));

System.out.println("Column label is   :"+rsmd.getColumnLabel(1));

System.out.println("Column name is   :"+rsmd.getColumnName(1));

System.out.println("Table name is     :"+rsmd.getTableName(1));

System.out.println("Column java type name :"+rsmd.getColumnType(1));

System.out.println("Column database type name :"+rsmd.getColumnTypeName(1));

c.close();
         

         }

     
}

Java JDBC programming Example 12

//Note: create  a DSN in the name of sql that is pointing the table name limbs in the database with three String column  names things,legs,arms



import java.sql.*;
public class Jdbc12{
     public static void main(String s[]) throws Exception
     {
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         Connection c =  DriverManager.getConnection("jdbc:odbc:sql");
        DatabaseMetaData dbm =c.getMetaData();
        
System.out.println("product name :"+dbm.getDatabaseProductName());
        
System.out.println("Product Version :"+dbm.getDatabaseProductVersion());
      
System.out.println("driver name :"+dbm.getDriverName());
       
System.out.println("driver version :"+dbm.getDriverVersion());

        System.out.println("URL :"+dbm.getURL());
System.out.println("User :"+dbm.getUserName());

System.out.println("driver Major version :"+dbm.getDriverMajorVersion());

System.out.println("driver Minor  version :"+dbm.getDriverMinorVersion());
System.out.println("\nDatabase String fns are  :"+dbm.getStringFunctions());
System.out.println("\nDatabase Numeric  fns are:"+dbm.getNumericFunctions());

System.out.println("\nDatabase System fns are:"+dbm.getSystemFunctions());
        
System.out.println("\nDatabase Date Time  fns are:"+dbm.getTimeDateFunctions());
c.close();
         

         }

     
}

Java JDBC programming Example 11

//Note: create  a DSN in the name of sql that is pointing the table name limbs in the database with three String column  names things,legs,arms


//PreparedAtatement  insert sql
import java.sql.*;
class Jdbc11{
public static void main(String...arg) throws Exception{
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         Connection c =  DriverManager.getConnection("jdbc:odbc:sql");
         PreparedStatement pstmt = c.prepareStatement
                                                  ("insert into limbs (things,legs,arms) values (?,?,?)");
         pstmt.setString(1,"dinosares"); 
         pstmt.setInt(2,10);
         pstmt.setInt(3,20);

        int i = pstmt.executeUpdate();
        System.out.println("No of records updated    :"+i);
       
     
      pstmt.close();
      c.close();
}}

ava JDBC programming Example 10

//Note: create  a DSN in the name of sql that is pointing the table name limbs in the database with three String column  names things,legs,arms




//PreparedStatement  update sql
import java.sql.*;
class Jdbc10{
public static void main(String...arg) throws Exception{
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         Connection c =  DriverManager.getConnection("jdbc:odbc:sql");
         PreparedStatement pstmt = c.prepareStatement
                                  ("update limbs set legs =? where arms =?");
         pstmt.setInt(1,5); //legs 8
         pstmt.setInt(2,0);
        int i = pstmt.executeUpdate();
        System.out.println("No of records updated    :"+i);
       
     
      pstmt.close();
      c.close();
}
}

Java JDBC programming Example 9

//Note: create  a DSN in the name of sql that is pointing the table name limbs in the database with three String column  names things,legs,arms



//Deleting a row

import java.sql.*;
import java.net.*;
class Jdbc9{
public static void main(String...arg) throws Exception{
System.out.println(InetAddress.getLocalHost());
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         Connection c =  DriverManager.getConnection("jdbc:odbc:sql");
         Statement  st = c.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
  ResultSet.CONCUR_UPDATABLE);
         ResultSet r = st.executeQuery("select * from limbs");
       r.next();
       r.next();
       r.deleteRow();
       System.out.println(" Row deleted :");
      r.close();
      st.close();
      c.close();
}
}

Java JDBC programming Example 8

//Note: create  a DSN in the name of sql that is pointing the table name limbs in the database with three String column  names things,legs,arms





//UPDATING ROWS NOT WORKING
//scrollable,sensitive concurent updatable record set
//records will be printed in reverse order
//updaing no of arms that means 3ed column
import java.sql.*;
import java.net.*;
class Jdbc8{
public static void main(String...arg) throws Exception{
System.out.println(InetAddress.getLocalHost());
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         Connection c =  DriverManager.getConnection("jdbc:odbc:sql");
         Statement  st = c.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
  ResultSet.CONCUR_UPDATABLE);
         ResultSet r = st.executeQuery("select * from limbs");
        try{  
r.afterLast();  //working only with scrollable rs
while(r.previous()){
       
              System.out.print(r.getString(1)+" ");
              System.out.print(r.getString(2)+" ");
                  int arms = r.getInt(3);
                                     arms = arms+5;
r.updateInt(3,arms);
              System.out.println(r.getInt(3));
  }
        
         }catch(Exception e){System.out.println("Hello");e.printStackTrace();}
      r.close();
      st.close();
      c.close();
}
}

Java JDBC programming Example 7

//Note: create  a DSN in the name of sql that is pointing the table name limbs in the database with three String column  names things,legs,arms//Avoid Hacking through PreparedStatement 



//PreparedStatement 
import java.sql.*;
class Jdbc7{
public static void main(String...arg) throws Exception{
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         Connection c =  DriverManager.getConnection("jdbc:odbc:sql");
         PreparedStatement pstmt = c.prepareStatement("select * from limbs where arms >?");
         pstmt.setInt(1,0); 
         ResultSet r = pstmt.executeQuery();
         try{  
 
while(r.next()){
       
              System.out.println(r.getString("things"));
              System.out.println(r.getInt("legs"));
              System.out.println(r.getInt("arms"));
   
        }
        
         }catch(Exception e){System.out.println("Error :");e.printStackTrace();}
      r.close();
      pstmt.close();
      c.close();
}
}

Java JDBC programming Example 6

/Note: create  a DSN in the name of sql that is pointing the table name limbs in the database with three String column  names things,legs,arms



//rs is not scrollable RETRIVEING COLUMN VALUES using column names
import java.sql.*;
class Jdbc6{
public static void main(String...arg) throws Exception{
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         Connection c =  DriverManager.getConnection("jdbc:odbc:sql");
         Statement  st = c.createStatement();
         ResultSet r = st.executeQuery("select * from limbs");
        try{

while(r.next()){
     
              System.out.println(r.getString("things"));
              System.out.println(r.getInt("legs"));
              System.out.println(r.getInt("arms"));
 
        }
       
         }catch(Exception e){System.out.println("Error :");e.printStackTrace();}
      r.close();
      st.close();
      c.close();
}
}

Java JDBC programming Example 5

//Note: create  a DSN in the name of sql that is pointing the table name limbs in the database with three String fields








//rs is not scrollable RETRIVEING COLUMN VALUES using column numbers
import java.sql.*;
class Jdbc5{
public static void main(String...arg) throws Exception{
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         Connection c =  DriverManager.getConnection("jdbc:odbc:sql");
         Statement  st = c.createStatement();
         ResultSet r = st.executeQuery("select * from limbs");
        try{  
 
while(r.next()){
       
              System.out.println(r.getString(1));
              System.out.println(r.getInt(2));
              System.out.println(r.getInt(3));
   
        }
        
         }catch(Exception e){System.out.println("Error :");e.printStackTrace();}
      r.close();
      st.close();
      c.close();
}
}

Java JDBC programming Example 4

//Note: create  a DSN in the name of sql that is pointing to the table name limbs in the database with three String fields





//FINDING  NO OF RECORDS
import java.sql.*;
import java.net.*;
class Jdbc4{
public static void main(String...arg) throws Exception{
System.out.println(InetAddress.getLocalHost());
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         Connection c =  DriverManager.getConnection("jdbc:odbc:sql");
         Statement  st = c.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
  ResultSet.CONCUR_UPDATABLE);
         ResultSet r = st.executeQuery("select * from limbs");
// finding no of  records   one way
          r.last();
          int norec = r.getRow();
        System.out.println("No of records  "+norec);
       
// finding no of  records   another  way
r = st.executeQuery("select count(*) FROM limbs");
r.next();
int count = r.getInt(1);
      System.out.println("No of records  "+count);
      r.close();
      st.close();
      c.close();
}
}


Java JDBC programming Example 3

//Note: create  a DSN in the name of sql that is pointing to the table name limbs in the database with three String fields





//rs scrollable
import java.sql.*;
import java.net.*;
class Jdbc3{
public static void main(String...arg) throws Exception{
System.out.println(InetAddress.getLocalHost());
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         Connection c =  DriverManager.getConnection("jdbc:odbc:sql");
         Statement  st = c.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
  ResultSet.CONCUR_UPDATABLE);
         ResultSet r = st.executeQuery("select * from limbs");
        try{
r.beforeFirst();//work only with scrollable rs
while(r.next()){
     
              System.out.print(r.getString(1)+" ");
              System.out.print(r.getString(2)+" ");
              System.out.println(r.getString(3));
  }
       
         }catch(Exception e){System.out.println("Error :");e.printStackTrace();}
      r.close();
      st.close();
      c.close();
}
}

Java JDBC programming Example 2

//Note: create  a DSN in the name of sql that is pointing the table name limbs in the database with three String fields






//rs is not scrollable
import java.sql.*;
class Jdbc2{
public static void main(String...arg) throws Exception{
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         Connection c =  DriverManager.getConnection("jdbc:odbc:sql");
         Statement  st = c.createStatement();
         ResultSet r = st.executeQuery("select * from limbs");
        try{  
 
while(r.next()){
                System.out.print(r.getString(1)+" ");
              System.out.print(r.getString(2)+" ");
              System.out.println(r.getString(3));
   
        }
        
         }catch(Exception e){System.out.println("Error :");e.printStackTrace();}
      r.close();
      st.close();
      c.close();
}
}

Java JDBC programming Example 1

//Note: create  a DSN in the name of sql that is pointing the table name limbs in the database with three String fields



//scrollable,sensitive concurent updatable record set
//records will be printed in reverse order
import java.sql.*;
import java.net.*;
class Jdbc1{
public static void main(String...arg) throws Exception{
System.out.println(InetAddress.getLocalHost());
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c =  DriverManager.getConnection("jdbc:odbc:sql");
Statement  st =  c.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
  ResultSet.CONCUR_UPDATABLE);
 ResultSet r = st.executeQuery("select * from limbs");
 try{
r.afterLast();  //working only with scrollable rs
while(r.previous()){
                System.out.print(r.getString(1)+" ");
             System.out.print(r.getString(2)+" ");
              System.out.println(r.getString(3));
  }
     
 }catch(Exception e)                          {System.out.println("Hello");e.printStackTrace();}
      r.close();
      st.close();
      c.close();
}
}

Java Object Serialization [Writing and Reading Objects] Programming Example

//Note: First Execute Demo1 for writing objects and then Demo2 for reading objects


//[1]
//Object Serialization in Java: Writing objects in to a file
import java.io.*;
class A implements Serializable{
int i=75;
}

class Demo1{//writing through byte stream Object Serialization in java
public static void main(String arg[]) throws IOException{
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("one.txt"));
A a=new A();
oos.writeObject(a);
oos.writeObject(new String("sun"));
oos.writeObject(new StringBuffer("micro"));
oos.writeObject(new StringBuilder("system"));
oos.close();
}
}



//[2]
//Object Serialization in Java: Reading objects from the file
import java.io.*;
class A implements Serializable{
int i=75;
}
class Demo2{//reading from  byte stream Object Serialization in java
public static void main(String arg[]) throws Exception{
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("one.txt"));
A a=(A)ois.readObject();
String s=(String)ois.readObject();
StringBuffer s1=(StringBuffer)ois.readObject();
StringBuilder s2=(StringBuilder)ois.readObject();
ois.close();
System.out.println(a.i);
System.out.println(s);
System.out.println(s1);
System.out.println(s2);

}
}

Java util package programming Examples

//Note:  Analysis the programs errors,outputs and Understand util package classes   in Java 


//[1]
import java.util.*;
class Demo1{
public static void main(String arg[]){
HashSet hs=new HashSet();
hs.add(new String("amar"));
hs.add(new Integer(75));
hs.add(new Float(75.8f));
hs.add(new Double(78.8));
System.out.println(hs);
}
}



//[2]
import java.util.*;
class Demo2{//ArrayList
public static void main(String arg[]){
ArrayList hs=new ArrayList();
hs.add(new String("amar"));
hs.add(new Integer(75));
hs.add(new Float(75.8f));
hs.add(new Double(78.8));
hs.add(new String("amar"));
hs.add(new StringBuilder("hai"));
System.out.println(hs);
}
}



//[3]
import java.util.*;
class Demo3{//ArrayList
public static void main(String arg[]){
ArrayList<String> hs=new ArrayList<String>();
hs.add(new String("amar"));
hs.add(new String("75"));
hs.add(new String("75.8f"));
hs.add(new String("78.8"));
hs.add(new String("amar"));
System.out.println(hs);
hs.add(3,"sun");
System.out.println("The value of the fourth index is"+hs.get(4));
hs.set(4,"micro");
System.out.println(hs.indexOf("sun"));
}
}



//[4]
import java.util.*;
class Demo4{//Generic Hashset
public static void main(String arg[]){
HashSet<Integer> hs=new HashSet<Integer>();
hs.add(new Integer(16));
hs.add(new Integer(75));
hs.add(new Integer(75));
hs.add(new Integer(78));
System.out.println(hs);
}
}



//[5]
import java.util.*;
class Demo77{//Iterating the elements
public static void main(String arg[]){
HashSet<Integer> hs=new HashSet<Integer>();
hs.add(new Integer(16));
hs.add(new Integer(75));
hs.add(new Integer(75));
hs.add(new Integer(78));
int sum=0;
for(Integer i:hs)
{
System.out.println(i);
sum=sum+i;
}
System.out.println(sum);
}
}



//[6]
import java.util.*;
class Demo6{//Iterating the elements
public static void main(String arg[]){
HashSet<Integer> hs=new HashSet<Integer>();
hs.add(new Integer(16));
hs.add(new Integer(75));
hs.add(new Integer(75));
hs.add(new Integer(78));
int sum=0;
Iterator i=hs.iterator();
Integer m;
while(i.hasNext()){
m=(Integer)i.next();
System.out.println(m);
sum=sum+m;
}
System.out.println(sum);
}
}




//[7]
import java.util.*;
class Demo7{//Hashset 
public static void main(String arg[]){
HashSet<Integer> hs=new HashSet<Integer>();
hs.add(new Integer(16));
hs.add(new Integer(75));
hs.add(new Integer(75));
hs.add(new Integer(78));
hs.add(new Integer(99));
boolean b=hs.contains(new Integer(99));
System.out.println("That element is available"+b);
b=hs.remove(new Integer(99));
System.out.println("That element is removed"+b);
System.out.println("The size of the hash set is"+hs.size());
}
}



//[8]
import java.util.*;
class Demo80{//HashMap
public static void main(String arg[]){
HashMap hm=new HashMap();
hm.put(new Integer(5),"sun");
hm.put(new Integer(6),"micro");
System.out.println(hm.get(new Integer(6)));
}
}

Java Inheritance Example Programs

//Note:  Analysis the programs errors,outputs and Understand Inheritance   in Java 


//[1]
class A{
public int i;
public void seti(int ii){
i=ii;
}
public void printi(){
System.out.println(i);
}
}
class B extends A{
public int j;
public void setj(int jj){
j=jj;
}
public void printj(){
System.out.println(j);
}
}
class Demo1{
public static void main(String arg[]){
B obj=new B();
obj.seti(5);
obj.setj(55);
obj.printi();
obj.printj();
}
}



//[2]
class A{
public int i;
public void seti(int ii){
i=ii;
}
public void printi(){
System.out.println("superclass i="+i);
}
}
class B extends A{
public int i;
public void seti(int ii){
i=ii;
super.seti(75);
}
public void printi(){
System.out.println("subclass i="+i);
super.printi();
}
}
class Demo2{
public static void main(String arg[]){
B obj=new B();
obj.seti(5);
obj.printi();
}
}



//[3]

class A{
public void print(){
System.out.println("i am from class A");
}
}
class B extends A{
public void print(){
System.out.println("i am from class B");
}
}

class Demo3{
public static void main(String arg[]){
A ob;
B ob1=new B();
ob=ob1;
ob.print();//B class print method
A ob2=new A();
ob=ob2;
ob2.print();//A class print method
}
}




//[4]

class Shape{
public void print(){
System.out.println("General class");
}
}

class Tri extends Shape{
public void print(){
System.out.println("Area of Traingle");
}
}
class Square extends Shape{
public void print(){
System.out.println("Area of the Square");
  }
  }
class Demo59{
public static void main(String arg[]){
Shape s;
Shape s1=new Shape();
Tri t1=new Tri();
Square sq=new Square();
s=s1;
s.print();//class shape print
s=t1;
s.print();//class Tri print
s=sq;
s.print();//class square print
}
}





Java for programming Example

//Note:  Analysis the programs errors,outputs and Understand for  in Java 



//[1]


class Fordemo71
{
public static void main(String arg[])
{
String a[]={"one","amar","siva"};
for(String i:a){
System.out.println(i);
}
}
}

//[2]

class Fordemo70
{
public static void main(String arg[])
{
int a[]={1,2,3,4,5};
int sum=0;
for(int i:a){
System.out.println(i);
sum=sum+i;
}
System.out.println(sum);
}

Java Generic Programming Example

//Note:  Analysis the programs errors,outputs and Understand Generic in Java 



//[1]

class Student<T>{//Generic class
private T name;
public void setName(T n){
name=n;
}
public T getName(){
return name;
}
}

class GenericDemo{
public static void main(String arg[]){
Student<String> s=new Student<String>();
s.setName("sun");
System.out.println(s.getName());
Student<StringBuffer> s1=new Student<StringBuffer>();
s.setName("sun1");
System.out.println(s.getName());


}
}

Passing objects to function : Call by Ref or Call by Value in Java ?

//Note:  Analysis the programs errors,outputs and Understand Passing objects to function : Call by Ref or Call by Value in Java ?


//[1]
//clall by value or call by reference ?
class RefDemo57{

public static void foo(String sb1){

sb1=sb1.concat(" world");
System.out.println("sb1 ="+sb1);   


public static void main(String arg[]){
String sb = new String("Hello");
System.out.println("sb ="+sb);  
System.out.println("sb ="+sb);  
}
}

Java constructor Programming Example

//Note:  Analysis the programs errors,outputs and Understand the constructor concept in Java


//[1]

class A{
public int i;
A(){i=75;}
public void printi(){
System.out.println("superclass i="+i);
}
}
class B extends A{
public int j;
B(){
Super();
j=70;
}
}
public void printj(){
System.out.println("subclass j="+j);
}
}
class SupDemo57{
public static void main(String arg[]){
B obj=new B();
obj.printj();
obj.printi();
}
}








Java Packages Programming Example 1

//Note:  Analysis the programs errors,outputs and Understand the Package concept in Java



//[1]
//package1
package pack1;
public class Cust{
int balance;
void seti(int bb){
balance=bb;
}
void print(){
System.out.println("balance="+balance);
}
}


//package2
package pack2;
public class Cust extends pack1.Cust{
public String name;
public void setname(String name){
this.name=name;
}
public void print(){
System.out.println("name="+name);
System.out.println("balance="+balance);
}
}






//importing package1,package2
import pack1.*;
import pack2.*;
class Test59{
public static void main(String arg[]){
pack1.Cust c1=new pack1.Cust();
c1.seti(5);
c1.print();
pack2.Cust c2=new pack2.Cust();
c2.setname("amar");
c2.print();
}
}

Java interface Programming Example 1

//Note:  Analysis the programs errors,outputs and Understand the interface concept in Java


//[1]
//Banks interface
interface Banks extends Bankp{
void tell();
void print();
}


//Implementation class
class Bank implements Banks{
public void tell(){
System.out.println("show me security for loan");
}
public void print(){
System.out.println("my certificate is my security for loan");
}
public void property(){
System.out.println("my property is");
}
}
class Test1{
public static void main(String arg[]){
Bank b1=new Bank();
b1.tell();
b1.print();
b1.property();
}
}

Java Wrapper class Example programs

//Note:  Analysis the programs errors,outputs and Understand the Wrapper class in Java



//[1]
class WrapperDemo1{
public static void main(String arg[])
{
//Wrapper class objects will convert to primitive types
Integer i=new Integer(75);
int ii=i.intValue();
System.out.println(ii);
float f=i.floatValue();
System.out.println(f);
}
}


//[2]
class WrapperDemo2{
public static void main(String... arg)
{
Integer i=Integer.parseInt(arg[2]);
int ii=i.intValue();
System.out.println(ii);
}
}



//[3]
class WrapperDemo2{
public static void main(String arg[])
{
//Wrapper class objects will be convert to primitive types through parameter passing
int i=75;
demo(i);//passing primitive
}
public static void demo(Integer i)//received by wrapper
{
System.out.println(i);
}
}

Java "static inner class var arg" Programming Examples

//Note:  Analysis the programs errors,outputs and Understand the Java static inner class var arg concepts


//[1]
class Staticdemo1{
public static void main(String arg[]){
Staticdemo50.demo();
System.out.println("No of arguments"+arg.length);
for(int i=0;i<arg.length;i++){
System.out.println(arg[i]);
}
}
public static void demo(){
System.out.println("hello i am static method");
}
static{
System.out.println("This will be printed first");
}
}


//[2]

class Outer{
int i=25;
public  void display(){
Inner io=new Inner();
io.test();
System.out.println("Inner class object is created");
         }
class Inner{
public void test(){
System.out.println("i="+i);
}

}
}
class Test2{
public static void main(String arg[]){
Outer ou=new Outer();
ou.display();
}
}



//[3]

class Bank{
public static void demo(int...a){
System.out.println("you send"+a.length+"parameters");
for(int i=0;i<a.length;i++){
System.out.println(a[i]);
}
}
}
class Vararg3{
public static void main(String arg[]){
Bank.demo();
Bank.demo(5);
Bank.demo(5,6,7);
}
}