Friday, February 12, 2016

StringExample9

package StringInJava;

public class StringExample9 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

String s1 = "Bhanu";

// charAt , it has character return type

System.out.println(s1.charAt(3));

System.out.println(s1.charAt(0));

System.out.println("--------------");

// contains has boolean return type
System.out.println(s1.contains("u"));

System.out.println(s1.contains("Bha"));

System.out.println(s1.contains("pm"));

System.out.println("---------------------");

System.out.println(s1.concat(" pratap"));
System.out.println(s1.concat(" Singh"));

System.out.println("---------------");

String s2 = "Bhanu Pratap singh";

String[] split = s2.split(" ");

System.out.println(split[0]);
System.out.println(split[1]);
System.out.println(split[2]);

       String s3 = "Bhanu Pratap singh";

String[] split1 = s3.split("Pratap");

System.out.println(split1[0]);
System.out.println(split1[1]);
//System.out.println(split1[2]);

}

}

StringExample8

package StringInJava;

public class StringExample8 {

/**
Suppose s1 and s2 are two string variables. If:

          s1 == s2 :0
          s1 > s2   :positive value
          s1 < s2   :negative value
*/
public static void main(String[] args) {
  String s1="Bhanu";
  String s2="Bhanu";
  String s3="Pratap";
  System.out.println(s1.compareTo(s2));//0
  System.out.println(s1.compareTo(s3));//-14(because s1>s3)
  System.out.println(s3.compareTo(s1));//14(because s3 < s1 )
 
         int a1 = (int)'a';
 
  System.out.println("ascii value of a is :-"+a1);
 
        int a2 = (int)'A';
 
  System.out.println("ascii value of A is :-"+a2);
}



}

StringExample7

package StringInJava;

public class StringExample7 {

/**
* String compare By equals() method By = = operator By compareTo() method
*/
public static void main(String[] args) {  
        /**
* The String  == operator compares the reference of the string
*/
String s5 = "Bhanu";
String s6 = "Bhanu";
String s7 = new String("Pratap");
// true (because both refer to same instance)
System.out.println(s5 == s6);
// false(because s7 refers to instance created in nonpool)
System.out.println(s5 == s7);

System.out.println(s6 == s7);

}

}

StringExample6

package StringInJava;

public class StringExample6 {

/**
* @param args
*/
public static void main(String[] args) {
String p1 = "Bhanu";
String p2 = "BHANU";

System.out.println(p1.equals(p2));// false
System.out.println(p1.equalsIgnoreCase(p2));// true

String p3 = "Test";
String p4 = "TEST";

System.out.println(p3.equals(p4));// false
System.out.println(p3.equalsIgnoreCase(p4));// true


String p5 = "java";
String p6 = "java";

System.out.println(p5.equals(p6));// true
System.out.println(p5.equalsIgnoreCase(p6));// true
}

}

StringExample5

package StringInJava;

public class StringExample5 {

/**
* String compare By equals() method By = = operator By compareTo() method
*/
public static void main(String[] args) {

String s1 = "Bhanu";
String s2 = "Bhanu";
String s3 = new String("Bhanu");
String s4 = "Pratap";
/**
* The String equals() method compares the original content of the string
*/
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s1.equals(s4));
        System.out.println("----------------------------");
       
       
        /**
* The String  == operator compares the reference of the string
*/
String s5 = "Bhanu";
String s6 = "Bhanu";
String s7 = new String("Bhanu");
// true (because both refer to same instance)
System.out.println(s5 == s6);
// false(because s7 refers to instance created in nonpool)
System.out.println(s5 == s7);

System.out.println(s6 == s7);

System.out.println("------------------------");
System.out.println(s5.equals(s7));
System.out.println(s6.equals(s7));
System.out.println("----------------");
System.out.println(s5==s4);

}

}

StringExample4

package StringInJava;

public class StringExample4 {

/**
* @param args
*/
public static void main(String[] args) {
String s = "Bhanu";
s = s.concat(" Pratap");
System.out.println(s);


String s1 = "Bhanu";
s1.concat(" Pratap");
System.out.println(s1);

String s3 = "Test";
s3 = s3.concat(" Method ");
System.out.println(s3);

}

}

StringExample3

package StringInJava;

public class StringExample3 {

/**
* @param args
*/
public static void main(String[] args) {

String s = "bhanu";
//concat() method appends the string at the end
s.concat("Pratap");
System.out.println(s);

String s1 = "Test";
s1.concat("Test1");
System.out.println(s1);

String s2 = "run";
s2.concat("Test");
System.out.println(s2);
}

}

StringExample2

package StringInJava;

public class StringExample2 {

/**
* @param args
*/
public static void main(String[] args) {
// creating string object by string literal
String s1 = "Bhanu";
String s2 = "Bhanu Pratap";
String s3 = "Bhanu Pratap Singh";
// converting char array to string
char ch[] = { 'T', 'e', 's', 't' };
String s4 = new String(ch);
char ch1[] = { 'T', 'e', 's', 't' ,'1'};
String s5 = new String(ch1);
char ch2[] = { 'T', 'e', 's', 't','2' };
String s6 = new String(ch2);
char ch3[] = { 'T', 'e', 's', 't','3'};
String s7 = new String(ch3);
// creating java string object by new keyword
String s8 = new String("example");
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
System.out.println(s5);
System.out.println(s6);
System.out.println(s7);
System.out.println(s8);



}

}

StringExample1

package StringInJava;

public class StringExample1 {

/**
Q.what is string

1. String is a class.
2. String represents set of characters.
3. String is immutable i.e. it cannot be changed , It is final

-----------------------------------------------------------
Q.How to create String object

     There are two ways to create String object:
     1. By string literal
     2. By new keyword
------------------------------------------------------------    
     String literal is created by using double quotes
      e.g String t = "Bhanu"; ( In constant pool)
      e.g String t1 = "Bhanu";
     To make Java more memory efficient (because no new objects are created if it exists already in string constant pool).
------------------------------------------------------------    
     By new keyword
      String t1 = new String("Bhanu"); ( In non constant pool, i.e heap area)

*/
public static void main(String[] args) {
// TODO Auto-generated method stub

}

}

Monday, February 1, 2016

MethodOverRiddingExample7

package methodOverridding;

public class MethodOverRiddingExample7 extends MethodOverRiddingExample6{

public int add(){
System.out.println(" I am from child class");
return 5;
}

public boolean add1(int a){
System.out.println(" I am from child add1");

}

/**
* for method overridding both child class and parent class method return type, method name
* and method argument should be same
*/

}

MethodOverRiddingExample6

package methodOverridding;

public class MethodOverRiddingExample6 {

public void add(){
System.out.println(" I am from parent class");
}

public void add1(int a){
System.out.println(" I am from parent add1");
}

}

MethodOverRiddingExample5

package methodOverridding;

public class MethodOverRiddingExample5 extends MethodOverRiddingExample4{

public void sum(int a, int b) {
int c = a + b;
System.out.println("sum is from child class with two argument:-" + c);
}

public void sum(int a, int b, int c) {
int d = a + b + c;
System.out.println("sum is from child class with three argument:-" + d);
}


void test4(){
System.out.println(" i am from child class");
}

   public static void main(String[] args) {

  MethodOverRiddingExample4 obj = new MethodOverRiddingExample5();
  /**
   * if you will make reference of parent class, then we can access only parent
   * class members through reference. at compile time
   */
 
  /**
* At run time method will be called based on the class for which we have
* created object, if method is not available in child class then it will
* check in parent class.
*/
 
  obj.sum(4, 5);
  obj.sum(4, 5, 6);
 
  obj.mutiply(5, 6);
 
 
 
  MethodOverRiddingExample5 obj1 = new MethodOverRiddingExample5();
 
  obj1.sum(2, 3);
  obj1.sum(2, 3, 7);
  obj.mutiply(7, 9);
  obj1.test4();
 
  /**
   * if you will make reference of child class, then we can access  parent
   * class members as well child class members at compile time
   */
 
 
   }
}

MethodOverRiddingExample4

package methodOverridding;

public class MethodOverRiddingExample4 {

public void sum(int a, int b){
int c = a+b;
System.out.println("sum is from parent with two argument:-"+c);
}

public void sum(int a, int b, int c){
int d = a+b+c;
System.out.println("sum is from parent with three argument:-"+d);
}

public void mutiply(int a, int b){
int d = a*b;
System.out.println("multiply is from parent:-"+d);
}

}

MethodOverridingExample3

package methodOverridding;

public class MethodOverridingExample3 extends MethodOverRiddingExampe1{

/*public void test1(){
System.out.println("I am from child  class");
}*/

public static void main(String[] args) {

MethodOverRiddingExampe1 obj = new MethodOverridingExample3();

obj.test1();

/**
* At run time method will be called based on the class for which we have
* created object.
*/
}

}

MethodOverRiddingExample2

package methodOverridding;

public class MethodOverRiddingExample2 extends MethodOverRiddingExampe1{

public void test1(){
System.out.println("I am from child  class");
}

public static void main(String[] args) {

MethodOverRiddingExample2 obj = new MethodOverRiddingExample2();

obj.test1();

/**
* At run time method will be called based on the class for which we have
* created object.
*/


}

}

MethodOverRiddingExampe1

package methodOverridding;

public class MethodOverRiddingExampe1 {

public void test1(){
System.out.println("I am from parent class");
}

}