0% found this document useful (0 votes)
27 views

String: String Is Basically An Object That Represents Sequence of Character Values. Ex

String objects in Java can be created in two ways: using the new operator or string assignment. When created without new, the String is stored in a string constant pool for reuse. If two String variables contain the same value without new, they will reference the same object. But Strings created with new will always be distinct objects even if they have the same value. The equals() method can be used to compare the values of two String objects rather than using ==, which compares object references.

Uploaded by

NaveenKumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

String: String Is Basically An Object That Represents Sequence of Character Values. Ex

String objects in Java can be created in two ways: using the new operator or string assignment. When created without new, the String is stored in a string constant pool for reuse. If two String variables contain the same value without new, they will reference the same object. But Strings created with new will always be distinct objects even if they have the same value. The equals() method can be used to compare the values of two String objects rather than using ==, which compares object references.

Uploaded by

NaveenKumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

String:

String is basically an object that represents sequence of character values.

Ex:
package com.anu.test;

public class Demo1 {

public static void main(String[] args) {


// TODO Auto-generated method stub

String s1="Anusha";
String s2 = new String("- Cute girl");
System.out.println(s1+ " " +s2);
}

Ex:
package com.anu.test;

public class Demo2 {

public static void main(String[] args) {


// TODO Auto-generated method stub
String s1="Anusha";
String s2 = new String("Developer");

String s3="Anusha";
String s4 = new String("Developer");

System.out.println(s1==s3);
System.out.println(s2==s4);

String s5 = new String("Anusha");


System.out.println(s1==s5);

String objects are created in two ways using:

1. New operator and


2. Assignment
String constant pool
Ex: String s1 = “Anusha”; Non Constant pool

Anusha

Developer

String s2 = new String (“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.

(In constant pool you can have only constant objects)

String s1=”Anusha”;

String s2 = new String(“Developers”);

Like, String s3 = “ Anusha”; //not allowed to create separate (Duplicate) object, refer to current object if
already existing

But,

String s4 = new String(“Developer”);

String s5 = new String(“Anusha”);

S1==S3 true

S2==S4 false

S1==S5 false

S3.equals(S5) true //we use, to compare values in the object

Ex:
package com.anu.test;

public class Demo2 {

public static void main(String[] args) {


// TODO Auto-generated method stub
String s1="Anusha";
String s2 = new String("Developer");
String s3="Anusha";
String s4 = new String("Developer");
String s5 = new String("Anusha");

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;

public class demo3 {

public static void main(String[] args) {


// TODO Auto-generated method stub
String s1="Anusha";
String s2="Ravish";
String s3=s1+" "+s2;
System.out.println("s3="+s3);

String s4="Anusha Ravish";


System.out.println("s4="+s4);

System.out.println(s3==s4);

String s5="Anusha"+" "+"Ravish";


System.out.println("s5="+s5);

System.out.println(s3==s5);
System.out.println(s4==s5);

String s6=s1+" "+"Ravish";


System.out.println(s3==s6);
System.out.println(s4==s6);
System.out.println(s4.equals(s6));
}

You might also like