Previous | Home | Next |
Example
//Different Condition Checked using Equal And Equalto method. package r4r.co.in; public class EqualAndEqualto { public static void main(String[] args) { String s1 = new String("GOOD MORNING"); //create String object String s2 = new String("GOOD MORNING"); String s3 = "GOOD MORNING"; //Create String Refrence String s4 = "GOOD MORNING"; StringBuffer s5 = new StringBuffer("GOOD MORNING"); //Create String object using StringBuffer System.out.print("Length of String s1&s2: " + s1.length()); System.out.print("\nLength of String s3&s4: " + s3.length()); System.out.print("\nLength of String s5: " + s5.length()); //Different Condition checked try { if (s1.equals(s2)) { System.out.print("\nCondition 1 Successfully match"); } else { System.out.print("\nCondition 1 not match"); } if (s1 == s2) { System.out.print("\nCondition 2 Successfully match"); } else { System.out.print("\nCondition 2 not match"); } if (s3.equals(s4)) { System.out.print("\nCondition 3 Successfully match"); } else { System.out.print("\nCondition 3 not match"); } if (s3 == s4) { System.out.print("\nCondition 4 Successfully match"); } else { System.out.print("\nCondition 4 not match"); } if (s1.equals(s5)) { System.out.print("\nCondition 5 Successfully match"); } else { System.out.print("\nCondition 5 not match"); } if (s3.equals(s5)) { System.out.print("\ncondition 6 Successfully match"); } else { System.out.print("\ncondition 6 not match"); } } catch (Exception e) { System.out.print(e); } } }
Output:
Length of String s1&s2: 12 Length of String s3&s4: 12 Length of String s5: 12 Condition 1 Successfully match Condition 2 not match Condition 3 Successfully match Condition 4 Successfully match Condition 5 not match Condition 6 not match
//Example for Store element from String into Array. package r4r.co.in; public class NewClass { public static void main(String[] args) { String s1 = "Following Demo belongs to NewClass"; System.out.println("Length of String s1: " + s1.length()); //Determine length of String System.out.println("Position of D:" + s1.indexOf('D')); //taking value position System.out.println("Position of b:" + s1.lastIndexOf('b')); //taking value position int start = 10; int end = 20; char c[] = new char[(end + 10) - start]; //Store element into array s1.getChars(start, end + 1, c, 0); System.out.print(c); } }
output:
Length of String s1: 34 Position of D:10 Position of b:15 Demo belong
Previous | Home | Next |