SlideShare a Scribd company logo
STRING IN JAVA
• Characters
• “Building blocks” of non-numeric data
• ’a’, ’$’, ’4’
• String
• Sequence of characters treated as single unit
• May include letters, digits, etc.
• Object of class String
• String name = “Frank N. Stein”;
• Strings are most widely used object in java language.
• In java there are three classes that can process them and create
them with nearly three similar methods are as follows:
• Class String
• Class StringBuffer
• Class StringBuilder
• All the three class are the part of java.lang package.
Overview of three string classes
• String str1 = “abcd”; (creating a string class object). ---(1)
• String: class String; str1: String object; “abcd”: sequence
of character.
• Char s1[]= {‘a’,’b’,’c’,’d’};. -----(2)
• The above two declaration are equivalent.
• Using new operator also we can create the object of class
String.
• StringBuffer bufstr= new StringBuffer(“abcd”);. ----(3)
• StringBuilder budstr= new StringBuilder(“abcd”);
Storage of the string
• The object of class string have a special storage facility which
is not available to the other two classes.
• We know the memory allocated to the java program is divided
into two segments.
• Stack
• Heap
• Variables are stored in heap while programs are stored in stack.
• Heap have a memory segment called “String constant pool”.
• The declaration of type 1 will going to store in string constant
pool. Where as declaration of type 2 will be stored in the
memory segment of the heap.
• No duplicate string can be created in the String constant pool.
• String stry= “abcd”; String strx =“abcd”.
•
strx and stry both
referenced to a same
string.
• == (true only if the strings are at the same address, i.e., same string).
• Method equals (true if the strings are identical)
String in JAVA --------------------------
Questions
1.
2.
3.
• 4.
Immutability
• The string object created by class String are immutable.
• “Immutable”: Once an object is created its value or content
cannot be changed.
• Implying waste of memory, as the old and new string will reside
in the memory.
• The object created by the class of StringBuffer are mutable and
are stored in the heap segment of the memory.
• The methods of StringBuffer are synchronized and hence they
are thread safe.
• The overburden of thread safety makes them heavy and lower
the program execution speed.
• The object of class StringBuilder are mutable but not thread
safe. The operation are fast as compared to StringBuffer and
there will be no memory loss as in the case of String class.
String in JAVA --------------------------
Constructor of class string
• The string class is final and we cannot extend this class.
• There are different number of constructor of class string through which
the strings can be created.
• String(): Create a string without any character.
• String(byte[] barray): Construct a new string by decoding the specified byte array.
• String(byte[] barray, Charset specifiedset): Construct a new string by decoding the
specified byte array using specified character set. The supported charsets are
UTF8, UTF16, UTF32, and ASCII
• byte[] bray = new byte[]{65,66,67,68};
String str2= new String(bray, “ascii”);
• String(byte[] barray, int offset, int length, String charsetName):
Constructor construct a string object by decoding the specified part of byte
array using specified character set.
• byte[] bray = new byte[]{65,66,67,68};
String str2= new String(bray, 1,3, “ascii”);
Throw UnsupportedEncodingException or IndexOutofBoundsException.
• String (char[] value): Constructor construct a string based on the
specified char array. Char cr []= {‘A’,’B’,’C’,’D’}; String str= new String(cr)
• String (char[], int offset, int count): Constructor construct a string with
character from a part of character array, starting from the offset up to given
length. String str= new String(cr, 0,2)
• Return AB; Throws IndexOutofBoundsException
• String(String “originalstring”): Constructor construct a string with string
as specified in the argument.
String str2= new String(“Let us learn java”);
• String(StringBuffer str):
• Constructor construct a string contains character in the string buffer.
StringBuffer cr = new StringBuffer{“Hello”}; String str= new String(cr)
• String(StringBuilder str):
• Constructor construct a string contains character in the string buffer.
StringBuilder cr = new StringBuilder{“Hello”}; String str= new String(cr)
Questions on different functions of String
1.
String in JAVA --------------------------
• 2.
String in JAVA --------------------------
3.
String in JAVA --------------------------
Some more Functions
• byte[] getBytes(): Encode a given string into a sequence of bytes and
returns an array of bytes.
• char toCharArray(): Converts the string into an array of characters and
returns the array.
• Int compareToIgnoreCase(): Compare the string by the ignoring the case
difference.
• startsWith(String str): Returns true if a string starts with the given substring.
• endsWith(String str): Returns true if a string ends with the given substring.
4.
5.
6.
String in JAVA --------------------------
• 7 .
• 8.
String in JAVA --------------------------
9.
String in JAVA --------------------------
• 10
Exception thrown by string class
• IndexOutOfBoundException:-
• UnsupportedEncodingException
• PatternSyntaxException
• NullPointerException
Class StringBuffer
The compiler allocate extra capacity for 16 more characters so that
small modification don't involve reallocation of string.
11.
• 12.
String in JAVA --------------------------

More Related Content

DOCX
Java R20 - UNIT-5.docx
PDF
Java R20 - UNIT-5.pdf
PPTX
Java String
PPTX
String Handling, Inheritance, Packages and Interfaces
PPTX
Java Strings.pptxJava Strings.pptxJava Strings.pptx
PPTX
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
PPTX
String handling
PPT
Charcater and Strings.ppt Charcater and Strings.ppt
Java R20 - UNIT-5.docx
Java R20 - UNIT-5.pdf
Java String
String Handling, Inheritance, Packages and Interfaces
Java Strings.pptxJava Strings.pptxJava Strings.pptx
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
String handling
Charcater and Strings.ppt Charcater and Strings.ppt

Similar to String in JAVA -------------------------- (20)

PPTX
Java string handling
PPTX
Introduction to Java Strings, By Kavita Ganesan
PPTX
Strings in Java
PPTX
Java string handling
PPT
Java Strings methods and operations.ppt
PPT
String classes and its methods.20
PPTX
Day_5.1.pptx
PPTX
Java Strings
PPTX
javastringexample problems using string class
PPTX
String in java, string constructors and operations
PPT
String Handling
PPTX
In the given example only one object will be created. Firstly JVM will not fi...
PDF
java.lang.String Class
PPT
Text processing
PPT
String handling session 5
PPTX
STRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVA
PDF
Module-1 Strings Handling.ppt.pdf
PPT
Strings in javamnjn ijnjun oinoin oinoi .ppt
PPT
Strings power point in detail with examples
PPTX
Java Strings - using Strings in practice
Java string handling
Introduction to Java Strings, By Kavita Ganesan
Strings in Java
Java string handling
Java Strings methods and operations.ppt
String classes and its methods.20
Day_5.1.pptx
Java Strings
javastringexample problems using string class
String in java, string constructors and operations
String Handling
In the given example only one object will be created. Firstly JVM will not fi...
java.lang.String Class
Text processing
String handling session 5
STRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVA
Module-1 Strings Handling.ppt.pdf
Strings in javamnjn ijnjun oinoin oinoi .ppt
Strings power point in detail with examples
Java Strings - using Strings in practice
Ad

Recently uploaded (20)

PDF
English Language Teaching from Post-.pdf
PPTX
Odoo 18 Sales_ Managing Quotation Validity
PPTX
Software Engineering BSC DS UNIT 1 .pptx
PPTX
How to Manage Bill Control Policy in Odoo 18
PPTX
ACUTE NASOPHARYNGITIS. pptx
PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
How to Manage Loyalty Points in Odoo 18 Sales
PDF
High Ground Student Revision Booklet Preview
PDF
Cell Biology Basics: Cell Theory, Structure, Types, and Organelles | BS Level...
PDF
Sunset Boulevard Student Revision Booklet
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
PPTX
Strengthening open access through collaboration: building connections with OP...
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PDF
Types of Literary Text: Poetry and Prose
PDF
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
English Language Teaching from Post-.pdf
Odoo 18 Sales_ Managing Quotation Validity
Software Engineering BSC DS UNIT 1 .pptx
How to Manage Bill Control Policy in Odoo 18
ACUTE NASOPHARYNGITIS. pptx
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
102 student loan defaulters named and shamed – Is someone you know on the list?
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
How to Manage Loyalty Points in Odoo 18 Sales
High Ground Student Revision Booklet Preview
Cell Biology Basics: Cell Theory, Structure, Types, and Organelles | BS Level...
Sunset Boulevard Student Revision Booklet
UPPER GASTRO INTESTINAL DISORDER.docx
Strengthening open access through collaboration: building connections with OP...
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
The Final Stretch: How to Release a Game and Not Die in the Process.
Types of Literary Text: Poetry and Prose
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
Ad

String in JAVA --------------------------

  • 2. • Characters • “Building blocks” of non-numeric data • ’a’, ’$’, ’4’ • String • Sequence of characters treated as single unit • May include letters, digits, etc. • Object of class String • String name = “Frank N. Stein”;
  • 3. • Strings are most widely used object in java language. • In java there are three classes that can process them and create them with nearly three similar methods are as follows: • Class String • Class StringBuffer • Class StringBuilder • All the three class are the part of java.lang package.
  • 4. Overview of three string classes • String str1 = “abcd”; (creating a string class object). ---(1) • String: class String; str1: String object; “abcd”: sequence of character. • Char s1[]= {‘a’,’b’,’c’,’d’};. -----(2) • The above two declaration are equivalent. • Using new operator also we can create the object of class String. • StringBuffer bufstr= new StringBuffer(“abcd”);. ----(3) • StringBuilder budstr= new StringBuilder(“abcd”);
  • 5. Storage of the string • The object of class string have a special storage facility which is not available to the other two classes. • We know the memory allocated to the java program is divided into two segments. • Stack • Heap • Variables are stored in heap while programs are stored in stack. • Heap have a memory segment called “String constant pool”.
  • 6. • The declaration of type 1 will going to store in string constant pool. Where as declaration of type 2 will be stored in the memory segment of the heap. • No duplicate string can be created in the String constant pool. • String stry= “abcd”; String strx =“abcd”. •
  • 7. strx and stry both referenced to a same string. • == (true only if the strings are at the same address, i.e., same string). • Method equals (true if the strings are identical)
  • 11. Immutability • The string object created by class String are immutable. • “Immutable”: Once an object is created its value or content cannot be changed. • Implying waste of memory, as the old and new string will reside in the memory. • The object created by the class of StringBuffer are mutable and are stored in the heap segment of the memory.
  • 12. • The methods of StringBuffer are synchronized and hence they are thread safe. • The overburden of thread safety makes them heavy and lower the program execution speed. • The object of class StringBuilder are mutable but not thread safe. The operation are fast as compared to StringBuffer and there will be no memory loss as in the case of String class.
  • 14. Constructor of class string • The string class is final and we cannot extend this class. • There are different number of constructor of class string through which the strings can be created. • String(): Create a string without any character. • String(byte[] barray): Construct a new string by decoding the specified byte array. • String(byte[] barray, Charset specifiedset): Construct a new string by decoding the specified byte array using specified character set. The supported charsets are UTF8, UTF16, UTF32, and ASCII • byte[] bray = new byte[]{65,66,67,68}; String str2= new String(bray, “ascii”);
  • 15. • String(byte[] barray, int offset, int length, String charsetName): Constructor construct a string object by decoding the specified part of byte array using specified character set. • byte[] bray = new byte[]{65,66,67,68}; String str2= new String(bray, 1,3, “ascii”); Throw UnsupportedEncodingException or IndexOutofBoundsException. • String (char[] value): Constructor construct a string based on the specified char array. Char cr []= {‘A’,’B’,’C’,’D’}; String str= new String(cr) • String (char[], int offset, int count): Constructor construct a string with character from a part of character array, starting from the offset up to given length. String str= new String(cr, 0,2) • Return AB; Throws IndexOutofBoundsException
  • 16. • String(String “originalstring”): Constructor construct a string with string as specified in the argument. String str2= new String(“Let us learn java”); • String(StringBuffer str): • Constructor construct a string contains character in the string buffer. StringBuffer cr = new StringBuffer{“Hello”}; String str= new String(cr) • String(StringBuilder str): • Constructor construct a string contains character in the string buffer. StringBuilder cr = new StringBuilder{“Hello”}; String str= new String(cr)
  • 17. Questions on different functions of String 1.
  • 21. 3.
  • 23. Some more Functions • byte[] getBytes(): Encode a given string into a sequence of bytes and returns an array of bytes. • char toCharArray(): Converts the string into an array of characters and returns the array. • Int compareToIgnoreCase(): Compare the string by the ignoring the case difference. • startsWith(String str): Returns true if a string starts with the given substring. • endsWith(String str): Returns true if a string ends with the given substring.
  • 24. 4.
  • 25. 5.
  • 26. 6.
  • 31. 9.
  • 34. Exception thrown by string class • IndexOutOfBoundException:- • UnsupportedEncodingException • PatternSyntaxException • NullPointerException Class StringBuffer The compiler allocate extra capacity for 16 more characters so that small modification don't involve reallocation of string.
  • 35. 11.