Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Strings in Java

Avatar for Mayuri Mayuri
September 16, 2026

Strings in Java

Strings in Java

Avatar for Mayuri

Mayuri

September 16, 2026

Other Decks in Education

Transcript

  1. STRINGS • Strings is group of Characters enclosed in single

    or double quote. • String is an immutable(not allowed Modification) • for each modification on the string object it creates separate objects in the memory • String concept is also thread safe bcz we can’t modify the object. • if we try to modify a new object will create so all immutable objects are thread safe (even wrapper classes) . • Example: String s="Java";
  2. EXAMPLES public class StringDemoS { public static void main(String[] args)

    { String s="Java"; System.out.println(s); s.concat("Programming"); System.out.println(s); s.concat("Language"); System.out.println(s); } } Output: Java Java Java
  3. WAYS TO CREATE STRINGS There are two ways to create

    String in Java 1.String Literal Approach 2.Creating Object
  4. CREATING OBJECT Syntax:String s = new String (); Example: •

    String s4=new String("JAVA"); • String s = new String (“welcome); • String s = new String (“welcome”); String s1 = new String (s);
  5. CONSTANT POOL • It is a section available in Heap

    Memory. • It only store object of String which has been created using String Literal. • Never accept duplicate String Object. • If we try to create duplicate Object of String instead of creating new one it provide only object Reference.
  6. EXAMPLE public class StringDemo { public static void main(String[] args)

    { String s1=new String (“You are best”); String s2=new String (“You Are best”); System.out.println(s1==s2); → false String s3= “You are best”; System.out.println(s1==s3); → false String s4= “You are best”; System.out.println(s3==s4); → true } }
  7. Important Methods Of String • public char charAt(int index) •

    public char charAt(int index) Example:String s= “adah”; System.out.println(s.equals(“Adah”)); → false • concat(String s) String s= “adah”; s=s.concat(“student”); System.out.println(s); → Java Programming