String: String Is Basically An Object That Represents Sequence of Character Values. Ex
String: String Is Basically An Object That Represents Sequence of Character Values. Ex
Ex:
package com.anu.test;
String s1="Anusha";
String s2 = new String("- Cute girl");
System.out.println(s1+ " " +s2);
}
Ex:
package com.anu.test;
String s3="Anusha";
String s4 = new String("Developer");
System.out.println(s1==s3);
System.out.println(s2==s4);
Anusha
Developer
String class object created without new operator will be created in string constant pool (This happens
only for string class) If we have one more object with same value without new operator then the
variable will point towords the already existing object.
String s1=”Anusha”;
Like, String s3 = “ Anusha”; //not allowed to create separate (Duplicate) object, refer to current object if
already existing
But,
S1==S3 true
S2==S4 false
S1==S5 false
Ex:
package com.anu.test;
System.out.println(s1==s3);
System.out.println(s2==s4);
System.out.println(s1==s5);
System.out.println("------------------------");
System.out.println(s1.equals(s3));
System.out.println(s2.equals(s4));
System.out.println(s1.equals(s5));
System.out.println(s1.equals(s2));
Ex:
package com.anu.test;
System.out.println(s3==s4);
System.out.println(s3==s5);
System.out.println(s4==s5);