Friday, February 12, 2016

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

}

}