Friday, February 11, 2011

BatchUpdates

import java.sql.*;
class BatchUpdates
{
public static void main(String[] args) throws Exception
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
Statement stmt=con.createStatement();
String s1="insert into emp3 values(1,'rahul')";
String s2="insert into emp3 values(2,'arun')";
String s3="insert into emp3 values(3,'pavan')";
String s4="insert into emp3 values(4,'gopi')";
String s5="insert into emp3 values(2,'arun')";
String s6="insert into emp3 values(2,'arun')";
stmt.addBatch(s1);
stmt.addBatch(s2);
stmt.addBatch(s3);
stmt.addBatch(s4);
stmt.addBatch(s5);
stmt.addBatch(s6);
int a[]=stmt.executeBatch();
for(int i=0;i<=a.length;i++) { System.out.println("no of rows inserted:------->"+a[i]);
}
}
}

CallableStmt

import java.sql.*;
class CallableStmt
{
public static void main(String[] args) throws Exception
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
CallableStatement pstmt=con.prepareCall("{call proc(?,?)}");
pstmt.setInt(1,10);
pstmt.setString(2,"arun");
pstmt.execute();
System.out.println("insert record is with Prepare Statement......");
con.close();
System.out.println("connection is closed");
}
}

PrepareStmtInput

import java.io.*;
import java.sql.*;
class PrepareStmtInput
{
public static void main(String[] args) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
PreparedStatement pstmt=con.prepareStatement("insert into emp3 values(?,?)");
System.out.println("Enteer emp no");
int empno=Integer.parseInt(br.readLine());
pstmt.setInt(1,empno);
System.out.println("enter emp name");
String empname=br.readLine();
pstmt.setString(2,"empname");
int a=pstmt.executeUpdate();
System.out.println("insert record is with Prepare Statement......"+a);
}
}

PrepareStmt

import java.sql.*;
class PrepareStmt
{
public static void main(String[] args) throws Exception
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
PreparedStatement pstmt=con.prepareStatement("insert into emp3 values(?,?)");
pstmt.setInt(1,10);
pstmt.setString(2,"anjali");
int a=pstmt.executeUpdate();
System.out.println("insert record is with Prepare Statement......"+a);
}
}

Prepare_statement

// PrepareStatement retriving the values from table student in a database

import java.sql.*;
public class Prepare_statement

{
public static void main(String args[])
{
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
System.out.println("thin driver is registerd");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
System.out.println("Connection is established to DB");
PreparedStatement pstmt=con.prepareStatement("select *from student where sno=?");
System.out.println("preparedStatement object is created");
pstmt.setInt(1,15);
ResultSet rs=pstmt.executeQuery();
System.out.println("sno"+" "+"name"+" "+"place");
System.out.println("--------------------------------------");
while(rs.next())
{
System.out.println();
System.out.print(rs.getInt("sno")+" ");
System.out.print(rs.getString("name")+" ");
System.out.print(rs.getString("place"));
}
pstmt.executeQuery();
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Registeration faild");
}
}
}

/*
Explanation:

prepared statement is improves the performence

*/

Retrive_Records

//Retriving the values from table student in a database

import java.sql.*;
public class Retrive_Records

{
public static void main(String args[])
{
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
System.out.println("thin driver is registerd");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
System.out.println("Connection is established to DB");
Statement stmt=con.createStatement();
System.out.println("Statement object is created");
ResultSet rs=stmt.executeQuery("select *from student");
System.out.println("sno"+" "+"name"+" "+"place");
System.out.println("--------------------------------------");
while(rs.next())
{
System.out.println();
System.out.print(rs.getInt("sno")+" ");
System.out.print(rs.getString("name")+" ");
System.out.print(rs.getString("place"));
}
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Registeration faild");
}
}
}

/*
Explanation:

For Select Querys we have to use the "executeQuery" method
it return the Results into a ResultSet object
by using next() method we can move the next row from one row to anethor
here we used differnt type getter method(we can use getString() for all data types)
but its not recomended
TRy with different type select querys

*/

UpdateRecord

import java.sql.*;
class UpdateRecord
{
public static void main(String[] args) throws Exception
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
Statement stmt=con.createStatement();
int a=stmt.executeUpdate("update emp3 where name='rahul'");
System.out.println("no of rows are Updated:------->"+a);
}
}

Update_Records

//Updating values in table student

import java.sql.*;
public class Update_Records

{
public static void main(String args[])
{
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
System.out.println("thin driver is registerd");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
System.out.println("Connection is established to DB");
Statement stmt=con.createStatement();
System.out.println("Statement object is created");
int a=stmt.executeUpdate("update student set sno=15 where sno=1");
System.out.println("Values are updated -------> "+a);
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Registeration faild");
}
}
}

/*
Explanation:

Here we use a SQL update Query
and it returns an interger value it indicates that how many record are updated.

*/

Statement_obj

//Creating a Statement reference

import java.sql.*;
public class Statement_obj
{
public static void main(String args[])
{
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
System.out.println("thin driver is registerd");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
System.out.println("Connection is established to DB");
Statement stmt=con.createStatement();
System.out.println("Statement object is created");

}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Registeration faild");
}
}
}

/*
Explanation:

Here we are not creating a Statement object but
we are creating a reference variable to Statement interface
and we are creating a object which implements the Statement interface
(Most of the Techniclal Lazy people calls it as Statement object)

*/

Table_create

//Creating a Table using java programe

import java.sql.*;
public class Table_create
{
public static void main(String args[])
{
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
System.out.println("thin driver is registerd");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
System.out.println("Connection is established to DB");
Statement stmt=con.createStatement();
System.out.println("Statement object is created");
stmt.executeUpdate("create table student(sno number(5),name varchar2(10),place varchar2(10))");
System.out.println("Table is crated -------> ");
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Registeration faild");
}
}
}

/*
Explanation:

Here we are used a method "executeUpdate()" it takes one parameter
as argument i.e SQL Query

*/

DisplayRecordIndex

//Retrive the data from DB and display it Using with INDEXS
import java.sql.*;
import java.sql.*;
class DisplayRecordIndex
{
public static void main(String[] args) throws Exception
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select eno,name from emp3");
System.out.println(rs);
while(rs.next())
{
int deno=rs.getInt(1);
System.out.println("Eno: "+deno);
String s=rs.getString(2);
System.out.println("Nane: "+s);
}
con.close();
System.out.println("connection is closed");
}
}

DisplayRecord

import java.sql.*;
class DisplayRecord
{
public static void main(String[] args) throws Exception
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select eno,name from emp3");
System.out.println(rs);
while(rs.next())
{
int deno=rs.getInt("eno");
System.out.println("Eno: "+deno);
String s=rs.getString("name");
System.out.println("Nane: "+s);
}
}
}

DeleteRecord

import java.sql.*;
class DeleteRecord
{
public static void main(String[] args) throws Exception
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
Statement stmt=con.createStatement();
int a=stmt.executeUpdate("delete from emp3 where eno=1");
System.out.println("no of rows DeleteRecord:------->"+a);
con.close();
System.out.println("connection is closed");
}
}

Delete_Records

//Deleting values in table student

import java.sql.*;
public class Delete_Records

{
public static void main(String args[])
{
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
System.out.println("thin driver is registerd");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
System.out.println("Connection is established to DB");
Statement stmt=con.createStatement();
System.out.println("Statement object is created");
int a=stmt.executeUpdate("delete from student where sno=1");
System.out.println("Values are deleted -------> "+a);
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Registeration faild");
}
}
}

/*
Explanation:

Here we use a SQL update Query
and it returns an interger value it indicates that how many record are deleted.

*/

InsertRecord

import java.sql.*;
class InsertRecord
{
public static void main(String[] args) throws Exception
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
Statement stmt=con.createStatement();
int a=stmt.executeUpdate("insert into emp3 values(1,'rahul')");
System.out.println("no of rows inserted:------->"+a);
con.close();
System.out.println("connection is closed");
}
}

Insert_Records

//inserting values into a table student

import java.sql.*;
public class Insert_Records

{
public static void main(String args[])
{
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
System.out.println("thin driver is registerd");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
System.out.println("Connection is established to DB");
Statement stmt=con.createStatement();
System.out.println("Statement object is created");
int a=stmt.executeUpdate("insert into student values(1,'rahul','hyd')");
System.out.println("Values are inseted -------> "+a);
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Registeration faild");
}
}
}

/*
Explanation:

Here we use a SQL insert Query
and it returns an interger value it indicates that how many record are inseted.

*/

DBConnect

import java.sql.*;
class DBConnect
{
public static void main(String[] args) throws Exception
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
Statement stmt=con.createStatement();
stmt.executeUpdate("create table emp3(eno number(5),name varchar2(20))");
System.out.println("table is created......");
con.close();
System.out.println("connection is closed");
}
}

JDBC

//Register orclae or thin driver using jdbc api

import java.sql.*;
public class DBRegister
{
public static void main(String args[])
{
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
//DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
System.out.println("thin driver is registerd");
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Registeration faild");
}
}
}

/*
Explanation:

To execute this program we have to set the class path for "ojdbc14.jar" file
From the above programe "DriverManager" is a static class
"registerDriver" is method it takes one arugument i.e object of OracleDriver class
which is under "oracle.jdbc.driver" package

*/

Wednesday, February 9, 2011

Getting Connection from DataBase

//get connection from DataBase using orclae or thin driver using jdbc api

import java.sql.*;
public class DBConnection
{
public static void main(String args[])
{
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
System.out.println("thin driver is registerd");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
System.out.println("Connection is established to DB");

}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Registeration faild");
}
}
}

/*
Explanation:

To get Connetion we has used "Connetion" interface
and getConnection method takes three parameters

1:URL
2:user name
3:password

*/

Registering Drivers

//Register orclae or thin driver using jdbc api

import java.sql.*;
public class DBRegister
{
public static void main(String args[])
{
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
//DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
System.out.println("thin driver is registerd");
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Registeration faild");
}
}
}

/*
Explanation:

To execute this program we have to set the class path for "ojdbc14.jar" file
From the above programe "DriverManager" is a static class
"registerDriver" is method it takes one arugument i.e object of OracleDriver class
which is under "oracle.jdbc.driver" package

*/

Friday, February 4, 2011

read string

// input from key board
import java.io.*;
class Inputstring
{
public static void main(String r[])throws IOException
{
//InputStreamReader id=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter a String vslue");
String s=br.readLine();
System.out.println("enter "+s);
}
}

read integer

// input from key board
import java.io.*;
class Inputint
{
public static void main(String r[])throws IOException
{
InputStreamReader id=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(id);

System.out.println("enter a interger");

int s=Integer.parseInt(br.readLine());

System.out.println("enter "+s);
}
}

arthemetic operators using switch

//arthemetic operators using switch
import java.io.*;
class Input2
{
public static void main(String[] args)throws IOException
{
char ch;
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
System.out.println("enter values of x");
int x=Integer.parseInt(br.readLine());
System.out.println("enter values of y");
int y=Integer.parseInt(br.readLine());
System.out.println("enter u r choice");
System.out.println("+ for additon");
System.out.println("- for subtraction");
System.out.println("/ for divide");
System.out.println("* for multipiy");
System.out.println("% for remainder");
ch=(char)br.read();
switch(ch)
{
case '+':
System.out.println("additon:"+(x+y));
break;
case '-':
System.out.println("sub:"+(x-y));
break;
case '*':
System.out.println("mul:"+(x*y));
break;
case '/':
System.out.println("divide:"+(x/y));
break;
case '%':
System.out.println("Remainder:"+(x%y));
break;
}
}
}

input from key board

// input from key board
import java.io.*;
class Input1
{
public static void main(String r[])throws IOException
{
//InputStreamReader id=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter a double vslue");
double s=Double.parseDouble(br.readLine());
System.out.println("enter "+s);
}
}

InputStreamReader

// input from key board
import java.io.*;
class Input
{
public static void main(String r[])throws IOException
{
InputStreamReader id=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(id);

System.out.println("enter achar");

char ch=(char)br.read();
System.out.println("enter "+ch);
}
}

inheritance

//Inheritance using one class propertice in anethor class without rewritng

class Teacher
{
int id;
String name;
String add;
float sal;

void setid(int id)
{
this.id=id;
}

int getid()
{
return id;
}

void setname(String name)
{
this.name=name;
}

String getname()
{
return name;
}

void setadd(String add)
{
this.add=add;
}

String getadd()
{
return add;
}

void setsal(float sal)
{
this.sal=sal;
}

float getsal()
{
return sal;
}
}

class Student extends Teacher
{
int marks;

void setmarks(int marks)
{
this.marks=marks;
}

int getmarks()
{
return marks;
}
}

class Inheritance
{
public static void main(String args[])
{
Student s=new Student();

s.setid(10);
s.setname("srinu");
s.setadd("H.No:-44, amerpet");
s.setmarks(899);

System.out.println("Id:------"+s.getid());
System.out.println("Name:------"+s.getname());
System.out.println("Addres:------"+s.getadd());
System.out.println("Marks:------"+s.getmarks());
}
};

inner class

//on Inner class

import java.io.*;
class BankAct
{
private double bal;
BankAct(double b)
{
bal=b;
}

void start(double r)
{
Intrest in=new Intrest(r);
in.calintrest(); //calling Inner class method
}


private class Intrest
{
private double rate;

Intrest(double r)
{
rate=r;
}

void calintrest()
{
double intrest=bal*rate/100;
System.out.println("Intrest--"+intrest);
bal+=intrest;
System.out.println("Bal=----"+bal);
}
}
}

class Innerclass
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("enter balance");
int b=Integer.parseInt(br.readLine());

BankAct account=new BankAct(b);

System.out.println("enter intrest");
int i=Integer.parseInt(br.readLine());

account.start(i);
}
}

Datatype

//on DATA TYPES
class Datatype
{
public static void main(String args[])
{
int i=5;
float f=2.5F;
char ch='k';
String s="krish";
double b=1.5e5;
byte d=3;
boolean bo=true;
System.out.println(" DATA TYPES");
System.out.println("-----------------------------------");
System.out.println(i+"is interger type");
System.out.println(f+"is floate type");
System.out.println(ch+"is a char type");
System.out.println(s+"is string tyep");
System.out.println(b+"is a double type");
System.out.println(d+" is a byte type");
System.out.println(bo+" is boolean type");
}
}

continue statement

//CONTINUE statement
class Cont
{
public static void main(String args[])
{
for(int i=10;i>=1;i--)
{
if(i>5)continue;
System.out.println(i);
}
}
}

arrays declarations

//arrays declare at progaming
class Arr
{
public static void main(String args[])
{
int arr[]={2,5,6,7,9};
System.out.println("arrays printing by indiaual");
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
System.out.println(arr[3]);
System.out.println(arr[4]);
}
}

do -while loop

//do -while loop
class Dowhile
{
public static void main(String arg[])
{
int x=2;
System.out.println("even numbers");
do
{
System.out.print(x+" ");
x+=2;
}while(x<=30);
}
}

Narrowing

//type casting

class One
{
void show1()
{
System.out.println("--------Super-------");
}
};

class Two extends One
{
void show2()
{
System.out.println("--------sub----------");
}
};

class Narrowing
{
public static void main()
{
One o;
o=(One)new Two();
o.show2();
}
};

return statement

//RETURN statement
class Returndemo
{
public static void main(String args[])
{
int x=1;
System.out.print("Before return");
if(x==1)return;
System.out.print("after retrn");
}
}

prime numbers

class Prime
{
public static void main(String args[])
{
for(int i=1;i<=50;i+=2)
if(i%2!=0)
System.out.println(i);
}
}

while statement

//demo on while statement;
class Whiledemo
{
public static void main(String args[])
{
int x=0;
while(x<=10)
{
System.out.print(x+" ");
x+=2;
}
}
}

static block

//static exeutes first static block b4 main static
class Test
{
public static void main(String[] args)
{
System.out.println("am main block static");
}
static
{
System.out.println("am in static block");
};
}

if demo

//test EVEN OR ODD no
class Test
{
public static void main(String args[])
{
int x=23;
if(x%2==0)
System.out.println(x+" is EVEN");
else
System.out.println(x+" is ODD");
}
}

switch statement demo

//SWITCH statement;
class Switchdemo
{
public static void main(String argd[])
{
char color='r';
switch(color)
{
case'B':
case 'b':System.out.println("blue");break;
case'G':
case 'g':System.out.println("green");break;
case'R':
case 'r':System.out.println("red");break;
//defult:System.out.println("unknown color");break;
}
}
}

super keyword

//using "super" we can call super class metods in sub class by using sub class obj
class One
{
int x;
One(int x)
{
this.x=x;
}
void show()
{
System.out.println("Super :x"+x);
}
}

class Two extends One
{
int x;
Two(int a,int b)
{
super(a); //calling super class variables
x=b;
}
void show()
{
System.out.println("Sub x---"+x);
super.show(); // calling super class method
}
};

class Super
{
public static void main(String[] args)
{
Two t=new Two(10,15);
t.show();
}
}

super keyword

//using "super" we can call super class metods in sub class by using sub class obj
class One
{
int x;
One(int x)
{
this.x=x;
}
void show()
{
System.out.println("Super :x"+x);
}
}

class Two extends One
{
int x;
Two(int a,int b)
{
super(a); //calling super class variables
x=b;
}
void show()
{
System.out.println("Sub x---"+x);
super.show(); // calling super class method
}
};

class Super
{
public static void main(String[] args)
{
Two t=new Two(10,15);
t.show();
}
}

Swap Demo

import java.io.*;
class Swap
{
void swap(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
System.out.print("after swap---");
System.out.print(x+" ,");
System.out.print(y);
}
void swap2(int x,int y)
{
x=(x+y)-x;
y=(x+y)-y;
System.out.print("after swap---");
System.out.print(x+" ,");
System.out.print(y);
}
}

class Swapdemo
{
public static void main(String[] args) throws IOException
{
InputStreamReader id=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(id);
Swap s=new Swap();
System.out.println("enter two values");
int x=Integer.parseInt(br.readLine());
int y=Integer.parseInt(br.readLine());
s.swap(x,y);
//s.swap2(x,y);
}
}

Stringbuffer Methods

import java.lang.StringBuffer.*;
class Stringbuffer
{
public static void main(String[] args)
{
StringBuffer sb=new StringBuffer("hello am san");
System.out.println("given string is :----"+sb);

System.out.println("appending str---"+sb.append(" hi "));

System.out.println("insert z at 10 postion---"+sb.insert(10,'x'));

System.out.println("delete at 5 to 10 posiotion---"+sb.delete(5,10));

System.out.println("reverse-------"+sb.reverse());

System.out.println("to srting ---"+sb.toString());

System.out.println("length of str---"+sb.length());

System.out.println("delete at 5 to10 posiotion---"+sb.delete(5,10));
}
}

String Methods

import java.lang.String.*;
class Strings
{
public static void main(String[] args)
{
String str=new String();
str="hello";
String str2=new String(" am ");

String str3="san";

System.out.println(str+str2+str3);//+ IS USED TO CONCAT THE STRINGS
String s=str.concat(str2); //concat tne strings
System.out.println(s.concat(str3));
int n=str3.length(); //returns the length of given string
System.out.println("length of san is :"+n);

System.out.println("Trim of :"+str3.trim());//deletes leading and tailing string

char ch=str3.charAt(2); //returns char of given positon
System.out.println("char at postion 2 is :"+ch);

int k=str.compareTo("helloo"); //compares two given strings
System.out.println("compatrion of two strings :"+k);

boolean t=str.startsWith("h");//returns true if stars with that
System.out.println("hello is stars with h: "+t);

boolean t1=str.endsWith("h");//returns true if ends with that
System.out.println("hello is ends with h: "+t1);

String sss="hello how is u r hekth is n";
int kk=sss.indexOf("is"); //give the position of first str in given str
System.out.println("give postion of first is in string : "+kk);

int kk1=sss.lastIndexOf("is"); //give the position of last str in given str
System.out.println("give postion of last is in string : "+kk1);

System.out.println(sss);
String sk=sss.replace('h','M');
System.out.println("replace of H with M: "+sk);

System.out.println("to lower string:---"+sk.toLowerCase());

System.out.println("to upper case string:---"+sk.toUpperCase());

// System.out.println("Sub string:------"+sk.subString());

}
}

Method Overriding

//Method Overriding

class One
{
void cal(double x)
{
System.out.println("Square :----"+(x*x));
}
}

class Two extends One
{
void cal(double x)
{
System.out.println("Sqare root:--------"+Math.sqrt(x));
}
};

class Overriding
{
public static void main(String arg[])
{
Two t=new Two();
t.cal(16.555555);
}
};

operators

import java.io.*;
import java.lang.String.*;
class Operators
{
public static void main(String[] args)throws IOException
{
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
System.out.println("enter ur values for x");
int x=Integer.parseInt(br.readLine());
System.out.println("enter ur values for y");
int y=Integer.parseInt(br.readLine());

System.out.println("enter ur choice");
System.out.println("A for arthemetic");
System.out.println("R relation ");
System.out.println("B for Bitwise");
System.out.println("L for logical");
System.out.println("I for increment r decrement");

char str=(char)br.read();
br.skip(1);
switch(str)
{
case 'a':
case 'A':

System.out.println("enter ur choice");
System.out.println("+ for addtion");
System.out.println("- for substraction");
System.out.println("* for mul");
System.out.println("/ for divide");
System.out.println("% for remainder");
br.skip(1);
char ch=(char)br.read();
switch(ch)
{
case '+':
System.out.print("addtion of x+y:"+(x+y));
break;

case '-':
System.out.print("subtraction of x+y:"+(x-y));
break;
case '*':
System.out.print("mul of x+y:"+(x*y));
break;

case '/':
System.out.print("divide of x+y:"+(x/y));
break;

case '%':
System.out.print("remainder of x+y:"+(x%y));
break;

default:
System.out.println("invalid input");
break;

}
break;

case 'i':
case 'I':
System.out.println("enter ur choice");
System.out.println("k for increment");
System.out.println("p for decrement");
br.skip(1);

char st=(char)br.read();

switch(st)
{
case 'k':
System.out.print(" pre Increment of x:"+(++x));
break;

case 'p':
System.out.print("post decrement of x:"+(x--));
break;
default:
System.out.println("invalid input");
break;

}
break;

case 'b':
case 'B':
System.out.println("enter ur choice");
System.out.println("~ for complement");
System.out.println("l for leftshift");
System.out.println("r for rightshift");
System.out.println("| for Or");
System.out.println("& for and");
System.out.println("^ for Xor");
System.out.println("! for not");
br.skip(1);

char stk=(char)br.read();

switch(stk)
{
case '~':
System.out.print(" complement of x:"+(~x));
break;

case 'l':
System.out.print("leftshift of x:"+(x>>(1)));
break;

case 'r':
System.out.print("rigttshift of x:"+(x<<(1)));
break;

case '|':
System.out.print("or of x or y:"+(x|y));
break;

case '&':
System.out.print("and of x & Y:"+(x&y));
break;

case '!':
//System.out.print("not of x:"+(!x));
break;

case '^':
System.out.print("Xor of x Xor y:"+(x^y));
break;
default:
System.out.println("invalid input");
break;

}
break;
default:
System.out.println("invalid input");
break;
}

}
}

foe-each loop

//for -each loop
class Foreach
{
public static void main(String arg[])
{
int arr[]={22,33,55,66,99};
for(int x:arr)
System.out.print(x+" ");
}
}

for loop demo

//demo on for statement;
class Fordemo
{
public static void main(String args[])
{
int x=1;

for(int k=10;k>=k/2;k--)
{
System.out.println(" ");
for(;x<=10;x++)
{
System.out.println();
{
for(int j=1;j<=x;++j)
System.out.print(" * ");
}
}
}
}
}

simple java programe

//simpe programe on java

public class Demo
{
public static void main(String args[])
{
System.out.println("hello java");
}
}